33
44
55class Printer :
6+ """
7+ Utility class to print to various types of terminals. This class won't
8+ print anything, to print use one of the subclasses.
9+ """
10+
611 def text (self , what : str ) -> None :
12+ """
13+ Normal, unformatted string, the newline won't be added automatically
14+ """
715 pass
816
917 def red (self , what : str , bold : bool = True ) -> None :
18+ """
19+ Print some text in red, optionally also in bold, the newline won't be
20+ added automatically.
21+ """
1022 pass
1123
1224 def green (self , what : str , bold : bool = True ) -> None :
25+ """
26+ Print some text in green, optionally also in bold, the newline won't be
27+ added automatically.
28+ """
1329 pass
1430
1531 def blue (self , what : str , bold : bool = True ) -> None :
32+ """
33+ Print some text in blue, optionally also in bold, the newline won't be
34+ added automatically.
35+ """
1636 pass
1737
1838 def yellow (self , what : str , bold : bool = True ) -> None :
39+ """
40+ Print some text in yellow, optionally also in bold, the newline won't be
41+ added automatically.
42+ """
1943 pass
2044
2145 def bold (self , what : str , bold : bool = True ) -> None :
46+ """
47+ Print some text in bold, the newline won't be added automatically.
48+ """
2249 pass
2350
2451
2552class StdoutPrinter (Printer ):
53+ """
54+ Printer that will print to stdout using the escape sequences to make the
55+ colors and so on.
56+ """
57+
2658 def __init__ (self ) -> None :
2759 self .bold_fmt = "\033 [1m"
2860 self .green_fmt = "\033 [32m"
@@ -43,40 +75,47 @@ def text(self, what: str) -> None:
4375
4476 def red (self , what : str , bold : bool = True ) -> None :
4577 print (
46- self .red_fmt + (self .bold_fmt
47- if bold else "" ) + what + self .reset_fmt ,
78+ self .red_fmt + (self .bold_fmt if bold else "" ) + what +
79+ self .reset_fmt ,
4880 end = "" )
4981
5082 def green (self , what : str , bold : bool = True ) -> None :
5183 print (
52- self .green_fmt + (self .bold_fmt
53- if bold else "" ) + what + self .reset_fmt ,
84+ self .green_fmt + (self .bold_fmt if bold else "" ) + what +
85+ self .reset_fmt ,
5486 end = "" )
5587
5688 def blue (self , what : str , bold : bool = True ) -> None :
5789 print (
58- self .blue_fmt + (self .bold_fmt
59- if bold else "" ) + what + self .reset_fmt ,
90+ self .blue_fmt + (self .bold_fmt if bold else "" ) + what +
91+ self .reset_fmt ,
6092 end = "" )
6193
6294 def yellow (self , what : str , bold : bool = True ) -> None :
6395 print (
64- self .yellow_fmt + (self .bold_fmt
65- if bold else "" ) + what + self .reset_fmt ,
96+ self .yellow_fmt + (self .bold_fmt if bold else "" ) + what +
97+ self .reset_fmt ,
6698 end = "" )
6799
68100 def bold (self , what : str , bold : bool = True ) -> None :
69101 print (self .bold_fmt + what + self .reset_fmt , end = "" )
70102
71103 def right (self , what : str ) -> None :
104+ """
105+ Print the text aligned to the right of the screen
106+ """
72107 print (self .right_fmt + self .left_fmt (len (what ) - 1 ) + what )
73108
74109
75110class CursesPrinter (Printer ):
111+ """
112+ Printer that will use the curses API in a curses Window.
113+ """
114+
76115 def __init__ (self , stdscr : 'curses._CursesWindow' ) -> None :
77116 self .stdscr = stdscr
78117 self .bold_fmt = curses .A_BOLD
79- if curses .COLORS >= 256 :
118+ if hasattr ( curses , "COLORS" ) and curses .COLORS >= 256 :
80119 self .green_fmt = curses .color_pair (82 )
81120 else :
82121 self .green_fmt = curses .color_pair (curses .COLOR_GREEN )
@@ -91,16 +130,16 @@ def red(self, what: str, bold: bool = True) -> None:
91130 self .stdscr .addstr (what , self .red_fmt | (self .bold_fmt if bold else 0 ))
92131
93132 def green (self , what : str , bold : bool = True ) -> None :
94- self .stdscr .addstr (what , self . green_fmt | ( self . bold_fmt
95- if bold else 0 ))
133+ self .stdscr .addstr (what ,
134+ self . green_fmt | ( self . bold_fmt if bold else 0 ))
96135
97136 def blue (self , what : str , bold : bool = True ) -> None :
98- self .stdscr .addstr (what , self . blue_fmt | ( self . bold_fmt
99- if bold else 0 ))
137+ self .stdscr .addstr (what ,
138+ self . blue_fmt | ( self . bold_fmt if bold else 0 ))
100139
101140 def yellow (self , what : str , bold : bool = True ) -> None :
102- self .stdscr .addstr (what , self . yellow_fmt | ( self . bold_fmt
103- if bold else 0 ))
141+ self .stdscr .addstr (what ,
142+ self . yellow_fmt | ( self . bold_fmt if bold else 0 ))
104143
105144 def bold (self , what : str , bold : bool = True ) -> None :
106145 self .stdscr .addstr (what , self .bold_fmt )
0 commit comments