Files
skillshare/internal/ui/ui.go
T
Willie ae80756b9a feat(ui): replace bright-black gray with SGR dim for consistent terminal rendering
\033[90m (bright black) renders too dark on many terminals and themes.
Replace with \x1b[0;2m (SGR dim attribute) which adapts to any foreground.

Changes:
- Add Dim constant and DimText() helper in internal/ui
- Replace all pterm.Gray() calls with DimText() in pterm.go
- Replace all ui.Gray references with ui.Dim across cmd/skillshare/
- Replace fixed 256-color grays (Color("247"/"242")) with lipgloss
  .Faint(true) in TUI styles (tui_colors.go, list_tui.go, log_tui.go)
- Fix progress bar layout: put count after % at fixed position,
  title last (truncated) — prevents count from being pushed off-screen
- Extract duplicate renderBar() from diff.go and search_batch.go
  into shared ui.RenderInlineBar() helper
2026-03-09 12:47:03 +08:00

259 lines
6.6 KiB
Go

package ui
import (
"fmt"
"os"
"strings"
"time"
)
// Base colors for terminal output
const (
Reset = "\033[0m"
Red = "\033[31m"
Green = "\033[32m"
Yellow = "\033[33m"
Blue = "\033[34m"
Magenta = "\033[35m"
Cyan = "\033[36m"
Orange = "\033[38;5;208m"
Purple = "\033[38;5;135m"
BrightRed = "\033[38;5;9m"
OrangeAlt = "\033[38;5;214m"
BrightBlue = "\033[38;5;12m"
Gray = "\033[90m"
Dim = "\x1b[0;2m" // SGR dim — works across all terminal themes
White = "\033[97m"
)
// Semantic color aliases for consistent theming
const (
// Primary brand color (yellow - matches logo)
Primary = Yellow
// Accent color for interactive elements
Accent = Cyan
// Muted color for secondary information
Muted = Dim
// Status colors
StatusSuccess = Green
StatusError = Red
StatusWarning = Yellow
StatusInfo = Cyan
)
// Bold variants
const (
Bold = "\033[1m"
BoldReset = "\033[22m"
)
// Severity color IDs (256-color palette) — single source of truth for both
// ANSI escape codes (SeverityColor) and lipgloss styles (SeverityColorID).
const (
SeverityIDCritical = "1" // red
SeverityIDHigh = "208" // orange
SeverityIDMedium = "3" // yellow
SeverityIDLow = "12" // bright blue — visible on dark backgrounds
SeverityIDInfo = "244" // medium gray — informational, lowest priority
)
// SeverityColor returns the ANSI color code for a given audit severity level.
func SeverityColor(severity string) string {
switch strings.ToUpper(severity) {
case "CRITICAL":
return Red
case "HIGH":
return Orange
case "MEDIUM":
return Yellow
case "LOW":
return Blue
case "INFO":
return Dim
default:
return ""
}
}
// SeverityColorID returns the 256-color palette ID for a severity level.
// Use with lipgloss: lipgloss.Color(ui.SeverityColorID("HIGH"))
func SeverityColorID(severity string) string {
switch strings.ToUpper(severity) {
case "CRITICAL":
return SeverityIDCritical
case "HIGH":
return SeverityIDHigh
case "MEDIUM":
return SeverityIDMedium
case "LOW":
return SeverityIDLow
case "INFO":
return SeverityIDInfo
default:
return ""
}
}
// Colorize wraps text with a color code and reset. Returns plain text if
// color is empty or stdout is not a TTY.
func Colorize(color, text string) string {
if color == "" || !IsTTY() {
return text
}
return color + text + Reset
}
// Success prints a success message
func Success(format string, args ...interface{}) {
fmt.Printf(Green+"✓ "+Reset+format+"\n", args...)
}
// Error prints an error message
func Error(format string, args ...interface{}) {
fmt.Printf(Red+"✗ "+Reset+format+"\n", args...)
}
// Warning prints a warning message
func Warning(format string, args ...interface{}) {
fmt.Printf(Yellow+"! "+Reset+format+"\n", args...)
}
// Info prints an info message
func Info(format string, args ...interface{}) {
fmt.Printf(Cyan+"→ "+Reset+format+"\n", args...)
}
// Status prints a status line
func Status(name, status, detail string) {
statusColor := Gray
switch status {
case "linked":
statusColor = Green
case "not exist":
statusColor = Yellow
case "has files":
statusColor = Blue
case "conflict", "broken":
statusColor = Red
}
fmt.Printf(" %-12s %s%-12s%s %s\n", name, statusColor, status, Reset, Dim+detail+Reset)
}
// Header prints a section header
func Header(text string) {
fmt.Printf("\n%s%s%s\n", Cyan, text, Reset)
fmt.Println(Dim + "─────────────────────────────────────────" + Reset)
}
// Checkbox returns a formatted checkbox string
func Checkbox(checked bool) string {
if checked {
return Green + "[x]" + Reset
}
return "[ ]"
}
// CheckboxItem prints a checkbox item with name and description
func CheckboxItem(checked bool, name, description string) {
checkbox := Checkbox(checked)
if description != "" {
fmt.Printf(" %s %-12s %s%s%s\n", checkbox, name, Dim, description, Reset)
} else {
fmt.Printf(" %s %s\n", checkbox, name)
}
}
// ActionLine prints an action-oriented diff line with symbol prefix.
//
// kind: "new"/"restore" (+ green), "modified" (~ cyan), "override" (! yellow),
// "orphan" (- red), "local" (← gray), "warn" (⚠ yellow)
func ActionLine(kind, text string) {
var icon, color string
switch kind {
case "new", "restore":
icon, color = "+", Green
case "modified":
icon, color = "~", Cyan
case "override":
icon, color = "!", Yellow
case "orphan":
icon, color = "-", Red
case "local":
icon, color = "←", Dim
// Legacy kinds for backward compatibility
case "sync":
icon, color = "→", Cyan
case "force":
icon, color = "⚠", Yellow
case "collect":
icon, color = "←", Dim
case "warn":
icon, color = "⚠", Yellow
default:
icon, color = " ", Reset
}
fmt.Printf(" %s%s%s %s\n", color, icon, Reset, text)
}
// isTTY checks if stdout is a terminal (for animation support)
func isTTY() bool {
fi, err := os.Stdout.Stat()
if err != nil {
return false
}
return (fi.Mode() & os.ModeCharDevice) != 0
}
// Logo prints the ASCII art logo with optional version and animation
// ModeLabel is an optional label appended after the version in the logo.
// Set to "project" to display "(project)" next to the version.
var ModeLabel string
// WithModeLabel appends " (project)" to text when ModeLabel is set.
func WithModeLabel(text string) string {
if ModeLabel != "" {
return text + " (" + ModeLabel + ")"
}
return text
}
func Logo(version string) {
LogoAnimated(version, isTTY())
}
// LogoAnimated prints the ASCII art logo with optional animation
func LogoAnimated(version string, animate bool) {
lines := []string{
Primary + ` _ _ _ _ _` + Reset,
Primary + ` ___| | _(_) | |___| |__ __ _ _ __ ___` + Reset,
Primary + `/ __| |/ / | | / __| '_ \ / _` + "`" + ` | '__/ _ \` + Reset,
Primary + `\__ \ <| | | \__ \ | | | (_| | | | __/` + Reset + ` ` + Muted + `https://github.com/runkids/skillshare` + Reset,
}
// Last line varies based on version
suffix := ""
if ModeLabel != "" {
suffix = ` ` + Accent + `(` + ModeLabel + `)` + Reset
}
if version != "" {
lines = append(lines, Primary+`|___/_|\_\_|_|_|___/_| |_|\__,_|_| \___|`+Reset+` `+Muted+`v`+version+Reset+suffix)
} else {
lines = append(lines, Primary+`|___/_|\_\_|_|_|___/_| |_|\__,_|_| \___|`+Reset+` `+Muted+`Sync skills across all AI CLI tools`+Reset+suffix)
}
if animate {
// Animated: fade in line by line (30ms per line = 150ms total)
for _, line := range lines {
fmt.Println(line)
time.Sleep(30 * time.Millisecond)
}
} else {
// Non-TTY: print all at once
for _, line := range lines {
fmt.Println(line)
}
}
fmt.Println()
}