package harness import ( "os" "strings" "github.com/charmbracelet/lipgloss" "github.com/muesli/termenv" "golang.org/x/term" ) // Local style vocabulary mirrors internal/tui's soft theme without importing it // (that would create an import cycle). Accents use the terminal palette; surfaces // adapt to light/dark terminals and reduce automatically on low-color terminals. var ( styleAccent = lipgloss.Color("6") styleMuted = lipgloss.Color("8") styleError = lipgloss.Color("1") styleSelection = lipgloss.AdaptiveColor{Light: "#DCE7F5", Dark: "#2B3A4C"} ) // plainOutput reports whether styled ANSI must be suppressed: NO_COLOR (any // value, per no-color.org) or a dumb terminal. func plainOutput() bool { if _, set := os.LookupEnv("NO_COLOR"); set { return true } return strings.EqualFold(os.Getenv("TERM"), "dumb") } // isTerminal reports whether a stream is a real TTY. A character device such as // /dev/null is not a TTY and must never launch an interactive picker. func isTerminal(stream any) bool { file, ok := stream.(*os.File) if !ok { return false } return term.IsTerminal(int(file.Fd())) } // pickerStyles renders plain text unchanged when color is unavailable. type pickerStyles struct{ plain bool } func (s pickerStyles) title(text string) string { if s.plain { return text } return lipgloss.NewStyle().Foreground(styleAccent).Bold(true).Render(text) } func (s pickerStyles) step(text string) string { if s.plain { return text } return lipgloss.NewStyle().Foreground(styleAccent).Bold(true).Render(text) } func (s pickerStyles) muted(text string) string { if s.plain { return text } return lipgloss.NewStyle().Foreground(styleMuted).Render(text) } func (s pickerStyles) error(text string) string { if s.plain { return text } return lipgloss.NewStyle().Foreground(styleError).Bold(true).Render(text) } // row marks the active selection with a textual caret and, when color is // available, a bold highlight padded to the content width. At reduced color // profiles (ANSI/16-color) a background is omitted because lipgloss quantizes // the adaptive surface to a colliding color (observed cyan-on-cyan); the caret // plus bold stays readable. ANSI256/TrueColor keep the adaptive background. func (s pickerStyles) row(text string, selected bool, width int) string { marker := " " if selected { marker = "› " } if s.plain { return marker + text } style := lipgloss.NewStyle() if selected { // termenv orders profiles by richness (TrueColor=0 ... Ascii=3), so a // profile index at or above ANSI is a reduced-color terminal. Those keep // the readable textual caret plus bold without the adaptive background // (which lipgloss quantizes to a colliding color, e.g. cyan-on-cyan). if lipgloss.ColorProfile() >= termenv.ANSI { style = style.Bold(true) } else { style = style.Foreground(styleAccent).Background(styleSelection).Bold(true) } } if width > 0 { style = style.Width(width) } return style.Render(marker + text) } // wrap breaks prose into lines no wider than width, on word boundaries. func wrap(text string, width int) []string { if width < 1 { width = 1 } var lines []string for _, paragraph := range strings.Split(text, "\n") { words := strings.Fields(paragraph) if len(words) == 0 { lines = append(lines, "") continue } line := words[0] for _, word := range words[1:] { if len(line)+1+len(word) > width { lines = append(lines, line) line = word continue } line += " " + word } lines = append(lines, line) } return lines }