Files
terdut-tui/internal/config/config.go
T
Niklas Ye 4a579bdbc6
CI / test (pull_request) Successful in 4s
Colour themes, defaulting to gruvbox dark
Every colour was a 256-colour ANSI index hardcoded in styles.go, so changing
the palette meant editing the styles themselves. This puts a semantic token set
between the two: styles name roles, a theme supplies the colours.

internal/theme holds the twelve tokens, the two built-ins (gruvbox-dark, the
new default, and gruvbox-light) and the loader for user themes in
~/.config/terdut-tui/themes/. A user file may 'extends:' a built-in and
override only what it cares about, and may shadow a built-in name to tweak it
in place. Unknown keys, malformed colours and incomplete themes are refused
with a message naming what went wrong.

Colours are truecolor hex now: lipgloss downsamples for 256- and 16-colour
terminals and honours NO_COLOR, so themes carry no fallbacks of their own.
An ANSI index is still accepted for anyone who would rather follow their
terminal's own palette.

The 21 package-level style vars become a Styles struct on the Model, which is
what rule 3 asked for all along; the four free functions in view.go take one as
their first argument. The embedded bubbles components are restyled from the
same tokens — otherwise a theme would leave a pink selected row and grey help
text behind. Note that the table's Cell style deliberately keeps no foreground:
bubbles renders cells before wrapping the row in Selected, so a colour there
cuts the selection highlight short.
2026-08-20 11:06:36 +02:00

67 lines
1.6 KiB
Go

package config
import (
"fmt"
"os"
"path/filepath"
"time"
"gopkg.in/yaml.v3"
)
const defaultRefreshInterval = 30 * time.Second
type Config struct {
ServerURL string
APIKey string
RefreshInterval time.Duration
Theme string
}
type rawConfig struct {
ServerURL string `yaml:"server_url"`
APIKey string `yaml:"api_key"`
RefreshInterval int `yaml:"refresh_interval,omitempty"` // seconds
Theme string `yaml:"theme,omitempty"`
}
func Load() (*Config, error) {
dir, err := os.UserConfigDir()
if err != nil {
return nil, fmt.Errorf("cannot determine config directory: %w", err)
}
path := filepath.Join(dir, "terdut-tui", "config.yaml")
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("config file not found at %s\n\nCreate it with:\n server_url: https://terdut.example.com\n api_key: <your-api-key>\n theme: gruvbox-dark # optional", path)
}
return nil, fmt.Errorf("cannot read config file: %w", err)
}
var raw rawConfig
if err := yaml.Unmarshal(data, &raw); err != nil {
return nil, fmt.Errorf("invalid config file: %w", err)
}
if raw.ServerURL == "" {
return nil, fmt.Errorf("config: 'server_url' is required")
}
if raw.APIKey == "" {
return nil, fmt.Errorf("config: 'api_key' is required")
}
interval := defaultRefreshInterval
if raw.RefreshInterval > 0 {
interval = time.Duration(raw.RefreshInterval) * time.Second
}
return &Config{
ServerURL: raw.ServerURL,
APIKey: raw.APIKey,
RefreshInterval: interval,
Theme: raw.Theme,
}, nil
}