335 lines
9.0 KiB
Go
335 lines
9.0 KiB
Go
// Package schema describes every Ghostty configuration option: its value
|
|
// kind, allowed values, documentation and default values.
|
|
//
|
|
// The type information is generated from Ghostty's Config.zig (see
|
|
// tools/genschema) and embedded into the binary. Documentation and defaults
|
|
// are refreshed at runtime from the installed Ghostty binary when available,
|
|
// so they always match the version the user actually runs.
|
|
package schema
|
|
|
|
import (
|
|
_ "embed"
|
|
"encoding/json"
|
|
"fmt"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Kind describes how the value of an option is edited and validated.
|
|
type Kind string
|
|
|
|
const (
|
|
KindBool Kind = "bool"
|
|
KindEnum Kind = "enum" // one of Values
|
|
KindChoice Kind = "choice" // one of Values or a custom value
|
|
KindFlags Kind = "flags" // comma separated set of Flags, "no-" prefix disables
|
|
KindInt Kind = "int" // integer within Min/Max
|
|
KindFloat Kind = "float" // floating point number
|
|
KindColor Kind = "color" // hex or X11 color name, plus optional Values
|
|
KindColorList Kind = "colorlist" // comma separated colors
|
|
KindMetric Kind = "metric" // integer or percentage, e.g. "2" or "-10%"
|
|
KindDuration Kind = "duration" // e.g. "750ms", "1h30m"
|
|
KindPadding Kind = "padding" // "2" or "2,4"
|
|
KindText Kind = "text"
|
|
KindPath Kind = "path"
|
|
KindTheme Kind = "theme"
|
|
KindFont Kind = "font"
|
|
KindKeybind Kind = "keybind"
|
|
KindPalette Kind = "palette"
|
|
)
|
|
|
|
// Option is a single configuration key.
|
|
type Option struct {
|
|
Key string `json:"key"`
|
|
ZigType string `json:"zigType,omitempty"`
|
|
Kind Kind `json:"kind"`
|
|
Repeatable bool `json:"repeatable,omitempty"`
|
|
Values []string `json:"values,omitempty"`
|
|
Flags []string `json:"flags,omitempty"`
|
|
Min *float64 `json:"min,omitempty"`
|
|
Max *float64 `json:"max,omitempty"`
|
|
Hint string `json:"hint,omitempty"`
|
|
Doc string `json:"doc,omitempty"`
|
|
Defaults []string `json:"defaults,omitempty"`
|
|
// NotListed marks options that Ghostty accepts but does not print
|
|
// with `+show-config`. They are kept even when refreshing from a live
|
|
// Ghostty binary.
|
|
NotListed bool `json:"notListed,omitempty"`
|
|
|
|
Category string `json:"-"`
|
|
Platform string `json:"-"` // "", "macOS" or "Linux"
|
|
}
|
|
|
|
// Default returns the default value for scalar options ("" when unset).
|
|
func (o *Option) Default() string {
|
|
if len(o.Defaults) == 0 {
|
|
return ""
|
|
}
|
|
return o.Defaults[len(o.Defaults)-1]
|
|
}
|
|
|
|
// DefaultList returns the default entries of a repeatable option, ignoring
|
|
// the empty placeholder entry Ghostty prints for empty lists.
|
|
func (o *Option) DefaultList() []string {
|
|
var out []string
|
|
for _, d := range o.Defaults {
|
|
if d != "" {
|
|
out = append(out, d)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// TypeLabel is a short human readable description of the value type.
|
|
func (o *Option) TypeLabel() string {
|
|
var s string
|
|
switch o.Kind {
|
|
case KindBool:
|
|
s = "boolean"
|
|
case KindEnum:
|
|
s = "one of " + strings.Join(o.Values, ", ")
|
|
case KindChoice:
|
|
s = strings.Join(o.Values, ", ") + " or custom value"
|
|
case KindFlags:
|
|
s = "flags: " + strings.Join(o.Flags, ", ")
|
|
case KindInt:
|
|
s = "integer"
|
|
if o.Min != nil && o.Max != nil {
|
|
s += fmt.Sprintf(" (%s to %s)", num(*o.Min), num(*o.Max))
|
|
} else if o.Min != nil {
|
|
s += fmt.Sprintf(" (%s or more)", num(*o.Min))
|
|
}
|
|
case KindFloat:
|
|
s = "number"
|
|
if o.Min != nil && o.Max != nil {
|
|
s += fmt.Sprintf(" (%s to %s)", num(*o.Min), num(*o.Max))
|
|
} else if o.Min != nil {
|
|
s += fmt.Sprintf(" (%s or more)", num(*o.Min))
|
|
}
|
|
case KindColor:
|
|
s = "color (#rrggbb or X11 name)"
|
|
if len(o.Values) > 0 {
|
|
s += " or " + strings.Join(o.Values, ", ")
|
|
}
|
|
case KindColorList:
|
|
s = "comma separated colors"
|
|
case KindMetric:
|
|
s = "integer or percentage (e.g. 2, -10%)"
|
|
case KindDuration:
|
|
s = "duration (e.g. 500ms, 5s, 1h30m)"
|
|
case KindPadding:
|
|
s = "pixels, optionally two values (e.g. 2 or 2,4)"
|
|
case KindPath:
|
|
s = "file path"
|
|
case KindTheme:
|
|
s = "theme name (or light:NAME,dark:NAME)"
|
|
case KindFont:
|
|
s = "font family"
|
|
case KindKeybind:
|
|
s = "key binding (trigger=action)"
|
|
case KindPalette:
|
|
s = "palette entries (N=color)"
|
|
default:
|
|
s = "text"
|
|
}
|
|
if o.Repeatable && o.Kind != KindKeybind && o.Kind != KindPalette {
|
|
s = "list of " + s
|
|
}
|
|
return s
|
|
}
|
|
|
|
// Schema is the full set of known options.
|
|
type Schema struct {
|
|
GhosttyVersion string `json:"ghosttyVersion"`
|
|
Options []*Option `json:"options"`
|
|
// Source describes where docs and defaults came from.
|
|
Source string `json:"-"`
|
|
|
|
byKey map[string]*Option
|
|
}
|
|
|
|
//go:embed schema.json
|
|
var embedded []byte
|
|
|
|
// Embedded returns the schema compiled into the binary.
|
|
func Embedded() (*Schema, error) {
|
|
var s Schema
|
|
if err := json.Unmarshal(embedded, &s); err != nil {
|
|
return nil, fmt.Errorf("decode embedded schema: %w", err)
|
|
}
|
|
s.Source = "built-in (Ghostty " + s.GhosttyVersion + ")"
|
|
s.finish()
|
|
return &s, nil
|
|
}
|
|
|
|
// Lookup returns the option for key, or nil.
|
|
func (s *Schema) Lookup(key string) *Option {
|
|
return s.byKey[key]
|
|
}
|
|
|
|
func (s *Schema) finish() {
|
|
s.byKey = make(map[string]*Option, len(s.Options))
|
|
for _, o := range s.Options {
|
|
o.Category = categorize(o.Key)
|
|
o.Platform = platform(o)
|
|
s.byKey[o.Key] = o
|
|
}
|
|
}
|
|
|
|
// Refresh replaces docs and defaults with the output of
|
|
// `ghostty +show-config --default --docs` from the installed Ghostty.
|
|
// Options unknown to the installed version are dropped; options unknown to
|
|
// the embedded schema are added with an inferred kind.
|
|
func (s *Schema) Refresh(docsOutput, version string) {
|
|
parsed := ParseDocs(docsOutput)
|
|
if len(parsed) == 0 {
|
|
return
|
|
}
|
|
var opts []*Option
|
|
seen := make(map[string]bool)
|
|
for _, p := range parsed {
|
|
seen[p.Key] = true
|
|
o := s.byKey[p.Key]
|
|
if o == nil {
|
|
o = inferOption(p)
|
|
}
|
|
o.Doc = p.Doc
|
|
o.Defaults = p.Defaults
|
|
opts = append(opts, o)
|
|
}
|
|
for _, o := range s.Options {
|
|
if !seen[o.Key] && o.NotListed {
|
|
opts = append(opts, o)
|
|
}
|
|
}
|
|
s.Options = opts
|
|
if version != "" {
|
|
s.GhosttyVersion = version
|
|
}
|
|
s.Source = "Ghostty " + s.GhosttyVersion
|
|
s.finish()
|
|
}
|
|
|
|
// inferOption guesses the kind of an option the embedded schema does not
|
|
// know about (e.g. one added in a newer Ghostty release).
|
|
func inferOption(p DocEntry) *Option {
|
|
o := &Option{Key: p.Key, Kind: KindText, Repeatable: len(p.Defaults) > 1}
|
|
d := ""
|
|
if len(p.Defaults) > 0 {
|
|
d = p.Defaults[0]
|
|
}
|
|
switch {
|
|
case d == "true" || d == "false":
|
|
o.Kind = KindBool
|
|
case isNumber(d):
|
|
o.Kind = KindFloat
|
|
}
|
|
return o
|
|
}
|
|
|
|
func num(f float64) string { return strconv.FormatFloat(f, 'f', -1, 64) }
|
|
|
|
func isNumber(s string) bool {
|
|
if s == "" {
|
|
return false
|
|
}
|
|
var f float64
|
|
_, err := fmt.Sscanf(s, "%g", &f)
|
|
return err == nil && strings.TrimLeft(s, "-+0123456789.") == ""
|
|
}
|
|
|
|
// DocEntry is one key parsed from `+show-config --default --docs`.
|
|
type DocEntry struct {
|
|
Key string
|
|
Doc string
|
|
Defaults []string
|
|
}
|
|
|
|
// ParseDocs parses the output of `ghostty +show-config --default --docs`.
|
|
// Keys that directly follow another key without their own comment block
|
|
// share the previous documentation (e.g. font-family-bold).
|
|
func ParseDocs(out string) []DocEntry {
|
|
var (
|
|
entries []DocEntry
|
|
index = map[string]int{}
|
|
pending []string
|
|
lastDoc string
|
|
)
|
|
for _, line := range strings.Split(out, "\n") {
|
|
line = strings.TrimSuffix(line, "\r")
|
|
switch {
|
|
case strings.HasPrefix(line, "#"):
|
|
text := strings.TrimPrefix(line, "#")
|
|
text = strings.TrimPrefix(text, " ")
|
|
pending = append(pending, text)
|
|
case strings.TrimSpace(line) == "":
|
|
continue
|
|
default:
|
|
key, value, ok := splitShowConfigLine(line)
|
|
if !ok {
|
|
continue
|
|
}
|
|
if len(pending) > 0 {
|
|
lastDoc = strings.TrimRight(strings.Join(pending, "\n"), "\n ")
|
|
pending = nil
|
|
}
|
|
if i, ok := index[key]; ok {
|
|
entries[i].Defaults = append(entries[i].Defaults, value)
|
|
continue
|
|
}
|
|
index[key] = len(entries)
|
|
entries = append(entries, DocEntry{Key: key, Doc: lastDoc, Defaults: []string{value}})
|
|
}
|
|
}
|
|
return entries
|
|
}
|
|
|
|
// splitShowConfigLine splits "key = value". The value is kept verbatim
|
|
// because leading whitespace can be significant (selection-word-chars).
|
|
func splitShowConfigLine(line string) (key, value string, ok bool) {
|
|
i := strings.Index(line, " =")
|
|
if i <= 0 {
|
|
return "", "", false
|
|
}
|
|
key = line[:i]
|
|
if strings.ContainsAny(key, " \t") {
|
|
return "", "", false
|
|
}
|
|
value = strings.TrimPrefix(line[i+2:], " ")
|
|
return key, value, true
|
|
}
|
|
|
|
// Categories returns category names in display order, restricted to those
|
|
// that contain at least one option.
|
|
func (s *Schema) Categories() []string {
|
|
present := map[string]bool{}
|
|
for _, o := range s.Options {
|
|
present[o.Category] = true
|
|
}
|
|
var out []string
|
|
for _, c := range categoryOrder {
|
|
if present[c] {
|
|
out = append(out, c)
|
|
delete(present, c)
|
|
}
|
|
}
|
|
var rest []string
|
|
for c := range present {
|
|
rest = append(rest, c)
|
|
}
|
|
sort.Strings(rest)
|
|
return append(out, rest...)
|
|
}
|
|
|
|
// InCategory returns the options of a category in schema order.
|
|
func (s *Schema) InCategory(cat string) []*Option {
|
|
var out []*Option
|
|
for _, o := range s.Options {
|
|
if o.Category == cat {
|
|
out = append(out, o)
|
|
}
|
|
}
|
|
return out
|
|
}
|