Skip to content

Commit 65c17bc

Browse files
committed
feat: zsh completion for -m --model option
refer: #121
1 parent 533c63e commit 65c17bc

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

completion/_apy

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ __model() {
1717
case "$words[1]" in
1818
edit-css)
1919
opts=( \
20-
'(-m --model)'{-m,--model}'[Specify model]:model:' \
20+
'(-m --model)'{-m,--model}'[Specify model]:model:_apy_models' \
2121
'(-s --sync-after)'{-s,--sync-after}'[Synchronize after edit]' \
2222
$opts_help \
2323
);;
@@ -75,15 +75,15 @@ _apy() {
7575
add)
7676
opts=( \
7777
'(-t --tags)'{-t,--tags}'[Specify tags]:tags:' \
78-
'(-m --model)'{-m,--model}'[Specify model]:model:' \
78+
'(-m --model)'{-m,--model}'[Specify model]:model:_apy_models' \
7979
'(-d --deck)'{-d,--deck}'[Specify deck]:deck:' \
8080
$opts_help \
8181
);;
8282
add-single)
8383
opts=( \
8484
'(-s --preset)'{-s,--preset}'[Specify a preset]:preset:' \
8585
'(-t --tags)'{-t,--tags}'[Specify tags]:tags:' \
86-
'(-m --model)'{-m,--model}'[Specify model]:model:' \
86+
'(-m --model)'{-m,--model}'[Specify model]:model:_apy_models' \
8787
'(-d --deck)'{-d,--deck}'[Specify deck]:deck:' \
8888
'::Fields' \
8989
$opts_help \
@@ -170,4 +170,22 @@ _apy() {
170170
_arguments $opts
171171
}
172172

173+
_apy_models() {
174+
local cache_dir="${XDG_CACHE_HOME:-/tmp/cache}/apy"
175+
local cache_file="$cache_dir/apy_models"
176+
177+
local -a models
178+
179+
[[ -d $cache_dir ]] || mkdir -p $cache_dir
180+
if [[ -f $cache_file && $(($(date +%s) - $(date -r $cache_file +%s))) -lt 300 ]]
181+
then
182+
models=(${(f)"$(<$cache_file)"})
183+
else
184+
models=(${(f)"$(apy list-models 2>/dev/null)"})
185+
print -l $models > $cache_file
186+
fi
187+
models=(${models//:/\\:})
188+
_describe 'model' models
189+
}
190+
173191
_apy "$@"

0 commit comments

Comments
 (0)