1package modes
2
3import (
4 "fmt"
5 "strings"
6
7 "github.com/mattn/go-runewidth"
8 "github.com/patriceckhart/zot/packages/tui"
9)
10
11// helpKeyRows is the list of keybindings shown by /help.
12var helpKeyRows = [][2]string{
13 {"enter", "submit the current input"},
14 {"shift+enter / alt+enter", "insert a newline"},
15 {"tab", "complete the highlighted slash command"},
16 {"esc", "cancel the current turn (while busy) - clear the input (while idle)"},
17 {"ctrl+c", "exit (while idle) - cancel the current turn (while busy)"},
18 {"ctrl+w", "delete previous word"},
19 {"alt+backspace", "delete previous word (same as ctrl+w)"},
20 {"ctrl+u / ctrl+k", "delete to start / end of line"},
21 {"ctrl+a / ctrl+e", "jump to start / end of line"},
22 {"alt+← / alt+→", "jump one word back / forward"},
23 {"ctrl+l", "redraw the screen"},
24 {"ctrl+o", "expand / collapse long tool results"},
25 {"pgup / pgdn", "scroll the chat one page up / down"},
26 {"up / down", "move in input, browse prompt history at top edge, scroll chat when empty"},
27}
28
29// renderHelpBlock builds the friendly /help view. Uses the shared
30// frameHeader/frameRule helpers so the rules match every other block
31// in the tui (tool results, code fences, dialogs) — full terminal width
32// in the muted colour.
33//
34// Slash commands and keybindings share the same label column width so
35// every description starts at the same x-position, regardless of which
36// section it lives in. The width is computed from the longest label
37// across BOTH lists, with a minimum of 14 cells so changes to either
38// list don't compress the column visually.
39func renderHelpBlock(th tui.Theme, width int, llamaConfigured bool, keymap []keymapBinding) []string {
40 if width < 20 {
41 width = 20
42 }
43
44 // Label column width uses display cells, not byte length, so
45 // single-cell multibyte runes (← → - ...) don't over-count and leave
46 // a raggedy right edge. `len("alt+← / alt+→")` is 17 bytes but
47 // only 13 cells; padding off byte length would either overshoot
48 // (setting labelWidth too high and wasting space on every row)
49 // or undershoot (never padding that row because len >= labelWidth
50 // already, leaving its description mis-aligned).
51 labelWidth := 14
52 for _, c := range slashCatalog {
53 if c.Name == "/llama" && !llamaConfigured {
54 continue
55 }
56 if n := runewidth.StringWidth(c.Name); n > labelWidth {
57 labelWidth = n
58 }
59 }
60 for _, k := range helpKeyRows {
61 if n := runewidth.StringWidth(k[0]); n > labelWidth {
62 labelWidth = n
63 }
64 }
65 for _, b := range keymap {
66 if n := runewidth.StringWidth(b.name); n > labelWidth {
67 labelWidth = n
68 }
69 }
70
71 pad := func(s string) string {
72 w := runewidth.StringWidth(s)
73 if w >= labelWidth {
74 return s
75 }
76 return s + strings.Repeat(" ", labelWidth-w)
77 }
78
79 var out []string
80 out = append(out, frameHeader(th, "zot help", width), "")
81
82 // commands section
83 out = append(out, tui.Bold("slash commands:"))
84 for _, c := range slashCatalog {
85 if c.Name == "/llama" && !llamaConfigured {
86 continue
87 }
88 out = append(out, fmt.Sprintf(" %s %s",
89 th.FG256(th.Accent, pad(c.Name)),
90 th.FG256(th.Muted, c.Desc)))
91 }
92
93 // keys section
94 out = append(out, "", tui.Bold("keys:"))
95 for _, k := range helpKeyRows {
96 out = append(out, fmt.Sprintf(" %s %s",
97 th.FG256(th.Accent, pad(k[0])),
98 th.FG256(th.Muted, k[1])))
99 }
100
101 if len(keymap) > 0 {
102 out = append(out, "", tui.Bold("custom keys:"))
103 for _, b := range keymap {
104 out = append(out, fmt.Sprintf(" %s %s",
105 th.FG256(th.Accent, pad(b.name)),
106 th.FG256(th.Muted, b.command)))
107 }
108 }
109
110 out = append(out, "", frameRule(th, width), "")
111 return out
112}