Skip to content

Commit 72b68ef

Browse files
committed
feat: allow simple tag list and add tag completion
1 parent 65c17bc commit 72b68ef

File tree

3 files changed

+56
-15
lines changed

3 files changed

+56
-15
lines changed

completion/_apy

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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 "$@"

src/apyanki/anki.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -328,26 +328,29 @@ def rename_model(self, old_model_name: str, new_model_name: str) -> None:
328328
_ = self.col.models.update_dict(model)
329329
self.modified = True
330330

331-
def list_tags(self, sort_by_count: bool = False) -> None:
331+
def list_tags(self, sort_by_count: bool = False, simple: bool = False) -> None:
332332
"""List all tags"""
333-
table = Table(show_edge=False, box=None, header_style="bold white")
334-
table.add_column("tag", style="cyan")
335-
table.add_column("notes", style="magenta", justify="right")
336-
337333
if sort_by_count:
338-
339334
def sorter(x: tuple[str, int]) -> str | int:
340335
return x[1]
341336
else:
342-
343337
def sorter(x: tuple[str, int]) -> str | int:
344338
return x[0]
345339

346340
tags = [(t, len(self.col.find_notes(f"tag:{t}"))) for t in self.col.tags.all()]
347-
for tag, n in sorted(tags, key=sorter):
348-
table.add_row(tag, str(n))
349341

350-
console.print(table)
342+
if simple:
343+
for t, _ in tags:
344+
console.print(t)
345+
else:
346+
table = Table(show_edge=False, box=None, header_style="bold white")
347+
table.add_column("tag", style="cyan")
348+
table.add_column("notes", style="magenta", justify="right")
349+
350+
for tag, n in sorted(tags, key=sorter):
351+
table.add_row(tag, str(n))
352+
353+
console.print(table)
351354

352355
def change_tags(self, query: str, tags: str, add: bool = True) -> None:
353356
"""Add/Remove tags from notes that match query"""

src/apyanki/cli.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,9 @@ def sync() -> None:
642642
@click.option(
643643
"-c", "--sort-by-count", is_flag=True, help="When listing tags, sort by note count"
644644
)
645+
@click.option(
646+
"-s", "--simple", is_flag=True, help="Only list available tags"
647+
)
645648
@click.option(
646649
"-p",
647650
"--purge",
@@ -652,6 +655,7 @@ def tag(
652655
query: str,
653656
add_tags: str | None,
654657
remove_tags: str | None,
658+
simple: bool,
655659
sort_by_count: bool,
656660
purge: bool,
657661
) -> None:
@@ -704,7 +708,7 @@ def tag(
704708
if (add_tags is None or add_tags == "") and (
705709
remove_tags is None or remove_tags == ""
706710
):
707-
a.list_tags(sort_by_count)
711+
a.list_tags(sort_by_count, simple)
708712
return
709713

710714
n_notes = len(list(a.find_notes(query)))

0 commit comments

Comments
 (0)