ayn2op 3 лет назад
Родитель
Сommit
433f9feb58
3 измененных файлов с 108 добавлено и 38 удалено
  1. 17 8
      config.go
  2. 11 3
      message_input.go
  3. 80 27
      messages_text.go

+ 17 - 8
config.go

@@ -16,16 +16,21 @@ type (
 
 	MessagesTextKeysConfig struct {
 		CommonKeysConfig `yaml:",inline"`
-		Reply            string `yaml:"reply"`
-		ReplyMention     string `yaml:"reply_mention"`
-		SelectPrevious   string `yaml:"select_previous"`
-		SelectNext       string `yaml:"select_next"`
+
+		Reply        string `yaml:"reply"`
+		ReplyMention string `yaml:"reply_mention"`
+
+		SelectPrevious string `yaml:"select_previous"`
+		SelectNext     string `yaml:"select_next"`
+		SelectFirst    string `yaml:"select_first"`
+		SelectLast     string `yaml:"select_last"`
 	}
 
 	MessageInputKeysConfig struct {
 		CommonKeysConfig `yaml:",inline"`
-		Send             string `yaml:"send"`
-		LaunchEditor     string `yaml:"launch_editor"`
+
+		Send         string `yaml:"send"`
+		LaunchEditor string `yaml:"launch_editor"`
 	}
 
 	KeysConfig struct {
@@ -45,12 +50,14 @@ type (
 
 	GuildsTreeThemeConfig struct {
 		CommonThemeConfig `yaml:",inline"`
-		Graphics          bool `yaml:"graphics"`
+
+		Graphics bool `yaml:"graphics"`
 	}
 
 	MessagesTextThemeConfig struct {
 		CommonThemeConfig `yaml:",inline"`
-		AuthorColor       string `yaml:"author_color"`
+
+		AuthorColor string `yaml:"author_color"`
 	}
 
 	MessageInputThemeConfig struct {
@@ -112,6 +119,8 @@ func newConfig() (*Config, error) {
 
 				SelectPrevious: "Up",
 				SelectNext:     "Down",
+				SelectFirst:    "Home",
+				SelectLast:     "End",
 			},
 			MessageInput: MessageInputKeysConfig{
 				CommonKeysConfig: commonKeys,

+ 11 - 3
message_input.go

@@ -68,10 +68,16 @@ func (mi *MessageInput) sendAction() {
 	}
 
 	var err error
-	if messagesText.selectedMessage != nil {
+	if messagesText.selectedMessage != -1 {
+		ms, err := discordState.Cabinet.Messages(guildsTree.selectedChannel.ID)
+		if err != nil {
+			log.Println(err)
+			return
+		}
+
 		data := api.SendMessageData{
 			Content:         text,
-			Reference:       &discord.MessageReference{MessageID: messagesText.selectedMessage.ID},
+			Reference:       &discord.MessageReference{MessageID: ms[messagesText.selectedMessage].ID},
 			AllowedMentions: &api.AllowedMentions{RepliedUser: option.False},
 		}
 
@@ -89,7 +95,9 @@ func (mi *MessageInput) sendAction() {
 		return
 	}
 
-	messageInput.reset()
+	messagesText.selectedMessage = -1
+	messagesText.Highlight()
+	mi.reset()
 }
 
 func (mi *MessageInput) launchEditorAction() {

+ 80 - 27
messages_text.go

@@ -16,19 +16,20 @@ const replyIndicator = '╭'
 type MessagesText struct {
 	*tview.TextView
 
-	selectedMessage *discord.Message
+	selectedMessage int
 	buf             bytes.Buffer
 }
 
 func newMessagesText() *MessagesText {
 	mt := &MessagesText{
 		TextView: tview.NewTextView(),
+
+		selectedMessage: -1,
 	}
 
 	mt.SetDynamicColors(true)
 	mt.SetRegions(true)
 	mt.SetWordWrap(true)
-	mt.SetHighlightedFunc(mt.onHighlighted)
 	mt.SetInputCapture(mt.onInputCapture)
 	mt.ScrollToEnd()
 	mt.SetChangedFunc(func() {
@@ -49,7 +50,7 @@ func newMessagesText() *MessagesText {
 }
 
 func (mt *MessagesText) reset() {
-	messagesText.selectedMessage = nil
+	messagesText.selectedMessage = -1
 
 	mt.SetTitle("")
 	mt.Clear()
@@ -122,26 +123,6 @@ func (mt *MessagesText) createFooter(m *discord.Message) {
 	}
 }
 
-func (mt *MessagesText) onHighlighted(added, removed, remaining []string) {
-	if len(added) == 0 {
-		return
-	}
-
-	sf, err := discord.ParseSnowflake(added[0])
-	if err != nil {
-		log.Println(err)
-		return
-	}
-
-	m, err := discordState.Cabinet.Message(guildsTree.selectedChannel.ID, discord.MessageID(sf))
-	if err != nil {
-		log.Println(err)
-		return
-	}
-
-	mt.selectedMessage = m
-}
-
 func (mt *MessagesText) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
 	switch event.Name() {
 	case config.Keys.MessagesText.Reply:
@@ -156,6 +137,12 @@ func (mt *MessagesText) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
 	case config.Keys.MessagesText.SelectNext:
 		mt.selectNextAction()
 		return nil
+	case config.Keys.MessagesText.SelectFirst:
+		mt.selectFirstAction()
+		return nil
+	case config.Keys.MessagesText.SelectLast:
+		mt.selectLastAction()
+		return nil
 	case config.Keys.MessagesText.Cancel:
 		guildsTree.selectedChannel = nil
 
@@ -168,7 +155,7 @@ func (mt *MessagesText) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
 }
 
 func (mt *MessagesText) replyAction(mention bool) {
-	if mt.selectedMessage == nil {
+	if mt.selectedMessage == -1 {
 		return
 	}
 
@@ -179,12 +166,78 @@ func (mt *MessagesText) replyAction(mention bool) {
 		title += "Replying to "
 	}
 
-	title += mt.selectedMessage.Author.Tag()
+	ms, err := discordState.Cabinet.Messages(guildsTree.selectedChannel.ID)
+	if err != nil {
+		log.Println(err)
+		return
+	}
+
+	title += ms[mt.selectedMessage].Author.Tag()
 	messageInput.SetTitle(title)
 
 	app.SetFocus(messageInput)
 }
 
-func (mt *MessagesText) selectPreviousAction() {}
+func (mt *MessagesText) selectPreviousAction() {
+	ms, err := discordState.Cabinet.Messages(guildsTree.selectedChannel.ID)
+	if err != nil {
+		log.Println(err)
+		return
+	}
 
-func (mt *MessagesText) selectNextAction() {}
+	// If no message is currently selected, select the latest message.
+	if len(mt.GetHighlights()) == 0 {
+		mt.selectedMessage = 0
+	} else {
+		if mt.selectedMessage < len(ms)-1 {
+			mt.selectedMessage++
+		}
+	}
+
+	mt.Highlight(ms[mt.selectedMessage].ID.String())
+	mt.ScrollToHighlight()
+}
+
+func (mt *MessagesText) selectNextAction() {
+	ms, err := discordState.Cabinet.Messages(guildsTree.selectedChannel.ID)
+	if err != nil {
+		log.Println(err)
+		return
+	}
+
+	// If no message is currently selected, select the latest message.
+	if len(mt.GetHighlights()) == 0 {
+		mt.selectedMessage = 0
+	} else {
+		if mt.selectedMessage > 0 {
+			mt.selectedMessage--
+		}
+	}
+
+	mt.Highlight(ms[mt.selectedMessage].ID.String())
+	mt.ScrollToHighlight()
+}
+
+func (mt *MessagesText) selectFirstAction() {
+	ms, err := discordState.Cabinet.Messages(guildsTree.selectedChannel.ID)
+	if err != nil {
+		log.Println(err)
+		return
+	}
+
+	mt.selectedMessage = len(ms) - 1
+	mt.Highlight(ms[mt.selectedMessage].ID.String())
+	mt.ScrollToHighlight()
+}
+
+func (mt *MessagesText) selectLastAction() {
+	ms, err := discordState.Cabinet.Messages(guildsTree.selectedChannel.ID)
+	if err != nil {
+		log.Println(err)
+		return
+	}
+
+	mt.selectedMessage = 0
+	mt.Highlight(ms[mt.selectedMessage].ID.String())
+	mt.ScrollToHighlight()
+}