package ui import ( "strings" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/x/ansi" ) type dialogKey struct{ key, label string } // dialog is a modal box drawn over the main view. type dialog struct { title string lines []string keys []dialogKey onKey func(key string) tea.Cmd scroll int warn bool quit bool // the unsaved-changes prompt: a second ctrl+c quits height int // visible body lines in the last render } func (d *dialog) update(a *App, km tea.KeyMsg) tea.Cmd { key := km.String() switch key { case "up", "k": d.scroll = max(0, d.scroll-1) return nil case "down", "j": d.scroll = min(d.scroll+1, max(0, len(d.lines)-d.height)) return nil case "pgup": d.scroll = max(0, d.scroll-d.height) return nil case "pgdown", " ": d.scroll = min(d.scroll+d.height, max(0, len(d.lines)-d.height)) return nil } for _, k := range d.keys { if k.key == key { a.dlg = nil if d.onKey != nil { return d.onKey(key) } return nil } } if key == "esc" || key == "q" || key == "enter" { a.dlg = nil if d.onKey != nil { return d.onKey("esc") } } return nil } func (d *dialog) view(width, height int) string { var keys []string for _, k := range d.keys { keys = append(keys, stKey.Render(k.key)+" "+k.label) } hint := strings.Join(keys, " ") // Size the box to its content, within the screen. want := max(40, ansi.StringWidth(hint)+18, ansi.StringWidth(d.title)+8) for _, l := range d.lines { want = max(want, ansi.StringWidth(l)+4) } w := min(width-4, 84, want) inner := w - 4 var body []string for _, l := range d.lines { if l == "" { body = append(body, "") continue } body = append(body, strings.Split(ansi.Wrap(l, inner, ""), "\n")...) } maxBody := max(3, height-8) d.height = min(len(body), maxBody) d.scroll = min(d.scroll, max(0, len(body)-d.height)) visible := body[d.scroll : d.scroll+d.height] if len(body) > d.height { hint += stDim.Render(" ↑↓ scroll") } lines := []string{""} for _, l := range visible { lines = append(lines, " "+l) } lines = append(lines, "", " "+hint) content := strings.Join(lines, "\n") color := colAccent if d.warn { color = colChanged } return framed(d.title, content, w, len(lines)+2, color) } func helpDialog(version, update string) *dialog { k := func(key, desc string) string { return stKey.Render(fit(key, 16)) + desc } title := "Help" if version != "" { title = "Help · ghosttyconf " + version } d := &dialog{ title: title, lines: []string{ stBold.Render("Browsing"), k("↑↓ / j k", "move"), k("← → / tab", "switch between categories and options"), k("enter", "edit the selected option (booleans toggle)"), k("space", "toggle a boolean / cycle through choices"), k("+ / -", "increase / decrease numbers"), k("r / del", "reset to the default (removes the line from the file)"), k("/", "search option names and documentation"), k("b", "open the key binding editor"), k("shift+↑↓", "scroll the documentation (also ctrl+u / ctrl+d)"), k("p", "hide options for other platforms"), k("u / ctrl+z", "undo the last change"), k("c", "show unsaved changes"), k("ctrl+s", "validate with Ghostty, save and reload Ghostty's config"), k("q / ctrl+c", "quit"), "", stBold.Render("Editors"), k("enter", "apply the value"), k("esc", "cancel (in list editors: done)"), k("↑↓", "choose; in number fields: increase / decrease"), k("tab", "complete the highlighted theme, font or action"), "", stBold.Render("Key bindings") + stDim.Render(" (b, or enter on keybind)"), k("enter", "change the selected binding (also built-in ones)"), k("a", "add a binding"), k("d", "unbind: the key no longer does anything in Ghostty"), k("r", "restore Ghostty's default for this key"), k("ctrl+r", "in the binding form: record the key combination"), k("/ c s", "filter · show changes only · sort by action or key"), k("R", "edit the raw keybind entries"), "", stBold.Render("List editors") + stDim.Render(" (font-family, env, config-file, …)"), k("a", "add an entry"), k("enter / e", "edit the entry; on a built-in default: copy it"), k("r", "edit the entry as raw text"), k("d", "delete the entry"), k("J / K", "move the entry down / up"), k("v", "show or hide Ghostty's built-in defaults"), "", stBold.Render("Markers"), stSet.Render("●") + " set in your config " + stChanged.Render("●") + " changed, not saved " + stChanged.Render("○") + " removed, not saved", "", "Comments, blank lines and the order of untouched lines in your config file are kept. " + "Before the first save a backup is written to .bak.", }, keys: []dialogKey{{"esc", "Close"}}, } if update != "" { note := stChanged.Render("ghosttyconf "+update+" is available") + stDim.Render(", run ") + "ghosttyconf --update" + stDim.Render(" after quitting") d.lines = append([]string{note, ""}, d.lines...) } return d }