style

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package style provides terminal styling with ANSI colors and text attributes.

Index

Constants

This section is empty.

Variables

View Source
var (
	Bold      = New().WithBold(true)
	Dim       = New().WithDim(true)
	Italic    = New().WithItalic(true)
	Underline = New().WithUnderline(true)
	Blink     = New().WithBlink(true)
	Reverse   = New().WithReverse(true)
	Strike    = New().WithStrike(true)
	Conceal   = New().WithConceal(true)
	Overline  = New().WithOverline(true)
)
View Source
var (
	Red     = New().WithForeground(StandardColor(1))
	Green   = New().WithForeground(StandardColor(2))
	Yellow  = New().WithForeground(StandardColor(3))
	Blue    = New().WithForeground(StandardColor(4))
	Magenta = New().WithForeground(StandardColor(5))
	Cyan    = New().WithForeground(StandardColor(6))
	White   = New().WithForeground(StandardColor(7))
	Black   = New().WithForeground(StandardColor(0))
)

Functions

This section is empty.

Types

type Attribute

type Attribute uint16

Attribute represents a text attribute (bold, italic, etc.).

const (
	AttrBold Attribute = 1 << iota
	AttrDim
	AttrItalic
	AttrUnderline
	AttrBlink
	AttrBlink2
	AttrReverse
	AttrConceal
	AttrStrike
	AttrUnderline2
	AttrFrame
	AttrEncircle
	AttrOverline
)

type Color

type Color struct {
	// contains filtered or unexported fields
}

Color represents a terminal color.

func Color256

func Color256(n int) Color

Color256 creates a color from the 256-color palette.

func DefaultColor

func DefaultColor() Color

DefaultColor returns the default terminal color.

func ParseColor

func ParseColor(s string) (Color, error)

ParseColor parses a color from a string. Supported formats:

  • Named colors: "red", "bright_blue", etc.
  • Hex: "#ff0000" or "#f00"
  • RGB: "rgb(255,0,0)"
  • 256-color: "color(123)"
  • Default: "default"

func StandardColor

func StandardColor(n int) Color

StandardColor creates a color from the standard 16-color palette. Numbers 0-7 are normal colors, 8-15 are bright variants.

func TrueColor

func TrueColor(r, g, b uint8) Color

TrueColor creates a 24-bit RGB color.

func (Color) ANSICodes

func (c Color) ANSICodes(foreground bool) string

ANSICodes returns the ANSI SGR parameters for this color. foreground=true for foreground color, false for background.

func (Color) Blend

func (c Color) Blend(other Color, factor float64) Color

Blend blends two colors together with the given factor (0.0 = c, 1.0 = other).

func (Color) Downgrade

func (c Color) Downgrade(system ColorSystem) Color

Downgrade returns a color suitable for the given color system.

func (Color) IsDefault

func (c Color) IsDefault() bool

IsDefault returns true if this is the default color.

func (Color) RGB

func (c Color) RGB() (r, g, b uint8)

RGB returns the RGB components of a color. For non-truecolor colors, this returns an approximation.

type ColorSystem

type ColorSystem int

ColorSystem represents the color capability of a terminal.

const (
	ColorSystemNone      ColorSystem = 0 // No color support
	ColorSystemStandard  ColorSystem = 1 // 16 colors (standard ANSI)
	ColorSystem256       ColorSystem = 2 // 256 colors
	ColorSystemTrueColor ColorSystem = 3 // 24-bit true color
)

type ColorType

type ColorType int

ColorType represents the type of color encoding.

const (
	ColorTypeDefault   ColorType = iota // Default terminal color
	ColorTypeStandard                   // Standard 16 colors (0-15)
	ColorType256                        // 256 color palette (0-255)
	ColorTypeTrueColor                  // 24-bit RGB
)

type Style

type Style struct {
	// contains filtered or unexported fields
}

Style represents terminal text styling with colors and attributes. It uses a three-state system for attributes:

  • Not set (will inherit from parent)
  • Explicitly on
  • Explicitly off

func New

func New() Style

New creates a new empty style.

func Parse

func Parse(s string) Style

Parse parses a style string like "bold red on blue".

func (Style) Add

func (s Style) Add(other Style) Style

Add combines two styles, with the other style taking precedence for any attributes that are explicitly set.

func (Style) Background

func (s Style) Background() *Color

Background returns the background color, or nil if not set.

func (Style) Foreground

func (s Style) Foreground() *Color

Foreground returns the foreground color, or nil if not set.

func (Style) IsBold

func (s Style) IsBold() bool

IsBold returns true if bold is explicitly set to on.

func (Style) IsDim

func (s Style) IsDim() bool

IsDim returns true if dim is explicitly set to on.

func (Style) IsEmpty

func (s Style) IsEmpty() bool

IsEmpty returns true if no styling is set.

func (Style) IsItalic

func (s Style) IsItalic() bool

IsItalic returns true if italic is explicitly set to on.

func (Style) IsStrike

func (s Style) IsStrike() bool

IsStrike returns true if strikethrough is explicitly set to on.

func (Style) IsUnderline

func (s Style) IsUnderline() bool

IsUnderline returns true if underline is explicitly set to on.

func (s Style) Link() string

Link returns the hyperlink URL, or empty string if not set.

func (Style) Render

func (s Style) Render(text string, colorSystem ColorSystem) string

Render wraps text with ANSI escape codes for this style.

func (Style) String

func (s Style) String() string

String returns a human-readable representation of the style.

func (Style) WithBackground

func (s Style) WithBackground(c Color) Style

WithBackground returns a new style with the given background color.

func (s Style) WithBlink(on bool) Style

WithBlink returns a new style with blink set.

func (Style) WithBold

func (s Style) WithBold(on bool) Style

WithBold returns a new style with bold set.

func (Style) WithConceal

func (s Style) WithConceal(on bool) Style

WithConceal returns a new style with conceal set.

func (Style) WithDim

func (s Style) WithDim(on bool) Style

WithDim returns a new style with dim set.

func (Style) WithForeground

func (s Style) WithForeground(c Color) Style

WithForeground returns a new style with the given foreground color.

func (Style) WithItalic

func (s Style) WithItalic(on bool) Style

WithItalic returns a new style with italic set.

func (s Style) WithLink(url string) Style

WithLink returns a new style with a hyperlink.

func (Style) WithOverline

func (s Style) WithOverline(on bool) Style

WithOverline returns a new style with overline set.

func (Style) WithReverse

func (s Style) WithReverse(on bool) Style

WithReverse returns a new style with reverse set.

func (Style) WithStrike

func (s Style) WithStrike(on bool) Style

WithStrike returns a new style with strikethrough set.

func (Style) WithUnderline

func (s Style) WithUnderline(on bool) Style

WithUnderline returns a new style with underline set.

Jump to

Keyboard shortcuts

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