Documentation
¶
Overview ¶
Package output provides CLI output formatting capabilities including progress indicators, JSON output, and color-coded output.
Index ¶
- func Data(data interface{})
- func Error(message string, fields ...map[string]interface{})
- func Info(message string, fields ...map[string]interface{})
- func IsColorSupported() bool
- func SetGlobalFormatter(formatter Formatter)
- func Success(message string, fields ...map[string]interface{})
- func Suggest(w io.Writer, target string, candidates []string)
- func Table(headers []string, rows [][]string)
- func Warning(message string, fields ...map[string]interface{})
- type DataOutput
- type Formatter
- type FormatterOptions
- type HumanFormatter
- func (h *HumanFormatter) Data(data interface{})
- func (h *HumanFormatter) Error(message string, fields ...map[string]interface{})
- func (h *HumanFormatter) GetLogLevel() zerolog.Level
- func (h *HumanFormatter) Info(message string, fields ...map[string]interface{})
- func (h *HumanFormatter) IsColorEnabled() bool
- func (h *HumanFormatter) SetColorEnabled(enabled bool)
- func (h *HumanFormatter) SetQuiet(quiet bool)
- func (h *HumanFormatter) SetVerbose(verbose bool)
- func (h *HumanFormatter) SetWriter(w io.Writer)
- func (h *HumanFormatter) Success(message string, fields ...map[string]interface{})
- func (h *HumanFormatter) Table(headers []string, rows [][]string)
- func (h *HumanFormatter) Warning(message string, fields ...map[string]interface{})
- type JSONFormatter
- func (j *JSONFormatter) Data(data interface{})
- func (j *JSONFormatter) Error(message string, fields ...map[string]interface{})
- func (j *JSONFormatter) Info(message string, fields ...map[string]interface{})
- func (j *JSONFormatter) SetQuiet(quiet bool)
- func (j *JSONFormatter) SetWriter(w io.Writer)
- func (j *JSONFormatter) Success(message string, fields ...map[string]interface{})
- func (j *JSONFormatter) Table(headers []string, rows [][]string)
- func (j *JSONFormatter) Warning(message string, fields ...map[string]interface{})
- type NoOpProgressIndicator
- type OutputFormat
- type OutputMessage
- type ProgressIndicator
- type ProgressOptions
- type TableOutput
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsColorSupported ¶
func IsColorSupported() bool
IsColorSupported checks if the terminal supports color output
Example ¶
ExampleIsColorSupported demonstrates checking color support
package main
import (
"fmt"
"github.com/snowdreamtech/unirtm/internal/cli/output"
)
func main() {
if output.IsColorSupported() {
fmt.Println("Terminal supports colors")
} else {
fmt.Println("Terminal does not support colors")
}
}
Output:
func SetGlobalFormatter ¶
func SetGlobalFormatter(formatter Formatter)
SetGlobalFormatter sets the global formatter instance
Example ¶
ExampleSetGlobalFormatter demonstrates using the global formatter
package main
import (
"github.com/snowdreamtech/unirtm/internal/cli/output"
)
func main() {
// Set up global formatter
formatter := output.NewFormatter(output.FormatterOptions{
Format: output.FormatHuman,
NoColor: true,
})
output.SetGlobalFormatter(formatter)
// Use global formatter functions
output.Info("Starting operation")
output.Success("Operation completed")
output.Warning("Potential issue detected")
output.Error("Operation failed")
}
Output:
Types ¶
type DataOutput ¶
type DataOutput struct {
Timestamp string `json:"timestamp"`
Data interface{} `json:"data"`
}
DataOutput represents structured data output
type Formatter ¶
type Formatter interface {
// Info outputs an informational message
Info(message string, fields ...map[string]interface{})
// Success outputs a success message
Success(message string, fields ...map[string]interface{})
// Warning outputs a warning message
Warning(message string, fields ...map[string]interface{})
// Error outputs an error message
Error(message string, fields ...map[string]interface{})
// Data outputs structured data
Data(data interface{})
// Table outputs tabular data
Table(headers []string, rows [][]string)
// SetWriter sets the output writer
SetWriter(w io.Writer)
}
Formatter defines the interface for output formatting
func DefaultFormatter ¶
func DefaultFormatter() Formatter
DefaultFormatter returns a formatter with default settings
func GetGlobalFormatter ¶
func GetGlobalFormatter() Formatter
GetGlobalFormatter returns the global formatter instance If no formatter has been set, it returns a default human formatter
func NewFormatter ¶
func NewFormatter(opts FormatterOptions) Formatter
NewFormatter creates a new formatter based on the specified options
Example ¶
ExampleNewFormatter demonstrates creating different formatter types
package main
import (
"github.com/snowdreamtech/unirtm/internal/cli/output"
)
func main() {
// Create a human-readable formatter
humanFormatter := output.NewFormatter(output.FormatterOptions{
Format: output.FormatHuman,
NoColor: true, // Disable colors for example output
})
humanFormatter.Info("Starting installation")
humanFormatter.Success("Installation completed")
// Create a JSON formatter
jsonFormatter := output.NewFormatter(output.FormatterOptions{
Format: output.FormatJSON,
})
jsonFormatter.Info("Starting installation")
// Output will be JSON formatted
}
Output:
type FormatterOptions ¶
type FormatterOptions struct {
// Format specifies the output format (human or json)
Format OutputFormat
// NoColor disables color output for human format
NoColor bool
// Color specifies the color mode (auto, always, never)
Color string
// Writer is the output writer (defaults to os.Stdout)
Writer io.Writer
// Quiet suppresses non-essential output
Quiet bool
// Verbose enables verbose output
Verbose bool
}
FormatterOptions contains options for creating a formatter
type HumanFormatter ¶
type HumanFormatter struct {
// contains filtered or unexported fields
}
HumanFormatter implements human-readable output with color support
func (*HumanFormatter) Data ¶
func (h *HumanFormatter) Data(data interface{})
Data outputs structured data in a human-readable format
func (*HumanFormatter) Error ¶
func (h *HumanFormatter) Error(message string, fields ...map[string]interface{})
Error outputs an error message
func (*HumanFormatter) GetLogLevel ¶
func (h *HumanFormatter) GetLogLevel() zerolog.Level
GetLogLevel returns the appropriate zerolog level based on formatter settings
func (*HumanFormatter) Info ¶
func (h *HumanFormatter) Info(message string, fields ...map[string]interface{})
Info outputs an informational message
func (*HumanFormatter) IsColorEnabled ¶
func (h *HumanFormatter) IsColorEnabled() bool
IsColorEnabled returns true if color output is enabled
func (*HumanFormatter) SetColorEnabled ¶
func (h *HumanFormatter) SetColorEnabled(enabled bool)
SetColorEnabled enables or disables color output
func (*HumanFormatter) SetQuiet ¶
func (h *HumanFormatter) SetQuiet(quiet bool)
SetQuiet enables or disables quiet mode
func (*HumanFormatter) SetVerbose ¶
func (h *HumanFormatter) SetVerbose(verbose bool)
SetVerbose enables or disables verbose mode
func (*HumanFormatter) SetWriter ¶
func (h *HumanFormatter) SetWriter(w io.Writer)
SetWriter sets the output writer
func (*HumanFormatter) Success ¶
func (h *HumanFormatter) Success(message string, fields ...map[string]interface{})
Success outputs a success message
func (*HumanFormatter) Table ¶
func (h *HumanFormatter) Table(headers []string, rows [][]string)
Table outputs tabular data
func (*HumanFormatter) Warning ¶
func (h *HumanFormatter) Warning(message string, fields ...map[string]interface{})
Warning outputs a warning message
type JSONFormatter ¶
type JSONFormatter struct {
// contains filtered or unexported fields
}
JSONFormatter implements JSON output for scripting and automation
Example ¶
ExampleJSONFormatter demonstrates JSON output format
package main
import (
"github.com/snowdreamtech/unirtm/internal/cli/output"
)
func main() {
formatter := output.NewFormatter(output.FormatterOptions{
Format: output.FormatJSON,
})
formatter.Info("Starting installation", map[string]interface{}{
"tool": "node",
"version": "20.0.0",
})
formatter.Success("Installation completed", map[string]interface{}{
"tool": "node",
"version": "20.0.0",
"install_path": "/usr/local/bin/node",
})
// Output will be JSON formatted with timestamps
}
Output:
func (*JSONFormatter) Data ¶
func (j *JSONFormatter) Data(data interface{})
Data outputs structured data in JSON format
func (*JSONFormatter) Error ¶
func (j *JSONFormatter) Error(message string, fields ...map[string]interface{})
Error outputs an error message in JSON format
func (*JSONFormatter) Info ¶
func (j *JSONFormatter) Info(message string, fields ...map[string]interface{})
Info outputs an informational message in JSON format
func (*JSONFormatter) SetQuiet ¶
func (j *JSONFormatter) SetQuiet(quiet bool)
SetQuiet enables or disables quiet mode
func (*JSONFormatter) SetWriter ¶
func (j *JSONFormatter) SetWriter(w io.Writer)
SetWriter sets the output writer
func (*JSONFormatter) Success ¶
func (j *JSONFormatter) Success(message string, fields ...map[string]interface{})
Success outputs a success message in JSON format
func (*JSONFormatter) Table ¶
func (j *JSONFormatter) Table(headers []string, rows [][]string)
Table outputs tabular data in JSON format
func (*JSONFormatter) Warning ¶
func (j *JSONFormatter) Warning(message string, fields ...map[string]interface{})
Warning outputs a warning message in JSON format
type NoOpProgressIndicator ¶
type NoOpProgressIndicator struct{}
NoOpProgressIndicator is a progress indicator that does nothing
Example ¶
ExampleNoOpProgressIndicator demonstrates a no-op progress indicator
package main
import (
"github.com/snowdreamtech/unirtm/internal/cli/output"
)
func main() {
// Useful for testing or when progress output is not desired
progress := output.NewNoOpProgressIndicator()
progress.Start()
progress.Update(50, 100)
progress.Finish()
// No output is produced
}
Output:
func (*NoOpProgressIndicator) Fail ¶
func (n *NoOpProgressIndicator) Fail(err error)
Fail does nothing
func (*NoOpProgressIndicator) SetMessage ¶
func (n *NoOpProgressIndicator) SetMessage(message string)
SetMessage does nothing
func (*NoOpProgressIndicator) Update ¶
func (n *NoOpProgressIndicator) Update(current, total int64)
Update does nothing
type OutputFormat ¶
type OutputFormat string
OutputFormat represents the output format type
const ( // FormatHuman is the human-readable output format with colors FormatHuman OutputFormat = "human" // FormatJSON is the JSON output format for scripting FormatJSON OutputFormat = "json" )
type OutputMessage ¶
type OutputMessage struct {
Timestamp string `json:"timestamp"`
Level string `json:"level"`
Message string `json:"message"`
Fields map[string]interface{} `json:"fields,omitempty"`
}
OutputMessage represents a JSON output message
type ProgressIndicator ¶
type ProgressIndicator interface {
// Start starts the progress indicator
Start()
// Update updates the progress with current and total values
Update(current, total int64)
// SetMessage sets the progress message
SetMessage(message string)
// Finish completes the progress indicator
Finish()
// Fail marks the progress as failed
Fail(err error)
}
ProgressIndicator represents a progress indicator for long-running operations
func NewNoOpProgressIndicator ¶
func NewNoOpProgressIndicator() ProgressIndicator
NewNoOpProgressIndicator creates a no-op progress indicator
func NewProgressIndicator ¶
func NewProgressIndicator(opts ProgressOptions) ProgressIndicator
NewProgressIndicator creates a new progress indicator
Example ¶
ExampleNewProgressIndicator demonstrates creating and using a progress indicator
package main
import (
"time"
"github.com/snowdreamtech/unirtm/internal/cli/output"
)
func main() {
progress := output.NewProgressIndicator(output.ProgressOptions{
Message: "Downloading node-v20.0.0.tar.gz",
ShowPercentage: true,
ShowBytes: true,
ShowSpeed: true,
NoColor: true,
})
progress.Start()
// Simulate download progress
total := int64(10 * 1024 * 1024) // 10 MB
for downloaded := int64(0); downloaded < total; downloaded += 1024 * 1024 {
progress.Update(downloaded, total)
time.Sleep(100 * time.Millisecond)
}
progress.Finish()
}
Output:
type ProgressOptions ¶
type ProgressOptions struct {
// Writer is the output writer (defaults to os.Stderr)
Writer io.Writer
// Message is the initial progress message
Message string
// ShowPercentage shows percentage completion
ShowPercentage bool
// ShowBytes shows byte counts (for downloads)
ShowBytes bool
// ShowSpeed shows transfer speed (for downloads)
ShowSpeed bool
// Width is the progress bar width in characters
Width int
// NoColor disables color output
NoColor bool
// Quiet suppresses progress output
Quiet bool
}
ProgressOptions contains options for creating a progress indicator
type TableOutput ¶
type TableOutput struct {
Timestamp string `json:"timestamp"`
Headers []string `json:"headers"`
Rows [][]string `json:"rows"`
}
TableOutput represents tabular data output