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