style

package
v0.11.13 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package style provides styling types, color stops, and defaults for clog.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InterpolateGradient

func InterpolateGradient(t float64, stops []ColorStop) colorful.Color

InterpolateGradient computes the color at position t (0.0-1.0) along the given gradient stops using CIE-LCh blending for perceptually uniform transitions. Edge cases: empty -> white, single stop -> that color, t outside range -> clamp to nearest stop.

func RenderBackticks added in v0.11.8

func RenderBackticks(s string, base, code *lipgloss.Style) string

RenderBackticks styles s for display: text inside a matched pair of backticks is rendered with code and the delimiters removed, while the surrounding text is rendered with base. A nil style renders its text unstyled.

When code is nil or s carries no backticks, s is rendered whole by base with any backticks left intact - the behaviour before backtick styling, and what a non-color writer falls back to. An unmatched trailing backtick is not a delimiter: the remainder of s (the backtick included) is rendered by base.

func StepGradient

func StepGradient(t float64, stops []ColorStop) colorful.Color

StepGradient returns the color of the last stop whose position is <= t. Edge cases: empty -> white, single stop -> that color, t before first stop -> first stop's color.

Types

type ColorStop

type ColorStop = gradient.ColorStop

ColorStop defines a color at a specific position along a gradient. Position is in the range 0.0-1.0.

func DefaultElapsedGradient

func DefaultElapsedGradient() []ColorStop

DefaultElapsedGradient returns the default green -> yellow -> red gradient used for Config.ElapsedGradient, tuned for a dark background.

Terminal-aware light/dark selection is applied by the [Logger]; see ElapsedGradientFor for the background-specific stops.

func DefaultPercentGradient

func DefaultPercentGradient() []ColorStop

DefaultPercentGradient returns the default red -> yellow -> green gradient used for Config.PercentGradient, tuned for a dark background.

Terminal-aware light/dark selection is applied by the [Logger]; see PercentGradientFor for the background-specific stops.

func ElapsedGradientFor added in v0.10.2

func ElapsedGradientFor(bg theme.Background) []ColorStop

ElapsedGradientFor returns the green -> yellow -> red gradient used for Config.ElapsedGradient and Config.DurationGradient, with stops chosen for readable contrast against bg.

The dark stops are vivid primaries that pop on a dark background; the light stops are darkened, saturated variants because pure green and yellow are nearly invisible on a light background.

func PercentGradientFor added in v0.10.2

func PercentGradientFor(bg theme.Background) []ColorStop

PercentGradientFor returns the red -> yellow -> green gradient used for Config.PercentGradient, with stops chosen for readable contrast against bg. It mirrors ElapsedGradientFor with the stop order reversed.

type Config

