1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Paul Buetow
package repl
import (
"strings"
"github.com/chzyer/readline"
)
// completer provides auto-completion for built-in commands.
// It returns suggestions for commands that match the current word being typed.
// The matching is case-insensitive.
//
// The function is used in tests; readline tab completion uses AutoCompleteAdapter instead.
//
// text: the current word being typed
// Returns a slice of strings for matching built-in commands
func completer(text string) []string {
if text == "" {
return nil
}
var suggestions []string
for _, cmd := range Commands() {
if strings.HasPrefix(strings.ToLower(cmd), strings.ToLower(text)) {
suggestions = append(suggestions, cmd)
}
}
return suggestions
}
// Ensure AutoCompleteAdapter implements readline.AutoCompleter at compile time.
var _ readline.AutoCompleter = (*AutoCompleteAdapter)(nil)
// AutoCompleteAdapter implements the readline AutoCompleter interface,
// providing tab-completion suggestions for built-in commands.
// When the first word is "help", it completes with help topics instead.
type AutoCompleteAdapter struct {
commands []string
}
// NewAutoCompleter creates a new AutoCompleteAdapter with the current list of built-in commands.
func NewAutoCompleter() *AutoCompleteAdapter {
return &AutoCompleteAdapter{
commands: Commands(),
}
}
// Do implements the readline.AutoCompleter interface.
// It returns matching command completions for the given line.
// When the first word is "help" followed by a space, it offers help topic completions.
func (a *AutoCompleteAdapter) Do(line []rune, pos int) ([][]rune, int) {
text := string(line[:pos])
words := strings.Fields(text)
if len(words) == 0 {
return a.completeCommands("")
}
// If first word is "help" and user typed a space after it (or more words),
// complete help topics. If first word is just "help" with no trailing space,
// complete the command "help".
if strings.ToLower(words[0]) == "help" && len(words) == 1 {
// Check if there's a trailing space — means user wants topics
if len(text) > 0 && text[len(text)-1] == ' ' {
return a.completeHelpTopics("")
}
// No trailing space — just completing the command "help"
}
if strings.ToLower(words[0]) == "help" && len(words) > 1 {
return a.completeHelpTopics(words[len(words)-1])
}
lastWord := words[len(words)-1]
return a.completeCommands(lastWord)
}
// completeCommands returns matching command completions and prefix length.
func (a *AutoCompleteAdapter) completeCommands(lastWord string) ([][]rune, int) {
var matches [][]rune
for _, cmd := range a.commands {
if strings.HasPrefix(strings.ToLower(cmd), strings.ToLower(lastWord)) {
matches = append(matches, []rune(cmd))
}
}
return a.withCommonPrefix(matches, lastWord)
}
// completeHelpTopics returns matching help topic completions and prefix length.
func (a *AutoCompleteAdapter) completeHelpTopics(lastWord string) ([][]rune, int) {
var matches [][]rune
topics := GetCompletionTopics()
for _, topic := range topics {
if strings.HasPrefix(strings.ToLower(topic), strings.ToLower(lastWord)) {
matches = append(matches, []rune(topic))
}
}
return a.withCommonPrefix(matches, lastWord)
}
// withCommonPrefix calculates the common prefix adjustment for readline.
func (a *AutoCompleteAdapter) withCommonPrefix(matches [][]rune, lastWord string) ([][]rune, int) {
if len(matches) == 0 {
return matches, 0
}
// Find common prefix length
minLen := len(lastWord)
for _, m := range matches {
compare := string(m)
i := 0
for i < len(lastWord) && i < len(compare) && lastWord[i] == compare[i] {
i++
}
if i < minLen {
minLen = i
}
}
return matches, minLen - len(lastWord)
}
|