theme

package
v0.0.0-...-1e2fe69 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

README

Termdodo Theme System

The theme system in Termdodo allows you to customize the appearance of your terminal UI applications.

Directory Structure

theme/
├── data/           # Theme JSON files
│   └── default.json
├── theme.go        # Theme management code
└── README.md       # This file

Theme File Format

Themes are defined in JSON files with the following structure:

{
    "name": "Theme Name",
    "description": "Theme description",
    "author": "Author name",
    "background": "#000000",
    "foreground": "#ffffff",
    "main_bg": "#000000",
    "main_fg": "#ffffff",
    "title": "#ff8f40",
    "meter_bg": [
        "#color1",
        "#color2",
        "#color3"
    ],
    "graph": [
        "#color1",
        "#color2",
        "#color3",
        "#color4"
    ],
    "border": "#border_color",
    "selected": "#selected_color",
    "highlight_bg": "#highlight_bg_color",
    "highlight_fg": "#highlight_fg_color"
}

Usage

import "github.com/deadjoe/termdodo/theme"

// Load the default theme
theme.LoadDefaultTheme()

// Load a custom theme
theme.LoadTheme("path/to/theme.json")

// Get a style for rendering
style := theme.GetStyle(theme.Current.MainFg, theme.Current.MainBg)

// Get a gradient style (useful for graphs and meters)
gradientStyle := theme.GetGradientStyle(theme.Current.Graph, 0.5) // position 0.0-1.0

Creating Custom Themes

  1. Create a new JSON file in the theme/data directory
  2. Follow the theme file format shown above
  3. Use hex color codes for all colors (e.g., "#ff0000" for red)
  4. Load your theme using theme.LoadTheme()

Built-in Themes

  • default.json: The default theme inspired by btop

Contributing New Themes

  1. Create your theme file in the theme/data directory
  2. Ensure all required colors are defined
  3. Test your theme with different widgets
  4. Submit a pull request

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrReadThemeFile is returned when the theme file cannot be read
	ErrReadThemeFile = errors.New("failed to read theme file")
	// ErrParseThemeFile is returned when the theme file cannot be parsed
	ErrParseThemeFile = errors.New("failed to parse theme file")
	// ErrMarshalTheme is returned when the theme cannot be marshaled
	ErrMarshalTheme = errors.New("failed to marshal theme")
	// ErrCreateThemeDir is returned when the theme directory cannot be created
	ErrCreateThemeDir = errors.New("failed to create theme directory")
	// ErrWriteThemeFile is returned when the theme file cannot be written
	ErrWriteThemeFile = errors.New("failed to write theme file")

	// Current holds the current theme
	Current Theme
)

Functions

func ColorToHex

func ColorToHex(color tcell.Color) string

ColorToHex converts tcell.Color to hex string

func GetColor

func GetColor(name string) tcell.Color

GetColor returns a tcell color from a color name

func GetGradientStyle

func GetGradientStyle(colors []string, position float64) tcell.Style

GetGradientStyle returns a style based on position in a gradient

func GetStyle

func GetStyle(fg, bg string) tcell.Style

GetStyle returns a tcell.Style with the given foreground and background colors

func LoadDefaultTheme

func LoadDefaultTheme()

LoadDefaultTheme loads the default theme

func LoadTheme

func LoadTheme(path string) error

LoadTheme loads a theme from a JSON file

func ParseHexColor

func ParseHexColor(hex string) tcell.Color

ParseHexColor parses a hex color string into tcell.Color

func RGBToColor

func RGBToColor(r, g, b uint64) tcell.Color

RGBToColor converts RGB values to tcell.Color

func SaveTheme

func SaveTheme(path string) error

SaveTheme saves the current theme to a JSON file

Types

type Theme

type Theme struct {
	Name        string      `json:"name"`
	Description string      `json:"description"`
	Author      string      `json:"author"`
	Background  tcell.Color `json:"-"`
	Foreground  tcell.Color `json:"-"`
	MainBg      tcell.Color `json:"-"`
	MainFg      tcell.Color `json:"-"`
	Title       tcell.Color `json:"-"`
	Meter       []string    `json:"meter_bg"`
	Graph       []string    `json:"graph"`
	Border      tcell.Color `json:"-"`
	Selected    tcell.Color `json:"-"`
	HighlightBg tcell.Color `json:"-"`
	HighlightFg tcell.Color `json:"-"`
	Accent      tcell.Color `json:"-"`

	// JSON fields for serialization
	BackgroundHex  string `json:"background"`
	ForegroundHex  string `json:"foreground"`
	MainBgHex      string `json:"main_bg"`
	MainFgHex      string `json:"main_fg"`
	TitleHex       string `json:"title"`
	BorderHex      string `json:"border"`
	SelectedHex    string `json:"selected"`
	HighlightBgHex string `json:"highlight_bg"`
	HighlightFgHex string `json:"highlight_fg"`
	AccentHex      string `json:"accent"`
}

Theme represents a complete color theme

func NewTheme

func NewTheme() *Theme

NewTheme creates a new theme with default values

func (*Theme) GetAccentStyle

func (t *Theme) GetAccentStyle() tcell.Style

GetAccentStyle returns the accent style for the theme

func (*Theme) GetBorderStyle

func (t *Theme) GetBorderStyle() tcell.Style

GetBorderStyle returns the border style for the theme

func (*Theme) GetGradientStyle

func (t *Theme) GetGradientStyle(position float64) tcell.Style

GetGradientStyle returns a style based on position in a gradient

func (*Theme) GetStyle

func (t *Theme) GetStyle() tcell.Style

GetStyle returns the default style for the theme

func (*Theme) SetAccent

func (t *Theme) SetAccent(color tcell.Color)

SetAccent sets the accent color

func (*Theme) SetBackground

func (t *Theme) SetBackground(color tcell.Color)

SetBackground sets the background color

func (*Theme) SetBorder

func (t *Theme) SetBorder(color tcell.Color)

SetBorder sets the border color

func (*Theme) SetForeground

func (t *Theme) SetForeground(color tcell.Color)

SetForeground sets the foreground color

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