config

package
v2.2.10 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package config provides configuration management for the CalcMark TUI/CLI. Configuration is loaded from TOML files with embedded defaults.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyColorModeOverride

func ApplyColorModeOverride(colorMode string)

ApplyColorModeOverride applies a color mode override (from CLI flag or env var). This is used to override the config file setting.

func DefaultsTOML

func DefaultsTOML() string

DefaultsTOML returns the raw embedded defaults.toml content.

func IsDarkMode

func IsDarkMode() bool

IsDarkMode returns whether we're rendering for dark background.

func ThemeConfigKnownKeys

func ThemeConfigKnownKeys() map[string]bool

ThemeConfigKnownKeys returns the set of known keys under [tui.theme]. Used to detect deprecated/unknown keys in user config and log warnings.

func ValidateColorMode

func ValidateColorMode(s string) bool

ValidateColorMode reports whether s is a valid color_mode value. Empty string is accepted (means "use default").

func ValidateFormat

func ValidateFormat(s string, validNames []string) bool

ValidateFormat reports whether s is a valid output format name. Empty string is accepted (means "use default"). validNames is the list of registered format names (passed in to avoid import cycles).

func ValidateHexColor

func ValidateHexColor(s string) bool

ValidateHexColor reports whether s is a valid hex color string. Empty string is accepted (means "use palette default"). Valid formats: #RGB, #RRGGBB (case-insensitive).

func XDGConfigPath

func XDGConfigPath() (string, error)

XDGConfigPath returns the XDG config file path for the current user. Respects $XDG_CONFIG_HOME per the XDG Base Directory Specification. Falls back to ~/.config/calcmark/config.toml if not set.

Types

type Config

type Config struct {
	// Locale is the BCP 47 locale tag for display formatting (e.g., "en-US", "de-DE").
	// Affects decimal and thousand separators in output. Top-level because locale
	// is application-wide and will eventually affect input parsing too.
	// Precedence: --locale flag > config.toml > "en-US" default.
	Locale      string            `mapstructure:"locale" toml:"locale"`
	TUI         TUIConfig         `mapstructure:"tui" toml:"tui"`
	Formatter   FormatterConfig   `mapstructure:"formatter" toml:"formatter"`
	Measurement MeasurementConfig `mapstructure:"measurement" toml:"measurement"`
}

Config is the root configuration structure.

func Get

func Get() *Config

Get returns the loaded configuration. Panics if Load() hasn't been called or failed.

func Load

func Load() (*Config, error)

Load initializes configuration from embedded defaults and user config files. Safe to call multiple times; only loads once. Returns the config and any error from loading.

func Reload

func Reload() (*Config, error)

Reload forces a fresh config load. Use for testing only.

type ConfigFileResult

type ConfigFileResult struct {
	Path  string
	Found bool
	Err   error // TOML syntax error, if any
}

ConfigFileResult describes the discovery status of a single config file path.

type FormatterConfig

type FormatterConfig struct {
	Verbose       bool   `mapstructure:"verbose" toml:"verbose"`
	IncludeErrors bool   `mapstructure:"include_errors" toml:"include_errors"`
	DefaultFormat string `mapstructure:"default_format" toml:"default_format"`

	// DateFormat is a user DSL string overriding the locale default
	// for Date display (e.g., "MON dd, YYYY", "YYYY-MM-dd").
	// See format/display/date_format_dsl.go for supported tokens.
	// Empty (default) uses the locale's date layout.
	DateFormat string `mapstructure:"date_format" toml:"date_format"`

	// PeriodDateFormat is the user DSL for date endpoints inside
	// Period output. Empty falls back to DateFormat, then to the
	// built-in compact "dd-MON-YYYY". Set this independently when
	// the verbose DateFormat would make twin-date output unreadable.
	PeriodDateFormat string `mapstructure:"period_date_format" toml:"period_date_format"`
}

FormatterConfig holds output formatter settings.

type MeasurementConfig

type MeasurementConfig struct {
	// Volume: "us" (default) or "imperial".
	// Controls how bare volume names (gallon, pint, cup, fl oz) are interpreted.
	Volume string `mapstructure:"volume" toml:"volume"`

	// Mass: "standard" (default) or "troy".
	// "standard" = avoirdupois (everyday weight: 1 oz = 28.35g).
	// "troy" = precious metals (1 troy oz = 31.10g).
	Mass string `mapstructure:"mass" toml:"mass"`

	// Ton: "short" (default), "long", or "metric".
	// "short" = US (2000 lb), "long" = Imperial (2240 lb), "metric" = 1000 kg.
	Ton string `mapstructure:"ton" toml:"ton"`

	// Strict: annotate bare ambiguous units in output (default true).
	// When true, "oz" displays as "us oz" or "troy oz" depending on convention.
	Strict bool `mapstructure:"strict" toml:"strict"`
}

MeasurementConfig holds default measurement conventions. These are global defaults that can be overridden per-document via frontmatter. Precedence: frontmatter > config.toml > built-in defaults (US Customary).

type Styles

