ui

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2026 License: MIT Imports: 7 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorHint

func ErrorHint(err error) (string, bool)

ErrorHint returns the first remediation hint attached anywhere in err's wrap chain.

func RequireTasksDir

func RequireTasksDir(tasksDir string) error

RequireTasksDir checks that tasksDir exists and is a directory.

func SetTermWidthFunc

func SetTermWidthFunc(fn func() int) func() int

SetTermWidthFunc replaces the terminal width detection function and returns the previous value so callers (typically tests) can restore it with defer.

func SetWarningWriter

func SetWarningWriter(w io.Writer) io.Writer

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

func TerminalWidth(fd int, defaultWidth int) int

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

func Truncate(s string, maxLen int) string

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

func ValidateFormat(format string, allowed []string) error

ValidateFormat checks that format is one of the allowed values and returns a user-facing error if it is not.

func WarnTo

func WarnTo(w io.Writer, format string, args ...any) error

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

func Warnf(format string, args ...any)

Warnf writes a formatted warning to stderr following the repo convention of "warning: ..." messages.

func WithHint

func WithHint(err error, hint string) error

WithHint wraps err with a remediation hint that callers can render separately from the main error line.

func WriterWidth

func WriterWidth(w io.Writer, defaultWidth int) int

WriterWidth returns the terminal width for w if w exposes a file descriptor attached to a terminal. Otherwise it returns defaultWidth.

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.

Jump to

Keyboard shortcuts

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