Documentation
¶
Overview ¶
Package console provides terminal UI components and formatting utilities for the gh-aw CLI.
Naming Convention: Format* vs Render* ¶
Functions in this package follow a consistent naming convention:
Format* functions return a formatted string for a single item or message. They are pure string transformations with no side effects. Examples: FormatSuccessMessage, FormatErrorMessage, FormatFileSize, FormatCommandMessage, FormatProgressMessage.
Render* functions produce multi-element or structured output (tables, boxes, trees, structs). They may return strings, slices of strings, or write directly to output. They are used when the output requires layout or structural composition. Examples: RenderTable, RenderStruct, RenderTitleBox, RenderErrorBox, RenderInfoSection, RenderTree, RenderComposedSections.
Output Routing ¶
All diagnostic output (messages, warnings, errors) should be written to stderr. Structured data output (JSON, hashes, graphs) should be written to stdout. Prefer Print* helpers (or fmt.Fprintln(os.Stderr, ...) with Format* helpers) for diagnostic output.
Package console provides terminal UI components including spinners for long-running operations.
Spinner Component ¶
The spinner provides visual feedback during long-running operations with a minimal dot animation (⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏). It automatically adapts to the environment:
- TTY Detection: Spinners only animate in terminal environments (disabled in pipes/redirects)
- Accessibility: Respects ACCESSIBLE environment variable to disable animations
- Color Adaptation: Uses lipgloss adaptive colors for light/dark terminal themes
Implementation ¶
This spinner uses idiomatic Bubble Tea patterns with tea.NewProgram() for proper message handling and rendering pipeline integration. It includes thread-safe lifecycle management:
- Thread-safe start/stop tracking with mutex protection
- Safe to call Stop/StopWithMessage before Start (no-op or message-only)
- Prevents multiple concurrent Start calls
- No deadlock when stopping before goroutine initializes
- Leverages Bubble Tea's message passing for updates
Usage Example ¶
spinner := console.NewSpinner("Loading...")
spinner.Start()
// Long-running operation
spinner.Stop()
Accessibility ¶
Spinners respect the ACCESSIBLE environment variable. When ACCESSIBLE is set to any value, spinner animations are disabled to support screen readers and accessibility tools.
export ACCESSIBLE=1 gh aw compile workflow.md # Spinners will be disabled
Index ¶
- func ClearLine()
- func ClearScreen()
- func ConfirmAction(title, affirmative, negative string) (bool, error)
- func FormatBanner() string
- func FormatCommandMessage(command string) string
- func FormatCommandMessageStderr(command string) string
- func FormatError(err CompilerError) string
- func FormatErrorChain(err error) string
- func FormatErrorMessage(message string) string
- func FormatErrorTextStderr(text string) string
- func FormatErrorWithSuggestions(message string, suggestions []string) string
- func FormatFileSize(size int64) string
- func FormatInfoMessage(message string) string
- func FormatInfoMessageStderr(message string) string
- func FormatListItem(item string) string
- func FormatListItemStderr(item string) string
- func FormatNumber(n int) string
- func FormatProgressMessage(message string) string
- func FormatProgressMessageStderr(message string) string
- func FormatPromptMessage(message string) string
- func FormatPromptMessageStderr(message string) string
- func FormatSectionHeader(header string) string
- func FormatSectionHeaderStderr(header string) string
- func FormatSuccessMessage(message string) string
- func FormatSuccessMessageStderr(message string) string
- func FormatTableHeaderStderr(text string) string
- func FormatTokens(tokens int) string
- func FormatVerboseMessage(message string) string
- func FormatVerboseMessageStderr(message string) string
- func FormatWarningMessage(message string) string
- func FormatWarningMessageStderr(message string) string
- func IsAccessibleMode() bool
- func IsCancelled(err error) bool
- func LogVerbose(verbose bool, message string)
- func NewConfirmForm(confirm *huh.Confirm) *huh.Form
- func NewForm(groups ...*huh.Group) *huh.Form
- func NewInputForm(input *huh.Input) *huh.Form
- func NewSelectForm[T comparable](selectField *huh.Select[T]) *huh.Form
- func PrintBanner()
- func PrintCommandMessage(command string)
- func PrintError(err CompilerError)
- func PrintErrorChain(err error)
- func PrintErrorMessage(message string)
- func PrintErrorTextStderr(text string)
- func PrintInfoMessage(message string)
- func PrintListItem(item string)
- func PrintProgressMessage(message string)
- func PrintPromptMessage(message string)
- func PrintSectionHeader(header string)
- func PrintSuccessMessage(message string)
- func PrintTableHeaderStderr(text string)
- func PrintVerboseMessage(message string)
- func PrintWarningMessage(message string)
- func PromptSecretInput(title, description string) (string, error)
- func RenderComposedSections(sections []string)
- func RenderErrorBox(title string) []string
- func RenderInfoSection(content string) []string
- func RenderStruct(v any) string
- func RenderTable(config TableConfig) string
- func RenderTitleBox(title string, width int) []string
- func ResetTimeLocation()
- func SetTimeLocation(location *time.Location)
- func ShowInteractiveList(title string, items []ListItem) (string, error)
- func ShowWelcomeBanner(description string)
- func ToRelativePath(path string) string
- type CompilerError
- type ErrorPosition
- type FormField
- type ListItem
- type ProgressBar
- type SelectOption
- type SpinnerWrapper
- type TableConfig
- type TreeNode
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClearLine ¶
func ClearLine()
ClearLine clears the current line in the terminal if stderr is a TTY Uses ANSI escape codes: \r moves cursor to start, \033[K clears to end of line
func ClearScreen ¶
func ClearScreen()
ClearScreen clears the terminal screen if stderr is a TTY Uses ANSI escape codes for cross-platform compatibility
func ConfirmAction ¶
ConfirmAction shows an interactive confirmation dialog using Bubble Tea (huh) Returns true if the user confirms, false if they cancel or an error occurs
func FormatBanner ¶
func FormatBanner() string
FormatBanner returns the ASCII logo formatted with purple GitHub color theme. It applies the purple color styling when running in a terminal (TTY).
func FormatCommandMessage ¶
FormatCommandMessage formats a command execution message
func FormatCommandMessageStderr ¶ added in v0.83.0
FormatCommandMessageStderr formats a command execution message for stderr output.
func FormatError ¶
func FormatError(err CompilerError) string
FormatError formats a CompilerError with Rust-like rendering
func FormatErrorChain ¶ added in v0.59.1
FormatErrorChain formats an error and its full unwrapped chain in a reading-friendly way. For wrapped errors (fmt.Errorf with %w), each level of the chain is shown on a new indented line. For errors whose message contains newlines (e.g. errors.Join), each line is indented after the first.
func FormatErrorMessage ¶
FormatErrorMessage formats a simple error message (for stderr output)
func FormatErrorTextStderr ¶ added in v0.82.3
FormatErrorTextStderr formats plain error-styled text for stderr output.
func FormatErrorWithSuggestions ¶
FormatErrorWithSuggestions formats an error message with actionable suggestions
func FormatFileSize ¶
FormatFileSize formats file sizes in a human-readable way (e.g., "1.2 KB", "3.4 MB")
func FormatInfoMessage ¶
FormatInfoMessage formats an informational message
func FormatInfoMessageStderr ¶ added in v0.81.5
FormatInfoMessageStderr formats an informational message for stderr output.
func FormatListItem ¶
FormatListItem formats an item in a list
func FormatListItemStderr ¶ added in v0.81.5
FormatListItemStderr formats a list item for stderr output.
func FormatNumber ¶
FormatNumber formats large numbers in a human-readable way (e.g., "1k", "1.2k", "1.12M")
func FormatProgressMessage ¶
FormatProgressMessage formats a progress/activity message
func FormatProgressMessageStderr ¶ added in v0.83.0
FormatProgressMessageStderr formats a progress/activity message for stderr output.
func FormatPromptMessage ¶
FormatPromptMessage formats a user prompt message
func FormatPromptMessageStderr ¶ added in v0.83.0
FormatPromptMessageStderr formats a user prompt message for stderr output.
func FormatSectionHeader ¶
FormatSectionHeader formats a section header with proper styling
func FormatSectionHeaderStderr ¶ added in v0.81.5
FormatSectionHeaderStderr formats a section header for stderr output.
func FormatSuccessMessage ¶
FormatSuccessMessage formats a success message with styling
func FormatSuccessMessageStderr ¶ added in v0.81.5
FormatSuccessMessageStderr formats a success message for stderr output.
func FormatTableHeaderStderr ¶ added in v0.82.3
FormatTableHeaderStderr formats table header text for stderr output.
func FormatTokens ¶ added in v0.79.8
FormatTokens formats a token count as a compact human-readable string. Zero is rendered as "-"; values below 1000 are rendered as plain integers; values in the thousands are rendered with one decimal place and a "K" suffix; values in the millions are rendered with one decimal place and an "M" suffix.
Examples:
FormatTokens(0) // "-" FormatTokens(500) // "500" FormatTokens(1500) // "1.5K" FormatTokens(1200000) // "1.2M"
func FormatVerboseMessage ¶
FormatVerboseMessage formats verbose debugging output
func FormatVerboseMessageStderr ¶ added in v0.83.0
FormatVerboseMessageStderr formats verbose debugging output for stderr output.
func FormatWarningMessage ¶
FormatWarningMessage formats a warning message
func FormatWarningMessageStderr ¶ added in v0.83.0
FormatWarningMessageStderr formats a warning message for stderr output.
func IsAccessibleMode ¶
func IsAccessibleMode() bool
IsAccessibleMode detects if accessibility mode should be enabled based on environment variables. Accessibility mode is enabled when: - ACCESSIBLE environment variable is set to any value - TERM environment variable is set to "dumb" - NO_COLOR environment variable is set to any value
This function should be used by UI components to determine whether to: - Disable animations and spinners - Simplify interactive elements - Use plain text instead of fancy formatting
func IsCancelled ¶ added in v0.82.8
IsCancelled reports whether err represents a deliberate user cancellation (Ctrl-C / Esc before form submission, i.e. huh.ErrUserAborted). Use this to distinguish graceful cancellation from genuine failures.
func LogVerbose ¶
LogVerbose outputs a verbose message to stderr only when verbose mode is enabled. This is a convenience helper to avoid repetitive if-verbose checks throughout the codebase.
func NewConfirmForm ¶ added in v0.82.3
NewConfirmForm creates a themed, accessibility-aware single-confirm form.
func NewForm ¶ added in v0.82.3
NewForm creates a huh form with gh-aw's default theme and accessibility mode.
func NewInputForm ¶ added in v0.82.3
NewInputForm creates a themed, accessibility-aware single-input form.
func NewSelectForm ¶ added in v0.82.3
func NewSelectForm[T comparable](selectField *huh.Select[T]) *huh.Form
NewSelectForm creates a themed, accessibility-aware single-select form.
func PrintBanner ¶
func PrintBanner()
PrintBanner prints the ASCII logo to stderr with purple GitHub color theme. This is used by the --banner flag to display the logo at the start of command execution.
func PrintCommandMessage ¶ added in v0.83.0
func PrintCommandMessage(command string)
PrintCommandMessage formats and prints a command message to stderr.
func PrintError ¶ added in v0.83.0
func PrintError(err CompilerError)
PrintError formats and prints a compiler error to stderr. FormatError already includes a trailing newline, so Fprint is used to avoid emitting a spurious blank line.
func PrintErrorChain ¶ added in v0.83.0
func PrintErrorChain(err error)
PrintErrorChain formats and prints an error chain to stderr.
func PrintErrorMessage ¶ added in v0.83.0
func PrintErrorMessage(message string)
PrintErrorMessage formats and prints a simple error message to stderr.
func PrintErrorTextStderr ¶ added in v0.83.0
func PrintErrorTextStderr(text string)
PrintErrorTextStderr formats and prints error-styled text to stderr.
func PrintInfoMessage ¶ added in v0.83.0
func PrintInfoMessage(message string)
PrintInfoMessage formats and prints an info message to stderr.
func PrintListItem ¶ added in v0.83.0
func PrintListItem(item string)
PrintListItem formats and prints a list item to stderr.
func PrintProgressMessage ¶ added in v0.83.0
func PrintProgressMessage(message string)
PrintProgressMessage formats and prints a progress message to stderr.
func PrintPromptMessage ¶ added in v0.83.0
func PrintPromptMessage(message string)
PrintPromptMessage formats and prints a prompt message to stderr.
func PrintSectionHeader ¶ added in v0.83.0
func PrintSectionHeader(header string)
PrintSectionHeader formats and prints a section header to stderr.
func PrintSuccessMessage ¶ added in v0.83.0
func PrintSuccessMessage(message string)
PrintSuccessMessage formats and prints a success message to stderr.
func PrintTableHeaderStderr ¶ added in v0.83.0
func PrintTableHeaderStderr(text string)
PrintTableHeaderStderr formats and prints a table header to stderr.
func PrintVerboseMessage ¶ added in v0.83.0
func PrintVerboseMessage(message string)
PrintVerboseMessage formats and prints a verbose message to stderr.
func PrintWarningMessage ¶ added in v0.83.0
func PrintWarningMessage(message string)
PrintWarningMessage formats and prints a warning message to stderr.
func PromptSecretInput ¶ added in v0.42.14
PromptSecretInput shows an interactive password input prompt with masking The input is masked for security and includes validation Returns the entered secret value or an error
func RenderComposedSections ¶
func RenderComposedSections(sections []string)
RenderComposedSections composes and outputs a slice of sections to stderr
func RenderErrorBox ¶
RenderErrorBox renders an error/warning message with a rounded border box
func RenderInfoSection ¶
RenderInfoSection renders an info section with left border emphasis
func RenderStruct ¶
RenderStruct renders a Go struct to console output using reflection and struct tags. It supports: - Rendering structs as markdown-style headers with key-value pairs - Rendering slices as tables using the console table renderer - Rendering maps as markdown headers
Struct tags: - `console:"title:My Title"` - Sets the title for a section - `console:"header:Column Name"` - Sets the column header name for table columns - `console:"omitempty"` - Skips zero values - `console:"-"` - Skips the field entirely
func RenderTable ¶
func RenderTable(config TableConfig) string
RenderTable renders a formatted table using lipgloss/table package
func RenderTitleBox ¶
RenderTitleBox renders a title with a double border box in TTY mode
func ResetTimeLocation ¶ added in v0.77.5
func ResetTimeLocation()
ResetTimeLocation clears any configured location override for rendered times.
func SetTimeLocation ¶ added in v0.77.5
SetTimeLocation configures the location used when rendering time.Time values.
func ShowInteractiveList ¶
ShowInteractiveList displays an interactive list using huh.Select with arrow key navigation. Returns the selected item's value, or an error if cancelled or failed.
Use this for standalone pickers outside a form context; prefer huh.Select directly when building a multi-field form with WithTheme/WithAccessible applied to the whole form.
func ShowWelcomeBanner ¶ added in v0.45.5
func ShowWelcomeBanner(description string)
ShowWelcomeBanner clears the screen and displays the welcome banner for interactive commands. Use this at the start of interactive commands (add, trial, init) for a consistent experience.
func ToRelativePath ¶
ToRelativePath converts an absolute path to a relative path from the current working directory If the relative path contains "..", returns the absolute path instead for clarity
Types ¶
type CompilerError ¶
type CompilerError struct {
Position ErrorPosition
Type string // "error", "warning", "info"
Message string
Context []string // Source code lines for context
Hint string // Optional hint for fixing the error
}
CompilerError represents a structured compiler error with position information
type ErrorPosition ¶
ErrorPosition represents a position in a source file
type FormField ¶ added in v0.42.14
type FormField struct {
Type string // "input", "password", "confirm", "select"
Title string
Description string
Placeholder string
Value any // Pointer to the value to store the result
Options []SelectOption // For select fields
Validate func(string) error // For input/password fields
}
FormField represents a generic form field configuration
type ListItem ¶
type ListItem struct {
// contains filtered or unexported fields
}
ListItem represents an item in an interactive list
func NewListItem ¶
NewListItem creates a new list item with title, description, and value
type ProgressBar ¶
type ProgressBar struct {
// contains filtered or unexported fields
}
ProgressBar provides a reusable progress bar component with TTY detection and graceful fallback to text-based progress for non-TTY environments.
Modes:
- Determinate: When total size is known (shows percentage and progress)
- Indeterminate: When total size is unknown (shows activity indicator)
Visual Features:
- Scaled color blend effect from purple to cyan (adaptive for light/dark terminals)
- Smooth color transitions using bubbles v2 blend capabilities
- Blend scales with filled portion for enhanced visual feedback
- Works well in both light and dark terminal themes
The gradient provides visual appeal without affecting functionality:
- TTY mode: Visual progress bar with smooth gradient transitions
- Non-TTY mode: Text-based percentage with human-readable byte sizes
func NewProgressBar ¶
func NewProgressBar(total int64) *ProgressBar
NewProgressBar creates a new progress bar with the specified total size (determinate mode) The progress bar automatically adapts to TTY/non-TTY environments
func (*ProgressBar) Update ¶
func (p *ProgressBar) Update(current int64) string
Update updates the current progress and returns a formatted string In determinate mode:
- TTY: Returns a visual progress bar with gradient and percentage
- Non-TTY: Returns text percentage with human-readable sizes
In indeterminate mode:
- TTY: Returns a pulsing progress indicator
- Non-TTY: Returns processing indicator with current value
type SelectOption ¶ added in v0.42.14
SelectOption represents a selectable option with a label and value
type SpinnerWrapper ¶
type SpinnerWrapper struct {
// contains filtered or unexported fields
}
SpinnerWrapper wraps the spinner functionality with TTY detection and Bubble Tea program
func NewSpinner ¶
func NewSpinner(message string) *SpinnerWrapper
NewSpinner creates a new spinner with the given message using MiniDot style. Automatically disabled when not running in a TTY or when ACCESSIBLE env var is set.
func (*SpinnerWrapper) Start ¶
func (s *SpinnerWrapper) Start()
func (*SpinnerWrapper) Stop ¶
func (s *SpinnerWrapper) Stop()
func (*SpinnerWrapper) StopWithMessage ¶
func (s *SpinnerWrapper) StopWithMessage(msg string)
func (*SpinnerWrapper) UpdateMessage ¶
func (s *SpinnerWrapper) UpdateMessage(message string)
type TableConfig ¶
type TableConfig struct {
Headers []string
Rows [][]string
Title string
ShowTotal bool
TotalRow []string
// TTYFunc overrides the default stdout TTY check used to determine whether
// to apply styling. Set this when the rendered string will be written to a
// file descriptor other than stdout (e.g. tty.IsStderrTerminal for stderr).
TTYFunc func() bool
}
TableConfig represents configuration for table rendering