Files
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

143 lines
3.5 KiB
Go

package ghostty
import (
"bufio"
"os"
"path/filepath"
"runtime"
"strings"
)
func xdgConfigDir() string {
if d := os.Getenv("XDG_CONFIG_HOME"); d != "" {
return filepath.Join(d, "ghostty")
}
return filepath.Join(os.Getenv("HOME"), ".config", "ghostty")
}
func macConfigDir() string {
return filepath.Join(os.Getenv("HOME"), "Library", "Application Support", "com.mitchellh.ghostty")
}
// ConfigCandidates lists the files Ghostty loads by default, in load order.
func ConfigCandidates() []string {
xdg := xdgConfigDir()
c := []string{filepath.Join(xdg, "config"), filepath.Join(xdg, "config.ghostty")}
if runtime.GOOS == "darwin" {
mac := macConfigDir()
c = append(c, filepath.Join(mac, "config"), filepath.Join(mac, "config.ghostty"))
}
return c
}
// DefaultConfigPath picks the config file to edit: the last existing file
// in Ghostty's load order (it has the final say), or the preferred location
// for a new file.
func DefaultConfigPath() string {
c := ConfigCandidates()
for i := len(c) - 1; i >= 0; i-- {
if fi, err := os.Stat(c[i]); err == nil && !fi.IsDir() {
return c[i]
}
}
if runtime.GOOS == "darwin" {
return filepath.Join(macConfigDir(), "config.ghostty")
}
return filepath.Join(xdgConfigDir(), "config.ghostty")
}
// ExistingConfigFiles returns all default config files that exist.
func ExistingConfigFiles() []string {
var out []string
for _, c := range ConfigCandidates() {
if fi, err := os.Stat(c); err == nil && !fi.IsDir() {
out = append(out, c)
}
}
return out
}
// Theme holds the colors of a theme file, used for previews.
type Theme struct {
Name string
Background string
Foreground string
Cursor string
Selection string
Palette [16]string
}
// ThemeDirs returns the directories searched for themes, user dirs first.
// Like Ghostty, this is the XDG config directory even on macOS, where the
// config file itself usually lives under Application Support.
func (g *Ghostty) ThemeDirs() []string {
dirs := []string{filepath.Join(xdgConfigDir(), "themes")}
if r := g.ResourcesDir(); r != "" {
dirs = append(dirs, filepath.Join(r, "themes"))
}
return dirs
}
// LoadTheme reads a theme by name (or path) for previewing. A leading "~/"
// stands for the home directory, as in Ghostty 1.4 and later.
func (g *Ghostty) LoadTheme(name string) (*Theme, error) {
var path string
if rest, ok := strings.CutPrefix(name, "~/"); ok {
path = filepath.Join(os.Getenv("HOME"), rest)
} else if filepath.IsAbs(name) {
path = name
} else {
for _, d := range g.ThemeDirs() {
p := filepath.Join(d, name)
if _, err := os.Stat(p); err == nil {
path = p
break
}
}
}
if path == "" {
return nil, os.ErrNotExist
}
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
t := &Theme{Name: name}
sc := bufio.NewScanner(f)
for sc.Scan() {
key, value, ok := strings.Cut(sc.Text(), "=")
if !ok {
continue
}
key, value = strings.TrimSpace(key), strings.TrimSpace(value)
switch key {
case "background":
t.Background = value
case "foreground":
t.Foreground = value
case "cursor-color":
t.Cursor = value
case "selection-background":
t.Selection = value
case "palette":
idx, color, ok := strings.Cut(value, "=")
if !ok {
continue
}
var n int
for _, c := range strings.TrimSpace(idx) {
if c < '0' || c > '9' {
n = -1
break
}
n = n*10 + int(c-'0')
}
if n >= 0 && n < 16 {
t.Palette[n] = strings.TrimSpace(color)
}
}
}
return t, sc.Err()
}