Skip to content

Commit 8c5dfa0

Browse files
committed
feat: add list-cards command
1 parent f7add39 commit 8c5dfa0

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

src/apyanki/anki.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,11 @@ def list_notes(self, query: str) -> None:
393393
for note in self.find_notes(query):
394394
cards.print_question(note.n.cards()[0])
395395

396+
def list_cards(self, query: str, verbose: bool) -> None:
397+
"""List cards that match a query"""
398+
for cid in self.col.find_cards(query):
399+
cards.card_pprint(self.col.get_card(cid), verbose)
400+
396401
def list_cards_as_table(self, query: str, opts_display: dict[str, bool]) -> None:
397402
"""List cards that match a query in tabular format"""
398403
width = console.width - 1

src/apyanki/cards.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,51 @@
66

77
from rich.text import Text
88

9-
from apyanki.console import console
9+
from apyanki.console import console, consolePlain
10+
from apyanki.fields import check_if_generated_from_markdown, prepare_field_for_cli
1011
from apyanki.fields import prepare_field_for_cli_oneline
1112

1213
if TYPE_CHECKING:
1314
from anki.cards import Card
1415

1516

17+
def card_pprint(card: Card, verbose: bool = True) -> None:
18+
"""Pretty print a card."""
19+
flag = get_flag(card)
20+
consolePlain.print(f"[green]# Card (cid: {card.id})[/green]{flag}\n")
21+
22+
if verbose:
23+
card_type = ["new", "learning", "review", "relearning"][int(card.type)]
24+
columned = [
25+
f"[yellow]nid:[/yellow] {card.nid}",
26+
f"[yellow]model:[/yellow] {card.note_type()['name']}",
27+
f"[yellow]type:[/yellow] {card_type}",
28+
f"[yellow]due:[/yellow] {card.due} days",
29+
f"[yellow]interval:[/yellow] {card.ivl} days",
30+
f"[yellow]repetitions:[/yellow] {card.reps}",
31+
f"[yellow]lapses:[/yellow] {card.lapses}",
32+
f"[yellow]ease:[/yellow] {int(card.factor / 10)} %",
33+
"",
34+
]
35+
for line in columned:
36+
consolePlain.print(line)
37+
38+
rendered = card.render_output()
39+
for title, field in [
40+
["Front", rendered.question_text],
41+
["Back", rendered.answer_text],
42+
]:
43+
is_markdown = check_if_generated_from_markdown(field)
44+
if is_markdown:
45+
title += " [italic](markdown)[/italic]"
46+
47+
console.print(f"[blue]## {title}[/blue]\n")
48+
prepared = prepare_field_for_cli(field)
49+
prepared = prepared.replace("\n\n", "\n")
50+
console.print(prepared)
51+
console.print()
52+
53+
1654
def card_field_to_text(field: str, max_width: int = 0) -> Text:
1755
prepared_field = prepare_field_for_cli_oneline(field)
1856
if max_width > 0:

src/apyanki/cli.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,29 @@ def rename(old_name: str, new_name: str) -> None:
374374
a.rename_model(old_name, new_name)
375375

376376

377+
@main.command("list-cards")
378+
@click.argument("query", required=False, nargs=-1)
379+
@click.option("-v", "--verbose", is_flag=True, help="Print details for each card")
380+
def list_cards(query: str, verbose: bool) -> None:
381+
"""List cards that match QUERY.
382+
383+
The default QUERY is "tag:marked OR -flag:0". This default can be
384+
customized in the config file `~/.config/apy/apy.json`, e.g. with
385+
386+
\b
387+
{
388+
"query": "tag:marked OR tag:leech"
389+
}
390+
"""
391+
if query:
392+
query = " ".join(query)
393+
else:
394+
query = cfg["query"]
395+
396+
with Anki(**cfg) as a:
397+
a.list_cards(query, verbose)
398+
399+
377400
@main.command("list-cards-table")
378401
@click.argument("query", required=False, nargs=-1)
379402
@click.option("-a", "--show-answer", is_flag=True, help="Display answer")

src/apyanki/note.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def print_cards(self) -> None:
158158
table.add_column("Interval", justify="right", header_style="white")
159159
table.add_column("Reps", justify="right", header_style="white")
160160
table.add_column("Lapses", justify="right", header_style="white")
161-
table.add_column("Factor", justify="right", header_style="white")
161+
table.add_column("Ease", justify="right", header_style="white")
162162
for card in sorted(self.n.cards(), key=lambda x: x.factor):
163163
table.add_row(
164164
"- " + str(card.template()["name"]) + cards.get_flag(card),

0 commit comments

Comments
 (0)