type Config struct {
	// Per-token styles for HCL syntax highlighting.
	// nil disables HCL highlighting; use [DefaultHCL] to enable.
	HCL *HCL
	// Per-token styles for JSON syntax highlighting.
	// nil disables JSON highlighting; use [DefaultJSON] to enable.
	JSON *JSON
	// Per-token styles for TOML syntax highlighting.
	// nil disables TOML highlighting; use [DefaultTOML] to enable.
	TOML *TOML
	// Per-token styles for YAML syntax highlighting.
	// nil disables YAML highlighting; use [DefaultYAML] to enable.
	YAML *YAML

	// Style for text inside `backticks`, in both the message and string field
	// values (the delimiters are removed) [nil = leave backticks intact]. A
	// non-color writer leaves the backticks as written.
	Backtick *lipgloss.Style
	// Style for divider line characters (see [clog.DividerBuilder]) [nil = plain text]
	DividerLine *lipgloss.Style
	// Style for divider title text [nil = plain text]
	DividerTitle *lipgloss.Style
	// Gradient stops for Duration fields (default: green -> yellow -> red).
	// Active only when FieldFormats.DurationGradientMax > 0; overrides FieldDurationNumber/FieldDurationUnit.
	DurationGradient []ColorStop
	// How duration gradient colors transition: [GradientFade] (smooth) or [GradientStep] (discrete).
	DurationGradientMode GradientMode
	// Duration unit -> thresholds (evaluated high->low).
	DurationThresholds ThresholdMap
	// Duration unit -> style override (e.g. "s" -> yellow).
	DurationUnits Map
	// Gradient stops for Elapsed fields (default: green -> yellow -> red).
	// Active only when FieldFormats.ElapsedGradientMax > 0; overrides FieldElapsedNumber/FieldElapsedUnit.
	ElapsedGradient []ColorStop
	// How elapsed gradient colors transition: [GradientFade] (smooth) or [GradientStep] (discrete).
	ElapsedGradientMode GradientMode
	// Style for the numeric segments of duration values (e.g. "1" in "1m30s") [nil = plain text]
	FieldDurationNumber *lipgloss.Style
	// Style for the unit segments of duration values (e.g. "m" in "1m30s") [nil = plain text]
	FieldDurationUnit *lipgloss.Style
	// Style for the numeric segments of elapsed-time values [nil = falls back to FieldDurationNumber]
	FieldElapsedNumber *lipgloss.Style
	// Style for the unit segments of elapsed-time values [nil = falls back to FieldDurationUnit]
	FieldElapsedUnit *lipgloss.Style
	// Style for error field values [nil = plain text]
	FieldError *lipgloss.Style
	// Style for the "/" separator in fraction values. nil = the fraction's
	// current (gradient or base) color with the faint attribute, so it reads
	// as a dimmed version of the numbers.
	FieldFractionSeparator *lipgloss.Style
	// Style for int/float field values [nil = plain text]
	FieldNumber *lipgloss.Style
	// Base style for Percent fields (foreground overridden by gradient). nil = gradient color only.
	FieldPercent *lipgloss.Style
	// Style for the numeric part of quantity values (e.g. "5" in "5km") [nil = plain text]
	FieldQuantityNumber *lipgloss.Style
	// Style for the unit part of quantity values (e.g. "km" in "5km") [nil = plain text]
	FieldQuantityUnit *lipgloss.Style
	// Style for the quote delimiters around quoted values [nil = same style as the value]
	FieldQuote *QuoteStyle
	// Style for string field values [nil = plain text]
	FieldString *lipgloss.Style
	// Style for time.Time field values [nil = plain text]
	FieldTime *lipgloss.Style
	// Style for field key names without a per-key override.
	KeyDefault *lipgloss.Style
	// Field key name -> value style (e.g. "path" -> blue).
	Keys Map
	// Level label style (e.g. "INF", "ERR").
	Levels LevelMap
	// Global message text style [nil = plain text]. Overridden by Messages[level] when set.
	Message *lipgloss.Style
	// Message text style per level. Takes precedence over Message when set.
	Messages LevelMap
	// Gradient stops for Percent fields (default: red -> yellow -> green).
	PercentGradient []ColorStop
	// Quantity unit -> thresholds (evaluated high->low).
	QuantityThresholds ThresholdMap
	// Unit string -> style override (e.g. "km" -> green).
	QuantityUnits Map
	// Style for key/value separator.
	Separator *lipgloss.Style
	// Symbol text style per level (e.g. make "warning" bold yellow).
	// nil entries render the symbol unstyled.
	Symbols LevelMap
	// Style for the timestamp prefix.
	Timestamp *lipgloss.Style
	// Values maps typed values to styles. Keys use Go equality.
	// Allows differentiating between e.g. `true` (bool) and "true" (string).
	Values ValueMap
}

Config holds lipgloss styles for the logger's pretty output. Pointer fields can be set to nil to disable that style entirely.

func Default

func Default() *Config

Default returns the default color styles.

func (*Config) Merge added in v0.7.15

func (c *Config) Merge(other *Config)

Merge applies non-zero fields from other into c. Pointer fields are overwritten when non-nil; map fields are merged key-by-key; slice fields are replaced when non-nil; scalar fields are overwritten when non-zero.

type GradientMode

type GradientMode int

GradientMode controls how gradient colors transition between stops.

const (
	// GradientFade smoothly interpolates between color stops.
	GradientFade GradientMode = iota
	// GradientStep uses discrete color jumps at stop boundaries.
	GradientStep
)

type HCL added in v0.8.1

type HCL struct {
	BlockType   *lipgloss.Style // block type identifiers (resource, variable, data, etc.)
	BoolFalse   *lipgloss.Style // false
	BoolTrue    *lipgloss.Style // true
	Comment     *lipgloss.Style // # and // and /* */ comments
	Key         *lipgloss.Style // attribute keys (identifier before =)
	NestedKey   *lipgloss.Style // attribute keys inside nested blocks (depth >= 2); falls back to Key
	Null        *lipgloss.Style // null
	Number      *lipgloss.Style // numeric literals
	Punctuation *lipgloss.Style // structural tokens (=, {, }, [, ])
	String      *lipgloss.Style // string values (quoted literals and quote markers)
}

HCL configures per-token lipgloss styles for HCL syntax highlighting. nil fields render the corresponding token unstyled.

Use DefaultHCL as a starting point for customization.

func DefaultHCL added in v0.8.1

func DefaultHCL() *HCL

DefaultHCL returns lipgloss styles for HCL tokens using clog's default dark theme. Terminal-aware light/dark selection is applied by the [Logger]; see github.com/gechr/clog.Logger.SetPrintTheme.

func NewHCL added in v0.8.2

func NewHCL(th *theme.Theme) *HCL

NewHCL returns lipgloss styles for HCL tokens using the given theme.

type JSON

