From c21ba4ed8dde23b34fdab77aa94b12b7bf471119 Mon Sep 17 00:00:00 2001 From: Ayyan Date: Fri, 11 Apr 2025 16:06:54 -0400 Subject: [PATCH] feat(config): add theme.title.align field (#525) --- internal/config/border.go | 67 +++++++++++++++++++++++++++++++ internal/config/theme.go | 83 +++++++++++---------------------------- internal/ui/util.go | 7 ++-- 3 files changed, 94 insertions(+), 63 deletions(-) create mode 100644 internal/config/border.go diff --git a/internal/config/border.go b/internal/config/border.go new file mode 100644 index 0000000..50fc497 --- /dev/null +++ b/internal/config/border.go @@ -0,0 +1,67 @@ +package config + +import "github.com/rivo/tview" + +type BorderPreset struct { + Horizontal rune + Vertical rune + TopLeft rune + TopRight rune + BottomLeft rune + BottomRight rune +} + +func (p *BorderPreset) UnmarshalTOML(v any) error { + switch v.(string) { + case "double": + *p = borderPresetDouble() + case "thick": + *p = borderPresetThick() + case "round": + *p = borderPresetRound() + case "hidden": + *p = BorderPreset{ + Horizontal: ' ', + Vertical: ' ', + TopLeft: ' ', + TopRight: ' ', + BottomLeft: ' ', + BottomRight: ' ', + } + } + + return nil +} + +func borderPresetDouble() BorderPreset { + return BorderPreset{ + Horizontal: tview.BoxDrawingsDoubleHorizontal, + Vertical: tview.BoxDrawingsDoubleVertical, + TopLeft: tview.BoxDrawingsDoubleDownAndRight, + TopRight: tview.BoxDrawingsDoubleDownAndLeft, + BottomLeft: tview.BoxDrawingsDoubleUpAndRight, + BottomRight: tview.BoxDrawingsDoubleUpAndLeft, + } +} + +func borderPresetThick() BorderPreset { + return BorderPreset{ + Horizontal: tview.BoxDrawingsHeavyHorizontal, + Vertical: tview.BoxDrawingsHeavyVertical, + TopLeft: tview.BoxDrawingsHeavyDownAndRight, + TopRight: tview.BoxDrawingsHeavyDownAndLeft, + BottomLeft: tview.BoxDrawingsHeavyUpAndRight, + BottomRight: tview.BoxDrawingsHeavyUpAndLeft, + } +} + +func borderPresetRound() BorderPreset { + return BorderPreset{ + Horizontal: tview.BoxDrawingsLightHorizontal, + Vertical: tview.BoxDrawingsLightVertical, + TopLeft: tview.BoxDrawingsLightArcDownAndRight, + TopRight: tview.BoxDrawingsLightArcDownAndLeft, + BottomLeft: tview.BoxDrawingsLightArcUpAndRight, + BottomRight: tview.BoxDrawingsLightArcUpAndLeft, + } +} diff --git a/internal/config/theme.go b/internal/config/theme.go index cae666a..19b2d11 100644 --- a/internal/config/theme.go +++ b/internal/config/theme.go @@ -4,53 +4,16 @@ import ( "github.com/rivo/tview" ) -type BorderPreset struct { - Horizontal rune - Vertical rune - TopLeft rune - TopRight rune - BottomLeft rune - BottomRight rune -} +type TitleAlign int -func (p *BorderPreset) UnmarshalTOML(v any) error { +func (ta *TitleAlign) UnmarshalTOML(v any) error { switch v.(string) { - case "double": - *p = BorderPreset{ - Horizontal: tview.BoxDrawingsDoubleHorizontal, - Vertical: tview.BoxDrawingsDoubleVertical, - TopLeft: tview.BoxDrawingsDoubleDownAndRight, - TopRight: tview.BoxDrawingsDoubleDownAndLeft, - BottomLeft: tview.BoxDrawingsDoubleUpAndRight, - BottomRight: tview.BoxDrawingsDoubleUpAndLeft, - } - case "thick": - *p = BorderPreset{ - Horizontal: tview.BoxDrawingsHeavyHorizontal, - Vertical: tview.BoxDrawingsHeavyVertical, - TopLeft: tview.BoxDrawingsHeavyDownAndRight, - TopRight: tview.BoxDrawingsHeavyDownAndLeft, - BottomLeft: tview.BoxDrawingsHeavyUpAndRight, - BottomRight: tview.BoxDrawingsHeavyUpAndLeft, - } - case "round": - *p = BorderPreset{ - Horizontal: tview.BoxDrawingsLightHorizontal, - Vertical: tview.BoxDrawingsLightVertical, - TopLeft: tview.BoxDrawingsLightArcDownAndRight, - TopRight: tview.BoxDrawingsLightArcDownAndLeft, - BottomLeft: tview.BoxDrawingsLightArcUpAndRight, - BottomRight: tview.BoxDrawingsLightArcUpAndLeft, - } - case "hidden": - *p = BorderPreset{ - Horizontal: ' ', - Vertical: ' ', - TopLeft: ' ', - TopRight: ' ', - BottomLeft: ' ', - BottomRight: ' ', - } + case "left": + *ta = tview.AlignLeft + case "center": + *ta = tview.AlignCenter + case "right": + *ta = tview.AlignRight } return nil @@ -67,12 +30,16 @@ type ( Preset BorderPreset `toml:"preset"` } + TitleTheme struct { + Color string `toml:"color"` + ActiveColor string `toml:"active_color"` + Align TitleAlign `toml:"align"` + } + Theme struct { BackgroundColor string `toml:"background_color"` - TitleColor string `toml:"title_color"` - ActiveTitleColor string `toml:"active_title_color"` - + Title TitleTheme `toml:"title"` Border BorderTheme `toml:"border"` GuildsTree GuildsTreeTheme `toml:"guilds_tree"` MessagesText MessagesTextTheme `toml:"messages_text"` @@ -100,26 +67,22 @@ type ( func defaultTheme() Theme { return Theme{ + BackgroundColor: "default", + Border: BorderTheme{ Enabled: true, Padding: [...]int{0, 0, 1, 1}, Color: "default", ActiveColor: "green", - Preset: BorderPreset{ - Horizontal: tview.Borders.Horizontal, - Vertical: tview.Borders.Vertical, - TopLeft: tview.Borders.TopLeft, - TopRight: tview.Borders.TopRight, - BottomLeft: tview.Borders.BottomLeft, - BottomRight: tview.Borders.BottomRight, - }, + Preset: borderPresetRound(), }, - BackgroundColor: "default", - - TitleColor: "default", - ActiveTitleColor: "green", + Title: TitleTheme{ + Color: "default", + ActiveColor: "green", + Align: tview.AlignLeft, + }, GuildsTree: GuildsTreeTheme{ AutoExpandFolders: true, diff --git a/internal/ui/util.go b/internal/ui/util.go index 1ae7b6c..e4b953e 100644 --- a/internal/ui/util.go +++ b/internal/ui/util.go @@ -8,19 +8,20 @@ import ( func NewConfiguredBox(box *tview.Box, cfg *config.Theme) *tview.Box { b := cfg.Border + t := cfg.Title p := b.Padding box. SetBorder(cfg.Border.Enabled). SetBorderColor(tcell.GetColor(b.Color)). SetBorderPadding(p[0], p[1], p[2], p[3]). - SetTitleAlign(tview.AlignLeft). + SetTitleAlign(int(t.Align)). SetFocusFunc(func() { box.SetBorderColor(tcell.GetColor(b.ActiveColor)) - box.SetTitleColor(tcell.GetColor(cfg.ActiveTitleColor)) + box.SetTitleColor(tcell.GetColor(t.ActiveColor)) }). SetBlurFunc(func() { box.SetBorderColor(tcell.GetColor(b.Color)) - box.SetTitleColor(tcell.GetColor(cfg.TitleColor)) + box.SetTitleColor(tcell.GetColor(t.Color)) }) return box }