-
Notifications
You must be signed in to change notification settings - Fork 448
Expand file tree
/
Copy paththeme.go
More file actions
372 lines (314 loc) · 12.9 KB
/
Copy paththeme.go
File metadata and controls
372 lines (314 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//go:build !js && !wasm
// Package styles provides centralized style and color definitions for terminal output.
//
// # Adaptive Color System
//
// This package uses compat.AdaptiveColor to automatically adapt colors based on the
// terminal background, ensuring good readability in both light and dark terminal themes.
// Each color constant includes both Light and Dark variants that are automatically
// selected based on the user's terminal configuration.
//
// # Design Philosophy
//
// Light Mode Strategy:
// - Uses darker, more saturated colors for visibility on light backgrounds
// - Ensures high contrast ratios for accessibility
// - Colors are muted to reduce visual fatigue
//
// Dark Mode Strategy:
// - Inspired by the Dracula color theme (https://draculatheme.com/)
// - Uses bright, vibrant colors optimized for dark backgrounds
// - Maintains consistency with popular dark terminal themes
//
// # Color Palette Overview
//
// The palette includes semantic colors for common CLI use cases:
// - Status colors: Error (red), Warning (orange), Success (green), Info (cyan)
// - Highlight colors: Purple (commands, file paths), Yellow (progress, attention)
// - Structural colors: Comment (muted text), Foreground (primary text), Background, Border
//
// Each color constant is documented with its light/dark hex values and semantic usage.
// For visual examples and usage guidelines, see scratchpad/styles-guide.md
//
// # Usage Example
//
// import "github.com/github/gh-aw/pkg/styles"
//
// // Using pre-configured styles
// fmt.Println(styles.Error.Render("Something went wrong"))
// fmt.Println(styles.Success.Render("Operation completed"))
//
// // Using color constants for custom styles
// customStyle := lipgloss.NewStyle().
// Foreground(styles.ColorInfo).
// Bold(true)
// fmt.Println(customStyle.Render("Custom styled text"))
package styles
import (
"os"
"runtime"
lipgloss "charm.land/lipgloss/v2"
"charm.land/lipgloss/v2/compat"
"github.com/charmbracelet/colorprofile"
)
func configureLipglossCompat() {
compat.HasDarkBackground = lipgloss.HasDarkBackground(os.Stdin, os.Stderr)
compat.Profile = colorprofile.Detect(os.Stderr, os.Environ())
}
func shouldConfigureLipglossCompat(goos string, stderrMode os.FileMode, statErr error) bool {
// On Windows, querying terminal capabilities against redirected/pipe handles
// can hang under some wrapper environments. Skip startup probing unless stderr
// is attached to a character device.
if goos == "windows" {
if statErr != nil {
return false
}
return stderrMode&(os.ModeDevice|os.ModeCharDevice) == (os.ModeDevice | os.ModeCharDevice)
}
return true
}
func init() {
stderrInfo, statErr := os.Stderr.Stat()
// Defensive fallback: Stat should not normally return (nil, nil). Treat that
// impossible state as an invalid stderr handle so Windows startup probing is
// safely skipped.
if statErr == nil && stderrInfo == nil {
statErr = os.ErrInvalid
}
// Zero mode means "unknown/unset"; on Windows this keeps the startup probe
// disabled unless stderr is explicitly confirmed as a character device.
stderrMode := os.FileMode(0)
if stderrInfo != nil {
stderrMode = stderrInfo.Mode()
}
if shouldConfigureLipglossCompat(runtime.GOOS, stderrMode, statErr) {
configureLipglossCompat()
}
}
// Hex color constants for light and dark variants.
// These are used both to build the AdaptiveColor values at runtime and to
// enable straightforward assertions in tests (same package).
const (
hexColorErrorLight = "#D73737"
hexColorErrorDark = "#FF5555"
hexColorWarningLight = "#E67E22"
hexColorWarningDark = "#FFB86C"
hexColorSuccessLight = "#27AE60"
hexColorSuccessDark = "#50FA7B"
hexColorInfoLight = "#2980B9"
hexColorInfoDark = "#8BE9FD"
hexColorPurpleLight = "#8E44AD"
hexColorPurpleDark = "#BD93F9"
hexColorYellowLight = "#B7950B"
hexColorYellowDark = "#F1FA8C"
hexColorCommentLight = "#6C7A89"
hexColorCommentDark = "#6272A4"
hexColorForegroundLight = "#2C3E50"
hexColorForegroundDark = "#F8F8F2"
hexColorBackgroundLight = "#ECF0F1"
hexColorBackgroundDark = "#282A36"
hexColorBorderLight = "#BDC3C7"
hexColorBorderDark = "#44475A"
hexColorTableAltRowLight = "#F5F5F5"
hexColorTableAltRowDark = "#1A1A1A"
)
// Adaptive colors that work well in both light and dark terminal themes.
// Light variants use darker, more saturated colors for visibility on light backgrounds.
// Dark variants use brighter colors (Dracula theme inspired) for dark backgrounds.
var (
// ColorError is used for error messages and critical issues.
ColorError = compat.AdaptiveColor{
Light: lipgloss.Color(hexColorErrorLight), // Darker red for light backgrounds
Dark: lipgloss.Color(hexColorErrorDark), // Bright red for dark backgrounds (Dracula)
}
// ColorWarning is used for warning messages and cautionary information.
ColorWarning = compat.AdaptiveColor{
Light: lipgloss.Color(hexColorWarningLight), // Darker orange for light backgrounds
Dark: lipgloss.Color(hexColorWarningDark), // Bright orange for dark backgrounds (Dracula)
}
// ColorSuccess is used for success messages and confirmations.
ColorSuccess = compat.AdaptiveColor{
Light: lipgloss.Color(hexColorSuccessLight), // Darker green for light backgrounds
Dark: lipgloss.Color(hexColorSuccessDark), // Bright green for dark backgrounds (Dracula)
}
// ColorInfo is used for informational messages
ColorInfo = compat.AdaptiveColor{
Light: lipgloss.Color(hexColorInfoLight), // Darker cyan/blue for light backgrounds
Dark: lipgloss.Color(hexColorInfoDark), // Bright cyan for dark backgrounds (Dracula)
}
// ColorPurple is used for file paths, commands, and highlights
ColorPurple = compat.AdaptiveColor{
Light: lipgloss.Color(hexColorPurpleLight), // Darker purple for light backgrounds
Dark: lipgloss.Color(hexColorPurpleDark), // Bright purple for dark backgrounds (Dracula)
}
// ColorYellow is used for progress messages and attention-grabbing content
ColorYellow = compat.AdaptiveColor{
Light: lipgloss.Color(hexColorYellowLight), // Darker yellow/gold for light backgrounds
Dark: lipgloss.Color(hexColorYellowDark), // Bright yellow for dark backgrounds (Dracula)
}
// ColorComment is used for secondary/muted information like line numbers
ColorComment = compat.AdaptiveColor{
Light: lipgloss.Color(hexColorCommentLight), // Muted gray-blue for light backgrounds
Dark: lipgloss.Color(hexColorCommentDark), // Muted purple-gray for dark backgrounds (Dracula)
}
// ColorForeground is used for primary text content
ColorForeground = compat.AdaptiveColor{
Light: lipgloss.Color(hexColorForegroundLight), // Dark gray for light backgrounds
Dark: lipgloss.Color(hexColorForegroundDark), // Light gray/white for dark backgrounds (Dracula)
}
// ColorBackground is used for highlighted backgrounds
ColorBackground = compat.AdaptiveColor{
Light: lipgloss.Color(hexColorBackgroundLight), // Light gray for light backgrounds
Dark: lipgloss.Color(hexColorBackgroundDark), // Dark purple/gray for dark backgrounds (Dracula)
}
// ColorBorder is used for table borders and dividers
ColorBorder = compat.AdaptiveColor{
Light: lipgloss.Color(hexColorBorderLight), // Light gray border for light backgrounds
Dark: lipgloss.Color(hexColorBorderDark), // Dark purple border for dark backgrounds (Dracula)
}
// ColorTableAltRow is used for alternating row backgrounds in tables (zebra striping)
ColorTableAltRow = compat.AdaptiveColor{
Light: lipgloss.Color(hexColorTableAltRowLight), // Subtle light gray for light backgrounds
Dark: lipgloss.Color(hexColorTableAltRowDark), // Subtle darker background for dark backgrounds
}
)
// Border definitions for consistent styling across CLI output.
// These provide a centralized set of border styles that adapt based on context.
var (
// RoundedBorder is the primary border style for most boxes and tables.
// It provides a softer, more polished appearance with rounded corners (╭╮╰╯).
// Used for: tables, error boxes, emphasis boxes, and informational panels.
RoundedBorder = lipgloss.RoundedBorder()
// NormalBorder is used for subtle borders and section dividers.
// It provides clean, simple straight lines suitable for left-side emphasis.
// Used for: info sections with left border, subtle dividers.
NormalBorder = lipgloss.NormalBorder()
// ThickBorder is available for special cases requiring extra visual weight.
// Use sparingly - RoundedBorder with bold/color is usually sufficient for emphasis.
// Reserved for: future use cases requiring maximum visual impact.
ThickBorder = lipgloss.ThickBorder()
)
// Pre-configured styles for common use cases
// Error style for error messages - bold red
var Error = lipgloss.NewStyle().
Bold(true).
Foreground(ColorError)
// Warning style for warning messages - bold orange
var Warning = lipgloss.NewStyle().
Bold(true).
Foreground(ColorWarning)
// Success style for success messages - bold green
var Success = lipgloss.NewStyle().
Bold(true).
Foreground(ColorSuccess)
// Info style for informational messages - bold cyan
var Info = lipgloss.NewStyle().
Bold(true).
Foreground(ColorInfo)
// FilePath style for file paths and locations - bold purple
var FilePath = lipgloss.NewStyle().
Bold(true).
Foreground(ColorPurple)
// LineNumber style for line numbers in error context - muted
var LineNumber = lipgloss.NewStyle().
Foreground(ColorComment)
// ContextLine style for source code context lines
var ContextLine = lipgloss.NewStyle().
Foreground(ColorForeground)
// Highlight style for error highlighting - inverted colors
var Highlight = lipgloss.NewStyle().
Background(ColorError).
Foreground(ColorBackground)
// Location style for directory/file location messages - bold orange
var Location = lipgloss.NewStyle().
Bold(true).
Foreground(ColorWarning)
// Command style for command execution messages - bold purple
var Command = lipgloss.NewStyle().
Bold(true).
Foreground(ColorPurple)
// Progress style for progress/activity messages - yellow
var Progress = lipgloss.NewStyle().
Foreground(ColorYellow)
// Prompt style for user prompt messages - bold green
var Prompt = lipgloss.NewStyle().
Bold(true).
Foreground(ColorSuccess)
// Count style for count/numeric status messages - bold cyan
var Count = lipgloss.NewStyle().
Bold(true).
Foreground(ColorInfo)
// Verbose style for verbose debugging output - italic muted
var Verbose = lipgloss.NewStyle().
Italic(true).
Foreground(ColorComment)
// ListHeader style for section headers in lists - bold underline green
var ListHeader = lipgloss.NewStyle().
Bold(true).
Underline(true).
Foreground(ColorSuccess)
// ListItem style for items in lists
var ListItem = lipgloss.NewStyle().
Foreground(ColorForeground)
// Table styles
// TableHeader style for table headers - bold muted
var TableHeader = lipgloss.NewStyle().
Bold(true).
Foreground(ColorComment)
// TableCell style for regular table cells
var TableCell = lipgloss.NewStyle().
Foreground(ColorForeground)
// TableTotal style for total/summary rows - bold green
var TableTotal = lipgloss.NewStyle().
Bold(true).
Foreground(ColorSuccess)
// TableTitle style for table titles - bold green
var TableTitle = lipgloss.NewStyle().
Bold(true).
Foreground(ColorSuccess)
// TableBorder style for table borders
var TableBorder = lipgloss.NewStyle().
Foreground(ColorBorder)
// MCP inspection styles
// ServerName style for MCP server names - bold purple
var ServerName = lipgloss.NewStyle().
Bold(true).
Foreground(ColorPurple)
// ServerType style for MCP server type information - cyan
var ServerType = lipgloss.NewStyle().
Foreground(ColorInfo)
// ErrorBox style for error boxes with rounded borders
var ErrorBox = lipgloss.NewStyle().
Border(RoundedBorder).
BorderForeground(ColorError).
Padding(1).
Margin(1)
// Header style for section headers with margin - bold green
var Header = lipgloss.NewStyle().
Bold(true).
Foreground(ColorSuccess).
MarginBottom(1)
// Tree styles for hierarchical output
// TreeEnumerator style for tree branch characters (├── └──)
var TreeEnumerator = lipgloss.NewStyle().
Foreground(ColorBorder)
// TreeNode style for tree node content
var TreeNode = lipgloss.NewStyle().
Foreground(ColorForeground)
// Schedule calendar intensity styles for the heatmap renderer
// ScheduleCalendarEmpty style for zero-trigger slots - muted
var ScheduleCalendarEmpty = lipgloss.NewStyle().
Foreground(ColorComment)
// ScheduleCalendarLow style for low-trigger slots (count == 1) - cyan
var ScheduleCalendarLow = lipgloss.NewStyle().
Foreground(ColorInfo)
// ScheduleCalendarMedium style for medium-trigger slots (count 2–3) - green
var ScheduleCalendarMedium = lipgloss.NewStyle().
Foreground(ColorSuccess)
// ScheduleCalendarHigh style for high-trigger slots (count 4–6) - orange
var ScheduleCalendarHigh = lipgloss.NewStyle().
Foreground(ColorWarning)
// ScheduleCalendarCritical style for critical-trigger slots (count 7+) - bold red
var ScheduleCalendarCritical = lipgloss.NewStyle().
Bold(true).
Foreground(ColorError)