Documentation
¶
Overview ¶
Package renderer provides output formatting implementations for CLI commands.
ConsoleRenderer outputs human-readable formatted text to a terminal using Lip Gloss styles from the Theme. It automatically respects NO_COLOR and other accessibility environment variables through the Theme system.
Package renderer provides output formatting implementations for CLI commands.
JSONRenderer outputs machine-readable JSON for scripting and automation. It accumulates all output (titles, sections, tables) and writes formatted JSON when Flush() is called.
Package renderer provides implementations of CLI output formatting.
This package provides concrete implementations of the interfaces.Renderer interface per ADR-002 (Interface Placement in Consumer Packages).
The Renderer pattern separates business logic from output formatting, enabling multiple output modes (console, JSON, TUI) without duplicating code. Commands produce data, Renderers display it in the appropriate format.
Available implementations:
- ConsoleRenderer: Human-friendly output with colors and formatting
- JSONRenderer: Machine-readable JSON output for scripting
- TUIRenderer: Interactive terminal UI (future implementation)
Package renderer provides type definitions for renderer implementations.
All interface types (Renderer, Section, Table, ProgressSpec, Progress) have been moved to internal/cli/interfaces per ADR-002.
This file is retained for any future renderer-specific types that are not part of the public interface.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConsoleProgress ¶
type ConsoleProgress struct {
// contains filtered or unexported fields
}
ConsoleProgress implements Progress interface using Bubbles progress component.
Renders a progress bar using ViewAs for standalone rendering without Bubble Tea event loop. Updates are written in-place using \r prefix.
func (*ConsoleProgress) Done ¶
func (p *ConsoleProgress) Done()
Done completes the progress bar and adds a newline.
Marks the progress as complete and writes a final newline to move to the next line. Subsequent calls are idempotent (no additional output).
func (*ConsoleProgress) Increment ¶
func (p *ConsoleProgress) Increment(n int64)
Increment updates the progress bar by the specified amount.
Calculates the new percentage and renders the progress bar in-place using \r (carriage return). Handles edge cases like zero total and overflow gracefully. If a label was provided, it is displayed before the progress bar using the Muted theme style.
type ConsoleRenderer ¶
type ConsoleRenderer struct {
// contains filtered or unexported fields
}
ConsoleRenderer implements the Renderer interface for human-readable terminal output.
ConsoleRenderer writes formatted output to an io.Writer using Lip Gloss styles from the Theme. All styling automatically respects NO_COLOR and other accessibility environment variables.
Example usage:
renderer := NewConsoleRenderer(os.Stdout)
renderer.Title("Project Created")
renderer.Section(Section{
Title: "Configuration",
Body: "Using Chi router with templ templates",
})
renderer.Flush()
ConsoleRenderer is safe for concurrent use from multiple goroutines as long as the underlying io.Writer is thread-safe.
func NewConsoleRenderer ¶
func NewConsoleRenderer(out io.Writer) *ConsoleRenderer
NewConsoleRenderer creates a new ConsoleRenderer that writes to the provided io.Writer.
The writer parameter is typically os.Stdout or os.Stderr, but can be any io.Writer for testing purposes (e.g., bytes.Buffer).
Example:
renderer := NewConsoleRenderer(os.Stdout)
func (*ConsoleRenderer) Flush ¶
func (r *ConsoleRenderer) Flush() error
Flush ensures all buffered output is written.
For ConsoleRenderer, this is a no-op since fmt.Fprintln writes directly to the underlying io.Writer without buffering. This method exists to satisfy the Renderer interface and maintain consistency with other renderer implementations that may need flushing (like JSONRenderer).
Always returns nil.
func (*ConsoleRenderer) Progress ¶
func (r *ConsoleRenderer) Progress(spec interfaces.ProgressSpec) interfaces.Progress
Progress creates a progress bar for tracking long-running operations.
Returns a ConsoleProgress instance that renders using Bubbles progress component with gradient colors. Uses ViewAs for standalone rendering without requiring a Bubble Tea event loop.
The progress bar updates in-place using \r (carriage return) and completes with a newline when Done() is called.
Example:
progress := renderer.Progress(interfaces.ProgressSpec{Label: "Downloading", Total: 100})
progress.Increment(25) // 25%
progress.Increment(50) // 75%
progress.Increment(25) // 100%
progress.Done() // Adds newline
func (*ConsoleRenderer) Section ¶
func (r *ConsoleRenderer) Section(sec interfaces.Section)
Section displays a titled section with body content.
If the section has a title, it is rendered using Theme.Title style. The body is rendered as plain text below the title. Both title and body are optional and will only be rendered if non-empty.
Example:
renderer.Section(interfaces.Section{
Title: "Database Configuration",
Body: "Using LibSQL with migrations enabled",
})
func (*ConsoleRenderer) Table ¶
func (r *ConsoleRenderer) Table(t interfaces.Table)
Table displays structured tabular data with aligned columns.
Headers are rendered using Theme.Title style. All columns are automatically sized to fit their content with proper alignment. Empty tables produce no output.
Example:
renderer.Table(interfaces.Table{
Headers: []string{"File", "Status", "Lines"},
Rows: [][]string{
{"user.go", "created", "42"},
{"user_test.go", "created", "128"},
},
})
func (*ConsoleRenderer) Title ¶
func (r *ConsoleRenderer) Title(s string)
Title displays a prominent title using the Theme.Title style.
The title is rendered with bold purple styling (when colors are enabled) and written on its own line.
Example:
renderer.Title("Welcome to Tracks")
type JSONRenderer ¶
type JSONRenderer struct {
// contains filtered or unexported fields
}
JSONRenderer implements the Renderer interface for machine-readable JSON output.
JSONRenderer accumulates all CLI output (titles, sections, tables) in memory and writes formatted JSON when Flush() is called. This enables CLI integration with scripts, CI/CD pipelines, and other tools that need structured data.
Example usage:
renderer := NewJSONRenderer(os.Stdout)
renderer.Title("Project Created")
renderer.Section(Section{
Title: "Configuration",
Body: "Using Chi router with templ templates",
})
renderer.Table(Table{
Headers: []string{"File", "Status"},
Rows: [][]string{{"user.go", "created"}},
})
renderer.Flush()
Output:
{
"title": "Project Created",
"sections": [
{"title": "Configuration", "body": "Using Chi router with templ templates"}
],
"tables": [
{"headers": ["File", "Status"], "rows": [["user.go", "created"]]}
]
}
JSONRenderer is not safe for concurrent use from multiple goroutines. Each command should use a single JSONRenderer instance sequentially.
func NewJSONRenderer ¶
func NewJSONRenderer(out io.Writer) *JSONRenderer
NewJSONRenderer creates a new JSONRenderer that writes to the provided io.Writer.
The writer parameter is typically os.Stdout or os.Stderr, but can be any io.Writer for testing purposes (e.g., bytes.Buffer).
Example:
renderer := NewJSONRenderer(os.Stdout)
func (*JSONRenderer) Flush ¶
func (r *JSONRenderer) Flush() error
Flush writes all accumulated data as formatted JSON.
The JSON output is indented with 2 spaces for readability. After flushing, the renderer's internal data is not cleared, so subsequent calls to Flush will output the same data (potentially with additions from new Title/Section/Table calls).
Always returns nil unless the underlying io.Writer returns an error.
Example:
renderer.Title("Project Created")
renderer.Flush() // Writes JSON to output
func (*JSONRenderer) Progress ¶
func (r *JSONRenderer) Progress(spec interfaces.ProgressSpec) interfaces.Progress
Progress returns a no-op Progress implementation for JSON output.
JSON output is not suitable for incremental progress updates, so this method returns a Progress instance that discards all updates. Progress is designed for interactive terminals, not machine-readable output.
Example:
progress := renderer.Progress(interfaces.ProgressSpec{Label: "Downloading", Total: 100})
progress.Increment(50) // No output
progress.Done() // No output
func (*JSONRenderer) Section ¶
func (r *JSONRenderer) Section(sec interfaces.Section)
Section adds a titled section to the JSON output.
Sections are accumulated and rendered as an array in the "sections" field. Multiple calls to Section will append to the array.
Example:
renderer.Section(interfaces.Section{
Title: "Database Configuration",
Body: "Using LibSQL with migrations enabled",
})
func (*JSONRenderer) Table ¶
func (r *JSONRenderer) Table(t interfaces.Table)
Table adds structured tabular data to the JSON output.
Tables are accumulated and rendered as an array in the "tables" field. Multiple calls to Table will append to the array.
Example:
renderer.Table(interfaces.Table{
Headers: []string{"File", "Status", "Lines"},
Rows: [][]string{
{"user.go", "created", "42"},
{"user_test.go", "created", "128"},
},
})
func (*JSONRenderer) Title ¶
func (r *JSONRenderer) Title(s string)
Title stores a prominent title for the JSON output.
The title is rendered as the "title" field in the JSON output. Multiple calls to Title will overwrite previous values.
Example:
renderer.Title("Welcome to Tracks")