-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
327 lines (263 loc) · 11.1 KB
/
__init__.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
from aqt import gui_hooks
from aqt import mw
import datetime
# import aqt
config = mw.addonManager.getConfig(__name__)
colors = {
0: config['manually-forgot-color'],
1: config['rated-again-color'], # again
2: config["rated-hard-color"], # hard
3: config["rated-good-color"], # good
4: config["rated-easy-color"] # easy
}
labels = {
0: "Manually <br> FORGOT on",
1: "Rated AGAIN on", # again
2: "Rated HARD on", # hard
3: "Rated GOOD on", # good
4: "Rated EASY on" # easy
}
types = {
0: "(Learning)",
1: "(Review)",
2: "(Relearn)",
3: "(Custom Study)",
4: ""
}
def init(card):
cmd = f"select ease, id, ivl, factor,type from revlog where cid = '{card.id}' ORDER BY id ASC "
rating_list = mw.col.db.all(cmd)
regularMode = False if (config["only-show-learning-reviews-in-learning-stage"] == "true") else True
n = len(rating_list)
if (n > 0): # check if a card has an previous ratings
combiner = "append" if (config["vertical-position"] == "bottom") else "prepend"
container = """
(function(){
$('#legend').remove()
$("#legend-container").remove()
$('body').%s(`
<div id = "legend-container">
<div id = "legend">
<div id = "squares">
</div>
</div>
</div>
`)
""" % (combiner)
sched = mw.col.schedVer()
# The total number of statistics
againSum = 0 # again
hardSum = 0 # hard
goodSum = 0 # good
easySum = 0 # easy
# The data of review statistics
allData = []
for review in rating_list:
rating = review[0]
timeInMs = review[1]
rawIvl = review[2]
rawEase = review[3]
rawRevType = review[4]
# "raw" means that the data still needs to be converted to a different format
if (timeInMs > 0 and (rawRevType != 0 or rating_list[n - 1][ 4] == 0 or regularMode == True)): # check if the card rating time is valid
# if the review type is 0, the card is not in learning stage, and special mode is on
if (rawIvl < 0): # if the interval is negative, it is expressed in seconds (learning steps)
interval = findNearestTimeMultiple(abs(rawIvl)) + "" # convert seconds to nearest multiple
elif (rawIvl < 30): # if the interval is positive, it is expressed in days
interval = str(rawIvl) + " days"
else:
interval = str(rawIvl // 30) + " months"
if (rawEase != 0):
ease = str(rawEase // 10) + "%"
else:
ease = "N/A"
if (sched == 1 and rawRevType != 1 and rating != 1): # case in 2.0 scheduler where there is no "hard" option, which requires all buttons other than "again" to offset up by 1
off_set = int(rating) + 1
againSum, hardSum, goodSum, easySum = countNumberOfTimes(off_set, againSum, hardSum, goodSum, easySum)
color_id = colors[off_set]
label = labels[off_set]
else:
againSum, hardSum, goodSum, easySum = countNumberOfTimes(rating, againSum, hardSum, goodSum, easySum)
color_id = colors[rating]
label = labels[rating]
date = datetime.datetime.fromtimestamp(timeInMs / 1000).strftime('%Y-%m-%d <br> %I:%M %p')
# aqt.utils.showText(str(element[4]))
reviewType = types[rawRevType]
singleData = {
"color": color_id,
"label": label,
"date": date,
"ease": ease,
"interval": interval,
"reviewType": reviewType,
}
allData.append(singleData)
# add card history to the container
container = addCardHistory(allData, container)
if config["show-label"] == "true":
container += ("""
$('#legend').prepend(`
<div class = "legend-label" > Card Rating <br> History (%s) <br> (%s-%s-%s-%s)
</div>
<div class="vl"></div>
`)
""" % (n, againSum, hardSum, goodSum, easySum) )
container += """
$('head').append(`
<style id="legend-style">
.square {
height: 20px!important;
width: 20px!important;
margin: 5px!important;
border-radius: 5px!important;
}
.night_mode #legend{
background-color: #46464A!important;
}
.tooltip {
position: relative!important;
display: inline-block!important;
}
.tooltip .tooltiptext {
visibility: hidden!important;
width: 200px!important;
max-width: 700px!important;
background-color: black!important;
font-family: ui-monospace,SFMono-Regular,SF Mono,Menlo,Consolas,Liberation Mono,monospace!important;
color: #fff!important;
text-align: center!important;
border-radius: 6px!important;
padding: 5px 0!important;
position: absolute!important;
z-index: 1!important;
top: 200%!important;
left: 50%!important;
font-size: 20px!important;
margin-left: -100px!important;
line-height: normal !important;
}
.tooltip .tooltiptext::after {
content: ""!important;
position: absolute!important;
bottom: 100%!important;
left: 50%!important;
margin-left: -5px!important;
border-width: 5px !important;
border-style: solid !important;
border-color: transparent transparent black transparent !important;
}
.tooltip:hover .tooltiptext {
visibility: visible !important;
}
#squares{
display: flex !important;
align-items: center !important;
max-width: 660px !important;
flex-wrap: wrap !important;
}
.legend-label{
display: flex !important;
align-items: center !important;
text-align: center !important;
font-weight: 1000 !important;
font-size: 15px !important;
font-family: ui-monospace,SFMono-Regular,SF Mono,Menlo,Consolas,Liberation Mono,monospace!important;
padding-left: 8px !important;
color: #CACACA !important;
}
.night_mode .legend-label{
color: #75757A !important;
}
#legend-container{
display: flex !important;
justify-content: center !important;
}
.vl {
border-left: 2px solid #75757A !important;
margin-left: 10px !important;
margin-right: 10px !important;
align-self: center !important;
height: 29px !important;
}
#legend{
direction: ltr !important;
display: flex !important;
justify-content: center !important;
border-radius: 5px !important;
width: max-content !important;
padding: 5px !important;
margin-top: 20px !important;
margin-bottom: 20px !important;
padding-left: 5px !important;
padding-right: 5px !important;
border-radius: 10px !important;
background-color: #F0F0F0 !important;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19) !important;
line-height: normal !important;
"""
closing_tag = ''' } </style>` ) })() '''
user_changeable = '''
width: %s!important;
zoom: %s!important;
''' % (config["width"], config["size"])
container += (user_changeable + closing_tag)
# aqt.utils.showText(str(mw.col.schedVer()))
else:
container = """ (function(){
$('#legend').remove()
$("#legend-container").remove()
})()
"""
mw.reviewer.web.eval(container)
def unInit(card):
mw.reviewer.web.eval("""
(function () {
$("#legend-container").remove()
$("#legend").remove()
})()
""")
if (config["constantly-show-addon"] == "true"):
gui_hooks.reviewer_did_show_question.append(init)
gui_hooks.reviewer_did_show_answer.append(init)
else:
gui_hooks.reviewer_did_show_question.append(unInit)
gui_hooks.reviewer_did_show_answer.append(init)
def findNearestTimeMultiple(seconds):
if (seconds < 60):
return str(seconds) + " secs"
elif (seconds < 3600):
return str(seconds // 60) + " mins"
elif (seconds < 86400):
return str(seconds // 3600) + " hours"
elif (seconds < 2592000):
return str(seconds // 86400) + " days"
else:
return str(seconds // 2592000) + " months"
def countNumberOfTimes(i, againSum, hardSum, goodSum, easySum):
if i == 1:
againSum += 1 # again
elif i == 2:
hardSum += 1 # hard
elif i == 3:
goodSum += 1 # good
elif i == 4:
easySum += 1 # easy
else:
pass
return againSum, hardSum, goodSum, easySum
def addCardHistory(allData, container):
javascript = """
$('#squares').append(
'<div class = "square tooltip" style = "background-color: %s"> <span class="tooltiptext">%s <br> %s <br> <br> Ease: %s <br> Ivl: %s <br> %s </span> </div>'
)
"""
limitNum = int(config['limit-number'])
lenOfallData = len(allData)
if (limitNum >= lenOfallData):
for i in range(lenOfallData):
container += javascript % (allData[i]['color'], allData[i]['label'], allData[i]['date'], allData[i]['ease'], allData[i]['interval'], allData[i]['reviewType'])
else:
for i in range(limitNum):
a = lenOfallData - limitNum + i
container += javascript % (allData[a]["color"], allData[a]["label"], allData[a]["date"], allData[a]["ease"], allData[a]["interval"], allData[a]["reviewType"])
return container