all repos — llm_aggregator @ b4f6beb3ba126b91482bcd5df0745445088d51d7

A CLI tool to aggregate RSS feeds and summarise them with LLMs

internal/style/style.go (view raw)

  1// Package style provides consistent text styling using lipgloss.
  2// It respects the NO_COLOR environment variable (https://no-color.org/).
  3package style
  4
  5import (
  6	"fmt"
  7	"os"
  8
  9	"github.com/charmbracelet/lipgloss"
 10)
 11
 12// Italic returns an italic style.
 13func Italic(text string) string {
 14	return lipgloss.NewStyle().Italic(true).Render(text)
 15}
 16
 17// Colour represents a lipgloss colour that can be applied.
 18type Colour lipgloss.Color
 19
 20// ANSI colour codes (standard 8-colour palette).
 21// Using ANSI escape codes for portability across terminals.
 22var (
 23	// Red for errors and warnings
 24	Red = Colour("1")
 25
 26	// Green for success messages
 27	Green = Colour("2")
 28
 29	// Blue for URLs
 30	Blue = Colour("12")
 31
 32	// Magenta for labels and info
 33	Magenta = Colour("5")
 34
 35	// Cyan for filepaths and values
 36	Cyan = Colour("6")
 37
 38	// White for headings
 39	White = Colour("15")
 40
 41	// Bright black for muted/debug text
 42	BrightBlack = Colour("8")
 43)
 44
 45// NoColor returns true if the NO_COLOR environment variable is set.
 46// This respects https://no-color.org/ standard.
 47func NoColor() bool {
 48	return os.Getenv("NO_COLOR") != ""
 49}
 50
 51// Warning returns an orange and bold warning message.
 52// Note: ANSI doesn't have orange, so we use red with bold styling.
 53func Warning(text string) string {
 54	if NoColor() {
 55		return lipgloss.NewStyle().Bold(true).Render("WARNING: " + text)
 56	}
 57	return lipgloss.NewStyle().
 58		Foreground(lipgloss.Color(Red)).
 59		Bold(true).
 60		Render("WARNING: " + text)
 61}
 62
 63// Warningf returns a formatted warning message.
 64func Warningf(format string, args ...any) string {
 65	return Warning(fmt.Sprintf(format, args...))
 66}
 67
 68// Error returns a red and bold error message.
 69func Error(text string) string {
 70	if NoColor() {
 71		return lipgloss.NewStyle().Bold(true).Render("ERROR: " + text)
 72	}
 73	return lipgloss.NewStyle().
 74		Foreground(lipgloss.Color(Red)).
 75		Bold(true).
 76		Render("ERROR: " + text)
 77}
 78
 79// Errorf returns a formatted error message.
 80func Errorf(format string, args ...any) string {
 81	return Error(fmt.Sprintf(format, args...))
 82}
 83
 84// Success returns a green success message.
 85func Success(text string) string {
 86	if NoColor() {
 87		return lipgloss.NewStyle().Bold(true).Render("✓ " + text)
 88	}
 89	return lipgloss.NewStyle().
 90		Foreground(lipgloss.Color(Green)).
 91		Bold(true).
 92		Render("✓ " + text)
 93}
 94
 95// Filepath returns a styled filepath in cyan.
 96func Filepath(filepath string) string {
 97	if NoColor() {
 98		return filepath
 99	}
100	return lipgloss.NewStyle().Foreground(lipgloss.Color(Cyan)).Render(filepath)
101}
102
103// FilepathStyled returns a lipgloss style for filepaths that can be applied to custom text.
104var FilepathStyled = lipgloss.NewStyle().Foreground(lipgloss.Color(Cyan))
105
106// URL returns a styled URL in blue with underline.
107func URL(url string) string {
108	if NoColor() {
109		return lipgloss.NewStyle().Underline(true).Render(url)
110	}
111	return lipgloss.NewStyle().
112		Foreground(lipgloss.Color(Blue)).
113		Underline(true).
114		Render(url)
115}
116
117// URLStyled returns a lipgloss style for URLs that can be applied to custom text.
118var URLStyled = lipgloss.NewStyle().Foreground(lipgloss.Color(Blue)).Underline(true)
119
120// Label returns a styled label/key in magenta and bold.
121func Label(text string) string {
122	if NoColor() {
123		return lipgloss.NewStyle().Bold(true).Render(text)
124	}
125	return lipgloss.NewStyle().
126		Foreground(lipgloss.Color(Magenta)).
127		Bold(true).
128		Render(text)
129}
130
131// LabelStyled returns a lipgloss style for labels.
132var LabelStyled = lipgloss.NewStyle().Foreground(lipgloss.Color(Magenta)).Bold(true)
133
134// Heading returns a styled heading.
135func Heading(text string) string {
136	return lipgloss.NewStyle().Bold(true).Render(text)
137}
138
139// Value returns styled value text.
140func Value(text string) string {
141	if NoColor() {
142		return text
143	}
144	return lipgloss.NewStyle().Foreground(lipgloss.Color(Cyan)).Render(text)
145}
146
147// ValueStyled returns a lipgloss style for values.
148var ValueStyled = lipgloss.NewStyle().Foreground(lipgloss.Color(Cyan))
149
150// Info returns a styled info message in cyan.
151func Info(text string) string {
152	if NoColor() {
153		return "ℹ " + text
154	}
155	return lipgloss.NewStyle().Foreground(lipgloss.Color(Cyan)).Render("ℹ " + text)
156}
157
158// Debug returns a styled debug message in grey.
159func Debug(text string) string {
160	if NoColor() {
161		return lipgloss.NewStyle().Italic(true).Render("Debug: " + text)
162	}
163	return lipgloss.NewStyle().
164		Foreground(lipgloss.Color(BrightBlack)).
165		Italic(true).
166		Render("Debug: " + text)
167}
168
169// Debugf returns a formatted debug message.
170func Debugf(format string, args ...any) string {
171	return Debug(fmt.Sprintf(format, args...))
172}