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.
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 FormatCountMessage(message 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 FormatListHeader(header string) string
- func FormatListItem(item string) string
- func FormatListItemStderr(item string) string
- func FormatLocationMessage(message 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 LayoutEmphasisBox(content string, color any) string
- func LayoutInfoSection(label, value string) string
- func LayoutJoinVertical(sections ...string) string
- func LayoutTitleBox(title string, width int) string
- func LogVerbose(verbose bool, message string)
- 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 PromptInput(title, description, placeholder string) (string, error)
- func PromptInputWithValidation(title, description, placeholder string, validate func(string) error) (string, error)
- func PromptMultiSelect(title, description string, options []SelectOption, limit int) ([]string, error)
- func PromptSecretInput(title, description string) (string, error)
- func PromptSelect(title, description string, options []SelectOption) (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 RenderTree(root TreeNode) string
- func ResetTimeLocation()
- func RunForm(fields []FormField) error
- 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 ¶
func FormatBanner ¶
func FormatBanner() string
func FormatCommandMessage ¶
func FormatCommandMessageStderr ¶ added in v0.83.0
func FormatCountMessage ¶
func FormatError ¶
func FormatError(err CompilerError) string
func FormatErrorChain ¶ added in v0.83.0
FormatErrorChain formats an error and its full unwrapped chain. In the WASM build there is no rich terminal styling; the top-level error message is sufficient for the JSON-based diagnostics that WASM consumers produce. Full chain formatting is provided by the non-WASM implementation.
func FormatErrorMessage ¶
func FormatErrorTextStderr ¶ added in v0.82.3
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 ¶
func FormatInfoMessageStderr ¶ added in v0.83.0
func FormatListHeader ¶
func FormatListItem ¶
func FormatListItemStderr ¶ added in v0.83.0
func FormatLocationMessage ¶
func FormatNumber ¶
FormatNumber formats large numbers in a human-readable way (e.g., "1k", "1.2k", "1.12M")
func FormatProgressMessage ¶
func FormatProgressMessageStderr ¶ added in v0.83.0
func FormatPromptMessage ¶
func FormatPromptMessageStderr ¶ added in v0.83.0
func FormatSectionHeader ¶
func FormatSectionHeaderStderr ¶ added in v0.83.0
func FormatSuccessMessage ¶
func FormatSuccessMessageStderr ¶ added in v0.83.0
func FormatTableHeaderStderr ¶ added in v0.82.3
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 ¶
func FormatVerboseMessageStderr ¶ added in v0.83.0
func FormatWarningMessage ¶
func FormatWarningMessageStderr ¶ added in v0.83.0
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 LayoutEmphasisBox ¶
func LayoutInfoSection ¶
func LayoutJoinVertical ¶
func LayoutTitleBox ¶
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 PrintBanner ¶
func PrintBanner()
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 PromptInput ¶ added in v0.42.14
func PromptInputWithValidation ¶ added in v0.42.14
func PromptMultiSelect ¶ added in v0.42.14
func PromptMultiSelect(title, description string, options []SelectOption, limit int) ([]string, error)
func PromptSecretInput ¶ added in v0.42.14
func PromptSelect ¶ added in v0.42.14
func PromptSelect(title, description string, options []SelectOption) (string, error)
func RenderComposedSections ¶
func RenderComposedSections(sections []string)
func RenderErrorBox ¶
func RenderInfoSection ¶
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
func RenderTitleBox ¶
func RenderTree ¶
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 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
}
func NewIndeterminateProgressBar ¶
func NewIndeterminateProgressBar() *ProgressBar
func NewProgressBar ¶
func NewProgressBar(total int64) *ProgressBar
func (*ProgressBar) Update ¶
func (p *ProgressBar) Update(current int64) string
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
}
func NewSpinner ¶
func NewSpinner(message string) *SpinnerWrapper
func (*SpinnerWrapper) IsEnabled ¶
func (s *SpinnerWrapper) IsEnabled() bool
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