cliui

package
v0.1.0-beta.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 7, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package cliui contains the terminal-facing presentation primitives shared by Overgent commands. It deliberately knows nothing about Projects, credentials, repositories, or the service protocol.

Index

Constants

This section is empty.

Variables

View Source
var ErrCancelled = errors.New("selection cancelled")

Functions

func FormatElapsed

func FormatElapsed(duration time.Duration) string

FormatElapsed implements the product-wide elapsed-time contract: 47s, 12m 16s, and 1h 04m. Negative durations are clamped to zero. Durations never use a colon because that reads as wall-clock time.

func IsTerminal

func IsTerminal(stream any) bool

IsTerminal checks real file-backed streams. Non-file readers and writers are deliberately non-interactive so buffers, pipes, and test doubles never cause an accidental prompt.

func Select

func Select(terminal Terminal, title, detail string, choices []Choice) (int, error)

Select presents a compact native terminal picker. It uses arrow keys (or j/k), Enter to accept, and Escape/q/Ctrl-C to cancel. Callers should retain a numbered-line fallback for non-interactive streams.

func Wrap

func Wrap(text string, width int) []string

Wrap folds text to at most width runes per line. It preserves explicit paragraph breaks and hard-wraps unusually long machine facts such as paths.

func WriteFields

func WriteFields(writer io.Writer, width int, fields []Field) error

WriteFields renders aligned labels when there is room and switches to a stacked layout on narrow terminals. Values are wrapped without terminal box drawing, preserving the design system's single reading order.

Types

type Choice

type Choice struct {
	Label       string
	Description string
}

type ColorMode

type ColorMode uint8

ColorMode controls ANSI styling. Auto is suitable for normal command use.

const (
	ColorAuto ColorMode = iota
	ColorAlways
	ColorNever
)

type Field

type Field struct {
	Label string
	Value string
}

Field is one human-facing label and value. Values may contain newlines.

type Options

type Options struct {
	In           io.Reader
	Out          io.Writer
	Err          io.Writer
	LookupEnv    func(string) (string, bool)
	IsTerminal   func(any) bool
	Color        ColorMode
	Unicode      UnicodeMode
	DefaultWidth int
}

Options defines the streams and environment used for terminal detection. LookupEnv and IsTerminal are injectable so command tests never need to alter the process environment or depend on the terminal running the test suite.

type Output

type Output struct {
	Mode OutputMode
	Out  io.Writer
	Err  io.Writer
}

Output owns one command's output contract. Its writers are supplied by the caller; it never reads global process output or sends data elsewhere.

func NewOutput

func NewOutput(terminal Terminal, mode OutputMode) Output

func (Output) Encode

func (output Output) Encode(value any) error

Encode writes exactly one JSON value followed by a newline.

func (Output) EncodeLine

func (output Output) EncodeLine(value any) error

EncodeLine writes one compact JSON value for a JSON Lines stream.

func (Output) Printf

func (output Output) Printf(format string, args ...any) error

Printf emits human prose and refuses to contaminate structured stdout.

type OutputMode

type OutputMode uint8

OutputMode distinguishes prose intended for a person from stable structured output intended for another program.

const (
	OutputHuman OutputMode = iota
	OutputJSON
	OutputJSONLines
)

func ParseOutputMode

func ParseOutputMode(value string) (OutputMode, error)

ParseOutputMode converts the public flag spelling to a mode.

func (OutputMode) MachineReadable

func (mode OutputMode) MachineReadable() bool

type Style

type Style uint8

Style is intentionally limited to Overgent's status and hierarchy channels. Adding a severity rainbow here would violate the product design system.

const (
	StyleAlert Style = iota
	StyleLive
	StyleMuted
	StyleBold
)

type Terminal

type Terminal struct {
	// contains filtered or unexported fields
}

Terminal is an immutable description of one command invocation's terminal.

func NewTerminal

func NewTerminal(options Options) Terminal

NewTerminal returns a terminal description with safe process defaults.

func (Terminal) Animated

func (terminal Terminal) Animated() bool

Animated reports whether the caller may rewrite the current line in place. It requires a real terminal and refuses a dumb one, where cursor control is not dependable. Callers must render a correct, complete result without it: in-place updating is an enhancement, never the only way a fact is shown.

func (Terminal) ColorEnabled

func (terminal Terminal) ColorEnabled() bool

ColorEnabled applies common CLI conventions: automatic color requires a TTY, is disabled by NO_COLOR even when its value is empty, and is disabled for a dumb terminal. An explicit ColorAlways overrides environment detection.

func (Terminal) Err

func (terminal Terminal) Err() io.Writer

func (Terminal) In

func (terminal Terminal) In() io.Reader

func (Terminal) InputIsTerminal

func (terminal Terminal) InputIsTerminal() bool

InputIsTerminal reports whether input is an interactive terminal.

func (Terminal) Interactive

func (terminal Terminal) Interactive() bool

Interactive is true only when both input and output are terminals. Callers should use this before prompting; output being a TTY alone is insufficient.

func (Terminal) Out

func (terminal Terminal) Out() io.Writer

func (Terminal) OutputIsTerminal

func (terminal Terminal) OutputIsTerminal() bool

OutputIsTerminal reports whether human output is attached to a terminal.

func (Terminal) Style

func (terminal Terminal) Style(style Style, text string) string

Style applies an ANSI style when color is enabled. The returned text always carries the same meaning without ANSI escapes.

func (Terminal) Symbol

func (terminal Terminal) Symbol(unicodeValue, asciiValue string) string

Symbol selects a Unicode glyph or its plain ASCII equivalent.

func (Terminal) UnicodeEnabled

func (terminal Terminal) UnicodeEnabled() bool

UnicodeEnabled reports whether glyphs should use their Unicode form. Explicit C/POSIX or non-UTF locales get ASCII, as does TERM=dumb. A missing locale is treated as UTF-8 because Go strings and supported modern terminals are UTF-8; callers can always request UnicodeNever.

func (Terminal) Width

func (terminal Terminal) Width() int

Width returns the output terminal width, then a valid COLUMNS value, then the configured default. Implausible widths are ignored to keep layouts usable.

type UnicodeMode

type UnicodeMode uint8

UnicodeMode controls whether presentation glyphs may use Unicode.

const (
	UnicodeAuto UnicodeMode = iota
	UnicodeAlways
	UnicodeNever
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL