@@ -74,15 +74,15 @@ _apy() {
7474 case "$words[1]" in
7575 add)
7676 opts=( \
77- '(-t --tags)'{-t,--tags}'[Specify tags]:tags:' \
77+ '(-t --tags)'{-t,--tags}'[Specify tags]:tags:_apy_tags ' \
7878 '(-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:' \
85- '(-t --tags)'{-t,--tags}'[Specify tags]:tags:' \
85+ '(-t --tags)'{-t,--tags}'[Specify tags]:tags:_apy_tags ' \
8686 '(-m --model)'{-m,--model}'[Specify model]:model:_apy_models' \
8787 '(-d --deck)'{-d,--deck}'[Specify deck]:deck:' \
8888 '::Fields' \
@@ -91,7 +91,7 @@ _apy() {
9191 add-from-file|update-from-file)
9292 opts=( \
9393 '::Markdown input file:_files -g "*.md"' \
94- '(-t --tags)'{-t,--tags}'[Specify tags]:tags:' \
94+ '(-t --tags)'{-t,--tags}'[Specify tags]:tags:_apy_tags ' \
9595 '(-d --deck)'{-d,--deck}'[Specify deck]:deck:' \
9696 '(-u --update-file)'{-u,--update-file}'[Update original file with note IDs]' \
9797 $opts_help \
@@ -159,7 +159,7 @@ _apy() {
159159 tag)
160160 opts=( \
161161 '(-a --add-tags)'{-a,--add-tags}'[Add specified tags]:tags:' \
162- '(-r --remove-tags)'{-r,--remove-tags}'[Remove specified tags]:tags:' \
162+ '(-r --remove-tags)'{-r,--remove-tags}'[Remove specified tags]:tags:_apy_tags ' \
163163 $opts_help \
164164 '::Query' \
165165 );;
@@ -188,4 +188,38 @@ _apy_models() {
188188 _describe 'model' models
189189}
190190
191+ _apy_tags() {
192+ local cache_dir="${XDG_CACHE_HOME:-/tmp/cache}/apy"
193+ local cache_file="$cache_dir/apy_tags"
194+
195+ local -a tags
196+
197+ [[ -d $cache_dir ]] || mkdir -p $cache_dir
198+ if [[ -f $cache_file && $(($(date +%s) - $(date -r $cache_file +%s))) -lt 300 ]]
199+ then
200+ tags=(${(f)"$(<$cache_file)"})
201+ else
202+ tags=(${(f)"$(apy tag -s 2>/dev/null)"})
203+ print -l $tags > $cache_file
204+ fi
205+
206+ local current_input="${words[CURRENT]}"
207+ local current_tag="${current_input##*,}"
208+ local prefix="${current_input%,*}"
209+
210+ local -a used_tags_array=(${(s:,:)prefix})
211+ local -a available_tags
212+ for tag in $tags; do
213+ if [[ ! " ${used_tags_array[@]} " =~ " ${tag} " ]]; then
214+ available_tags+=($tag)
215+ fi
216+ done
217+
218+ if [[ $current_input == *","* ]]; then
219+ compadd -P "${prefix}," -a available_tags
220+ else
221+ _describe 'tags' available_tags
222+ fi
223+ }
224+
191225_apy "$@"
0 commit comments