initial commit [141.14.140.180,mike]
This commit is contained in:
commit
156bb18c91
50 files changed
+13466
No files matched your search
@@ -0,0 +1,264 @@
|
||||
// Package ghostty talks to the installed Ghostty binary: it locates it,
|
||||
// reads documentation, defaults, themes, fonts and actions from it, and
|
||||
// validates config files with `ghostty +validate-config`.
|
||||
package ghostty
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Ghostty is an installed Ghostty binary.
|
||||
type Ghostty struct {
|
||||
Bin string
|
||||
Version string
|
||||
}
|
||||
|
||||
var (
|
||||
reVersion = regexp.MustCompile(`Ghostty (\d+\.\d+\.\d+\S*)`)
|
||||
reValidation = regexp.MustCompile(`^.+?:(\d+):(.*)$`)
|
||||
)
|
||||
|
||||
// Find locates the ghostty binary. An explicit path takes precedence, then
|
||||
// $PATH, then the usual macOS application locations.
|
||||
func Find(explicit string) (*Ghostty, error) {
|
||||
var candidates []string
|
||||
if explicit != "" {
|
||||
candidates = append(candidates, explicit)
|
||||
} else {
|
||||
if p, err := exec.LookPath("ghostty"); err == nil {
|
||||
candidates = append(candidates, p)
|
||||
}
|
||||
if runtime.GOOS == "darwin" {
|
||||
candidates = append(candidates,
|
||||
"/Applications/Ghostty.app/Contents/MacOS/ghostty",
|
||||
filepath.Join(os.Getenv("HOME"), "Applications/Ghostty.app/Contents/MacOS/ghostty"),
|
||||
)
|
||||
}
|
||||
}
|
||||
for _, c := range candidates {
|
||||
if fi, err := os.Stat(c); err != nil || fi.IsDir() {
|
||||
continue
|
||||
}
|
||||
g := &Ghostty{Bin: c}
|
||||
out, err := g.run(5*time.Second, "+version")
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if m := reVersion.FindStringSubmatch(out); m != nil {
|
||||
g.Version = m[1]
|
||||
}
|
||||
return g, nil
|
||||
}
|
||||
if explicit != "" {
|
||||
return nil, fmt.Errorf("%s is not a usable ghostty binary", explicit)
|
||||
}
|
||||
return nil, errors.New("ghostty binary not found")
|
||||
}
|
||||
|
||||
func (g *Ghostty) run(timeout time.Duration, args ...string) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, g.Bin, args...)
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
// Keep Ghostty from treating our pipe as an interactive terminal.
|
||||
cmd.Stdin = nil
|
||||
err := cmd.Run()
|
||||
if err != nil && stdout.Len() == 0 {
|
||||
msg := strings.TrimSpace(stderr.String())
|
||||
if msg == "" {
|
||||
msg = err.Error()
|
||||
}
|
||||
return "", fmt.Errorf("ghostty %s: %s", strings.Join(args, " "), msg)
|
||||
}
|
||||
return stdout.String(), err
|
||||
}
|
||||
|
||||
// DefaultsWithDocs returns `ghostty +show-config --default --docs`.
|
||||
func (g *Ghostty) DefaultsWithDocs() (string, error) {
|
||||
out, err := g.run(10*time.Second, "+show-config", "--default", "--docs")
|
||||
if err != nil && out == "" {
|
||||
return "", err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Themes lists all available theme names.
|
||||
func (g *Ghostty) Themes() ([]string, error) {
|
||||
out, err := g.run(10*time.Second, "+list-themes", "--plain")
|
||||
if out == "" && err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var names []string
|
||||
seen := map[string]bool{}
|
||||
for _, line := range strings.Split(out, "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
// Lines look like "Dracula (resources)" or "Mine (user)".
|
||||
if i := strings.LastIndex(line, " ("); i > 0 && strings.HasSuffix(line, ")") {
|
||||
line = line[:i]
|
||||
}
|
||||
if !seen[line] {
|
||||
seen[line] = true
|
||||
names = append(names, line)
|
||||
}
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
// Fonts lists the font families Ghostty can find.
|
||||
func (g *Ghostty) Fonts() ([]string, error) {
|
||||
out, err := g.run(20*time.Second, "+list-fonts")
|
||||
if out == "" && err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var families []string
|
||||
seen := map[string]bool{}
|
||||
for _, line := range strings.Split(out, "\n") {
|
||||
if line == "" || line[0] == ' ' || line[0] == '\t' {
|
||||
continue
|
||||
}
|
||||
line = strings.TrimSpace(line)
|
||||
if !seen[line] {
|
||||
seen[line] = true
|
||||
families = append(families, line)
|
||||
}
|
||||
}
|
||||
sort.Slice(families, func(i, j int) bool {
|
||||
return strings.ToLower(families[i]) < strings.ToLower(families[j])
|
||||
})
|
||||
return families, nil
|
||||
}
|
||||
|
||||
// Action is a keybinding action.
|
||||
type Action struct {
|
||||
Name string
|
||||
Summary string // first line of the documentation
|
||||
Doc string
|
||||
}
|
||||
|
||||
// Actions lists all keybinding actions with their documentation.
|
||||
func (g *Ghostty) Actions() ([]Action, error) {
|
||||
out, err := g.run(10*time.Second, "+list-actions", "--docs")
|
||||
if out == "" && err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return parseActions(out), nil
|
||||
}
|
||||
|
||||
func parseActions(out string) []Action {
|
||||
var actions []Action
|
||||
var cur *Action
|
||||
var doc []string
|
||||
flush := func() {
|
||||
if cur == nil {
|
||||
return
|
||||
}
|
||||
cur.Doc = strings.TrimSpace(strings.Join(doc, "\n"))
|
||||
if i := strings.Index(cur.Doc, "\n\n"); i >= 0 {
|
||||
cur.Summary = strings.ReplaceAll(cur.Doc[:i], "\n", " ")
|
||||
} else {
|
||||
cur.Summary = strings.ReplaceAll(cur.Doc, "\n", " ")
|
||||
}
|
||||
actions = append(actions, *cur)
|
||||
}
|
||||
for _, line := range strings.Split(out, "\n") {
|
||||
if line != "" && line[0] != ' ' && strings.HasSuffix(line, ":") {
|
||||
flush()
|
||||
cur = &Action{Name: strings.TrimSuffix(line, ":")}
|
||||
doc = nil
|
||||
continue
|
||||
}
|
||||
if cur != nil {
|
||||
doc = append(doc, strings.TrimPrefix(line, " "))
|
||||
}
|
||||
}
|
||||
flush()
|
||||
return actions
|
||||
}
|
||||
|
||||
// ValidationError is one problem reported by `ghostty +validate-config`.
|
||||
type ValidationError struct {
|
||||
Line int // 0 if unknown
|
||||
Message string
|
||||
}
|
||||
|
||||
func (e ValidationError) String() string {
|
||||
if e.Line > 0 {
|
||||
return fmt.Sprintf("line %d: %s", e.Line, e.Message)
|
||||
}
|
||||
return e.Message
|
||||
}
|
||||
|
||||
// Validate runs `ghostty +validate-config` on path.
|
||||
func (g *Ghostty) Validate(path string) ([]ValidationError, error) {
|
||||
abs, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out, runErr := g.run(15*time.Second, "+validate-config", "--config-file="+abs)
|
||||
var errs []ValidationError
|
||||
sc := bufio.NewScanner(strings.NewReader(out))
|
||||
for sc.Scan() {
|
||||
line := strings.TrimSpace(sc.Text())
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
// Lines look like "/path/to/config:3:font-size: invalid value".
|
||||
e := ValidationError{Message: line}
|
||||
if m := reValidation.FindStringSubmatch(line); m != nil {
|
||||
e.Line, _ = strconv.Atoi(m[1])
|
||||
e.Message = m[2]
|
||||
}
|
||||
errs = append(errs, e)
|
||||
}
|
||||
if runErr != nil && len(errs) == 0 {
|
||||
var exitErr *exec.ExitError
|
||||
if !errors.As(runErr, &exitErr) {
|
||||
return nil, runErr
|
||||
}
|
||||
errs = append(errs, ValidationError{Message: "ghostty reported an invalid configuration"})
|
||||
}
|
||||
return errs, nil
|
||||
}
|
||||
|
||||
// ResourcesDir returns Ghostty's resources directory (where the bundled
|
||||
// themes live), or "".
|
||||
func (g *Ghostty) ResourcesDir() string {
|
||||
if d := os.Getenv("GHOSTTY_RESOURCES_DIR"); d != "" {
|
||||
return d
|
||||
}
|
||||
if g == nil {
|
||||
return ""
|
||||
}
|
||||
bin, err := filepath.EvalSymlinks(g.Bin)
|
||||
if err != nil {
|
||||
bin = g.Bin
|
||||
}
|
||||
dir := filepath.Dir(bin)
|
||||
for _, c := range []string{
|
||||
filepath.Join(dir, "../Resources/ghostty"), // macOS app bundle
|
||||
filepath.Join(dir, "../share/ghostty"), // Linux prefix install
|
||||
} {
|
||||
if fi, err := os.Stat(filepath.Join(c, "themes")); err == nil && fi.IsDir() {
|
||||
return filepath.Clean(c)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
Reference in new issue
Block a user