|
6 | 6 |
|
7 | 7 | from rich.text import Text |
8 | 8 |
|
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 |
10 | 11 | from apyanki.fields import prepare_field_for_cli_oneline |
11 | 12 |
|
12 | 13 | if TYPE_CHECKING: |
13 | 14 | from anki.cards import Card |
14 | 15 |
|
15 | 16 |
|
| 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 | + |
16 | 54 | def card_field_to_text(field: str, max_width: int = 0) -> Text: |
17 | 55 | prepared_field = prepare_field_for_cli_oneline(field) |
18 | 56 | if max_width > 0: |
|
0 commit comments