From 6e27a4640442537a7f96ad0b1e32c38ea4c456ff Mon Sep 17 00:00:00 2001 From: rigormorrtiss Date: Tue, 31 Aug 2021 10:33:36 +0400 Subject: [PATCH] feat(ui): enable regions for message selection --- ui/textviews.go | 1 + util/discord.go | 27 ++++++++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/ui/textviews.go b/ui/textviews.go index ed81584..8847a1a 100644 --- a/ui/textviews.go +++ b/ui/textviews.go @@ -8,6 +8,7 @@ import ( func NewMessagesTextView(app *tview.Application) *tview.TextView { v := tview.NewTextView() v. + SetRegions(true). SetDynamicColors(true). SetWordWrap(true). ScrollToEnd(). diff --git a/util/discord.go b/util/discord.go index 305347e..97855bc 100644 --- a/util/discord.go +++ b/util/discord.go @@ -9,11 +9,18 @@ import ( "github.com/rivo/tview" ) -// WriteMessage parses and writes the parsed message to the provided textview. +// WriteMessage parses, renders, and writes a message to the given TextView. func WriteMessage(v *tview.TextView, m *discordgo.Message, clientID string) { var b strings.Builder + switch m.Type { case discordgo.MessageTypeDefault, discordgo.MessageTypeReply: + // Define a new region and assign message ID as the region ID. + // Learn more: https://pkg.go.dev/github.com/rivo/tview#hdr-Regions_and_Highlights + b.WriteString("[\"") + b.WriteString(m.ID) + b.WriteString("\"]") + // Render the message associated with crosspost, channel follow add, pin, or a reply. if rm := m.ReferencedMessage; rm != nil { b.WriteString(" ╭ ") b.WriteString("[::d]") @@ -26,22 +33,22 @@ func WriteMessage(v *tview.TextView, m *discordgo.Message, clientID string) { b.WriteString("[::-]\n") } - + // Render the author of the message. parseAuthor(&b, m.Author, clientID) - + // If the message content is not empty, parse the message mentions (users mentioned in the message) and render the message content. if m.Content != "" { m.Content = parseMessageMentions(m.Content, m.Mentions, clientID) b.WriteString(m.Content) } - + // If the edited timestamp of the message is not empty; it implies that the message has been edited, hence render the message with edited label for distinction if m.EditedTimestamp != "" { b.WriteString(" [::d](edited)[::-]") } - + // TODO: render message embeds for range m.Embeds { b.WriteString("\n") } - + // Render the message attachments (attached files to the message). for _, a := range m.Attachments { b.WriteString("\n[") b.WriteString(a.Filename) @@ -55,6 +62,7 @@ func WriteMessage(v *tview.TextView, m *discordgo.Message, clientID string) { b.WriteString(m.Author.Username) b.WriteString("[-]") b.WriteString(" joined the server") + fmt.Fprintln(v, b.String()) } } @@ -63,9 +71,9 @@ func parseMessageMentions(content string, mentions []*discordgo.User, clientID s for _, mUser := range mentions { var color string if mUser.ID == clientID { - color = "[#000000:#FEE75C]" - } else { color = "[:#5865F2]" + } else { + color = "[#EB459E]" } content = strings.NewReplacer( @@ -82,6 +90,7 @@ func parseMessageMentions(content string, mentions []*discordgo.User, clientID s } func parseAuthor(b *strings.Builder, u *discordgo.User, clientID string) { + // If the message author is the client, modify the text color for distinction. if u.ID == clientID { b.WriteString("[#57F287]") } else { @@ -90,7 +99,7 @@ func parseAuthor(b *strings.Builder, u *discordgo.User, clientID string) { b.WriteString(u.Username) b.WriteString("[-] ") - + // If the message author is a bot account, render the message with bot label for distinction. if u.Bot { b.WriteString("[#EB459E]BOT[-] ") }