Skip to content

Conversation

dylandreimerink
Copy link
Member

@dylandreimerink dylandreimerink commented Jul 14, 2025

This commit adds tab completion for the shell. When in the REPL the key can be used to give suggestions. When typing the initial command it will suggest commands starting with the typed characters. Repeated presses of the key will cycle through the suggestions.

When pressing while in an argument or flag value, a callback on the command is called if it exists. This callback can return a slice of values to suggest. For arguments, all arguments up and including the current argument are passed to the callback. For flags, the flag name and value typed so far are passed to the callback.

To accommodate the "remote shell" used by Cilium, a hidden flag is now implicitly added to all commands --autocomplete. When a command is called with this flag the engine will intercept the command and return a line separated list of suggestions instead of executing the command. For example: test --autocomplete='arg1 --flag1=val1 ar' will be interpreted the same as test arg1 --flag1=val1 ar would on the REPL and the output will be a list of suggestions:

> test --autocomplete='arg1 --flag1=val1 ar'
test arg1 --flag1=val1 argument2
test arg1 --flag1=val1 arg2
test arg1 --flag1=val1 args2

The help command also has a hidden flag -a which if passed will simply list all available commands without additional help text.

> help -a
cat
cd
chmod

This should allow the remote shell in Cilium to provide tab completion in the same way as the REPL.

Fixes: #54

@dylandreimerink dylandreimerink marked this pull request as ready for review July 24, 2025 08:51
@dylandreimerink dylandreimerink requested a review from a team as a code owner July 24, 2025 08:51
@dylandreimerink dylandreimerink requested review from derailed and joamaki and removed request for a team July 24, 2025 08:51
Copy link

@derailed derailed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dylandreimerink Very cool!! a few suggestions feel free to ignore...


state.suggestions = nil
for _, suggestion := range argsCB(engineState, nil, "") {
state.suggestions = append(state.suggestions, line[:state.pos]+suggestion)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be a good idea to actually add behavior on the autocompletestate. Perhaps this would allow us to make sure adequate bound checks are in placed in a central location.
Not to mention we can test the various inputs.
Speaking of which might be great to add some tests here to ensure we produce the correct suggestions under various conditions.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Agreed that would be a good idea. I will as this in a followup PR to ensure we don't regress this on accident.

This commit adds tab completion for the shell. When in the REPL
the <tab> key can be used to give suggestions. When typing the initial
command it will suggest commands starting with the typed characters.
Repeated presses of the <tab> key will cycle through the suggestions.

When pressing <tab> while in an argument or flag value, a callback
on the command is called if it exists. This callback can return a
slice of values to suggest. For arguments, all arguments up and
including the current argument are passed to the callback. For flags,
the flag name and value typed so far are passed to the callback.

To accommodate the "remote shell" used by Cilium, a hidden flag is now
implicitly added to all commands `--autocomplete`. When a command
is called with this flag the engine will intercept the command
and return a line separated list of suggestions instead of executing
the command. For example: `test --autocomplete='arg1 --flag1=val1 ar'`
will be interpreted the same as `test arg1 --flag1=val1 ar` would on
the REPL and the output will be a list of suggestions:

```
> test --autocomplete='arg1 --flag1=val1 ar'
test arg1 --flag1=val1 argument2
test arg1 --flag1=val1 arg2
test arg1 --flag1=val1 args2
```

The `help` command also has a hidden flag `-a` which if passed will
simply list all available commands without additional help text.

```
> help -a
cat
cd
chmod
```

This should allow the remote shell in Cilium to provide tab completion
in the same way as the REPL.

Signed-off-by: Dylan Reimerink <dylan.reimerink@isovalent.com>
@dylandreimerink dylandreimerink merged commit 28e7a35 into main Jul 31, 2025
1 check passed
@dylandreimerink dylandreimerink deleted the feature/autocomplete branch July 31, 2025 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Script autocomplete
3 participants