-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathtestMathZshCompletionScript().zsh
More file actions
185 lines (161 loc) · 5.55 KB
/
testMathZshCompletionScript().zsh
File metadata and controls
185 lines (161 loc) · 5.55 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
#compdef math
__math_complete() {
local -ar non_empty_completions=("${@:#(|:*)}")
local -ar empty_completions=("${(M)@:#(|:*)}")
_describe -V '' non_empty_completions -- empty_completions -P $'\'\''
}
__math_custom_complete() {
local -a completions
completions=("${(@f)"$("${command_name}" "${@}" "${command_line[@]}")"}")
if [[ "${#completions[@]}" -gt 1 ]]; then
__math_complete "${completions[@]:0:-1}"
fi
}
__math_cursor_index_in_current_word() {
if [[ -z "${QIPREFIX}${IPREFIX}${PREFIX}" ]]; then
printf 0
else
printf %s "${#${(z)LBUFFER}[-1]}"
fi
}
_math() {
emulate -RL zsh -G
setopt extendedglob nullglob numericglobsort
unsetopt aliases banghist
local -xr SAP_SHELL=zsh
local -x SAP_SHELL_VERSION
SAP_SHELL_VERSION="$(builtin emulate zsh -c 'printf %s "${ZSH_VERSION}"')"
local -r SAP_SHELL_VERSION
local context state state_descr line
local -A opt_args
local -r command_name="${words[1]}"
local -ar command_line=("${words[@]}")
local -ir current_word_index="$((CURRENT - 1))"
local -i ret=1
local -ar arg_specs=(
'--version[Show the version.]'
'(-h --help)'{-h,--help}'[Show help information.]'
'(-): :->command'
'(-)*:: :->arg'
)
_arguments -w -s -S : "${arg_specs[@]}" && ret=0
case "${state}" in
command)
local -ar subcommands=(
'add:Print the sum of the values.'
'multiply:Print the product of the values.'
'stats:Calculate descriptive statistics.'
'help:Show subcommand help information.'
)
_describe -V subcommand subcommands
;;
arg)
case "${words[1]}" in
add|multiply|stats|help)
"_math_${words[1]}"
;;
esac
;;
esac
return "${ret}"
}
_math_add() {
local -i ret=1
local -ar arg_specs=(
'(--hex-output -x)'{--hex-output,-x}'[Use hexadecimal notation for the result.]'
'*:values:'
'--version[Show the version.]'
'(-h --help)'{-h,--help}'[Show help information.]'
)
_arguments -w -s -S : "${arg_specs[@]}" && ret=0
return "${ret}"
}
_math_multiply() {
local -i ret=1
local -ar arg_specs=(
'(--hex-output -x)'{--hex-output,-x}'[Use hexadecimal notation for the result.]'
'*:values:'
'--version[Show the version.]'
'(-h --help)'{-h,--help}'[Show help information.]'
)
_arguments -w -s -S : "${arg_specs[@]}" && ret=0
return "${ret}"
}
_math_stats() {
local -i ret=1
local -ar arg_specs=(
'--version[Show the version.]'
'(-h --help)'{-h,--help}'[Show help information.]'
'(-): :->command'
'(-)*:: :->arg'
)
_arguments -w -s -S : "${arg_specs[@]}" && ret=0
case "${state}" in
command)
local -ar subcommands=(
'average:Print the average of the values.'
'stdev:Print the standard deviation of the values.'
'quantiles:Print the quantiles of the values (TBD).'
)
_describe -V subcommand subcommands
;;
arg)
case "${words[1]}" in
average|stdev|quantiles)
"_math_stats_${words[1]}"
;;
esac
;;
esac
return "${ret}"
}
_math_stats_average() {
local -i ret=1
local -ar ___kind=('mean' 'median' 'mode')
local -ar arg_specs=(
'--kind[The kind of average to provide.]:kind:{__math_complete "${___kind[@]}"}'
'*:values:'
'--version[Show the version.]'
'(-h --help)'{-h,--help}'[Show help information.]'
)
_arguments -w -s -S : "${arg_specs[@]}" && ret=0
return "${ret}"
}
_math_stats_stdev() {
local -i ret=1
local -ar arg_specs=(
'*:values:'
'--version[Show the version.]'
'(-h --help)'{-h,--help}'[Show help information.]'
)
_arguments -w -s -S : "${arg_specs[@]}" && ret=0
return "${ret}"
}
_math_stats_quantiles() {
local -i ret=1
local -ar ___one_of_four=('alphabet' 'alligator' 'branch' 'braggart')
local -ar arg_specs=(
'*:values:'
'--file[Input file or directory to process (default section).]:file:_files -g '\''*.txt *.md'\'''
'--one-of-four[Choose one of four predefined options]:one-of-four:{__math_complete "${___one_of_four[@]}"}'
'--custom-arg[Custom argument]:custom-arg:{__math_custom_complete ---completion stats quantiles -- --custom-arg "${current_word_index}" "$(__math_cursor_index_in_current_word)"}'
'--custom-deprecated-arg[Deprecated custom argument]:custom-deprecated-arg:{__math_custom_complete ---completion stats quantiles -- --custom-deprecated-arg}'
'--shell[Run a shell command for input or completion]:shell:{local -a list;list=(${(f)"$(head -100 /usr/share/dict/words | tail -50)"});_describe -V "" list}'
'--custom[Custom user-provided option with dynamic completion]:custom:{__math_custom_complete ---completion stats quantiles -- --custom "${current_word_index}" "$(__math_cursor_index_in_current_word)"}'
'--custom-deprecated[Deprecated custom option]:custom-deprecated:{__math_custom_complete ---completion stats quantiles -- --custom-deprecated}'
'--version[Show the version.]'
'(-h --help)'{-h,--help}'[Show help information.]'
)
_arguments -w -s -S : "${arg_specs[@]}" && ret=0
return "${ret}"
}
_math_help() {
local -i ret=1
local -ar arg_specs=(
'*:subcommands:'
'--version[Show the version.]'
)
_arguments -w -s -S : "${arg_specs[@]}" && ret=0
return "${ret}"
}
_math