Files
skillshare/internal/theme/theme.go
T
Willie 36e22477fe feat(theme): add internal/theme package with palettes and detection
Introduces a unified light/dark terminal theme system:
- Theme struct with sync.Once-backed Get()/Reset() singleton
- darkPalette (softened Primary from 15 to 252) and lightPalette
  (new, WCAG AA compliant against white backgrounds)
- Style constructors (Primary, Muted, Accent, Success, Warning,
  Danger, Info, Title, SelectedRow, Badge, Severity)
- ANSISet for plain CLI output
- Resolution precedence: NO_COLOR > SKILLSHARE_THEME > OSC 11 probe
  > dark fallback
- 300ms timeout on OSC 11 probe with documented goroutine leak tradeoff
- canDetect() gate for non-TTY, CI, TERM=dumb, Windows-safe
- WCAG contrast tests as regression guard for future palette edits

NO_COLOR env var follows the no-color.org spec: only activates when
set AND non-empty (empty string does not enable no-color mode).

This commit adds the package but does not migrate any existing
callers. The next commits migrate list TUIs (fixes #125), plain CLI
output, and remaining TUIs.

Refs: #125
2026-04-10 14:26:44 +08:00

67 lines
1.9 KiB
Go

// Package theme provides a unified light/dark terminal theme system.
//
// The theme is resolved once at first access via theme.Get() and cached
// for the remainder of the process. Resolution precedence:
//
// 1. NO_COLOR env var set → disable all colors
// 2. SKILLSHARE_THEME=light|dark → explicit override
// 3. SKILLSHARE_THEME=auto (or unset) → probe terminal background
// 4. Fallback to dark palette
//
// Usage from TUI code:
//
// style := theme.Primary()
// rendered := style.Render("text")
//
// Usage from plain CLI output:
//
// ansi := theme.ANSI()
// fmt.Printf("%s%s%s\n", ansi.Success, "done", ansi.Reset)
package theme
import "sync"
// Mode represents the user's resolved theme preference.
type Mode string
const (
// ModeAuto is the default before resolution; never appears on a resolved Theme.
ModeAuto Mode = "auto"
// ModeLight means the palette is tuned for light terminal backgrounds.
ModeLight Mode = "light"
// ModeDark means the palette is tuned for dark terminal backgrounds.
ModeDark Mode = "dark"
)
// Theme holds the resolved, immutable theme state for the current process.
type Theme struct {
// Mode is the resolved palette mode (never ModeAuto after Get()).
Mode Mode
// Source describes how Mode was decided. See resolve() for valid values.
Source string
// NoColor is true when NO_COLOR env var was set; all style/ansi
// constructors must return plain text when this is true.
NoColor bool
palette palette
}
var (
current *Theme
once sync.Once
)
// Get returns the process-wide theme singleton, resolving it on first call.
// Safe for concurrent use.
func Get() *Theme {
once.Do(func() { current = resolve() })
return current
}
// Reset clears the cached theme so the next Get() re-resolves. Intended
// for tests that need to change env vars and observe a new resolution.
func Reset() {
once = sync.Once{}
current = nil
}