Skip to content

Commit 5d989aa

Browse files
authored
[ENHANCEMENT] Add support for autocompletion with flags in REPL mode (#3057)
* [ENHANCEMENT] Add support for autocompletion with flags in REPL mode Signed-off-by: Ilya Eryomenko <[email protected]> * [CLEANUP] Fix lint issue Signed-off-by: Ilya Eryomenko <[email protected]> --------- Signed-off-by: Ilya Eryomenko <[email protected]>
1 parent 98deb46 commit 5d989aa

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

internal/action/repl.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,31 @@ import (
1313
"github.com/urfave/cli/v2"
1414
)
1515

16+
type mindFlagsCompleter struct {
17+
base readline.AutoCompleter
18+
}
19+
20+
func (f *mindFlagsCompleter) Do(line []rune, pos int) ([][]rune, int) {
21+
// Get the portion of the line up to the cursor and split to tokens
22+
origInput := string(line[:pos])
23+
tokens := strings.Fields(origInput)
24+
filtered := make([]string, 0, len(tokens))
25+
for _, tok := range tokens {
26+
// Skip flag tokens
27+
if strings.HasPrefix(tok, "-") {
28+
continue
29+
}
30+
filtered = append(filtered, tok)
31+
}
32+
sanitized := strings.Join(filtered, " ")
33+
// Add additional space if had one on original line
34+
if len(origInput) > 0 && origInput[len(origInput)-1] == ' ' {
35+
sanitized += " "
36+
}
37+
38+
return f.base.Do([]rune(sanitized), len([]rune(sanitized)))
39+
}
40+
1641
func (s *Action) entriesForCompleter(ctx context.Context) ([]*readline.PrefixCompleter, error) {
1742
args := []*readline.PrefixCompleter{}
1843
list, err := s.Store.List(ctx, tree.INF)
@@ -144,7 +169,7 @@ READ:
144169
// the list of secrets may have changed, e.g. due to
145170
// the user adding a new secret.
146171
cfg := rl.GetConfig()
147-
cfg.AutoComplete = s.prefixCompleter(c)
172+
cfg.AutoComplete = &mindFlagsCompleter{base: s.prefixCompleter(c)}
148173
if err := rl.SetConfig(cfg); err != nil {
149174
debug.Log("Failed to set readline config: %s", err)
150175

0 commit comments

Comments
 (0)