thememanager

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package thememanager provides functionality for managing and loading color themes for terminal user interfaces.

The package supports loading themes from both embedded files and custom directories. Themes are defined in YAML format and contain base and accent colors that can be used for consistent styling across the application.

Basic usage:

// Create a new theme manager with default settings
tm := thememanager.New()

// Load the default theme
err := tm.LoadTheme("")

// Or load a specific theme
err := tm.LoadTheme("dark")

// Get the current theme for use
theme := tm.GetTheme()

Custom theme directory:

// Create a theme manager with a custom theme directory
tm := thememanager.New(thememanager.WithDirectory("/path/to/themes"))

Theme files should be YAML files with the .yaml extension and follow this structure:

base:
  base00: "#1A1B26"  # Background
  base01: "#24283B"  # Light Background
  base02: "#292E42"  # Selection Background
  base03: "#565F89"  # Comments, Invisibles
  base04: "#A9B1D6"  # Dark Foreground
accent:
  accent0: "#FF9E64" # Orange
  accent1: "#9ECE6A" # Green
  accent2: "#7AA2F7" # Blue

Color Validation:

All colors in the theme must:

  • Be valid hexadecimal color codes
  • Start with '#' character
  • Be present for all required fields (no missing colors)

Default Theme:

When no theme name is provided (empty string), the manager will:

  • Load the "default" theme from the embedded themes
  • Use this as the fallback theme for the application
  • Return an error if the default theme is not found or invalid

The package uses the following error constants for error handling:

  • ErrThemeNotFound: Returned when a requested theme file cannot be found
  • ErrReadingTheme: Returned when there's an error reading the theme file
  • ErrParsingTheme: Returned when the theme file cannot be parsed
  • ErrDecodingTheme: Returned when theme YAML decoding fails
  • ErrMissingColors: Returned when required colors are missing from theme

The Manager interface defines the core functionality that theme managers must implement:

  • LoadTheme: Loads a theme by name
  • GetTheme: Returns the currently loaded theme

Thread Safety:

The theme manager is safe for concurrent access:

  • LoadTheme operations are synchronized
  • GetTheme returns a pointer to the theme, which should be treated as read-only
  • Theme modifications should be done through LoadTheme only

Index

Constants

View Source
const (
	// ErrMissingColors is the error message for missing required colors.
	ErrMissingColors = "missing required colors"
	// ErrDecodingTheme is returned when theme decoding fails.
	ErrDecodingTheme = "failed to decode theme"
	// ErrInvalidColor is returned when a color is not a valid hex code.
	ErrInvalidColor = "invalid color format"
)
View Source
const (
	// ErrThemeNotFound is returned when a theme is not found.
	ErrThemeNotFound = "theme not found"
	// ErrReadingTheme is returned when theme file cannot be read.
	ErrReadingTheme = "failed to read theme file"
	// ErrParsingTheme is returned when theme file cannot be parsed.
	ErrParsingTheme = "failed to parse theme file"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AccentColors

type AccentColors struct {
	// Accent0 is used for command text and prompts.
	Accent0 lipgloss.Color `yaml:"accent0"`
	// Accent1 is used for agent messages and success states.
	Accent1 lipgloss.Color `yaml:"accent1"`
	// Accent2 is used for tool output and links.
	Accent2 lipgloss.Color `yaml:"accent2"`
}

AccentColors contains the accent color palette.

type BaseColors

type BaseColors struct {
	// Base00 is used for primary background.
	Base00 lipgloss.Color `yaml:"base00"`
	// Base01 is used for secondary background (status bars, input).
	Base01 lipgloss.Color `yaml:"base01"`
	// Base02 is used for borders and dividers.
	Base02 lipgloss.Color `yaml:"base02"`
	// Base03 is used for muted or disabled text.
	Base03 lipgloss.Color `yaml:"base03"`
	// Base04 is used for primary text content.
	Base04 lipgloss.Color `yaml:"base04"`
}

BaseColors contains the base color palette.

type Manager

type Manager interface {
	// LoadTheme loads a named theme from the theme manager.
	LoadTheme(name string) error
	// GetTheme returns the current theme.
	GetTheme() *Theme
}

Manager is the interface for the theme manager.

type Option

type Option func(*ThemeManager)

Option is a function that modifies the theme manager.

func WithDirectory

func WithDirectory(dir string) Option

WithDirectory sets the directory for the theme manager.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger sets the logger for the theme manager.

type Theme

type Theme struct {
	// BaseColors contains the base color palette.
	BaseColors BaseColors `yaml:"base"`
	// AccentColors contains the accent color palette.
	AccentColors AccentColors `yaml:"accent"`
}

Theme defines the color palette for the application TUI.

func (*Theme) UnmarshalYAML

func (t *Theme) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML implements the yaml.Unmarshaler interface.

type ThemeManager

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

ThemeManager is the manager for the themes.

func New

func New(opts ...Option) *ThemeManager

New creates a new theme manager.

func (*ThemeManager) GetTheme

func (tm *ThemeManager) GetTheme() *Theme

GetTheme returns the current theme.

func (*ThemeManager) LoadTheme

func (tm *ThemeManager) LoadTheme(name string) (err error)

LoadTheme loads a named theme from the theme manager.

Jump to

Keyboard shortcuts

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