Documentation
¶
Overview ¶
Package terminal provides styled CLI output with TTY-aware ANSI colors, progress indicators, and spinners. Ported from the Java CLI's Terminal class.
Index ¶
- Constants
- func ClearLine()
- func Error(message string)
- func ErrorList(items []string)
- func FormatBytes(b uint64) string
- func FormatDuration(d time.Duration) string
- func FormatNumber(n int64) string
- func FormatTime(s string) string
- func Info(message string)
- func IsTTY() bool
- func KeyValue(label, value string)
- func KeyValueW(label, value string, width int)
- func List(items []string)
- func ProgressBar(label string, current, total uint64)
- func ReadLine(prompt string) string
- func Section(title string)
- func Success(message string)
- func Table(headers []string, rows [][]string)
- func Title(title string)
- func ToString(color string, parts ...string) string
- func Warn(message string)
- type Progress
- type Spinner
Constants ¶
const ( Reset = "\033[0m" Bold = "\033[1m" Dim = "\033[2m" Default = "\033[39m" Black = "\033[30m" Red = "\033[31m" Green = "\033[32m" Yellow = "\033[33m" Blue = "\033[34m" Magenta = "\033[35m" Cyan = "\033[36m" White = "\033[37m" BrightBlack = "\033[90m" BrightRed = "\033[91m" BrightGreen = "\033[92m" BrightYellow = "\033[93m" BrightBlue = "\033[94m" BrightMagenta = "\033[95m" BrightCyan = "\033[96m" BrightWhite = "\033[97m" )
ANSI color and style constants
Variables ¶
This section is empty.
Functions ¶
func ClearLine ¶ added in v1.9.0
func ClearLine()
ClearLine erases the current line, so that a message can be printed over a progress bar without leaving half a bar behind it. It is a no-op when output is not a terminal, where there is no line to overwrite.
func ErrorList ¶ added in v1.9.0
func ErrorList(items []string)
ErrorList prints a numbered list of items to stderr, so that a list belonging to an Error message stays on the same stream as its heading.
func FormatBytes ¶
FormatBytes formats a byte count into a human-readable string (e.g., "1.2 MB").
func FormatDuration ¶
FormatDuration formats a duration for human display with at most 1 decimal place. Examples: "245 ms", "2.5 s", "1.2 min", "3 h", "2 d"
func FormatNumber ¶
FormatNumber formats an integer with comma separators (e.g., 1500 → "1,500").
func FormatTime ¶
FormatTime parses an ISO8601/RFC3339 time string and returns it formatted in RFC1123 in the local timezone. If parsing fails, the original string is returned.
func IsTTY ¶
func IsTTY() bool
IsTTY returns true if stdout is a terminal and color output is not suppressed.
func KeyValue ¶
func KeyValue(label, value string)
KeyValue prints an indented key-value pair with the default label width (15).
func ProgressBar ¶
ProgressBar displays a progress bar. In TTY mode it overwrites the current line. In non-TTY mode it only prints the final line when current == total. Silently returns when total is 0.
func Success ¶
func Success(message string)
Success prints a green checkmark followed by the message.
Types ¶
type Progress ¶ added in v1.8.0
type Progress struct {
// contains filtered or unexported fields
}
Progress is a progress bar measured in bytes. It is safe for concurrent use: bytes may be reported from many upload goroutines while the label is updated from another. Nothing is drawn until Add reports the first bytes, and the zero total case draws nothing at all.
func NewProgress ¶ added in v1.8.0
NewProgress creates a progress bar for total bytes. A zero total is valid and renders nothing.
func (*Progress) Add ¶ added in v1.8.0
Add records n more bytes of progress and repaints, at most once per redrawInterval.
type Spinner ¶
type Spinner struct {
// contains filtered or unexported fields
}
Spinner displays an animated braille spinner with a message.
func NewSpinner ¶
NewSpinner creates and starts a new spinner. In non-TTY mode it prints the message once.