v0.0.22 e7dbf3d

slash_suggest.go RawHistoryFolder

1package modes
2
3import (
4	"strings"
5
6	"github.com/patriceckhart/zot/internal/tui"
7)
8
9// slashCommand is one entry in the autocomplete popup.
10type slashCommand struct {
11	Name string // with leading "/"
12	Desc string
13}
14
15// slashCancelsTurn reports whether the named slash command, when run
16// while a turn is in flight, requires the active turn to be cancelled
17// first. The destructive commands (those that mutate the transcript
18// or rebuild the agent) need a quiet state; the rest run alongside
19// the streaming response without trouble.
20func slashCancelsTurn(head string) bool {
21	switch head {
22	case "/clear", "/compact", "/logout", "/login", "/model":
23		return true
24	}
25	return false
26}
27
28// slashCatalog lists every slash command the interactive mode handles.
29// Keep in sync with runSlash().
30var slashCatalog = []slashCommand{
31	{"/help", "show key bindings and commands"},
32	{"/login", "log in via api key or subscription"},
33	{"/logout", "clear a provider's credentials"},
34	{"/model", "pick a model (or /model <id>)"},
35	{"/sessions", "resume a previous session for this directory"},
36	{"/jump", "scroll the chat to a previous turn (or /jump <text>)"},
37	{"/btw", "side-chat that doesn't add to the main thread (saves tokens)"},
38	{"/compact", "summarize and replace the transcript to free up context"},
39	{"/lock", "confine tools to the current directory"},
40	{"/unlock", "allow tools to touch paths outside this directory"},
41	{"/clear", "clear the chat transcript"},
42	{"/exit", "exit zot"},
43}
44
45// slashSuggester renders the popup that appears when the editor starts
46// with "/". It does not own any input state — the editor drives.
47type slashSuggester struct {
48	cursor int
49}
50
51// looksLikeSlashCommand reports whether text is an attempt at a slash
52// command (valid or not). Returns true for things like "/foo" or
53// "/bar baz" but false for paths ("/Users/pat/...") and regexes
54// ("/foo.bar/") so those can be sent to the model as-is.
55//
56// The head after "/" must be a single simple word: only letters,
57// digits, hyphens, and underscores. That excludes paths (contain "/"),
58// regexes (contain "."), and URLs.
59func looksLikeSlashCommand(text string) bool {
60	text = strings.TrimSpace(text)
61	if len(text) < 2 || text[0] != '/' {
62		return false
63	}
64	head := text[1:]
65	if i := strings.IndexAny(head, " \t\n"); i >= 0 {
66		head = head[:i]
67	}
68	if head == "" {
69		return false
70	}
71	for _, r := range head {
72		if !(r >= 'a' && r <= 'z') && !(r >= 'A' && r <= 'Z') &&
73			!(r >= '0' && r <= '9') && r != '-' && r != '_' {
74			return false
75		}
76	}
77	return true
78}
79
80// isKnownSlashCommand reports whether text's head matches a registered
81// slash command name in slashCatalog.
82func isKnownSlashCommand(text string) bool {
83	text = strings.TrimSpace(text)
84	if text == "" || text[0] != '/' {
85		return false
86	}
87	head := text
88	if i := strings.IndexAny(text, " \t\n"); i >= 0 {
89		head = text[:i]
90	}
91	for _, c := range slashCatalog {
92		if c.Name == head {
93			return true
94		}
95	}
96	return false
97}
98
99func newSlashSuggester() *slashSuggester { return &slashSuggester{} }
100
101// matches returns the commands whose name has input as a prefix.
102// If input is just "/", everything is shown.
103func (s *slashSuggester) matches(input string) []slashCommand {
104	input = strings.TrimRight(input, " ")
105	if input == "" || !strings.HasPrefix(input, "/") {
106		return nil
107	}
108	// If there is a space, the user has moved past the command name.
109	if idx := strings.IndexByte(input, ' '); idx >= 0 {
110		return nil
111	}
112	var out []slashCommand
113	for _, c := range slashCatalog {
114		if strings.HasPrefix(c.Name, input) {
115			out = append(out, c)
116		}
117	}
118	return out
119}
120
121// clampCursor keeps the cursor inside the current match list.
122func (s *slashSuggester) clampCursor(n int) {
123	if n <= 0 {
124		s.cursor = 0
125		return
126	}
127	if s.cursor < 0 {
128		s.cursor = 0
129	}
130	if s.cursor >= n {
131		s.cursor = n - 1
132	}
133}
134
135// Up / Down navigate the suggestion list.
136func (s *slashSuggester) Up()   { s.cursor-- }
137func (s *slashSuggester) Down() { s.cursor++ }
138
139// Active reports whether the popup is visible for the given input.
140func (s *slashSuggester) Active(input string) bool {
141	return len(s.matches(input)) > 0
142}
143
144// Selection returns the currently highlighted command for input, or "".
145func (s *slashSuggester) Selection(input string) string {
146	m := s.matches(input)
147	if len(m) == 0 {
148		return ""
149	}
150	s.clampCursor(len(m))
151	return m[s.cursor].Name
152}
153
154// Render returns the popup lines or nil.
155func (s *slashSuggester) Render(input string, th tui.Theme, width int) []string {
156	m := s.matches(input)
157	if len(m) == 0 {
158		return nil
159	}
160	s.clampCursor(len(m))
161	var lines []string
162	for i, c := range m {
163		name := c.Name
164		if len(name) < 10 {
165			name = name + strings.Repeat(" ", 10-len(name))
166		}
167		plain := "  " + name + "  " + c.Desc
168		if i == s.cursor {
169			lines = append(lines, th.PadHighlight(plain, width))
170		} else {
171			lines = append(lines, th.FG256(th.Muted, plain))
172		}
173	}
174	lines = append(lines, th.FG256(th.Muted, "  ↑/↓ navigate · tab complete · enter run"))
175	return lines
176}
177
178// Reset puts the cursor back to the first match. Call this whenever the
179// input changes in a way that reshapes the match list.
180func (s *slashSuggester) Reset() { s.cursor = 0 }

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 477

↑↓ moveenter openesc close