Skip to content

Commit e36bde0

Browse files
authored
[CLI] Tab completion support (#67)
* Add support for bash, zsh and fish tab completion
1 parent e034024 commit e36bde0

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ Once installed, use the centml CLI tool with the following command:
2222
centml
2323
```
2424

25+
If you want tab completion, run
26+
```bash
27+
source scripts/completions/completion.<shell language>
28+
```
29+
Shell language can be: bash, zsh, fish
30+
(Hint: add `source /path/to/completions/completion.<shell language>` to your `~/.bashrc`, `~/.zshrc` or `~/.config/fish/completions/centml.fish`)
31+
2532
### Compilation
2633

2734
centml-python-client's compiler feature allows you to compile your ML model remotely using the [hidet](https://hidet.org/docs/stable/index.html) backend. \
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
_centml_completion() {
2+
local IFS=$'\n'
3+
local response
4+
5+
response=$(env COMP_WORDS="${COMP_WORDS[*]}" COMP_CWORD=$COMP_CWORD _CENTML_COMPLETE=bash_complete $1)
6+
7+
for completion in $response; do
8+
IFS=',' read type value <<< "$completion"
9+
10+
if [[ $type == 'dir' ]]; then
11+
COMPREPLY=()
12+
compopt -o dirnames
13+
elif [[ $type == 'file' ]]; then
14+
COMPREPLY=()
15+
compopt -o default
16+
elif [[ $type == 'plain' ]]; then
17+
COMPREPLY+=($value)
18+
fi
19+
done
20+
21+
return 0
22+
}
23+
24+
_centml_completion_setup() {
25+
complete -o nosort -F _centml_completion centml
26+
}
27+
28+
_centml_completion_setup;
29+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function _centml_completion;
2+
set -l response (env _CENTML_COMPLETE=fish_complete COMP_WORDS=(commandline -cp) COMP_CWORD=(commandline -t) centml);
3+
4+
for completion in $response;
5+
set -l metadata (string split "," $completion);
6+
7+
if test $metadata[1] = "dir";
8+
__fish_complete_directories $metadata[2];
9+
else if test $metadata[1] = "file";
10+
__fish_complete_path $metadata[2];
11+
else if test $metadata[1] = "plain";
12+
echo $metadata[2];
13+
end;
14+
end;
15+
end;
16+
17+
complete --no-files --command centml --arguments "(_centml_completion)";
18+

scripts/completions/completion.zsh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#compdef centml
2+
3+
_centml_completion() {
4+
local -a completions
5+
local -a completions_with_descriptions
6+
local -a response
7+
(( ! $+commands[centml] )) && return 1
8+
9+
response=("${(@f)$(env COMP_WORDS="${words[*]}" COMP_CWORD=$((CURRENT-1)) _CENTML_COMPLETE=zsh_complete centml)}")
10+
11+
for type key descr in ${response}; do
12+
if [[ "$type" == "plain" ]]; then
13+
if [[ "$descr" == "_" ]]; then
14+
completions+=("$key")
15+
else
16+
completions_with_descriptions+=("$key":"$descr")
17+
fi
18+
elif [[ "$type" == "dir" ]]; then
19+
_path_files -/
20+
elif [[ "$type" == "file" ]]; then
21+
_path_files -f
22+
fi
23+
done
24+
25+
if [ -n "$completions_with_descriptions" ]; then
26+
_describe -V unsorted completions_with_descriptions -U
27+
fi
28+
29+
if [ -n "$completions" ]; then
30+
compadd -U -V unsorted -a completions
31+
fi
32+
}
33+
34+
if [[ $zsh_eval_context[-1] == loadautofunc ]]; then
35+
# autoload from fpath, call function directly
36+
_centml_completion "$@"
37+
else
38+
# eval/source/. command, register function for later
39+
compdef _centml_completion centml
40+
fi
41+

0 commit comments

Comments
 (0)