type JSON struct {
	// Mode controls rendering behaviour.
	// JSONModeJSON (default) preserves standard JSON quoting.
	// JSONModeHuman strips quotes from identifier-like keys and simple string values.
	// JSONModeFlat flattens nested object keys with dot notation; arrays are kept intact.
	Mode JSONMode
	// Indent pretty-prints JSON with the given indentation string (e.g. "  "
	// or "\t"). Empty string (default) flattens to a single line.
	Indent string
	// PreserveFormat keeps original whitespace intact instead of stripping
	// it. When true, Indent is ignored.
	PreserveFormat bool
	// OmitCommas omits the comma between items. JSONSpacingAfterComma still
	// applies and can be used to keep a space separator: {"a":1 "b":2}.
	OmitCommas bool
	// Spacing controls where spaces are inserted. Zero (default) means no spaces.
	// Use JSONSpacingAll for {"key": "value", "n": 1} style output.
	Spacing JSONSpacing

	BoolFalse      *lipgloss.Style // false
	BoolTrue       *lipgloss.Style // true
	Key            *lipgloss.Style // Object keys
	Null           *lipgloss.Style // null
	Number         *lipgloss.Style // Numeric values - base fallback for all number sub-styles
	NumberFloat    *lipgloss.Style // Floating-point values; falls back to Number
	NumberInteger  *lipgloss.Style // Integer values; falls back to Number
	NumberNegative *lipgloss.Style // Negative numbers; falls back to Number
	NumberPositive *lipgloss.Style // Positive numbers (with or without explicit sign); falls back to Number
	NumberZero     *lipgloss.Style // Zero; falls back to NumberPositive, then Number
	String         *lipgloss.Style // String values

	Brace       *lipgloss.Style // { } (nested)
	BraceRoot   *lipgloss.Style // { } (outermost object; falls back to Brace if nil)
	Bracket     *lipgloss.Style // [ ] (nested)
	BracketRoot *lipgloss.Style // [ ] (outermost array; falls back to Bracket if nil)
	Colon       *lipgloss.Style // :
	Comma       *lipgloss.Style // ,
}

JSON configures per-token lipgloss styles for JSON syntax highlighting. nil fields render the corresponding token unstyled.

Use DefaultJSON as a starting point for customization.

func DefaultJSON

func DefaultJSON() *JSON

DefaultJSON returns lipgloss styles for JSON tokens using clog's default dark theme. Terminal-aware light/dark selection is applied by the [Logger]; see github.com/gechr/clog.Logger.SetPrintTheme.

func NewJSON added in v0.8.2

func NewJSON(th *theme.Theme) *JSON

NewJSON returns lipgloss styles for JSON tokens using the given theme.

func (*JSON) WithSpacing

func (s *JSON) WithSpacing(spacing JSONSpacing) *JSON

WithSpacing returns the receiver with the given spacing flags applied. It modifies and returns the same pointer for fluent chaining:

styles.JSON = style.DefaultJSON().WithSpacing(style.JSONSpacingAll)

type JSONMode

type JSONMode int

JSONMode controls how JSON is rendered.

const (
	// JSONModeJSON renders standard JSON (default).
	JSONModeJSON JSONMode = iota
	// JSONModeHuman renders in HJSON style: keys and simple string values are
	// unquoted, making output more readable at a glance.
	JSONModeHuman
	// JSONModeFlat flattens nested object keys using dot notation and renders
	// scalar values without unnecessary quotes. Arrays are kept intact.
	// Example: {"user":{"name":"alice"},"tags":["a","b"]}
	//       →  {user.name:alice,tags:[a,b]}
	JSONModeFlat
)

type JSONSpacing

type JSONSpacing uint

JSONSpacing is a bitmask controlling where spaces are inserted in JSON output.

const (
	// JSONSpacingAfterColon inserts a space after each colon: {"key": "value"}.
	JSONSpacingAfterColon JSONSpacing = 1 << iota
	// JSONSpacingAfterComma inserts a space after each comma: {"a": 1, "b": 2}.
	JSONSpacingAfterComma
	// JSONSpacingBeforeObject inserts a space before a nested object value: {"key": {"n":1}}.
	JSONSpacingBeforeObject
	// JSONSpacingBeforeArray inserts a space before a nested array value: {"tags": ["a","b"]}.
	JSONSpacingBeforeArray
	// JSONSpacingAll enables all spacing options.
	JSONSpacingAll = JSONSpacingAfterColon | JSONSpacingAfterComma | JSONSpacingBeforeObject | JSONSpacingBeforeArray
)

type LevelMap

type LevelMap = map[level.Level]*lipgloss.Style

LevelMap maps log levels to lipgloss styles.

func DefaultMessages

func DefaultMessages() LevelMap

