tuitheme

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package tuitheme reads the TUI theme file: the colors and styles a terminal program draws its interface, markdown and code in, keyed by role. A theme generator writes one per theme, and every program on this package reads the same file, so one theme change recolors them all.

The format is JSON with three groups, ui, markdown and syntax. A value is "#rrggbb", a terminal palette slot from "0" to "15", or a style object of fg, bg, bold, italic, underline, strikethrough and dim. The schema published beside this package documents every key:

https://raw.githubusercontent.com/datapointchris/gotermstyle/main/tuitheme/schema/v1.json

A role is a group and a key, such as "ui.selection" or "syntax.keyword.return". A key the file leaves out falls back along its dots, then to the built-in default. The default is written in palette slots, so a program with no theme file draws in the terminal's own 16 colors.

Every program finds the same file through DefaultPath. TUI_THEME_FILE points them all at another one, such as a theme under test.

Load a theme once, paint each role through a gotermstyle.Palette, and load it again when Notify relays a reload. Load always returns a theme to draw. On an error it is the built-in default, and a program reloading keeps its own:

path, err := tuitheme.DefaultPath()
if err != nil {
	return err
}
theme, err := tuitheme.Load(path)
if err != nil {
	log.Print(err) // theme is the built-in default
}
fmt.Println(pal.Paint(theme.Code("ui.selection"), row))

reloads := make(chan os.Signal, 1)
tuitheme.Notify(reloads)
for range reloads {
	if next, err := tuitheme.Load(path); err == nil {
		theme = next
	}
}

A Theme never changes once loaded, so it is safe to share between goroutines.

Index

Constants

View Source
const PathEnv = "TUI_THEME_FILE"

PathEnv names the variable that points every program at another theme file.

Variables

This section is empty.

Functions

func DefaultPath

func DefaultPath() (string, error)

DefaultPath is the theme file a program loads: $TUI_THEME_FILE when set, else $XDG_CONFIG_HOME/tui-theme/current.json, else ~/.config/tui-theme/current.json. A theme generator installs the current theme at the XDG path. A relative XDG_CONFIG_HOME is ignored, as the XDG spec asks.

func Notify

func Notify(c chan<- os.Signal)

Notify relays each request to reread the theme file to c. The request is SIGUSR2, which a theme generator sends every running reader after it moves current.json. A Go program that never calls Notify catches SIGUSR2 and ignores it, so the signal is safe to send any Go program by name.

func Stop

func Stop(c chan<- os.Signal)

Stop ends Notify's relaying to c.

Types

type Color

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

Color is one of a style's two colors: unset, which is the terminal's own, a slot of the terminal's 16, or an exact RGB value.

func (Color) RGB

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

RGB is this color's exact value, if it has one.

func (Color) Slot

func (c Color) Slot() (int, bool)

Slot is the terminal palette slot this color names, if it names one.

func (Color) String

func (c Color) String() string

String is the color as the file writes it: "#rrggbb", a slot from "0" to "15", or "" when unset.

type Style

type Style struct {
	Fg, Bg        Color
	Bold          bool
	Italic        bool
	Underline     bool
	Strikethrough bool
	Dim           bool
}

Style is what one role draws in.

func (Style) Code

func (s Style) Code() string

Code is the style as one SGR sequence, for gotermstyle.Palette.Paint. It opens with a reset, as the house palette's codes do, so it replaces whatever came before. A style that sets nothing is "", which Paint leaves unpainted.

type Theme

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

Theme is a theme file's roles over the built-in default.

func Default

func Default() *Theme

Default is the built-in theme, in the terminal's 16 colors.

func Load

func Load(path string) (*Theme, error)

Load reads the theme file at path, and always returns a theme to draw. A file that does not exist is the built-in default with no error, because a terminal with no theme file is the ordinary case. On any other error the theme is the built-in default too, and a program reloading keeps the theme it has.

func (*Theme) Code

func (t *Theme) Code(role string) string

Code is role's style as an SGR sequence, for gotermstyle.Palette.Paint.

func (*Theme) Style

func (t *Theme) Style(role string) Style

Style is what role draws in: the file's value for it or the nearest key along its dots, else the built-in default's, else a style that sets nothing.

Jump to

Keyboard shortcuts

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