Documentation
¶
Overview ¶
Package cliui is OpenPanda's terminal presentation layer: one colour vocabulary, one spinner, one live status line, shared by every CLI surface.
It exists because the colour handling used to be copy-pasted — five separate `"\x1b[" + code + "m" + s + "\x1b[0m"` sites, each with its own TTY check and none of them honouring NO_COLOR. A palette is built once from the terminal's actual capabilities and passed down; a disabled palette returns its input untouched, so callers never branch on "are we on a TTY" again.
Nothing here knows about locales: every human-readable word arrives from the caller (already translated through internal/i18n).
Index ¶
- Constants
- Variables
- func DisplayWidth(s string) int
- func Frames(p Palette) []string
- func HumanCount(n int64) string
- func HumanDuration(d time.Duration) string
- func RuneWidth(r rune) int
- func Truncate(s string, max int, unicode bool) string
- func TruncateTail(s string, max int, unicode bool) string
- type Palette
- func (p Palette) Accent(s string) string
- func (p Palette) Blue(s string) string
- func (p Palette) Bold(s string) string
- func (p Palette) Command(s string) string
- func (p Palette) Cyan(s string) string
- func (p Palette) Danger(s string) string
- func (p Palette) Dim(s string) string
- func (p Palette) Enabled() bool
- func (p Palette) Glyph(uni, ascii string) string
- func (p Palette) Green(s string) string
- func (p Palette) Heading(s string) string
- func (p Palette) Info(s string) string
- func (p Palette) Italic(s string) string
- func (p Palette) Magenta(s string) string
- func (p Palette) MarkArrow() string
- func (p Palette) MarkBullet() string
- func (p Palette) MarkFail() string
- func (p Palette) MarkOK() string
- func (p Palette) Muted(s string) string
- func (p Palette) Red(s string) string
- func (p Palette) SGR(code, s string) string
- func (p Palette) Separator() string
- func (p Palette) Success(s string) string
- func (p Palette) Unicode() bool
- func (p Palette) Warn(s string) string
- func (p Palette) Yellow(s string) string
- type Status
- func (s *Status) Elapsed() time.Duration
- func (s *Status) Hint(h string)
- func (s *Status) Log(line string)
- func (s *Status) Note(n string)
- func (s *Status) Preview(text string)
- func (s *Status) Running() bool
- func (s *Status) SetInterval(d time.Duration) *Status
- func (s *Status) SetTokenWord(w string) *Status
- func (s *Status) SetTokens(n int64)
- func (s *Status) SetWidth(fn func() int) *Status
- func (s *Status) Start(verb string)
- func (s *Status) Stats() string
- func (s *Status) Stop() time.Duration
- func (s *Status) Suspend(f func())
- func (s *Status) Verb(v string)
Constants ¶
const FrameInterval = 80 * time.Millisecond
FrameInterval is one spinner step. Fast enough to read as motion, slow enough that a remote session over ssh is not repainting constantly.
Variables ¶
var ASCIIWheel = []string{"-", "\\", "|", "/"}
ASCIIWheel is the fallback spinner. Four frames, so it visibly ticks rather than pretending to be smooth.
var Braille = []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}
Braille is the default spinner: ten frames of U+280x dots, one glyph wide, which reads as smooth rotation at ~80ms per frame.
Functions ¶
func DisplayWidth ¶
DisplayWidth sums the column count of s. Escape sequences are NOT accounted for — pass unstyled text (the status line measures before it paints).
func HumanCount ¶
HumanCount abbreviates a token/byte count: 1234 → "1.2k", 1234567 → "1.2M". Exact below 1000, because "3 tokens" reads better than "0.0k".
func HumanDuration ¶
HumanDuration renders an elapsed time the way a progress line wants it: sub-second work in milliseconds, seconds with one decimal until it stops being interesting, then m/s. Never wider than 7 columns.
func RuneWidth ¶
RuneWidth approximates the terminal column count of r: East Asian and emoji ranges render double-wide, control and combining marks zero. A compact table — full wcwidth tables are overkill for a line editor.
Types ¶
type Palette ¶
type Palette struct {
// contains filtered or unexported fields
}
Palette carries the terminal's presentation capabilities: whether colour may be emitted, whether 24-bit colour is available (the brand green degrades to plain green without it), and whether non-ASCII glyphs render at all — a bare Linux VT has the encoding but not the font.
The zero Palette is the safe one: no colour, no unicode. That makes it the right default for pipes, CI logs and tests.
func New ¶
New builds a palette for a stream. tty says whether that stream is a terminal; unicode whether non-ASCII glyphs are expected to render. Both are facts the caller already knows (cmd/panda has stdoutIsTTY and termSupportsUnicode); the environment overrides below are ours to apply.
func Plain ¶
func Plain() Palette
Plain is the no-styling palette: every method returns its argument. Used for piped output, --json paths and tests.
func (Palette) Accent ¶
Accent is the OpenPanda wordmark green — the one place a 24-bit terminal gets the exact brand colour and everything else gets plain green.
func (Palette) Bold ¶
The base vocabulary. Semantic names live below; these stay for the handful of places that genuinely mean "this word is yellow".
func (Palette) Command ¶
Command tints a literal the user can type back (a subcommand, a slash command, a flag) so scanning help output lands on the actionable words.
func (Palette) MarkBullet ¶
func (Palette) MarkOK ¶
Mark glyphs for finished lines: success, failure, and the neutral bullet that prefixes progress notes.
func (Palette) SGR ¶
SGR wraps s in an arbitrary SGR parameter string ("1;33"). Prefer the named methods; this is the escape hatch for callers with a code already in hand.
func (Palette) Separator ¶
Separator is the mid-line divider used between status fields and in the banner's hint list.
func (Palette) Success ¶
Semantic layer. Call these from feature code so a later theme change is one edit here rather than a grep for "33".
type Status ¶
type Status struct {
// contains filtered or unexported fields
}
Status is a live one-line progress indicator. Construct with NewStatus; all methods are safe to call from any goroutine, and every one of them is a no-op on a Status that is not running, so callers need no state of their own.
func NewStatus ¶
NewStatus builds a status line writing to w. live enables the animation and in-place repainting; pass false for anything that is not an interactive terminal.
func (*Status) Note ¶
Note sets the trailing detail on the status line ("running tool memory_get"). It is ephemeral — use Log for something the user should still see afterwards.
func (*Status) Preview ¶
Preview shows a live tail of text that is still arriving — the words the model has streamed since the last line break. Without it a long paragraph looks identical to a stalled request: the spinner turns and nothing else moves until a newline finally lands. Whatever does not fit is cut from the FRONT, so the newest words are the ones on screen. Passing "" clears it.
func (*Status) SetInterval ¶
SetInterval overrides the frame interval (tests use a fast one).
func (*Status) SetTokenWord ¶
SetTokenWord supplies the translated word for the token unit.
func (*Status) SetWidth ¶
SetWidth supplies the terminal width, re-read on every paint so a resized window is honoured immediately. Without it the line is never truncated.
func (*Status) Start ¶
Start begins a run with verb as the current activity ("thinking", "routing"). Calling Start on an already-running status just changes the verb, so a caller that cannot tell whether a previous run ended is still correct.
func (*Status) Stats ¶
Stats renders the run's numbers for a closing line: "2.3s · 1.2k tokens". Empty when there is nothing to report.
func (*Status) Stop ¶
Stop ends the run, erases the status line and returns how long it took. It is idempotent, so `defer st.Stop()` alongside an explicit Stop is fine.