From 1a5364a69650d9e1efc25a0ef4a9c6fcc2e04746 Mon Sep 17 00:00:00 2001 From: ruinivist Date: Sat, 21 Mar 2026 05:58:44 +0000 Subject: [PATCH] refactor: add a clean termstyle file for styling --- internal/output/output.go | 14 ++------ internal/output/termstyle.go | 70 ++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 internal/output/termstyle.go diff --git a/internal/output/output.go b/internal/output/output.go index fd01894..2ed2287 100644 --- a/internal/output/output.go +++ b/internal/output/output.go @@ -7,12 +7,6 @@ import ( "strings" ) -const ( - bold = "\x1b[1m" - italic = "\x1b[3m" - reset = "\x1b[0m" -) - var ( palette = struct { miroPrefixGreen uint32 @@ -22,18 +16,14 @@ var ( miroChevronTeal: 0x1DD3B0, } - chevron = ansiColor(palette.miroChevronTeal) - prefix = ansiColor(palette.miroPrefixGreen) + bold + italic + "miro" + reset + " " + chevron + bold + italic + "›" + reset + " " + chevron = NewStyle().FG(palette.miroChevronTeal).Bold().Italic().Apply("›") + prefix = NewStyle().FG(palette.miroPrefixGreen).Bold().Italic().Apply("miro") + " " + chevron + " " ) func Prefix() string { return prefix } -func ansiColor(rgb uint32) string { - return fmt.Sprintf("\x1b[38;2;%d;%d;%dm", byte(rgb>>16), byte(rgb>>8), byte(rgb)) -} - func Format(msg string) string { body := strings.TrimRight(msg, "\n") suffix := msg[len(body):] diff --git a/internal/output/termstyle.go b/internal/output/termstyle.go new file mode 100644 index 0000000..4538fe2 --- /dev/null +++ b/internal/output/termstyle.go @@ -0,0 +1,70 @@ +package output + +import "fmt" + +const ( + ansiBold = "\x1b[1m" + ansiItalic = "\x1b[3m" + ansiReset = "\x1b[0m" +) + +type Style struct { + bold bool + italic bool + fg *uint32 + bg *uint32 +} + +func NewStyle() Style { + return Style{} +} + +func (s Style) Bold() Style { + s.bold = true + return s +} + +func (s Style) Italic() Style { + s.italic = true + return s +} + +func (s Style) FG(rgb uint32) Style { + s.fg = &rgb + return s +} + +func (s Style) BG(rgb uint32) Style { + s.bg = &rgb + return s +} + +func (s Style) Apply(text string) string { + if !s.bold && !s.italic && s.fg == nil && s.bg == nil { + return text + } + + style := "" + if s.bold { + style += ansiBold + } + if s.italic { + style += ansiItalic + } + if s.fg != nil { + style += ansiFG(*s.fg) + } + if s.bg != nil { + style += ansiBG(*s.bg) + } + + return style + text + ansiReset +} + +func ansiFG(rgb uint32) string { + return fmt.Sprintf("\x1b[38;2;%d;%d;%dm", byte(rgb>>16), byte(rgb>>8), byte(rgb)) +} + +func ansiBG(rgb uint32) string { + return fmt.Sprintf("\x1b[48;2;%d;%d;%dm", byte(rgb>>16), byte(rgb>>8), byte(rgb)) +}