-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts-autocomplete
37 lines (28 loc) · 989 Bytes
/
scripts-autocomplete
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
#!/bin/bash
_scripts_autocomplete() {
# Entrypoint location
local lib_path="/usr/local/lib/scripts"
# Current word (currently being typed)
local cur="${COMP_WORDS[COMP_CWORD]}"
# Previous word (word before $cur)
local prev="${COMP_WORDS[COMP_CWORD-1]}"
# Get the list of providers (first argument suggestions)
local providers=$(ls -1 "$lib_path")
# Get the actions for the selected provider (second argument suggestions)
if [[ $COMP_CWORD -eq 2 ]]
then
# If provider already typed and exists
if [[ -d "$lib_path/$prev" ]]
then
# Store available actions under typed provider
actions=$(ls -1 "$lib_path/$prev" | sed 's/\.sh$//')
fi
# Search actions that match $cur
COMPREPLY=( $(compgen -W "$actions" -- "$cur") )
return
fi
# Default to suggesting providers
COMPREPLY=( $(compgen -W "$providers" -- "$cur") )
}
# Register the completion function for the "scripts" command
complete -F _scripts_autocomplete scripts