141 lines
3.3 KiB
Go
141 lines
3.3 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.
|
|
func (g *Ghostty) ThemeDirs() []string {
|
|
dirs := []string{filepath.Join(xdgConfigDir(), "themes")}
|
|
if runtime.GOOS == "darwin" {
|
|
dirs = append(dirs, filepath.Join(macConfigDir(), "themes"))
|
|
}
|
|
if r := g.ResourcesDir(); r != "" {
|
|
dirs = append(dirs, filepath.Join(r, "themes"))
|
|
}
|
|
return dirs
|
|
}
|
|
|
|
// LoadTheme reads a theme by name (or absolute path) for previewing.
|
|
func (g *Ghostty) LoadTheme(name string) (*Theme, error) {
|
|
var path string
|
|
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()
|
|
}
|