Documentation
¶
Overview ¶
Package console provides TTY-only interactive I/O utilities: prompts, secret input, and (eventually) formatted output, colors, and spinners. All functions require a real terminal; they return an error or zero value when stdout is not a TTY.
Index ¶
- func Error(msg string)
- func Errorf(format string, args ...any)
- func GreenCheck() string
- func Info(msg string)
- func Infof(format string, args ...any)
- func IsBothTTY() bool
- func IsStdoutTTY() bool
- func IsTTY() bool
- func OpenBrowser(rawURL string) error
- func PrintError(msg string)
- func PromptYesNo(question string) bool
- func ReadPrompt(label, defaultVal string) (string, error)
- func ReadSecret(prompt string) (string, error)
- func RedX() string
- func RenderMarkdown(text string) string
- func RenderTextBox(w io.Writer, box TextBox)
- func StripANSI(s string) string
- func Success(msg string)
- func Successf(format string, args ...any)
- func Warning(msg string)
- func Warningf(format string, args ...any)
- func WithBold(text string) string
- func WithBoldColor(c Color, text string) string
- func WithColor(c Color, text string) string
- func WithHighlight(text string) string
- func WithItalic(text string) string
- func YellowWarning() string
- type Color
- type StageProgress
- type TextBox
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GreenCheck ¶
func GreenCheck() string
GreenCheck returns a green ✓ when stdout is a TTY, or plain "✓" otherwise.
func IsBothTTY ¶
func IsBothTTY() bool
IsBothTTY returns true when both stdin and stdout are interactive terminals. Use this before launching interactive TUI programs.
func IsStdoutTTY ¶
func IsStdoutTTY() bool
IsStdoutTTY returns true when stdout is an interactive terminal.
func OpenBrowser ¶
OpenBrowser attempts to open rawURL in the default system browser. Returns an error if rawURL is not a well-formed http(s) URL, or if the browser cannot be launched; callers should fall back to printing the URL for the user to open manually.
func PrintError ¶
func PrintError(msg string)
PrintError prints msg to stderr. On a TTY the text is red; otherwise plain.
func PromptYesNo ¶
PromptYesNo prints question to stderr (appending " y/N ") and returns true if the user answers y or yes. Returns false if not a TTY.
func ReadPrompt ¶
ReadPrompt prints "label [defaultVal]: " to stderr and reads a line from the terminal. Returns defaultVal if the user presses enter without typing anything.
func ReadSecret ¶
ReadSecret prints prompt to stderr and reads a masked line from the terminal.
func RenderMarkdown ¶
RenderMarkdown renders a lightweight subset of Markdown (bold, italic, inline code, fenced code blocks, headers, lists, blockquotes, links) as ANSI-styled plain text for terminal display. It is a line-oriented best-effort renderer, not a full CommonMark implementation: nested/overlapping emphasis, tables, raw HTML, footnotes/reference-style links, checkbox lists, and cross-line paragraph rewrapping are not specially handled and degrade to plain text. Styling delegates to the With* helpers in this package, which already no-op to plain text when stdout isn't a TTY.
func RenderTextBox ¶
RenderTextBox writes box to w. Text is written verbatim — callers must not wrap it themselves; wrapping is left to the terminal so words and URLs are never split, and the output stays copy-paste clean.
func Success ¶
func Success(msg string)
Success prints msg to stdout in green, prefixed with a checkmark.
func Warning ¶
func Warning(msg string)
Warning prints msg to stdout in yellow, prefixed with a warning symbol.
func WithBoldColor ¶
WithBoldColor wraps text in a bold ANSI code, additionally tinted with c when c != 0, when stdout is a TTY.
func WithHighlight ¶
WithHighlight renders text in red on a dark grey background when stdout is a TTY, used for inline code spans.
func WithItalic ¶
WithItalic wraps text in an italic ANSI code (no color) when stdout is a TTY.
func YellowWarning ¶
func YellowWarning() string
YellowWarning returns a yellow ⚠ when stdout is a TTY, or plain "⚠" otherwise.
Types ¶
type Color ¶
type Color int
Color is an ANSI foreground color code.
const ( ColorBlack Color = 30 ColorRed Color = 31 ColorGreen Color = 32 ColorYellow Color = 33 ColorBlue Color = 34 ColorMagenta Color = 35 ColorCyan Color = 36 ColorWhite Color = 37 ColorBrightBlack Color = 90 ColorBrightRed Color = 91 ColorBrightGreen Color = 92 ColorBrightYellow Color = 93 ColorBrightBlue Color = 94 ColorBrightMagenta Color = 95 ColorBrightCyan Color = 96 ColorBrightWhite Color = 97 )
type StageProgress ¶
type StageProgress struct {
// contains filtered or unexported fields
}
StageProgress reports liveness for a long-running operation.
func StartStage ¶
func StartStage(text string) *StageProgress
StartStage starts an animated spinner on interactive terminals and heartbeat messages when stdout is redirected.
func (*StageProgress) Fail ¶
func (s *StageProgress) Fail(msg string)
Fail ends the stage with an error line.
func (*StageProgress) Success ¶
func (s *StageProgress) Success(msg string)
Success ends the stage with a success line carrying the elapsed time.
func (*StageProgress) Warn ¶
func (s *StageProgress) Warn(msg string)
Warn ends the stage with a warning line.