Documentation
¶
Overview ¶
Package formatter provides text formatting and color output capabilities for Charta.
This package defines the Formatter interface that output plugins must implement, along with style constants and color utilities. The default implementation is provided by the ansi subpackage.
Architecture ¶
The formatter system uses a plugin architecture where implementations register themselves in the Plugins map. This allows for different output backends (ANSI terminal, HTML, plain text, etc.) while maintaining a consistent API.
Style Constants ¶
Text styles are defined as integer constants:
- Reset: Reset all formatting (0)
- Bold: Bold text (1)
- Faint: Dim/faint text (2)
- Italic: Italic text (3)
- Underline: Underlined text (4)
Alignment Constants ¶
Text alignment options:
- AlignLeft: Left-align text (0)
- AlignCenter: Center text (1)
- AlignRight: Right-align text (2)
Colors ¶
Available colors depend on terminal capabilities:
- Basic: black, red, green, yellow, blue, magenta, cyan, white
- Extended: orange, purple (truecolor terminals only)
- Light variants: light-red, light-green, etc.
Index ¶
Constants ¶
const ( Reset = iota // Reset all attributes to default Bold // Bold or increased intensity Faint // Faint or decreased intensity Italic // Italic text Underline // Underlined text Force = 255 )
Style constants for text formatting. These correspond to ANSI SGR (Select Graphic Rendition) parameters.
const ( AlignLeft = iota // Left-align text (default) AlignCenter // Center text within width AlignRight // Right-align text within width )
Alignment constants for text positioning within a fixed width.
Variables ¶
var Plugins = make(map[string]func() Formatter)
Plugins is the registry of available formatter implementations. Each plugin registers a factory function that creates Formatter instances with the specified style, alignment, width, and colors.
The factory function signature:
func(style []int, align, width int, foreground, background string) Formatter
Example usage:
f := formatter.Plugins["ansi"]([]int{formatter.Bold}, formatter.AlignLeft, 20, "red", "")
f.Print("Hello")
Functions ¶
func GetListColors ¶
func GetListColors() []interface{}
GetListColors returns the list of available colors based on terminal capabilities.
Returns:
- []interface{}: Slice of available color names as interfaces
func GetListColorsArray ¶
func GetListColorsArray() []string
GetListColorsArray converts the list of available colors to a string slice. This is useful for displaying available color options to users.
Returns:
- []string: Array of color names as strings
func GetListLightColors ¶
func GetListLightColors() []interface{}
GetListLightColors returns the list of available light colors based on terminal capabilities.
Returns:
- []interface{}: Slice of available color names as interfaces
func GetListLightColorsArray ¶
func GetListLightColorsArray() []string
GetListLightColorsArray converts the list of available colors to a string slice. This is useful for displaying available color options to users.
Returns:
- []string: Array of color names as strings
func GetTotalListColors ¶
func GetTotalListColors() []interface{}
GetTotalListColors returns all available colors (both normal and light variants) as a slice of interfaces, suitable for use with argument parsing libraries.
Returns:
- []interface{}: Combined array of all color names as interfaces
func GetTotalListColorsArray ¶
func GetTotalListColorsArray() []string
GetTotalListColorsArray returns all available colors (both normal and light variants) as a sorted string slice. This includes all colors supported by the current terminal.
Returns:
- []string: Combined array of all color names
Types ¶
type Format ¶ added in v0.12.0
type Format struct {
Style []int // Style codes (Bold, Italic, etc.)
Align int // Alignment (AlignLeft, AlignCenter, AlignRight)
Width int // Output width in characters
Foreground string // Foreground color name or RGB string
Background string // Background color name or RGB string
}
Formatter implements formatter.Formatter using ANSI escape codes. It supports text styling, foreground/background colors, and alignment.
func (*Format) Inf ¶ added in v0.12.0
Inf returns the underflow indicator character. Returns "" (Unicode underflow symbol) when ANSI is enabled, or "<" when NoAnsi is true for plain text compatibility.
func (*Format) PrintGraph ¶ added in v0.12.1
func (f *Format) PrintGraph()
type Formatter ¶
type Formatter interface {
// Print outputs formatted text without a trailing newline.
Print(newline bool)
PrintGraph()
// Sprint returns the formatted text as a string without outputting it.
AddText(format Format, text string) (ok bool)
// GetGradient generates a slice of Formatters representing a color gradient.
// Parameters:
// - min: Starting color name
// - mid: Middle color name (empty for two-color gradient)
// - max: Ending color name
// - n: Number of gradient steps
GetGradient(min, mid, max string, n int) (gradient []string)
TestLenght(format Format, text string) (ok bool)
AlignText(text string) string
Sup() string
Inf() string
}
Formatter defines the interface for text output formatting. Implementations handle styling, coloring, and alignment of text output.
The interface supports:
- Direct output: [Print], [Println]
- String generation: [Sprint]
- Color gradients: [GetGradient]
- Dynamic width: [SetWidth]
Directories
¶
| Path | Synopsis |
|---|---|
|
Package ansi provides ANSI escape code formatting for terminal output.
|
Package ansi provides ANSI escape code formatting for terminal output. |
|
Package mathjax provides mathjax formatting for terminal output.
|
Package mathjax provides mathjax formatting for terminal output. |
|
Package ansi provides ANSI escape code formatting for terminal output.
|
Package ansi provides ANSI escape code formatting for terminal output. |
|
Package svg provides SVG output formatting.
|
Package svg provides SVG output formatting. |