terminal

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

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

View Source
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 Error

func Error(message string)

Error prints a red "error:" prefix to stderr.

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

func FormatBytes(b uint64) string

FormatBytes formats a byte count into a human-readable string (e.g., "1.2 MB").

func FormatDuration

func FormatDuration(d time.Duration) string

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

func FormatNumber(n int64) string

FormatNumber formats an integer with comma separators (e.g., 1500 → "1,500").

func FormatTime

func FormatTime(s string) string

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 Info

func Info(message string)

Info prints a dim/faint message.

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 KeyValueW

func KeyValueW(label, value string, width int)

KeyValueW prints an indented key-value pair with a custom label width.

func List

func List(items []string)

List prints a numbered list of items.

func ProgressBar

func ProgressBar(label string, current, total uint64)

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 ReadLine

func ReadLine(prompt string) string

ReadLine prints a prompt and reads a line of input from stdin.

func Section

func Section(title string)

Section prints a bold section header: \n:: Title\n

func Success

func Success(message string)

Success prints a green checkmark followed by the message.

func Table

func Table(headers []string, rows [][]string)

Table prints an auto-aligned table with dim headers.

func Title

func Title(title string)

Title prints a titled header: \n# Title\n

func ToString

func ToString(color string, parts ...string) string

ToString wraps text with the given ANSI color code. This function does NOT respect TTY detection — it always applies the color. Used by the log handler which manages its own output destination.

func Warn

func Warn(message string)

Warn prints a yellow "warning:" prefix.

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

func NewProgress(label string, total uint64) *Progress

NewProgress creates a progress bar for total bytes. A zero total is valid and renders nothing.

func (*Progress) Add added in v1.8.0

func (p *Progress) Add(n uint64)

Add records n more bytes of progress and repaints, at most once per redrawInterval.

func (*Progress) Current added in v1.8.0

func (p *Progress) Current() uint64

Current returns the bytes reported so far.

func (*Progress) Done added in v1.8.0

func (p *Progress) Done()

Done paints the bar one final time and terminates the line. It is idempotent, so a caller that finishes the bar early can safely defer another call.

func (*Progress) SetLabel added in v1.8.0

func (p *Progress) SetLabel(label string)

SetLabel replaces the label 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

func NewSpinner(message string) *Spinner

NewSpinner creates and starts a new spinner. In non-TTY mode it prints the message once.

func (*Spinner) Stop

func (s *Spinner) Stop()

Stop halts the spinner animation and clears the line.

Jump to

Keyboard shortcuts

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