473 lines
12 KiB
Go
473 lines
12 KiB
Go
package ui
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/charmbracelet/bubbles/textinput"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/lipgloss"
|
|
"github.com/charmbracelet/x/ansi"
|
|
|
|
"ghosttyconf/internal/ghostty"
|
|
"ghosttyconf/internal/keybind"
|
|
"ghosttyconf/internal/schema"
|
|
)
|
|
|
|
// keybindManager shows the effective key bindings — Ghostty's defaults
|
|
// merged with the user's keybind entries — and lets the user change,
|
|
// unbind, restore and add bindings. Edits are translated into the minimal
|
|
// set of keybind entries.
|
|
type keybindManager struct {
|
|
opt *schema.Option
|
|
set *keybind.Set
|
|
original []string
|
|
defaults []string
|
|
actions []ghostty.Action
|
|
summaries map[string]string
|
|
|
|
rows []keybind.Binding
|
|
cursor int
|
|
top int
|
|
height int
|
|
filter textinput.Model
|
|
filtering bool
|
|
changesOnly bool
|
|
byTrigger bool
|
|
msg string
|
|
msgStyle lipgloss.Style
|
|
|
|
child editor
|
|
childFor *keybind.Binding // binding being edited; nil when adding
|
|
childRaw bool // child is the raw entry list
|
|
newItem func(string) editor
|
|
}
|
|
|
|
func newKeybindManager(o *schema.Option, values []string, actions []ghostty.Action, newItem func(string) editor) *keybindManager {
|
|
m := &keybindManager{
|
|
opt: o,
|
|
original: append([]string(nil), values...),
|
|
defaults: o.DefaultList(),
|
|
actions: actions,
|
|
summaries: map[string]string{},
|
|
newItem: newItem,
|
|
filter: newInput("", "filter by key, action or description"),
|
|
}
|
|
m.filter.Prompt = "/ "
|
|
m.filter.Blur()
|
|
for _, a := range actions {
|
|
m.summaries[a.Name] = a.Summary
|
|
}
|
|
m.set = keybind.NewSet(m.defaults, values)
|
|
m.refresh("")
|
|
return m
|
|
}
|
|
|
|
func (m *keybindManager) capturing() bool {
|
|
c, ok := m.child.(interface{ capturing() bool })
|
|
return ok && c.capturing()
|
|
}
|
|
|
|
// refresh recomputes the visible rows and keeps the cursor on norm (or on
|
|
// the current row when norm is empty).
|
|
func (m *keybindManager) refresh(norm string) {
|
|
if norm == "" && m.cursor < len(m.rows) {
|
|
norm = m.rows[m.cursor].Norm
|
|
}
|
|
query := strings.TrimSpace(m.filter.Value())
|
|
terms := strings.Fields(strings.ToLower(query))
|
|
// A query that is a valid trigger ("cmd+d") also finds that exact
|
|
// binding, whatever spelling it uses.
|
|
exact, err := keybind.Normalize(query)
|
|
if err != nil || query == "" {
|
|
exact = ""
|
|
}
|
|
var rows []keybind.Binding
|
|
for _, b := range m.set.Bindings() {
|
|
if m.changesOnly && b.Source == keybind.Default && !b.InConfig {
|
|
continue
|
|
}
|
|
if exact != "" && b.Norm == exact {
|
|
rows = append(rows, b)
|
|
continue
|
|
}
|
|
hay := strings.ToLower(b.Trigger + " " + b.Action + " " + b.Source.String() + " " + m.summaries[keybind.ActionName(b.Action)])
|
|
ok := true
|
|
for _, t := range terms {
|
|
if !strings.Contains(hay, t) {
|
|
ok = false
|
|
break
|
|
}
|
|
}
|
|
if ok {
|
|
rows = append(rows, b)
|
|
}
|
|
}
|
|
sort.SliceStable(rows, func(i, j int) bool {
|
|
a, b := rows[i], rows[j]
|
|
if (a.Norm == exact) != (b.Norm == exact) {
|
|
return a.Norm == exact
|
|
}
|
|
if m.byTrigger {
|
|
return a.Norm < b.Norm
|
|
}
|
|
if a.Action != b.Action {
|
|
return a.Action < b.Action
|
|
}
|
|
return a.Norm < b.Norm
|
|
})
|
|
m.rows = rows
|
|
m.cursor = min(m.cursor, max(0, len(rows)-1))
|
|
for i, b := range rows {
|
|
if b.Norm == norm {
|
|
m.cursor = i
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
func (m *keybindManager) selected() *keybind.Binding {
|
|
if m.cursor < len(m.rows) {
|
|
b := m.rows[m.cursor]
|
|
return &b
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *keybindManager) changed() bool {
|
|
return !equalStrings(m.set.Values(), m.original)
|
|
}
|
|
|
|
func (m *keybindManager) setMsg(s string, st lipgloss.Style) { m.msg, m.msgStyle = s, st }
|
|
|
|
func (m *keybindManager) openEditor(b *keybind.Binding) {
|
|
value := ""
|
|
if b != nil {
|
|
value = b.Trigger + "=" + b.Action
|
|
}
|
|
e := newKeybindEditor(value, m.actions)
|
|
if b == nil {
|
|
e.setFocus(0)
|
|
}
|
|
e.conflict = func(trigger string) string {
|
|
n, err := keybind.Normalize(trigger)
|
|
if err != nil || (b != nil && n == b.Norm) {
|
|
return ""
|
|
}
|
|
if cur, ok := m.set.Lookup(n); ok {
|
|
return fmt.Sprintf("already bound to %s (%s); saving replaces that binding", cur.Action, cur.Source)
|
|
}
|
|
return ""
|
|
}
|
|
m.child, m.childFor, m.childRaw = e, b, false
|
|
}
|
|
|
|
func (m *keybindManager) Update(msg tea.Msg) (tea.Cmd, *result) {
|
|
if m.child != nil {
|
|
cmd, res := m.child.Update(msg)
|
|
if res != nil {
|
|
m.finishChild(res)
|
|
}
|
|
return cmd, nil
|
|
}
|
|
km, ok := msg.(tea.KeyMsg)
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
key := km.String()
|
|
|
|
if m.filtering {
|
|
switch key {
|
|
case "esc":
|
|
m.filter.SetValue("")
|
|
m.filtering = false
|
|
m.filter.Blur()
|
|
m.refresh("")
|
|
case "enter", "down", "tab":
|
|
m.filtering = false
|
|
m.filter.Blur()
|
|
default:
|
|
var cmd tea.Cmd
|
|
before := m.filter.Value()
|
|
m.filter, cmd = m.filter.Update(km)
|
|
if m.filter.Value() != before {
|
|
m.cursor, m.top = 0, 0
|
|
m.refresh("")
|
|
}
|
|
return cmd, nil
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
if c, moved := moveCursor(key, m.cursor, len(m.rows), max(1, m.height-1)); moved {
|
|
m.cursor = c
|
|
return nil, nil
|
|
}
|
|
m.msg = ""
|
|
b := m.selected()
|
|
switch key {
|
|
case "esc", "q":
|
|
if m.filter.Value() != "" && key == "esc" {
|
|
m.filter.SetValue("")
|
|
m.refresh("")
|
|
return nil, nil
|
|
}
|
|
if !m.changed() {
|
|
return nil, cancelled()
|
|
}
|
|
v := m.set.Values()
|
|
if len(v) == 0 {
|
|
return nil, &result{}
|
|
}
|
|
return nil, valuesResult(v...)
|
|
case "/":
|
|
m.filtering = true
|
|
return m.filter.Focus(), nil
|
|
case "enter", "e":
|
|
if b != nil {
|
|
m.openEditor(b)
|
|
}
|
|
case "a", "n", "+", "insert":
|
|
m.openEditor(nil)
|
|
case "d", "x", "delete", "backspace":
|
|
if b == nil {
|
|
break
|
|
}
|
|
if b.Source == keybind.Unbound {
|
|
m.setMsg(b.Trigger+" is already unbound. Press r to restore it.", stDim)
|
|
break
|
|
}
|
|
m.set.Unbind(*b)
|
|
m.setMsg("Unbound "+b.Trigger+".", stChanged)
|
|
m.refresh(b.Norm)
|
|
case "r":
|
|
if b == nil {
|
|
break
|
|
}
|
|
m.set.Restore(*b)
|
|
if b.Default != "" {
|
|
m.setMsg("Restored "+b.Trigger+" → "+b.Default+".", stChanged)
|
|
} else {
|
|
m.setMsg("Removed "+b.Trigger+" (it has no default binding).", stChanged)
|
|
}
|
|
m.refresh(b.Norm)
|
|
case "c":
|
|
m.changesOnly = !m.changesOnly
|
|
m.refresh("")
|
|
case "s":
|
|
m.byTrigger = !m.byTrigger
|
|
m.refresh("")
|
|
case "R":
|
|
le := newListEditor(m.opt, m.set.Values(), m.newItem)
|
|
m.child, m.childFor, m.childRaw = le, nil, true
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func (m *keybindManager) finishChild(res *result) {
|
|
defer func() { m.child, m.childFor, m.childRaw = nil, nil, false }()
|
|
if res.cancel {
|
|
return
|
|
}
|
|
if m.childRaw {
|
|
m.set = keybind.NewSet(m.defaults, res.values)
|
|
m.refresh("")
|
|
m.setMsg("Updated the raw keybind entries.", stChanged)
|
|
return
|
|
}
|
|
if len(res.values) == 0 {
|
|
return
|
|
}
|
|
v := res.values[0]
|
|
if v == "clear" {
|
|
m.set = keybind.NewSet(m.defaults, append(m.set.Values(), "clear"))
|
|
m.refresh("")
|
|
m.setMsg("Added keybind = clear: all built-in bindings are removed.", stChanged)
|
|
return
|
|
}
|
|
if err := m.set.Change(m.childFor, v); err != nil {
|
|
m.setMsg("Not changed: "+err.Error(), stError)
|
|
return
|
|
}
|
|
trigger, action := keybind.Split(v)
|
|
norm := keybind.NormalizeLoose(trigger)
|
|
if m.changesOnly || m.filter.Value() != "" {
|
|
// Make sure the edited binding stays visible.
|
|
m.changesOnly = false
|
|
m.filter.SetValue("")
|
|
}
|
|
m.refresh(norm)
|
|
m.setMsg("Bound "+trigger+" → "+action+".", stChanged)
|
|
}
|
|
|
|
func (m *keybindManager) View(width, height int) string {
|
|
if m.child != nil {
|
|
title := "New binding"
|
|
switch {
|
|
case m.childRaw:
|
|
title = "Raw keybind entries"
|
|
case m.childFor != nil:
|
|
title = "Change " + m.childFor.Trigger
|
|
if m.childFor.Source == keybind.Default {
|
|
title += stDim.Render(" (built-in binding)")
|
|
}
|
|
}
|
|
return stBold.Render(title) + "\n\n" + m.child.View(width, height-2)
|
|
}
|
|
|
|
var lines []string
|
|
counts := map[keybind.Source]int{}
|
|
for _, b := range m.set.Bindings() {
|
|
counts[b.Source]++
|
|
}
|
|
summary := fmt.Sprintf("%d active · %s · %s · %s",
|
|
counts[keybind.Default]+counts[keybind.Changed]+counts[keybind.Custom],
|
|
stChanged.Render(fmt.Sprintf("%d changed", counts[keybind.Changed])),
|
|
stSet.Render(fmt.Sprintf("%d custom", counts[keybind.Custom])),
|
|
stError.Render(fmt.Sprintf("%d unbound", counts[keybind.Unbound])))
|
|
view := "all"
|
|
if m.changesOnly {
|
|
view = "changes only"
|
|
}
|
|
sortBy := "action"
|
|
if m.byTrigger {
|
|
sortBy = "key"
|
|
}
|
|
lines = append(lines, summary+stDim.Render(fmt.Sprintf(" showing %s, sorted by %s", view, sortBy)))
|
|
if m.set.HasClear() {
|
|
lines = append(lines, stChanged.Render("Your config contains keybind = clear: built-in bindings before it are removed."))
|
|
}
|
|
if m.filtering || m.filter.Value() != "" {
|
|
m.filter.Width = max(10, width-4)
|
|
lines = append(lines, m.filter.View())
|
|
}
|
|
lines = append(lines, "")
|
|
|
|
trigW := 12
|
|
for _, b := range m.rows {
|
|
trigW = max(trigW, ansi.StringWidth(b.Trigger))
|
|
}
|
|
trigW = min(trigW+2, width*40/100)
|
|
srcW := 9
|
|
actW := max(10, width-trigW-srcW-3)
|
|
lines = append(lines, stDim.Render(" "+fit("Key", trigW)+fit("Action", actW)+" Source"))
|
|
|
|
detailH := 3
|
|
listH := max(3, height-len(lines)-detailH-1)
|
|
m.height = listH
|
|
m.top = scrollWindow(m.cursor, m.top, listH, len(m.rows))
|
|
if len(m.rows) == 0 {
|
|
lines = append(lines, stDim.Render(" No bindings match."))
|
|
}
|
|
for i := m.top; i < len(m.rows) && i < m.top+listH; i++ {
|
|
lines = append(lines, renderRow(m.rowSegs(m.rows[i], i == m.cursor, trigW, actW), width, i == m.cursor))
|
|
}
|
|
for len(lines) < height-detailH {
|
|
lines = append(lines, "")
|
|
}
|
|
lines = append(lines, m.detail(width)...)
|
|
return strings.Join(lines, "\n")
|
|
}
|
|
|
|
func (m *keybindManager) rowSegs(b keybind.Binding, selected bool, trigW, actW int) []seg {
|
|
action := b.Action
|
|
if len(b.Chain) > 0 {
|
|
action += " + " + strings.Join(b.Chain, " + ")
|
|
}
|
|
actSt := lipgloss.NewStyle()
|
|
var src seg
|
|
switch b.Source {
|
|
case keybind.Default:
|
|
src = styled("default", stDim)
|
|
actSt = stDim
|
|
if b.InConfig {
|
|
actSt = lipgloss.NewStyle()
|
|
}
|
|
case keybind.Changed:
|
|
src = styled("changed", stChanged)
|
|
case keybind.Custom:
|
|
src = styled("custom", stSet)
|
|
case keybind.Unbound:
|
|
src = styled("unbound", stError)
|
|
actSt = stDim.Strikethrough(true)
|
|
}
|
|
return []seg{
|
|
cursorSeg(selected),
|
|
styled(fit(b.Trigger, trigW), stKey),
|
|
styled(fit(action, actW), actSt),
|
|
plain(" "),
|
|
src,
|
|
}
|
|
}
|
|
|
|
func (m *keybindManager) detail(width int) []string {
|
|
b := m.selected()
|
|
var lines []string
|
|
if m.msg != "" {
|
|
lines = append(lines, m.msgStyle.Render(ansi.Truncate(m.msg, width, "…")))
|
|
} else {
|
|
lines = append(lines, "")
|
|
}
|
|
if b == nil {
|
|
return append(lines, "", "")
|
|
}
|
|
var info string
|
|
switch b.Source {
|
|
case keybind.Default:
|
|
info = "Ghostty's built-in binding."
|
|
case keybind.Changed:
|
|
info = "Changed in your config. Ghostty's default: " + b.Default + " (press r to restore)."
|
|
case keybind.Custom:
|
|
info = "Added in your config."
|
|
case keybind.Unbound:
|
|
info = "Removed in your config. Press r to restore " + b.Default + "."
|
|
}
|
|
sum := m.summaries[keybind.ActionName(b.Action)]
|
|
lines = append(lines,
|
|
stDim.Render(ansi.Truncate(info, width, "…")),
|
|
ansi.Truncate(stKey.Render(keybind.ActionName(b.Action))+" "+sum, width, "…"))
|
|
return lines
|
|
}
|
|
|
|
// docFor shows the documentation of the selected binding's action and all
|
|
// keys bound to it.
|
|
func (m *keybindManager) docFor() (id, title, body string, ok bool) {
|
|
b := m.selected()
|
|
if m.child != nil || b == nil {
|
|
return "", "", "", false
|
|
}
|
|
name := keybind.ActionName(b.Action)
|
|
var doc string
|
|
for _, a := range m.actions {
|
|
if a.Name == name {
|
|
doc = a.Doc
|
|
break
|
|
}
|
|
}
|
|
if doc == "" {
|
|
doc = "No documentation available for this action."
|
|
}
|
|
var keys []string
|
|
for _, other := range m.set.Bindings() {
|
|
if keybind.ActionName(other.Action) == name && other.Source != keybind.Unbound {
|
|
keys = append(keys, "* `"+other.Trigger+"` → `"+other.Action+"` ("+other.Source.String()+")")
|
|
}
|
|
}
|
|
body = doc
|
|
if len(keys) > 0 {
|
|
body += "\n\nKeys bound to " + name + ":\n\n" + strings.Join(keys, "\n")
|
|
}
|
|
return fmt.Sprintf("action:%s#%d", name, len(m.set.Values())) + strings.Join(m.set.Values(), "\x00"), name, body, true
|
|
}
|
|
|
|
func (m *keybindManager) Help() string {
|
|
if m.child != nil {
|
|
return m.child.Help()
|
|
}
|
|
if m.filtering {
|
|
return "type to filter · enter/↓ results · esc clear"
|
|
}
|
|
return "enter change · a add · d unbind · r restore · / filter · c changes only · s sort · R raw · esc done"
|
|
}
|