formatter

package
v0.12.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 2, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

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:

Alignment Constants

Text alignment options:

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

View Source
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.

View Source
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

View Source
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) AddText added in v0.12.0

func (f *Format) AddText(format Format, text string) (ok bool)

func (*Format) AlignText added in v0.12.0

func (f *Format) AlignText(text string) string

func (*Format) Inf added in v0.12.0

func (f *Format) Inf() string

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) Print added in v0.12.0

func (f *Format) Print(newline bool)

func (*Format) PrintGraph added in v0.12.1

func (f *Format) PrintGraph()

func (*Format) Sup added in v0.12.0

func (f *Format) Sup() string

Sup returns the overflow indicator character. Returns "🯟" (Unicode overflow symbol) when ANSI is enabled, or ">" when NoAnsi is true for plain text compatibility.

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]

type RGB

type RGB struct {
	R int // Red component (0-255)
	G int // Green component (0-255)
	B int // Blue component (0-255)
}

RGB represents a color in the RGB (Red, Green, Blue) color space. Each component is an integer value from 0 to 255. Used for truecolor (24-bit) terminal output.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL