// Command ghosttyconf is a terminal editor for the Ghostty configuration. package main import ( "flag" "fmt" "os" "path/filepath" tea "github.com/charmbracelet/bubbletea" "ghosttyconf/internal/config" "ghosttyconf/internal/ghostty" "ghosttyconf/internal/schema" "ghosttyconf/internal/ui" ) func main() { configPath := flag.String("config", "", "config file to edit (default: Ghostty's default config file)") ghosttyBin := flag.String("ghostty", "", "path to the ghostty binary (default: search $PATH and /Applications)") offline := flag.Bool("offline", false, "do not run ghostty; use the built-in option list") printPath := flag.Bool("print-path", false, "print the config file that would be edited and exit") noReload := flag.Bool("no-reload", false, "do not ask Ghostty to reload its configuration after saving") flag.Usage = func() { fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [flags] [config-file]\n\n", filepath.Base(os.Args[0])) fmt.Fprintln(flag.CommandLine.Output(), "A terminal editor for the Ghostty configuration. Flags:") flag.PrintDefaults() } flag.Parse() if flag.NArg() > 1 { flag.Usage() os.Exit(2) } if flag.NArg() == 1 { *configPath = flag.Arg(0) } path := *configPath if path == "" { path = ghostty.DefaultConfigPath() } if abs, err := filepath.Abs(path); err == nil { path = abs } if *printPath { fmt.Println(path) return } if err := run(path, *ghosttyBin, *offline, !*noReload); err != nil { fmt.Fprintln(os.Stderr, "ghosttyconf:", err) os.Exit(1) } } func run(path, ghosttyBin string, offline, autoReload bool) error { sch, err := schema.Embedded() if err != nil { return err } var gh *ghostty.Ghostty if !offline { gh, err = ghostty.Find(ghosttyBin) if err != nil && ghosttyBin != "" { return err } if gh != nil { if docs, err := gh.DefaultsWithDocs(); err == nil { sch.Refresh(docs, gh.Version) } } } file, err := config.Load(path) if err != nil { return fmt.Errorf("read config: %w", err) } app := ui.New(sch, gh, file) app.SetAutoReload(autoReload) p := tea.NewProgram(app, tea.WithAltScreen(), tea.WithMouseCellMotion()) defer ui.RestoreTerminal() _, err = p.Run() return err }