DefaultMessages returns the default per-level message styles (empty map; falls back to the global Config.Message style when entries are nil).

type Map

type Map = map[string]*lipgloss.Style

Map maps string keys to lipgloss styles (e.g. field key names or unit strings).

type QuoteStyle added in v0.11.4

type QuoteStyle struct {
	Style   lipgloss.Style
	Inherit bool
}

QuoteStyle configures how the quote delimiters around a quoted value are styled. When Inherit is true, Style is overlaid onto the value's own resolved style (the delimiters keep the value's color but gain Style's own attributes, e.g. bold); when false, Style is used as-is, ignoring the value.

func (QuoteStyle) Resolve added in v0.11.4

func (q QuoteStyle) Resolve(base *lipgloss.Style) lipgloss.Style

Resolve returns the concrete delimiter style for a value whose resolved style is base (nil when the value has no style). With Inherit set, Style's explicitly-set attributes win and any unset ones are taken from base; otherwise Style is returned unchanged.

type TOML added in v0.8.1

type TOML struct {
	BoolFalse   *lipgloss.Style // false
	BoolTrue    *lipgloss.Style // true
	Comment     *lipgloss.Style // # comment text
	DateTime    *lipgloss.Style // date, time, datetime values
	Float       *lipgloss.Style // floating point values
	Integer     *lipgloss.Style // integer values
	Key         *lipgloss.Style // bare and dotted keys
	Punctuation *lipgloss.Style // structural tokens (=, [, ], {, }, ,)
	String      *lipgloss.Style // basic and literal strings
	TableKey    *lipgloss.Style // [table] and [[array]] header keys
}

TOML configures per-token lipgloss styles for TOML syntax highlighting. nil fields render the corresponding token unstyled.

Use DefaultTOML as a starting point for customization.

func DefaultTOML added in v0.8.1

func DefaultTOML() *TOML

DefaultTOML returns lipgloss styles for TOML tokens using clog's default dark theme. Terminal-aware light/dark selection is applied by the [Logger]; see github.com/gechr/clog.Logger.SetPrintTheme.

func NewTOML added in v0.8.2

func NewTOML(th *theme.Theme) *TOML

NewTOML returns lipgloss styles for TOML tokens using the given theme.

type Threshold

type Threshold struct {
	Value float64        // Minimum numeric value (inclusive) to trigger this style.
	Style ThresholdStyle // Style overrides for number and unit segments.
}

Threshold defines a style override when a quantity's numeric value meets or exceeds the given threshold. Thresholds are evaluated in descending order - the first match wins.

type ThresholdMap

type ThresholdMap = map[string]Thresholds

ThresholdMap maps unit strings to their thresholds (evaluated high -> low).

type ThresholdStyle

type ThresholdStyle struct {
	Number *lipgloss.Style // Override for the number segment (nil = keep default).
	Unit   *lipgloss.Style // Override for the unit segment (nil = keep default).
}

ThresholdStyle holds optional style overrides for the number and unit segments of a quantity or duration value. nil fields keep the default style.

type Thresholds

type Thresholds = []Threshold

Thresholds is a list of Threshold entries, evaluated high -> low (first match wins).

type ValueMap

type ValueMap = map[any]*lipgloss.Style

ValueMap maps typed values to lipgloss styles. Keys use Go equality (e.g. bool true != string "true").

func DefaultValues

func DefaultValues() ValueMap

DefaultValues returns sensible default styles for common value strings.

type YAML added in v0.8.1

type YAML struct {
	Alias       *lipgloss.Style // *alias reference
	Anchor      *lipgloss.Style // &anchor name
	BoolFalse   *lipgloss.Style // false, no, off
	BoolTrue    *lipgloss.Style // true, yes, on
	Comment     *lipgloss.Style // # comment text
	Key         *lipgloss.Style // mapping keys
	Null        *lipgloss.Style // null, ~
	Number      *lipgloss.Style // int, float, hex, octal, binary, inf, nan
	Punctuation *lipgloss.Style // structural tokens (:, -, [, ], {, }, ,)
	String      *lipgloss.Style // string values (plain, single-quoted, double-quoted)
	Tag         *lipgloss.Style // !!str, !!int, !custom
}

YAML configures per-token lipgloss styles for YAML syntax highlighting. nil fields render the corresponding token unstyled.

Use DefaultYAML as a starting point for customization.

func DefaultYAML added in v0.8.1

func DefaultYAML() *YAML

DefaultYAML returns lipgloss styles for YAML tokens using clog's default dark theme. Terminal-aware light/dark selection is applied by the [Logger]; see github.com/gechr/clog.Logger.SetPrintTheme.

func NewYAML added in v0.8.2

func NewYAML(th *theme.Theme) *YAML

NewYAML returns lipgloss styles for YAML tokens using the given theme.

Jump to

Keyboard shortcuts

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