forked from ospalh/anki-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeck_name_in_title.py
105 lines (84 loc) · 3.27 KB
/
deck_name_in_title.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# -*- mode: python ; coding: utf-8 -*-
# © 2012–2013 Roland Sieker <ospalh@gmail.com>
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
"""
Anki-2 add-on to show the deck name in the window title.
"""
import sys
import os
from anki.hooks import wrap, addHook
from aqt import mw
## Several separators between the current ‘activity’ (file, directory,
## web page) and the program name seem common. Pick one to taste:
title_separator = u' – '
# title_separator = u' : '
# title_separator = u' - '
## Show the sub-deck the current card belongs to
show_subdeck = True
# show_subdeck = False
# How to separate the clicked-on deck from the subdeck.
subdeck_format = u'{parent}:–:{child}'
# subdeck_format = u'{parent}(::{child})' # Old style
## Use either "Anki" or the program file name.
use_argv_0 = False
# use_argv_0 = True
__version__ = '1.3.0'
def get_prog_name():
"""Return either "Anki" org argv[0]"""
if use_argv_0 and sys.argv[0]:
return os.path.basename(sys.argv[0])
return u'Anki'
class DeckNamer(object):
"""Functions to set the title to the deck name in Anki2 """
def __init__(self):
self.prog_name = get_prog_name()
self.profile_string = u''
self.deck_name = u''
self.subdeck_name = u''
def get_deck_name(self):
"""Return the deck name"""
try:
self.deck_name = mw.col.decks.current()['name']
self.subdeck_name = self.deck_name
except AttributeError:
self.deck_name = u''
return self.deck_name
def get_profile_string(self):
"""
Return the profile name.
When there is more than one profile, return that name,
together with the title separator.
"""
if len(mw.pm.profiles()) > 1 and mw.pm.name:
self.profile_string = mw.pm.name + title_separator
else:
self.profile_string = u''
return self.profile_string
def deck_browser_title(self):
"""Set the window title when we are in the deck browser."""
mw.setWindowTitle(self.get_profile_string() + self.prog_name)
def overview_title(self):
"""Set the window title when we are at the overview."""
mw.setWindowTitle(self.get_deck_name() + title_separator +
self.profile_string + self.prog_name)
def card_title(self):
"""Set the window title when we are reviewing."""
self.overview_title()
old_subdeck_name = self.subdeck_name
self.subdeck_name = mw.col.decks.get(mw.reviewer.card.did)['name']
if old_subdeck_name == self.subdeck_name:
return
if self.subdeck_name == self.deck_name:
self.overview_title()
return
mw.setWindowTitle(
subdeck_format.format(
parent=self.deck_name,
child=self.subdeck_name[(len(self.deck_name) + 2):])
+ title_separator + self.profile_string + self.prog_name)
deck_namer = DeckNamer()
mw.deckBrowser.show = wrap(mw.deckBrowser.show, deck_namer.deck_browser_title)
mw.overview.show = wrap(mw.overview.show, deck_namer.overview_title)
mw.reviewer.show = wrap(mw.reviewer.show, deck_namer.overview_title)
if show_subdeck:
addHook('showQuestion', deck_namer.card_title)