This repository was archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprinter.py
More file actions
145 lines (116 loc) · 4.41 KB
/
printer.py
File metadata and controls
145 lines (116 loc) · 4.41 KB
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
#!/usr/bin/env python3
import curses
class Printer:
"""
Utility class to print to various types of terminals. This class won't
print anything, to print use one of the subclasses.
"""
def text(self, what: str) -> None:
"""
Normal, unformatted string, the newline won't be added automatically
"""
pass
def red(self, what: str, bold: bool = True) -> None:
"""
Print some text in red, optionally also in bold, the newline won't be
added automatically.
"""
pass
def green(self, what: str, bold: bool = True) -> None:
"""
Print some text in green, optionally also in bold, the newline won't be
added automatically.
"""
pass
def blue(self, what: str, bold: bool = True) -> None:
"""
Print some text in blue, optionally also in bold, the newline won't be
added automatically.
"""
pass
def yellow(self, what: str, bold: bool = True) -> None:
"""
Print some text in yellow, optionally also in bold, the newline won't be
added automatically.
"""
pass
def bold(self, what: str, bold: bool = True) -> None:
"""
Print some text in bold, the newline won't be added automatically.
"""
pass
class StdoutPrinter(Printer):
"""
Printer that will print to stdout using the escape sequences to make the
colors and so on.
"""
def __init__(self) -> None:
self.bold_fmt = "\033[1m"
self.green_fmt = "\033[32m"
self.red_fmt = "\033[31m"
self.blue_fmt = "\033[34m"
self.yellow_fmt = "\033[33m"
self.reset_fmt = "\033[0m"
self.right_fmt = "\033[1000C"
# pylint: disable=no-self-use
def left_fmt(self, amount: int) -> str:
return "\033[%dD" % amount
# pylint: enable=no-self-use
def text(self, what: str) -> None:
print(what, end="")
def red(self, what: str, bold: bool = True) -> None:
print(
self.red_fmt + (self.bold_fmt if bold else "") + what +
self.reset_fmt,
end="")
def green(self, what: str, bold: bool = True) -> None:
print(
self.green_fmt + (self.bold_fmt if bold else "") + what +
self.reset_fmt,
end="")
def blue(self, what: str, bold: bool = True) -> None:
print(
self.blue_fmt + (self.bold_fmt if bold else "") + what +
self.reset_fmt,
end="")
def yellow(self, what: str, bold: bool = True) -> None:
print(
self.yellow_fmt + (self.bold_fmt if bold else "") + what +
self.reset_fmt,
end="")
def bold(self, what: str, bold: bool = True) -> None:
print(self.bold_fmt + what + self.reset_fmt, end="")
def right(self, what: str) -> None:
"""
Print the text aligned to the right of the screen
"""
print(self.right_fmt + self.left_fmt(len(what) - 1) + what)
class CursesPrinter(Printer):
"""
Printer that will use the curses API in a curses Window.
"""
def __init__(self, stdscr: 'curses._CursesWindow') -> None:
self.stdscr = stdscr
self.bold_fmt = curses.A_BOLD
if hasattr(curses, "COLORS") and curses.COLORS >= 256:
self.green_fmt = curses.color_pair(82)
else:
self.green_fmt = curses.color_pair(curses.COLOR_GREEN)
self.red_fmt = curses.color_pair(curses.COLOR_RED)
self.blue_fmt = curses.color_pair(curses.COLOR_BLUE)
self.yellow_fmt = curses.color_pair(curses.COLOR_YELLOW)
def text(self, what: str) -> None:
self.stdscr.addstr(what)
def red(self, what: str, bold: bool = True) -> None:
self.stdscr.addstr(what, self.red_fmt | (self.bold_fmt if bold else 0))
def green(self, what: str, bold: bool = True) -> None:
self.stdscr.addstr(what,
self.green_fmt | (self.bold_fmt if bold else 0))
def blue(self, what: str, bold: bool = True) -> None:
self.stdscr.addstr(what,
self.blue_fmt | (self.bold_fmt if bold else 0))
def yellow(self, what: str, bold: bool = True) -> None:
self.stdscr.addstr(what,
self.yellow_fmt | (self.bold_fmt if bold else 0))
def bold(self, what: str, bold: bool = True) -> None:
self.stdscr.addstr(what, self.bold_fmt)