Documentation
¶
Overview ¶
Package ui provides shared CLI formatting helpers for terminal output. It centralizes color/style primitives, warning output, format flag validation, and task-directory checks so command and renderer code can reuse a single implementation.
Index ¶
- func ErrorHint(err error) (string, bool)
- func RequireTasksDir(tasksDir string) error
- func SetTermWidthFunc(fn func() int) func() int
- func SetWarningWriter(w io.Writer) io.Writer
- func TermWidth() int
- func TerminalWidth(fd int, defaultWidth int) int
- func Truncate(s string, maxLen int) string
- func ValidateFormat(format string, allowed []string) error
- func WarnTo(w io.Writer, format string, args ...any) error
- func Warnf(format string, args ...any)
- func WithHint(err error, hint string) error
- func WriterWidth(w io.Writer, defaultWidth int) int
- type ColorSet
- type TextWriter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ErrorHint ¶
ErrorHint returns the first remediation hint attached anywhere in err's wrap chain.
func RequireTasksDir ¶
RequireTasksDir checks that tasksDir exists and is a directory.
func SetTermWidthFunc ¶
SetTermWidthFunc replaces the terminal width detection function and returns the previous value so callers (typically tests) can restore it with defer.
func SetWarningWriter ¶
SetWarningWriter replaces the warning destination and returns the previous value so callers (typically tests) can restore it. Passing nil restores the default (os.Stderr at call time).
func TermWidth ¶
func TermWidth() int
TermWidth returns the current terminal width in columns. Returns 0 when stdout is not a terminal, signaling callers to skip width-based truncation so piped and test output stays deterministic.
func TerminalWidth ¶
TerminalWidth returns the width of the terminal attached to fd. If fd is not a terminal or the width cannot be determined, defaultWidth is returned.
func Truncate ¶
Truncate shortens s to at most maxLen runes, replacing the final rune with "…" when truncation is needed. If maxLen is zero or negative, s is returned unchanged.
func ValidateFormat ¶
ValidateFormat checks that format is one of the allowed values and returns a user-facing error if it is not.
func WarnTo ¶
WarnTo writes a formatted warning to w. When w is nil, it follows the same fallback chain as Warnf: the configured warning writer when present, or os.Stderr at call time.
func Warnf ¶
Warnf writes a formatted warning to stderr following the repo convention of "warning: ..." messages.
Types ¶
type ColorSet ¶
type ColorSet struct {
Bold func(a ...interface{}) string
Green func(a ...interface{}) string
Red func(a ...interface{}) string
Yellow func(a ...interface{}) string
Cyan func(a ...interface{}) string
Dim func(a ...interface{}) string
}
ColorSet holds color formatting functions used by CLI renderers.
func NewColorSet ¶
func NewColorSet() ColorSet
NewColorSet returns a ColorSet wired to ANSI terminal colors via fatih/color. Color output is automatically disabled when stdout is not a TTY.
type TextWriter ¶
type TextWriter struct {
// contains filtered or unexported fields
}
TextWriter wraps an io.Writer and remembers the first write error so callers can render linearly and check the final error once at the end.
func NewTextWriter ¶
func NewTextWriter(w io.Writer) TextWriter
NewTextWriter returns a TextWriter that writes to w.
func (*TextWriter) Err ¶
func (tw *TextWriter) Err() error
Err returns the first write error seen by the TextWriter.
func (*TextWriter) Print ¶
func (tw *TextWriter) Print(args ...any)
Print writes args to the underlying writer unless a previous write failed.
func (*TextWriter) Printf ¶
func (tw *TextWriter) Printf(format string, args ...any)
Printf formats according to format and writes the result unless a previous write failed.
func (*TextWriter) Println ¶
func (tw *TextWriter) Println(args ...any)
Println writes args with a trailing newline unless a previous write failed.