-
Notifications
You must be signed in to change notification settings - Fork 1
/
runme.zsh
56 lines (54 loc) · 1.71 KB
/
runme.zsh
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
# Zsh completion for runme
_runme_completion()
{
local scriptfile=$(runme --runme-file 2>/dev/null)
if [[ ! -f "$scriptfile" ]]; then
return 0
fi
local line="${words[2,-1]}"
local IFS=$'\n'
local compgen_values=( $(runme --runme-compgen "$scriptfile" "$line" 2>/dev/null) )
local candicates=()
local option_values=()
local value_kind=0
for item in ${compgen_values[@]}; do
if [[ "$item" == '-'* ]]; then
option_values+=( "$item" )
elif [[ "$item" == \`*\` ]]; then
local choices=( $(runme "${item:1:-1}" 2>/dev/null) )
candicates=( "${candicates[@]}" "${choices[@]}" )
elif [[ "$item" == '<'* ]]; then
if echo "$item" | grep -qi '<args>...'; then
value_kind=1
elif echo "$item" | grep -qi '\(file\|path\)>\(\.\.\.\)\?'; then
value_kind=2
elif echo "$item" | grep -qi 'dir>\(\.\.\.\)\?'; then
value_kind=3
else
value_kind=9
fi
else
candicates+=( "$item" )
fi
done
if [[ "$value_kind" == 0 ]]; then
if [[ "${#candicates[@]}" -eq 0 ]]; then
candicates=( "${option_values[@]}" )
fi
elif [[ "$value_kind" == 1 ]]; then
if [[ "${#candicates[@]}" -eq 0 ]]; then
candicates=( "${option_values[@]}" )
fi
if [[ "${#candicates[@]}" -eq 0 ]]; then
_path_files
fi
elif [[ "$value_kind" == 2 ]]; then
_path_files
elif [[ "$value_kind" == 3 ]]; then
_path_files -/
fi
if [[ ${#candicates[@]} -gt 0 ]]; then
compadd -- $candicates[@]
fi
}
compdef _runme_completion runme