console

package
v3.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(msg string)

Error prints msg to stderr in red, prefixed with an X.

func Errorf

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

Errorf formats and prints an error line. See Error.

func GreenCheck

func GreenCheck() string

GreenCheck returns a green ✓ when stdout is a TTY, or plain "✓" otherwise.

func Info

func Info(msg string)

Info prints msg to stdout in cyan, prefixed with an arrow.

func Infof

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

Infof formats and prints an informational line. See Info.

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 IsTTY

func IsTTY() bool

func OpenBrowser

func OpenBrowser(rawURL string) error

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

func PromptYesNo(question string) bool

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

func ReadPrompt(label, defaultVal string) (string, error)

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

func ReadSecret(prompt string) (string, error)

ReadSecret prints prompt to stderr and reads a masked line from the terminal.

func RedX

func RedX() string

RedX returns a red ✗ when stdout is a TTY, or plain "✗" otherwise.

func RenderMarkdown

func RenderMarkdown(text string) string

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

func RenderTextBox(w io.Writer, box TextBox)

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 StripANSI

func StripANSI(s string) string

StripANSI removes ANSI/CSI escape sequences from s.

func Success

func Success(msg string)

Success prints msg to stdout in green, prefixed with a checkmark.

func Successf

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

Successf formats and prints a success line. See Success.

func Warning

func Warning(msg string)

Warning prints msg to stdout in yellow, prefixed with a warning symbol.

func Warningf

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

Warningf formats and prints a warning line. See Warning.

func WithBold

func WithBold(text string) string

WithBold wraps text in a bold ANSI code (no color) when stdout is a TTY.

func WithBoldColor

func WithBoldColor(c Color, text string) string

WithBoldColor wraps text in a bold ANSI code, additionally tinted with c when c != 0, when stdout is a TTY.

func WithColor

func WithColor(c Color, text string) string

WithColor wraps text in ANSI color codes when stdout is a TTY.

func WithHighlight

func WithHighlight(text string) string

WithHighlight renders text in red on a dark grey background when stdout is a TTY, used for inline code spans.

func WithItalic

func WithItalic(text string) string

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.

type TextBox

type TextBox struct {
	Icon        string
	Header      string
	HeaderColor Color
	Text        string
}

TextBox is a lightweight section marker: a colorized header line (optionally preceded by Icon, suppressed when stdout isn't a TTY), then Text verbatim, then a dimmed fixed-width closing rule.

Jump to

Keyboard shortcuts

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