forked from kudrykv/latex-yearly-planner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
79 lines (61 loc) · 1.91 KB
/
parser.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
import signal
import sys
# Read stdin for \[[0-9]+(?:\/[0-9]+)?\]
START_CHAR = "["
END_CHAR = "]"
class Printer:
passes = 1
prevous_page = -1
prevous_string = ""
denominator = 0
def emit(self, page_number):
# set denominator if page_number < last_page_number
if page_number < self.prevous_page:
self.denominator = self.prevous_page
self.passes += 1
self.prevous_page = page_number
current_string = f"processing (pass={self.passes}): {page_number}"
if self.denominator:
current_string += f"/{self.denominator}"
# Add |b for prevous_string length
current_string = "\b" * len(self.prevous_string) + current_string
self.prevous_string = current_string[len(self.prevous_string) :]
print(current_string, end="")
sys.stdout.flush()
def clear(self):
print(
"\b" * len(self.prevous_string)
+ " " * len(self.prevous_string)
+ "\b" * len(self.prevous_string),
end="",
)
sys.stdout.flush()
try:
stored = ""
buff = ""
printer = Printer()
while True:
buff = sys.stdin.read(1)
# Exit once stding is empty
if buff == "":
printer.clear()
break
# Page numbers are delimited by square brakets like: [<number>]
# Once the ] character is matched, emit the stored number
if buff == "]" and len(stored) > 1:
printer.emit(int(stored[1:]))
# Save the number between square brackets
elif len(stored) > 0 and buff != "]":
try:
int(buff)
stored += buff
except:
stored = ""
# Mark number start
elif buff == "[":
stored = "["
# Empty stored buffer on mismatch
else:
stored = ""
except KeyboardInterrupt:
exit(1)