Files
ghosttyconf/internal/schema/validate.go
T
mikeandClaude Opus 5.5 df261910c5 Support Ghostty 1.4, version 1.0.2
The built-in schema now describes Ghostty 1.4.0: 212 options, the new
choices of copy-on-select, window-padding-balance = equal, number limits
that also take "unlimited", and categories for the new options.

genschema reads Ghostty's compatibility table, so old names and values
Ghostty still accepts are no longer reported as errors: a renamed key
such as scrollback-limit is shown in yellow and renamed in place with
enter, a replaced option names its successors, and old values such as
copy-on-select = true are accepted with a note.

Theme previews expand ~/ like Ghostty 1.4 and only look where Ghostty
looks for user themes. Theme names with parentheses of their own keep
them. A live test checks every ghostty command against the installed
Ghostty. Docs and screenshots updated for 1.4.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 10:44:23 +01:00

204 lines
5.1 KiB
Go

package schema
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"ghosttyconf/internal/colors"
"ghosttyconf/internal/keybind"
)
var (
reMetric = regexp.MustCompile(`^[+-]?\d+$|^[+-]?\d+(\.\d+)?%$`)
reDuration = regexp.MustCompile(`^(\s*\d+\s*(y|w|d|h|m|s|ms|µs|us|ns)\s*)+$`)
rePadding = regexp.MustCompile(`^\d+(\s*,\s*\d+)?$`)
rePalette = regexp.MustCompile(`^\s*(\d{1,3})\s*=\s*(.+?)\s*$`)
)
// Unquote strips one pair of surrounding double quotes, as Ghostty does
// when reading a config file.
func Unquote(s string) string {
if len(s) >= 2 && s[0] == '"' && s[len(s)-1] == '"' {
return s[1 : len(s)-1]
}
return s
}
// Validate checks a single (raw, possibly quoted) value for the option. For
// repeatable options it validates one list entry.
func (o *Option) Validate(raw string) error {
v := strings.TrimSpace(Unquote(raw))
if v == "" {
// An empty value resets the option to its default; always allowed.
return nil
}
if contains(o.OldValues, v) {
return nil
}
switch o.Kind {
case KindBool:
if v != "true" && v != "false" {
return errors.New(`expected "true" or "false"`)
}
case KindEnum:
if !contains(o.Values, v) {
return fmt.Errorf("expected one of: %s", strings.Join(o.Values, ", "))
}
case KindFlags:
return o.validateFlags(v)
case KindInt:
if contains(o.Values, v) { // e.g. "unlimited"
return nil
}
n, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return errors.New("expected a whole number")
}
return o.checkRange(float64(n))
case KindFloat:
f, err := strconv.ParseFloat(v, 64)
if err != nil {
return errors.New("expected a number")
}
return o.checkRange(f)
case KindColor:
if contains(o.Values, v) || colors.Valid(v) {
return nil
}
return errors.New("expected a color like #1e1e2e, 1e1e2e, #fff or an X11 name like \"dark slate gray\"")
case KindColorList:
for _, part := range strings.Split(v, ",") {
if !colors.Valid(part) {
return fmt.Errorf("%q is not a valid color", strings.TrimSpace(part))
}
}
case KindMetric:
if !reMetric.MatchString(v) {
return errors.New("expected an integer (e.g. 2, -1) or a percentage (e.g. 10%, -5%)")
}
case KindDuration:
if v != "0" && !reDuration.MatchString(v) {
return errors.New("expected a duration such as 750ms, 5s, 2m or 1h30m")
}
case KindPadding:
if !rePadding.MatchString(v) {
return errors.New("expected one or two non-negative integers, e.g. 4 or 4,8")
}
case KindKeybind:
if v == "clear" {
return nil
}
trigger, action := SplitKeybind(v)
if trigger == "" || action == "" {
return errors.New("expected trigger=action, e.g. ctrl+shift+t=new_tab (or \"clear\")")
}
case KindPalette:
m := rePalette.FindStringSubmatch(v)
if m == nil {
return errors.New("expected N=COLOR, e.g. 4=#89b4fa")
}
if n, _ := strconv.Atoi(m[1]); n > 255 {
return errors.New("palette index must be between 0 and 255")
}
if !colors.Valid(m[2]) {
return fmt.Errorf("%q is not a valid color", m[2])
}
}
return nil
}
func (o *Option) validateFlags(v string) error {
if v == "true" || v == "false" {
return nil
}
for _, part := range strings.Split(v, ",") {
name := strings.TrimPrefix(strings.TrimSpace(part), "no-")
if !contains(o.Flags, name) {
return fmt.Errorf("unknown flag %q (valid: %s)", name, strings.Join(o.Flags, ", "))
}
}
return nil
}
func (o *Option) checkRange(f float64) error {
if o.Min != nil && f < *o.Min {
return fmt.Errorf("must be at least %s", num(*o.Min))
}
if o.Max != nil && f > *o.Max {
return fmt.Errorf("must be at most %s", num(*o.Max))
}
return nil
}
// ParseFlags returns the state of every flag for a flags value, starting
// from the option's default the same way Ghostty does.
func (o *Option) ParseFlags(value string) map[string]bool {
state := make(map[string]bool, len(o.Flags))
apply := func(v string) {
v = strings.TrimSpace(Unquote(v))
if v == "true" || v == "false" {
for _, f := range o.Flags {
state[f] = v == "true"
}
return
}
for _, part := range strings.Split(v, ",") {
part = strings.TrimSpace(part)
if part == "" {
continue
}
on := !strings.HasPrefix(part, "no-")
state[strings.TrimPrefix(part, "no-")] = on
}
}
apply(o.Default())
if value != "" {
apply(value)
}
return state
}
// FormatFlags renders flag states in Ghostty's explicit form,
// e.g. "cursor,no-sudo,title".
func (o *Option) FormatFlags(state map[string]bool) string {
parts := make([]string, len(o.Flags))
for i, f := range o.Flags {
if state[f] {
parts[i] = f
} else {
parts[i] = "no-" + f
}
}
return strings.Join(parts, ",")
}
// SplitKeybind splits "trigger=action" exactly like Ghostty does.
func SplitKeybind(v string) (trigger, action string) {
return keybind.Split(v)
}
// SplitPalette splits a palette entry into its index and color.
func SplitPalette(v string) (index int, color string, ok bool) {
m := rePalette.FindStringSubmatch(Unquote(v))
if m == nil {
return 0, "", false
}
n, err := strconv.Atoi(m[1])
if err != nil || n > 255 {
return 0, "", false
}
return n, m[2], true
}
func contains(list []string, v string) bool {
for _, s := range list {
if s == v {
return true
}
}
return false
}