-
Notifications
You must be signed in to change notification settings - Fork 21
/
completions.sh
172 lines (149 loc) · 4.59 KB
/
completions.sh
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
_tbot()
{
local cur prev words
_init_completion || return
# Collect testcase path arguments
local index=0
local path_args=()
local curdir="$PWD"
local workdir="${curdir}"
while [[ $index -lt ${#words[@]} ]]; do
local current_word="${words[$index]}"
if [[ "$current_word" == @(-T|-t) ]]; then
local index=$((index + 1))
[[ "${words[$index]}" == "=" ]] && index=$((index + 1))
local path="${words[$index]}"
__expand_tilde_by_ref path
path_args+=("$current_word" "$path")
elif [[ "$current_word" == @* ]]; then
path_args=("${path_args[@]}" "${current_word}")
elif [[ "$current_word" == -C ]]; then
local index=$((index + 1))
[[ "${words[$index]}" == "=" ]] && index=$((index + 1))
local path="${words[$index]}"
__expand_tilde_by_ref path
path_args+=("$current_word" "$path")
workdir="$path"
fi
local index=$((index + 1))
done
if [[ "$prev" == -C ]]; then
_filedir -d
return
fi
if [[ "$prev" == -T ]]; then
cd "${workdir}" && _filedir -d
cd "${curdir}"
return
fi
if [[ "$prev" == @(-t|-b|-l|--board|--lab) ]]; then
cd "${workdir}" && _filedir py
cd "${curdir}"
# Remove __pycache__ for convenience
COMPREPLY=("${COMPREPLY[@]/*__pycache__*/}")
return
fi
if [[ "$cur" == \@* ]]; then
cur="${cur:1}"
_filedir
# If the completion is a directory, append a / and prevent a space
# being added.
for i in "${!COMPREPLY[@]}"; do
if [[ -d "${COMPREPLY[$i]}" ]]; then
COMPREPLY[$i]+=/
compopt -o nospace
fi
done
COMPREPLY=("${COMPREPLY[@]/#/@}")
return
fi
if [[ "$prev" == "--log" ]]; then
cd "${workdir}" && _filedir
cd "${curdir}"
return
fi
if [[ "$prev" == @(-f) ]]; then
COMPREPLY=()
return
fi
if [[ "$cur" == -* ]]; then
mapfile -t COMPREPLY < <(compgen -W '-h -b -l -T -t -f -v -q -s -i -C -p
--help
--board
--lab
--log
--log-auto
--version
--list-testcases
--list-files
--list-flags
--show
--interactive
' -- "$cur")
else
# Collecting testcases can be really slow, so we cache them for
# a small amount of time (5 seconds)
local cache_age=$(($(date +%s) - ${__tbot_testcase_cache_time:-0}))
if [[ -z $__tbot_testcase_cache || $cache_age -gt 5 ]]; then
__tbot_testcase_cache=$(tbot --list-testcases "${path_args[@]}" 2>/dev/null | grep -v "selftest_")
__tbot_testcase_cache_time=$(date +%s)
fi
mapfile -t COMPREPLY < <(compgen -W "$__tbot_testcase_cache" -- "$cur")
fi
} &&
complete -F _tbot tbot
_newbot()
{
local cur prev words
_init_completion || return
local index=0
local path_args=()
local curdir="$PWD"
local workdir="${curdir}"
while [[ $index -lt ${#words[@]} ]]; do
local current_word="${words[$index]}"
if [[ "$current_word" == -C ]]; then
local index=$((index + 1))
[[ "${words[$index]}" == "=" ]] && index=$((index + 1))
local path="${words[$index]}"
__expand_tilde_by_ref path
path_args+=("$current_word" "$path")
workdir="$path"
fi
local index=$((index + 1))
done
if [[ "$prev" == @(-b|-l|--board|--lab|-c|--config) ]]; then
compopt -o nospace
mapfile -t COMPREPLY < <(newbot -C "${workdir}" --complete-module "$cur")
return
fi
if [[ "$prev" == -C ]]; then
_filedir -d
return
fi
if [[ "$cur" == -* ]]; then
mapfile -t COMPREPLY < <(compgen -W '-h -C -c -f -v -q
--help
--config
--version
' -- "$cur")
return
fi
if [[ "$cur" == \@* ]]; then
cur="${cur:1}"
_filedir
# If the completion is a directory, append a / and prevent a space
# being added.
for i in "${!COMPREPLY[@]}"; do
if [[ -d "${COMPREPLY[$i]}" ]]; then
COMPREPLY[$i]+=/
compopt -o nospace
fi
done
COMPREPLY=("${COMPREPLY[@]/#/@}")
return
fi
compopt -o nospace
mapfile -t COMPREPLY < <(newbot -C "${workdir}" --complete-testcase "$cur")
} &&
complete -F _newbot newbot