type Styles struct {
	Title         lipgloss.Style
	PinnedPanel   lipgloss.Style
	Error         lipgloss.Style
	ErrorOutput   lipgloss.Style
	Help          lipgloss.Style
	Prompt        lipgloss.Style
	Output        lipgloss.Style
	Changed       lipgloss.Style
	Var           lipgloss.Style
	Hint          lipgloss.Style
	Header        lipgloss.Style
	Syntax        lipgloss.Style
	Example       lipgloss.Style
	Separator     lipgloss.Style
	ModeIndicator lipgloss.Style

	// Editor styles
	EditLine    lipgloss.Style // Background for line being edited
	Cursor      lipgloss.Style // Cursor style
	CurrentLine lipgloss.Style // Current line highlight
	LineNumber  lipgloss.Style // Line number style
	SourceText  lipgloss.Style // Normal source text color

	// Calculation result display styles
	CalcVarName lipgloss.Style // Variable name in result
	CalcArrow   lipgloss.Style // Arrow in result
	CalcValue   lipgloss.Style // Calculated value in result

	// Markdown preview styles
	MdText   lipgloss.Style // Body text
	MdH1     lipgloss.Style // H1 heading
	MdH2     lipgloss.Style // H2 heading
	MdH3Plus lipgloss.Style // H3+ headings
	MdLink   lipgloss.Style // Links
	MdQuote  lipgloss.Style // Block quotes
	MdCode   lipgloss.Style // Inline code
	MdCodeBg lipgloss.Style // Code with background

	// Pane backgrounds
	SourcePane    lipgloss.Style // Source pane background
	PreviewPane   lipgloss.Style // Preview pane background
	StatusBar     lipgloss.Style // Status bar background
	ContextFooter lipgloss.Style // Context footer background

	// Input and prompt styles
	PromptLabel lipgloss.Style // Prompt label style (e.g., "Save as:")
	InputText   lipgloss.Style // User input text style
	InputCursor lipgloss.Style // Input cursor style

	// Source pane syntax highlighting (block-level)
	SourceFrontmatter lipgloss.Style // Frontmatter lines (YAML between ---)
	SourceMarkdown    lipgloss.Style // Markdown prose lines
	SourceCalc        lipgloss.Style // Calculation lines
}

Styles holds pre-built lipgloss styles derived from the semantic palette. This avoids rebuilding styles on every render call. All colors use lipgloss.AdaptiveColor from the theme package, so they automatically resolve to the correct light/dark value at Render() time.

func GetStyles

func GetStyles() Styles

GetStyles returns pre-built lipgloss styles from the loaded theme. Panics if Load() hasn't been called or failed.

type TUIConfig

type TUIConfig struct {
	Theme            ThemeConfig `mapstructure:"theme" toml:"theme"`
	DarkMode         bool        `mapstructure:"dark_mode" toml:"-"` // Deprecated: use ColorMode instead
	ColorMode        string      `mapstructure:"color_mode" toml:"color_mode"`
	UnicodeFractions *bool       `mapstructure:"unicode_fractions" toml:"unicode_fractions"` // nil = default (true)
}

TUIConfig holds TUI-specific settings.

type ThemeConfig

type ThemeConfig struct {
	Primary   string `mapstructure:"primary" toml:"primary"`     // Titles, prompts, variable names
	Accent    string `mapstructure:"accent" toml:"accent"`       // Borders, highlights
	Error     string `mapstructure:"error" toml:"error"`         // Error messages
	Warning   string `mapstructure:"warning" toml:"warning"`     // Changed indicators
	Muted     string `mapstructure:"muted" toml:"muted"`         // Help text
	Dimmed    string `mapstructure:"dimmed" toml:"dimmed"`       // Hints, suggestions
	Output    string `mapstructure:"output" toml:"output"`       // Calculation results
	Bright    string `mapstructure:"bright" toml:"bright"`       // Syntax emphasis
	Separator string `mapstructure:"separator" toml:"separator"` // Divider lines

	// Pane backgrounds
	SourcePaneBg  string `mapstructure:"source_pane_bg" toml:"source_pane_bg"`   // Background color for source pane
	PreviewPaneBg string `mapstructure:"preview_pane_bg" toml:"preview_pane_bg"` // Background color for preview pane
	StatusBarBg   string `mapstructure:"status_bar_bg" toml:"status_bar_bg"`     // Background color for status bar
}

ThemeConfig defines user-facing color overrides as hex strings. These override the corresponding AdaptiveColor palette slot (light or dark) based on the configured color_mode. Internal structural colors (popup borders, separator shades, etc.) are not user-configurable and derive from the palette.

func (ThemeConfig) BuildStyles

func (t ThemeConfig) BuildStyles() Styles

BuildStyles creates lipgloss.Style instances from the semantic palette. User overrides from ThemeConfig are applied by overriding the appropriate palette slot before building. Since AdaptiveColor resolves at Render() time, this function only needs to be called once.

type ValidationResult

type ValidationResult struct {
	Files  []ConfigFileResult
	Errors []string
}

ValidationResult collects all config file results and validation errors.

func Validate

func Validate(validFormatNames []string) *ValidationResult

Validate discovers config files, checks TOML syntax, loads effective config, and validates all field values. validFormatNames is the list of registered format names (from format.FormatNames()).

func (*ValidationResult) OK

func (r *ValidationResult) OK() bool

OK returns true when no validation errors were found.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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