From a442103c898b60ecf5b18824b94d686bb0c63731 Mon Sep 17 00:00:00 2001 From: rigormorrtiss Date: Wed, 1 Sep 2021 13:35:32 +0400 Subject: [PATCH] fix(util): end text region with closing region tag --- discordo.go | 37 ++++++++++++++++++------------------- util/discord.go | 4 ++++ 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/discordo.go b/discordo.go index 8d1efb5..df989ea 100644 --- a/discordo.go +++ b/discordo.go @@ -90,6 +90,16 @@ func onAppInputCapture(e *tcell.EventKey) *tcell.EventKey { return e } +func findIndexByMessageID(ms []*discordgo.Message, mID string) int { + for i, m := range ms { + if mID == m.ID { + return i + } + } + + return -1 +} + func onMessagesTextViewInputCapture(e *tcell.EventKey) *tcell.EventKey { if selectedChannel == nil { return nil @@ -108,19 +118,15 @@ func onMessagesTextViewInputCapture(e *tcell.EventKey) *tcell.EventKey { } else { // Find the index of the currently highlighted message in the // *discordgo.Channel.Messages slice. - var idx int - for i, v := range ms { - if hs[0] == v.ID { - idx = i - break - } - } - // If the index of the currently highlighted message is equal to zero + idx := findIndexByMessageID(ms, hs[0]) + // If the index of the currently highlighted message is equal to + // zero // (first message in the TextView), do not handle the event. if idx == 0 { return nil } - // Highlight the message just before the currently highlighted message. + // Highlight the message just before the currently highlighted + // message. messagesTextView. Highlight(ms[idx-1].ID). ScrollToHighlight() @@ -139,22 +145,15 @@ func onMessagesTextViewInputCapture(e *tcell.EventKey) *tcell.EventKey { } else { // Find the index of the highlighted message in the // *discordgo.Channel.Messages slice. - var idx int - for i, v := range selectedChannel.Messages { - if v.Type == discordgo.MessageTypeDefault || v.Type == discordgo.MessageTypeReply { - if hs[0] == v.ID { - idx = i - break - } - } - } + idx := findIndexByMessageID(ms, hs[0]) // If the index of the currently highlighted message is equal to the // total number of elements in the *discordgo.Channel.Messages // slice, do not handle the event. if idx == len(ms)-1 { return nil } - // Highlight the message just after the currently highlighted message. + // Highlight the message just after the currently highlighted + // message. messagesTextView. Highlight(ms[idx+1].ID). ScrollToHighlight() diff --git a/util/discord.go b/util/discord.go index a8923a2..6ad915b 100644 --- a/util/discord.go +++ b/util/discord.go @@ -64,6 +64,10 @@ func WriteMessage(v *tview.TextView, m *discordgo.Message, clientID string) { b.WriteString("]: ") b.WriteString(a.URL) } + // Tags with no region ID ([""]) do not start new regions. They can + // therefore be used to mark the end of a region. + b.WriteString("[\"") + b.WriteString("\"]") fmt.Fprintln(v, b.String()) case discordgo.MessageTypeGuildMemberJoin: