-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_complete
68 lines (54 loc) · 1.21 KB
/
auto_complete
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
#! /bin/bash
# get script path
export EC_SCRIPT_PATH=$(readlink -f "${BASH_SOURCE[0]}")
export EC_SCRIPT_DIR=$(dirname ${EC_SCRIPT_PATH})
export EC_PATH="ec"
# load dir
function loadDirList() {
# $1: Dir to load
# $2: Parameter to receive the result
unset $2
i="0"
for file in $1/*; do
name="${file#$1/}"
if [ -d "$file" ]; then
continue
elif [ "$name" == "__init__.py" ]; then
continue
fi
eval $2[$i]="${name%.py}"
i=$((i + 1))
done
# clean up
unset file
unset i
}
function ec_auto_complete() {
local cmd="${1##*/}"
local word=${COMP_WORDS[COMP_CWORD]}
local line=${COMP_LINE}
local list
# find parameters start with -
local index=$COMP_CWORD
for w in ${COMP_WORDS[@]}; do
if [[ ${w} == -* ]]; then
index=$((index - 1))
fi
done
case "$index" in
1)
loadDirList "$EC_SCRIPT_DIR/cfg" "LIST"
;;
2)
loadDirList "$EC_SCRIPT_DIR/cmd" "LIST"
;;
esac
list="${LIST[@]}"
COMPREPLY=( $(compgen -W "$list" -- "$word") )
unset LIST
return 0
}
complete -F ec_auto_complete "$EC_PATH"
unset EC_SCRIPT_PATH
# unset EC_SCRIPT_DIR # keep this value
unset EC_PATH