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,488 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/charmbracelet/x/ansi"
|
||||
|
||||
"ghosttyconf/internal/keybind"
|
||||
"ghosttyconf/internal/schema"
|
||||
)
|
||||
|
||||
func (a *App) View() string {
|
||||
if a.width < 40 || a.height < 12 {
|
||||
return "Terminal too small for the Ghostty config editor (min. 40×12)."
|
||||
}
|
||||
bodyH := a.height - 3
|
||||
var body string
|
||||
if a.editor != nil {
|
||||
body = a.viewEditor(a.width, bodyH)
|
||||
} else {
|
||||
body = a.viewBrowse(a.width, bodyH)
|
||||
}
|
||||
out := a.viewHeader() + "\n" + body + "\n" + a.viewFooter()
|
||||
if a.dlg != nil {
|
||||
out = overlay(out, a.dlg.view(a.width, a.height), a.width, a.height)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (a *App) viewHeader() string {
|
||||
title := stTitle.Render("Ghostty Config Editor") + " "
|
||||
right := stHeader.Render(fmt.Sprintf(" %s · %d options ", a.sch.Source, len(a.sch.Options)))
|
||||
if lipgloss.Width(title)+lipgloss.Width(right)+20 > a.width {
|
||||
right = ""
|
||||
}
|
||||
path := shortPath(a.file.Path)
|
||||
if !a.file.Exists {
|
||||
path += " (new file)"
|
||||
}
|
||||
pathW := a.width - lipgloss.Width(title) - lipgloss.Width(right)
|
||||
if ansi.StringWidth(path) > pathW {
|
||||
// Keep the end of the path, which is the informative part.
|
||||
path = "…" + ansi.TruncateLeft(path, ansi.StringWidth(path)-pathW+1, "")
|
||||
}
|
||||
return title + fit(stHeader.Render(path), pathW) + right
|
||||
}
|
||||
|
||||
func (a *App) viewFooter() string {
|
||||
right := ""
|
||||
if n := len(a.changedKeys()); n > 0 {
|
||||
right = stChanged.Render(fmt.Sprintf("● %d unsaved ", n))
|
||||
}
|
||||
status := a.statusStyle.Render(a.status)
|
||||
statusW := a.width - lipgloss.Width(right) - 1
|
||||
line1 := " " + fit(status, statusW) + right
|
||||
|
||||
var help string
|
||||
switch {
|
||||
case a.editor != nil:
|
||||
help = a.editor.Help() + " · shift+↑↓ scroll docs"
|
||||
case a.searching:
|
||||
help = "type to search keys and docs · enter/↓ go to results · esc clear"
|
||||
case a.focus == paneCats:
|
||||
help = "↑↓ category · → options · / search · b keybindings · c changes · ctrl+s save · ? help · q quit"
|
||||
default:
|
||||
help = "↑↓ move · enter edit · space toggle · +/- adjust · r reset · u undo · / search · ctrl+s save · ? help · q quit"
|
||||
}
|
||||
return line1 + "\n" + stDim.Render(fit(" "+help, a.width))
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Browse screen
|
||||
|
||||
func (a *App) viewBrowse(w, h int) string {
|
||||
catW := 26
|
||||
if w < 90 {
|
||||
catW = 20
|
||||
}
|
||||
rightW := w - catW
|
||||
a.catRect = rect{0, 1, catW, h}
|
||||
cats := panel("Categories", a.renderCats(catW-2, h-2), catW, h, a.focus == paneCats && !a.searching)
|
||||
|
||||
optTitle := a.cats[a.catIdx]
|
||||
if a.query != "" || a.searching {
|
||||
optTitle = fmt.Sprintf("Search (%d)", len(a.opts))
|
||||
} else {
|
||||
optTitle = fmt.Sprintf("%s (%d)", optTitle, len(a.opts))
|
||||
}
|
||||
|
||||
if rightW >= 120 {
|
||||
optW := rightW * 52 / 100
|
||||
docW := rightW - optW
|
||||
a.optRect = rect{catW, 1, optW, h}
|
||||
a.docRect = rect{catW + optW, 1, docW, h}
|
||||
opts := panel(optTitle, a.renderOpts(optW-2, h-2), optW, h, a.focus == paneOpts)
|
||||
content := a.renderDocs(a.selected(), docW-4, h-2)
|
||||
docs := panel(a.docTitle(), content, docW, h, false)
|
||||
return lipgloss.JoinHorizontal(lipgloss.Top, cats, opts, docs)
|
||||
}
|
||||
optH := max(7, h*55/100)
|
||||
docH := h - optH
|
||||
a.optRect = rect{catW, 1, rightW, optH}
|
||||
a.docRect = rect{catW, 1 + optH, rightW, docH}
|
||||
opts := panel(optTitle, a.renderOpts(rightW-2, optH-2), rightW, optH, a.focus == paneOpts)
|
||||
content := a.renderDocs(a.selected(), rightW-4, docH-2)
|
||||
docs := panel(a.docTitle(), content, rightW, docH, false)
|
||||
return lipgloss.JoinHorizontal(lipgloss.Top, cats, lipgloss.JoinVertical(lipgloss.Left, opts, docs))
|
||||
}
|
||||
|
||||
// catRows returns the rendered category rows; -1 marks the separator.
|
||||
func (a *App) catRows() []int {
|
||||
rows := []int{0, 1, -1}
|
||||
for i := 2; i < len(a.cats); i++ {
|
||||
rows = append(rows, i)
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
func (a *App) catRowIndex(row int) int {
|
||||
rows := a.catRows()
|
||||
if row < 0 || row >= len(rows) {
|
||||
return -1
|
||||
}
|
||||
return rows[row]
|
||||
}
|
||||
|
||||
func (a *App) renderCats(w, h int) string {
|
||||
rows := a.catRows()
|
||||
sel := 0
|
||||
for i, r := range rows {
|
||||
if r == a.catIdx {
|
||||
sel = i
|
||||
}
|
||||
}
|
||||
a.catTop = scrollWindow(sel, a.catTop, h, len(rows))
|
||||
var lines []string
|
||||
for i := a.catTop; i < len(rows) && len(lines) < h; i++ {
|
||||
ci := rows[i]
|
||||
if ci < 0 {
|
||||
lines = append(lines, stDim.Render(strings.Repeat("─", w)))
|
||||
continue
|
||||
}
|
||||
name := a.cats[ci]
|
||||
n := len(a.categoryOptions(name))
|
||||
count := fmt.Sprintf("%d", n)
|
||||
selected := ci == a.catIdx && a.query == ""
|
||||
nameSt := lipgloss.NewStyle()
|
||||
if ci == 0 && n > 0 {
|
||||
nameSt = stSet
|
||||
}
|
||||
if selected && a.focus == paneCats {
|
||||
nameSt = nameSt.Bold(true)
|
||||
}
|
||||
nameW := w - 2 - len(count) - 1
|
||||
segs := []seg{cursorSeg(selected), styled(fit(name, nameW), nameSt), plain(" "), styled(count, stDim)}
|
||||
lines = append(lines, renderRow(segs, w, selected && a.focus == paneCats))
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
func (a *App) renderOpts(w, h int) string {
|
||||
var lines []string
|
||||
listY := a.optRect.y + 1
|
||||
if a.searching || a.query != "" {
|
||||
a.search.Width = max(10, w-4)
|
||||
lines = append(lines, a.search.View(), "")
|
||||
h -= 2
|
||||
listY += 2
|
||||
}
|
||||
a.optListY = listY
|
||||
if len(a.opts) == 0 {
|
||||
msg := "No options."
|
||||
switch {
|
||||
case a.query != "":
|
||||
msg = "No options match your search."
|
||||
case a.cats[a.catIdx] == catSet:
|
||||
msg = "Your config file does not set any options yet."
|
||||
}
|
||||
lines = append(lines, stDim.Render(" "+msg))
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
keyW := 0
|
||||
for _, o := range a.opts {
|
||||
keyW = max(keyW, len(o.Key))
|
||||
}
|
||||
keyW = min(keyW+1, w*55/100)
|
||||
valW := w - keyW - 4
|
||||
|
||||
a.optTop = scrollWindow(a.optIdx, a.optTop, h, len(a.opts))
|
||||
for i := a.optTop; i < len(a.opts) && i < a.optTop+h; i++ {
|
||||
o := a.opts[i]
|
||||
selected := i == a.optIdx
|
||||
keySt := lipgloss.NewStyle()
|
||||
switch {
|
||||
case a.unknown[o.Key] == o:
|
||||
keySt = stError
|
||||
case o.Platform != "" && a.osName != "" && o.Platform != a.osName:
|
||||
keySt = stDim
|
||||
}
|
||||
if selected && a.focus == paneOpts {
|
||||
keySt = keySt.Bold(true)
|
||||
}
|
||||
segs := []seg{cursorSeg(selected), a.marker(o), styled(fit(o.Key, keyW), keySt), plain(" ")}
|
||||
segs = append(segs, a.valueSegs(o, valW)...)
|
||||
lines = append(lines, renderRow(segs, w, selected && a.focus == paneOpts))
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
// marker shows whether an option is set in the file and whether it has
|
||||
// unsaved changes.
|
||||
func (a *App) marker(o *schema.Option) seg {
|
||||
now := a.file.GetAll(o.Key)
|
||||
changed := !equalStrings(now, a.saved.GetAll(o.Key))
|
||||
switch {
|
||||
case changed && len(now) == 0:
|
||||
return styled("○ ", stChanged)
|
||||
case changed:
|
||||
return styled("● ", stChanged)
|
||||
case len(now) > 0:
|
||||
return styled("● ", stSet)
|
||||
}
|
||||
return plain(" ")
|
||||
}
|
||||
|
||||
// valueSegs summarizes the current value for the option list.
|
||||
func (a *App) valueSegs(o *schema.Option, w int) []seg {
|
||||
vals := a.file.GetAll(o.Key)
|
||||
set := len(vals) > 0
|
||||
switch {
|
||||
case o.Kind == schema.KindKeybind:
|
||||
counts := map[keybind.Source]int{}
|
||||
for _, b := range keybind.NewSet(o.DefaultList(), vals).Bindings() {
|
||||
counts[b.Source]++
|
||||
}
|
||||
var parts []seg
|
||||
add := func(n int, label string, st lipgloss.Style) {
|
||||
if n == 0 {
|
||||
return
|
||||
}
|
||||
if len(parts) > 0 {
|
||||
parts = append(parts, styled(" · ", stDim))
|
||||
}
|
||||
parts = append(parts, styled(fmt.Sprintf("%d %s", n, label), st))
|
||||
}
|
||||
add(counts[keybind.Changed], "changed", stChanged)
|
||||
add(counts[keybind.Custom], "custom", stSet)
|
||||
add(counts[keybind.Unbound], "unbound", stError)
|
||||
if len(parts) == 0 {
|
||||
return []seg{styled(fmt.Sprintf("%d built-in bindings", counts[keybind.Default]), stDim)}
|
||||
}
|
||||
return parts
|
||||
case o.Kind == schema.KindPalette:
|
||||
if set {
|
||||
return []seg{plain(fmt.Sprintf("%d color(s) overridden", len(vals)))}
|
||||
}
|
||||
return []seg{styled("default palette", stDim)}
|
||||
case o.Repeatable:
|
||||
if set {
|
||||
s := vals[0]
|
||||
if o.Kind == schema.KindKeybind {
|
||||
s = keybindSummary(vals[0])
|
||||
}
|
||||
if len(vals) > 1 {
|
||||
return []seg{plain(ansi.Truncate(s, max(5, w-10), "…")), styled(fmt.Sprintf(" +%d more", len(vals)-1), stDim)}
|
||||
}
|
||||
return []seg{plain(s)}
|
||||
}
|
||||
defs := o.DefaultList()
|
||||
switch len(defs) {
|
||||
case 0:
|
||||
return []seg{styled("—", stDim)}
|
||||
case 1:
|
||||
return []seg{styled(defs[0], stDim)}
|
||||
}
|
||||
return []seg{styled(fmt.Sprintf("%d built-in entries", len(defs)), stDim)}
|
||||
}
|
||||
v := o.Default()
|
||||
st := stDim
|
||||
if set {
|
||||
v, st = vals[len(vals)-1], lipgloss.NewStyle()
|
||||
if o.Validate(v) != nil {
|
||||
st = stError
|
||||
}
|
||||
}
|
||||
if v == "" {
|
||||
return []seg{styled("—", stDim)}
|
||||
}
|
||||
v = strings.ReplaceAll(v, "\t", "\\t")
|
||||
if o.Kind == schema.KindColor || o.Kind == schema.KindColorList {
|
||||
if sw := swatch(strings.Split(v, ",")[0]); sw != "" {
|
||||
return []seg{rawSeg(sw), plain(" "), styled(v, st)}
|
||||
}
|
||||
}
|
||||
return []seg{styled(v, st)}
|
||||
}
|
||||
|
||||
func keybindSummary(v string) string {
|
||||
t, act := schema.SplitKeybind(v)
|
||||
if act == "" {
|
||||
return v
|
||||
}
|
||||
return t + " → " + act
|
||||
}
|
||||
|
||||
// renderDocs renders the documentation panel content for o into a.doc and
|
||||
// returns the visible part.
|
||||
func (a *App) renderDocs(o *schema.Option, w, h int) string {
|
||||
if o == nil {
|
||||
return ""
|
||||
}
|
||||
a.doc.Width, a.doc.Height = w, h
|
||||
id := o.Key
|
||||
var content func() string
|
||||
// Editors can show documentation of their own (e.g. of the selected
|
||||
// keybind action) instead of the option's.
|
||||
if dp, ok := a.editor.(interface {
|
||||
docFor() (id, title, body string, ok bool)
|
||||
}); ok {
|
||||
if did, title, body, ok := dp.docFor(); ok {
|
||||
id = did
|
||||
content = func() string { return stAccent.Bold(true).Render(title) + "\n\n" + renderDoc(body, w) }
|
||||
}
|
||||
}
|
||||
if content == nil {
|
||||
content = func() string { return a.docContent(o, w) }
|
||||
}
|
||||
key := fmt.Sprintf("%s|%d|%d", id, w, a.rev)
|
||||
if a.editor != nil {
|
||||
key += "|edit"
|
||||
}
|
||||
if key != a.docFor {
|
||||
if !strings.HasPrefix(a.docFor, id+"|") {
|
||||
a.doc.GotoTop()
|
||||
}
|
||||
a.docFor = key
|
||||
a.doc.SetContent(content())
|
||||
}
|
||||
lines := strings.Split(a.doc.View(), "\n")
|
||||
for i := range lines {
|
||||
lines[i] = " " + lines[i]
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
// docTitle is the documentation panel title, with a scroll position when
|
||||
// the text does not fit. Call it after renderDocs.
|
||||
func (a *App) docTitle() string {
|
||||
if a.doc.TotalLineCount() <= a.doc.Height {
|
||||
return "Documentation"
|
||||
}
|
||||
return fmt.Sprintf("Documentation · %d%% · shift+↑↓ scroll", int(a.doc.ScrollPercent()*100))
|
||||
}
|
||||
|
||||
func (a *App) docContent(o *schema.Option, w int) string {
|
||||
var b strings.Builder
|
||||
b.WriteString(stAccent.Bold(true).Render(o.Key) + "\n\n")
|
||||
row := func(label, value string) {
|
||||
b.WriteString(stLabel.Render(label) + " " + ansi.Wrap(value, max(10, w-10), "") + "\n")
|
||||
}
|
||||
if a.editor == nil {
|
||||
row("Type", o.TypeLabel())
|
||||
vals := a.file.GetAll(o.Key)
|
||||
switch {
|
||||
case o.Repeatable:
|
||||
defs := o.DefaultList()
|
||||
if len(defs) > 0 {
|
||||
row("Default", fmt.Sprintf("%d built-in entries", len(defs)))
|
||||
} else {
|
||||
row("Default", "none")
|
||||
}
|
||||
if len(vals) == 0 {
|
||||
row("Value", stDim.Render("not set"))
|
||||
} else {
|
||||
row("Value", fmt.Sprintf("%d entr%s in your config", len(vals), plural(len(vals), "y", "ies")))
|
||||
for i, v := range vals {
|
||||
if i == 8 {
|
||||
b.WriteString(stDim.Render(fmt.Sprintf(" … and %d more", len(vals)-8)) + "\n")
|
||||
break
|
||||
}
|
||||
b.WriteString(" " + ansi.Truncate(v, max(10, w-10), "…") + "\n")
|
||||
}
|
||||
}
|
||||
default:
|
||||
def := o.Default()
|
||||
if def == "" {
|
||||
def = stDim.Render("(unset)")
|
||||
} else if sw := swatch(def); sw != "" && o.Kind == schema.KindColor {
|
||||
def = sw + " " + def
|
||||
}
|
||||
row("Default", strings.ReplaceAll(def, "\t", "\\t"))
|
||||
if len(vals) == 0 {
|
||||
row("Value", stDim.Render("not set (uses default)"))
|
||||
} else {
|
||||
v := vals[len(vals)-1]
|
||||
if sw := swatch(v); sw != "" && o.Kind == schema.KindColor {
|
||||
v = sw + " " + v
|
||||
}
|
||||
row("Value", v+" "+stSet.Render("● set in config"))
|
||||
if err := o.Validate(vals[len(vals)-1]); err != nil {
|
||||
b.WriteString(stError.Render(" ✗ invalid: "+err.Error()) + "\n")
|
||||
}
|
||||
if len(vals) > 1 {
|
||||
b.WriteString(stChanged.Render(fmt.Sprintf(" set %d times; the last one wins", len(vals))) + "\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
if o.Platform != "" {
|
||||
row("Platform", o.Platform+" only")
|
||||
}
|
||||
row("Category", o.Category)
|
||||
b.WriteString("\n")
|
||||
}
|
||||
if o.Doc == "" {
|
||||
b.WriteString(stDim.Render("No documentation available."))
|
||||
} else {
|
||||
b.WriteString(renderDoc(o.Doc, w))
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func plural(n int, one, many string) string {
|
||||
if n == 1 {
|
||||
return one
|
||||
}
|
||||
return many
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Editor screen
|
||||
|
||||
func (a *App) viewEditor(w, h int) string {
|
||||
o := a.editOpt
|
||||
title := "Edit " + o.Key
|
||||
if w >= 110 {
|
||||
edW := w * 55 / 100
|
||||
docW := w - edW
|
||||
a.optRect = rect{0, 1, edW, h}
|
||||
a.docRect = rect{edW, 1, docW, h}
|
||||
left := panel(title, a.renderEditor(edW-4, h-2), edW, h, true)
|
||||
content := a.renderDocs(o, docW-4, h-2)
|
||||
right := panel(a.docTitle(), content, docW, h, false)
|
||||
return lipgloss.JoinHorizontal(lipgloss.Top, left, right)
|
||||
}
|
||||
edH := max(10, h*62/100)
|
||||
switch a.editor.(type) {
|
||||
case *keybindManager, *listEditor, *paletteEditor:
|
||||
edH = max(10, h*75/100) // long lists need the room
|
||||
}
|
||||
docH := h - edH
|
||||
a.optRect = rect{0, 1, w, edH}
|
||||
a.docRect = rect{0, 1 + edH, w, docH}
|
||||
top := panel(title, a.renderEditor(w-4, edH-2), w, edH, true)
|
||||
if docH < 3 {
|
||||
return top
|
||||
}
|
||||
content := a.renderDocs(o, w-4, docH-2)
|
||||
bottom := panel(a.docTitle(), content, w, docH, false)
|
||||
return lipgloss.JoinVertical(lipgloss.Left, top, bottom)
|
||||
}
|
||||
|
||||
func (a *App) renderEditor(w, h int) string {
|
||||
o := a.editOpt
|
||||
var head []string
|
||||
head = append(head, stLabel.Render("Type")+" "+ansi.Truncate(o.TypeLabel(), max(10, w-10), "…"))
|
||||
if !o.Repeatable {
|
||||
def := o.Default()
|
||||
if def == "" {
|
||||
def = stDim.Render("(unset)")
|
||||
}
|
||||
cur, ok := a.file.Get(o.Key)
|
||||
if !ok {
|
||||
cur = stDim.Render("not set")
|
||||
}
|
||||
head = append(head,
|
||||
stLabel.Render("Default")+" "+ansi.Truncate(strings.ReplaceAll(def, "\t", "\\t"), max(10, w-10), "…"),
|
||||
stLabel.Render("Current")+" "+ansi.Truncate(cur, max(10, w-10), "…"))
|
||||
}
|
||||
head = append(head, "")
|
||||
body := a.editor.View(w, h-len(head))
|
||||
lines := append(head, strings.Split(body, "\n")...)
|
||||
for i := range lines {
|
||||
lines[i] = " " + lines[i]
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
Reference in new issue
Block a user