Files
skillshare/internal/theme/detect.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

122 lines
3.3 KiB
Go

package theme
import (
"os"
"runtime"
"time"
"github.com/charmbracelet/lipgloss"
"golang.org/x/term"
)
// probeTimeout is the maximum time resolve() waits for an OSC 11 background
// color query response. Chosen empirically: long enough for tmux passthrough
// and slow SSH links, short enough to not stall CLI startup.
const probeTimeout = 300 * time.Millisecond
// resolve decides the theme at process start. It never returns nil.
//
// Precedence (highest first):
//
// 1. NO_COLOR set (any value) → NoColor=true, palette arbitrary.
// 2. SKILLSHARE_THEME=light → light palette, Source="env".
// 3. SKILLSHARE_THEME=dark → dark palette, Source="env".
// 4. SKILLSHARE_THEME=auto / unset / unknown → probe terminal.
// 5. Probe skipped or failed → dark palette fallback.
func resolve() *Theme {
// 1. NO_COLOR — https://no-color.org/ (only when non-empty per spec)
if v, ok := os.LookupEnv("NO_COLOR"); ok && v != "" {
return &Theme{
Mode: ModeDark,
Source: "no-color",
NoColor: true,
palette: darkPalette,
}
}
// 2-3. Explicit SKILLSHARE_THEME override
switch os.Getenv("SKILLSHARE_THEME") {
case "light":
return &Theme{Mode: ModeLight, Source: "env", palette: lightPalette}
case "dark":
return &Theme{Mode: ModeDark, Source: "env", palette: darkPalette}
}
// "", "auto", or unknown values fall through to detection.
// 4. Auto detection gate
if !canDetect() {
return &Theme{
Mode: ModeDark,
Source: "fallback-dark-no-tty",
palette: darkPalette,
}
}
// 5. OSC 11 probe with timeout
mode, ok := probeBackgroundWithTimeout(probeTimeout)
if !ok {
return &Theme{
Mode: ModeDark,
Source: "fallback-dark-probe-failed",
palette: darkPalette,
}
}
if mode == ModeLight {
return &Theme{Mode: ModeLight, Source: "detected", palette: lightPalette}
}
return &Theme{Mode: ModeDark, Source: "detected", palette: darkPalette}
}
// canDetect reports whether it is safe to send an OSC 11 query. Guards
// against:
// - Non-TTY stdin/stdout (piped, redirected, CI)
// - CI env var set
// - TERM=dumb or empty
//
// On Windows returns true unconditionally because lipgloss uses WinAPI
// instead of OSC 11 there.
func canDetect() bool {
if runtime.GOOS == "windows" {
return true
}
if !term.IsTerminal(int(os.Stdin.Fd())) {
return false
}
if !term.IsTerminal(int(os.Stdout.Fd())) {
return false
}
if os.Getenv("CI") != "" {
return false
}
t := os.Getenv("TERM")
if t == "" || t == "dumb" {
return false
}
return true
}
// probeBackgroundWithTimeout calls lipgloss.HasDarkBackground in a goroutine
// and returns ("dark"|"light", true) on success or ("", false) on timeout.
//
// KNOWN LEAK: When the probe times out, the goroutine remains blocked
// reading from stdin until the process exits. This is acceptable because
// the leak occurs at most once per process and holds only a goroutine
// (no file handles or locks). The alternative — a custom OSC 11 probe
// with SetReadDeadline — would add ~50 lines of terminal protocol code
// without a meaningful benefit.
func probeBackgroundWithTimeout(timeout time.Duration) (Mode, bool) {
ch := make(chan bool, 1)
go func() {
ch <- lipgloss.HasDarkBackground()
}()
select {
case isDark := <-ch:
if isDark {
return ModeDark, true
}
return ModeLight, true
case <-time.After(timeout):
return "", false
}
}