-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathremaining.py
65 lines (43 loc) · 1.93 KB
/
remaining.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Copyright: Ren Tatsumoto <tatsu at autistici.org>
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import re
from typing import Callable, Optional
from anki.collection import Collection
from anki.consts import REVLOG_RESCHED
from anki.hooks import wrap
from aqt.reviewer import Reviewer
from .config import RemainingCountType, config
HTML_TAG = re.compile(r"<[^<>]+>", flags=re.IGNORECASE | re.MULTILINE)
def strip_html_tags(s: str) -> str:
return re.sub(HTML_TAG, "", s)
def to_number(s: str) -> Optional[int]:
try:
return int(s.strip())
except ValueError:
return None
def sum_remaining(html: str) -> int:
return sum(n for split in strip_html_tags(html).split("+") if (n := to_number(split)) is not None)
def format_remaining_cards(self: Reviewer, get_default_html: Callable[[Reviewer], str]):
if config.remaining_count_type == RemainingCountType.none:
return ""
elif config.remaining_count_type == RemainingCountType.single:
return f'<span class="ajt__total-count">Left: {sum_remaining(get_default_html(self))}</span>'
else:
return get_default_html(self).strip()
def prev_day_cutoff_ms(col: Collection) -> int:
return (col.sched.day_cutoff - 86_400) * 1000
def studied_today_count(col: Collection) -> int:
return col.db.scalar(
""" SELECT COUNT(*) FROM revlog WHERE type != ? AND id > ? """,
REVLOG_RESCHED,
prev_day_cutoff_ms(col),
)
def format_studied_today(col: Collection) -> str:
if not config.show_reps_done_today:
return ""
return f'<span class="ajt__studied-today">Reps: {studied_today_count(col)}</span>'
def wrap_remaining(self: Reviewer, _old: Callable[[Reviewer], str]) -> str:
return format_remaining_cards(self, _old) + format_studied_today(self.mw.col)
def init():
# noinspection PyProtectedMember
Reviewer._remaining = wrap(Reviewer._remaining, wrap_remaining, "around")