From db4ded3b037563851ebf0f7439832d8ba279dce5 Mon Sep 17 00:00:00 2001 From: ayn2op Date: Tue, 10 Jun 2025 17:20:34 -0400 Subject: [PATCH] feat(config): add fine-grained style control to theme.messages_text --- cmd/messages_text.go | 31 ++++++++++++------------------- internal/config/config.toml | 11 +++++------ internal/config/theme.go | 11 +++++------ internal/markdown/renderer.go | 32 +++++++++++++++++++------------- 4 files changed, 41 insertions(+), 44 deletions(-) diff --git a/cmd/messages_text.go b/cmd/messages_text.go index 825d881..3735ccd 100644 --- a/cmd/messages_text.go +++ b/cmd/messages_text.go @@ -52,7 +52,6 @@ func newMessagesText(cfg *config.Config) *messagesText { SetRegions(true). SetWordWrap(true). ScrollToEnd(). - SetTextColor(tcell.GetColor(t.MessagesText.ContentColor)). SetHighlightedFunc(mt.onHighlighted). SetChangedFunc(func() { app.Draw() @@ -60,13 +59,7 @@ func newMessagesText(cfg *config.Config) *messagesText { SetTitle("Messages"). SetInputCapture(mt.onInputCapture) - markdown.DefaultRenderer.AddOptions( - renderer.WithOption("emojiColor", t.MessagesText.EmojiColor), - renderer.WithOption("linkColor", t.MessagesText.LinkColor), - renderer.WithOption("mentionColor", t.MessagesText.MentionColor), - renderer.WithOption("showNicknames", t.MessagesText.ShowNicknames), - ) - + markdown.DefaultRenderer.AddOptions(renderer.WithOption("theme", t.MessagesText)) return mt } @@ -132,10 +125,8 @@ func (mt *messagesText) createMsg(msg discord.Message) { } case discord.InlinedReplyMessage: mt.createReplyMsg(msg) - case discord.ChannelPinnedMessage: - fmt.Fprint(mt, "["+mt.cfg.Theme.MessagesText.ContentColor+"]"+msg.Author.Username+" pinned a message"+"[-:-:-]") - + fmt.Fprintf(mt, "%s pinned a message", msg.Author.DisplayOrUsername()) default: mt.drawTimestamps(msg.Timestamp) mt.drawAuthor(msg) @@ -154,8 +145,8 @@ func (mt *messagesText) drawTimestamps(ts discord.Timestamp) { func (mt *messagesText) drawAuthor(msg discord.Message) { name := mt.authorName(msg.Author, msg.GuildID) - color := mt.authorColor(msg.Author, msg.GuildID) - fmt.Fprintf(mt, "[%s]%s[-] ", color, name) + fg, bg, _ := mt.authorStyle(msg.Author, msg.GuildID).Decompose() + _, _ = fmt.Fprintf(mt, "[%s:%s]%s[-] ", fg.String(), bg.String(), name) } func (mt *messagesText) drawContent(msg discord.Message) { @@ -188,10 +179,12 @@ func (mt *messagesText) createDefaultMsg(msg discord.Message) { for _, a := range msg.Attachments { fmt.Fprintln(mt) + + fg, bg, _ := mt.cfg.Theme.MessagesText.AttachmentStyle.Decompose() if mt.cfg.ShowAttachmentLinks { - fmt.Fprintf(mt, "[%s][%s]:\n%s[-]", mt.cfg.Theme.MessagesText.AttachmentColor, a.Filename, a.URL) + fmt.Fprintf(mt, "[%s:%s][%s]:\n%s[-]", fg, bg, a.Filename, a.URL) } else { - fmt.Fprintf(mt, "[%s][%s][-]", mt.cfg.Theme.MessagesText.AttachmentColor, a.Filename) + fmt.Fprintf(mt, "[%s:%s][%s][-]", fg, bg, a.Filename) } } } @@ -230,16 +223,16 @@ func (mt *messagesText) createForwardedMsg(msg discord.Message) { fmt.Fprintf(mt, " [::d](%s)[-:-:-] ", mt.formatTimestamp(msg.MessageSnapshots[0].Message.Timestamp)) } -func (mt *messagesText) authorColor(user discord.User, gID discord.GuildID) string { - color := mt.cfg.Theme.MessagesText.AuthorColor +func (mt *messagesText) authorStyle(user discord.User, gID discord.GuildID) config.StyleWrapper { + style := mt.cfg.Theme.MessagesText.AuthorStyle if app.cfg.Theme.MessagesText.ShowUsernameColors && gID.IsValid() { // Use color from highest role in guild if c, ok := discordState.MemberColor(gID, user.ID); ok { - color = c.String() + style = config.StyleWrapper{Style: tcell.StyleDefault.Foreground(tcell.GetColor(c.String()))} } } - return color + return style } func (mt *messagesText) selectedMsg() (*discord.Message, error) { diff --git a/internal/config/config.toml b/internal/config/config.toml index 99a31e8..a1600e6 100644 --- a/internal/config/config.toml +++ b/internal/config/config.toml @@ -121,9 +121,8 @@ show_user_nicks = true show_user_colors = true reply_indicator = ">" forwarded_indicator = "<" -author_color = "aqua" -content_color = "white" -emoji_color = "green" -link_color = "blue" -attachment_color = "yellow" -mention_color = "blue" +author_style = { foreground = "aqua" } +mention_style = { foreground = "blue" } +emoji_style = { foreground = "green" } +url_style = { foreground = "blue" } +attachment_style = { foreground = "yellow" } diff --git a/internal/config/theme.go b/internal/config/theme.go index 2cc98d7..e326bde 100644 --- a/internal/config/theme.go +++ b/internal/config/theme.go @@ -105,11 +105,10 @@ type ( ReplyIndicator string `toml:"reply_indicator"` ForwardedIndicator string `toml:"forwarded_indicator"` - AuthorColor string `toml:"author_color"` - ContentColor string `toml:"content_color"` - EmojiColor string `toml:"emoji_color"` - LinkColor string `toml:"link_color"` - AttachmentColor string `toml:"attachment_color"` - MentionColor string `toml:"mention_color"` + AuthorStyle StyleWrapper `toml:"author_style"` + MentionStyle StyleWrapper `toml:"mention_style"` + EmojiStyle StyleWrapper `toml:"emoji_style"` + URLStyle StyleWrapper `toml:"url_style"` + AttachmentStyle StyleWrapper `toml:"attachment_style"` } ) diff --git a/internal/markdown/renderer.go b/internal/markdown/renderer.go index 7a37b3f..736b228 100644 --- a/internal/markdown/renderer.go +++ b/internal/markdown/renderer.go @@ -4,6 +4,7 @@ import ( "fmt" "io" + "github.com/ayn2op/discordo/internal/config" "github.com/diamondburned/ningen/v3/discordmd" "github.com/yuin/goldmark/ast" gmr "github.com/yuin/goldmark/renderer" @@ -75,20 +76,22 @@ func (r *renderer) renderFencedCodeBlock(w io.Writer, n *ast.FencedCodeBlock, en func (r *renderer) renderAutoLink(w io.Writer, n *ast.AutoLink, entering bool, source []byte) { if entering { - linkColor := r.config.Options["linkColor"].(string) - io.WriteString(w, "["+linkColor+"]") + theme := r.config.Options["theme"].(config.MessagesTextTheme) + fg, bg, _ := theme.URLStyle.Decompose() + _, _ = fmt.Fprintf(w, "[%s:%s]", fg, bg) w.Write(n.URL(source)) } else { - io.WriteString(w, "[-::]") + io.WriteString(w, "[-:-]") } } func (r *renderer) renderLink(w io.Writer, n *ast.Link, entering bool) { if entering { - linkColor := r.config.Options["linkColor"].(string) - io.WriteString(w, fmt.Sprintf("[%s:::%s]", linkColor, n.Destination)) + theme := r.config.Options["theme"].(config.MessagesTextTheme) + fg, bg, _ := theme.URLStyle.Decompose() + _, _ = fmt.Fprintf(w, "[%s:%s::%s]", fg, bg, n.Destination) } else { - io.WriteString(w, "[-:::-]") + io.WriteString(w, "[-:-::-]") } } @@ -136,15 +139,17 @@ func (r *renderer) renderInline(w io.Writer, n *discordmd.Inline, entering bool) func (r *renderer) renderMention(w io.Writer, n *discordmd.Mention, entering bool) { if entering { - mentionColor := r.config.Options["mentionColor"].(string) - _, _ = fmt.Fprintf(w, "[%s::b]", mentionColor) + theme := r.config.Options["theme"].(config.MessagesTextTheme) + fg, bg, _ := theme.MentionStyle.Decompose() + _, _ = fmt.Fprintf(w, "[%s:%s:b]", fg, bg) switch { case n.Channel != nil: io.WriteString(w, "#"+n.Channel.Name) case n.GuildUser != nil: username := n.GuildUser.DisplayOrUsername() - if r.config.Options["showNicknames"].(bool) && n.GuildUser.Member != nil && n.GuildUser.Member.Nick != "" { + theme := r.config.Options["theme"].(config.MessagesTextTheme) + if theme.ShowNicknames && n.GuildUser.Member != nil && n.GuildUser.Member.Nick != "" { username = n.GuildUser.Member.Nick } io.WriteString(w, "@"+username) @@ -152,16 +157,17 @@ func (r *renderer) renderMention(w io.Writer, n *discordmd.Mention, entering boo io.WriteString(w, "@"+n.GuildRole.Name) } } else { - io.WriteString(w, "[-::B]") + io.WriteString(w, "[-:-:B]") } } func (r *renderer) renderEmoji(w io.Writer, n *discordmd.Emoji, entering bool) { if entering { - emojiColor := r.config.Options["emojiColor"].(string) - io.WriteString(w, "["+emojiColor+"]") + theme := r.config.Options["theme"].(config.MessagesTextTheme) + fg, bg, _ := theme.EmojiStyle.Decompose() + fmt.Fprintf(w, "[%s:%s]", fg, bg) io.WriteString(w, ":"+n.Name+":") } else { - io.WriteString(w, "[-]") + io.WriteString(w, "[-:-]") } }