Files
mikeandClaude Opus 5.5 fb5602452a Add self-update, version 1.0.1
ghosttyconf --update installs the newest release from git.micw.org,
--check-update only checks. The download lands next to the running
binary, is started once with --version and only then replaces it.

Once a day a normal start asks in the background and mentions a newer
release in the header, at the top of the help and on stderr after
quitting; only on a terminal and not with GHOSTTYCONF_NO_UPDATE_CHECK
set. selfupdate.go is nscan's copyable template, with goca's --update
spelling in the hints.

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

124 lines
3.4 KiB
Go

// 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() {
// The background look for a newer release (selfupdate.go). Taken before
// the flags, so it can stay out of the help.
if len(os.Args) == 2 && os.Args[1] == updateRefreshFlag {
selfUpdate.refresh()
return
}
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")
showVersion := flag.Bool("version", false, "print the version and exit")
update := flag.Bool("update", false, "install the newest release in place of this binary and exit")
checkUpdate := flag.Bool("check-update", false, "check for a newer release and exit")
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [flags] [config-file]\n\n", filepath.Base(os.Args[0]))
fmt.Fprintf(flag.CommandLine.Output(), "A terminal editor for the Ghostty configuration (version %s). Flags:\n", Version)
flag.PrintDefaults()
}
flag.Parse()
if *showVersion {
fmt.Println("ghosttyconf", Version)
return
}
if *update || *checkUpdate {
var err error
if *update {
err = selfUpdate.install(os.Stdout)
} else {
err = selfUpdate.check(os.Stdout)
}
if err != nil {
fmt.Fprintln(os.Stderr, "ghosttyconf:", err)
os.Exit(1)
}
return
}
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)
app.SetVersion(Version)
// Costs nothing: the hint comes from a note in the cache, and the asking
// happens at most once a day, in the background.
if hint := selfUpdate.daily(); hint != "" {
app.SetUpdate(selfUpdate.loadState().Latest)
// Once more after the editor has closed, where `ghosttyconf --update`
// can be typed right away. Deferred before RestoreTerminal, so it
// runs after it.
defer fmt.Fprintln(os.Stderr, hint)
}
p := tea.NewProgram(app, tea.WithAltScreen(), tea.WithMouseCellMotion())
defer ui.RestoreTerminal()
_, err = p.Run()
return err
}