-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtyping_accuracy.py
70 lines (55 loc) · 1.77 KB
/
typing_accuracy.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
import curses
from plugin import Plugin
class TypingAccuracyPlugin(Plugin):
"""
A fun plugin that shows how many times you've removed characters versus how many times you've added characters.
"""
__singleton = None
def __new__(cls, *args, **kwargs):
"""
Creates a singleton of the class.
"""
if cls.__singleton is None:
cls.__singleton = super().__new__(cls)
return cls.__singleton
def __init__(self, app):
super().__init__(app)
# How many characters have been typed vs how many were removed
self.removed_chars, self.added_chars = 0, 0
def init(self):
"""
Defines two color pairs for the colors of the bar.
"""
self.green_pair = self.create_pair(curses.COLOR_BLACK, curses.COLOR_GREEN)
self.red_pair = self.create_pair(curses.COLOR_BLACK, curses.COLOR_RED)
def update_on_keypress(self, key: str):
"""
Updates the count of characters and displays the percentage of them being removed/added.
"""
# Checks if the key is a backspace key, and if so, adds it to the removed characters
if key in ("KEY_BACKSPACE", "\b", "\0"):
self.removed_chars += 1
# Then we increment the amount of total characters
self.added_chars += 1
# Displays the progress bar with the percentage of characters removed/added
percentage = self.removed_chars / self.added_chars
self.app.stdscr.addstr(
self.app.rows - 5,
self.app.cols - 20,
" " * 10,
curses.color_pair(self.red_pair)
)
self.app.stdscr.addstr(
self.app.rows - 5,
self.app.cols - 20,
" " * int((1 - percentage) * 10),
curses.color_pair(self.green_pair)
)
self.app.stdscr.addstr(
self.app.rows - 5,
self.app.cols - 9,
f"{((1 - percentage) * 100):.1f}%",
curses.color_pair(10 + (percentage > 0.5)) | curses.A_REVERSE
)
def init(app):
return TypingAccuracyPlugin(app)