1
|
|
|
""" |
2
|
|
|
Experimental replacement for fancy consoles. |
3
|
|
|
""" |
4
|
|
|
import abc |
5
|
|
|
import logging |
6
|
|
|
import sys |
7
|
|
|
from typing import Callable, Optional, TypeVar, Generic, Sequence |
8
|
|
|
|
9
|
|
|
from colorama import Style, Fore |
|
|
|
|
10
|
|
|
|
11
|
|
|
from pocketutils.core.input_output import Writeable |
12
|
|
|
from pocketutils.misc.messages import * |
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
|
logger = logging.getLogger("pocketutils") |
16
|
|
|
L = TypeVar("L", covariant=True) |
|
|
|
|
17
|
|
|
M = TypeVar("M", covariant=True) |
|
|
|
|
18
|
|
|
|
19
|
|
|
|
20
|
|
|
class AbstractColorConsole(Generic[L, M], metaclass=abc.ABCMeta): |
|
|
|
|
21
|
|
|
def print(self, *lines: str, level: L, mod: Optional[M] = None) -> None: |
|
|
|
|
22
|
|
|
for line in self.format(*lines, level=level, mod=mod): |
23
|
|
|
self._stream.write(line) |
24
|
|
|
|
25
|
|
|
@property |
26
|
|
|
def _stream(self) -> Writeable: |
27
|
|
|
raise NotImplementedError() |
28
|
|
|
|
29
|
|
|
@property |
30
|
|
|
def _reset(self) -> int: |
31
|
|
|
return Style.RESET_ALL |
32
|
|
|
|
33
|
|
|
def format(self, *lines: str, level: L, mod: Optional[M] = None) -> Sequence[str]: |
|
|
|
|
34
|
|
|
raise NotImplementedError() |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
class ColorConsole(AbstractColorConsole[MsgLevel, bool]): |
|
|
|
|
38
|
|
|
def __init__( |
|
|
|
|
39
|
|
|
self, |
|
|
|
|
40
|
|
|
stream: Writeable, |
|
|
|
|
41
|
|
|
top: str, |
|
|
|
|
42
|
|
|
bottom: str, |
|
|
|
|
43
|
|
|
left: str, |
|
|
|
|
44
|
|
|
right: str, |
|
|
|
|
45
|
|
|
pad_top: int, |
|
|
|
|
46
|
|
|
pad_bottom: int, |
|
|
|
|
47
|
|
|
width: int, |
|
|
|
|
48
|
|
|
mapping: Callable[[MsgLevel], int], |
|
|
|
|
49
|
|
|
): |
50
|
|
|
self._the_stream = stream |
51
|
|
|
self._top, self._bottom, self._left, self._right = top, bottom, left, right |
52
|
|
|
self._pad_top, self._pad_bottom = pad_top, pad_bottom |
53
|
|
|
self._width = width |
54
|
|
|
self._mapping = mapping |
55
|
|
|
|
56
|
|
|
def format(self, *lines: str, level: MsgLevel, mod: Optional[bool] = None) -> Sequence[str]: |
|
|
|
|
57
|
|
|
color = str(self._mapping(level)) |
58
|
|
|
width = self._width |
59
|
|
|
reset = str(self._reset) |
60
|
|
|
if mod: |
61
|
|
|
top, bottom, left, right = self._top, self._bottom, self._left, self._right |
62
|
|
|
pad_top = ["" for _ in range(self._pad_top)] |
63
|
|
|
pad_bottom = ["" for _ in range(self._pad_bottom)] |
64
|
|
|
else: |
65
|
|
|
top, bottom, left, right = "", "", "", "" |
66
|
|
|
pad_top, pad_bottom = [], [] |
67
|
|
|
|
68
|
|
|
def cl(text: str): |
|
|
|
|
69
|
|
|
return color + left + text.center(width - len(left) - len(right)) + right + reset |
70
|
|
|
|
71
|
|
|
return [ |
72
|
|
|
*pad_top, |
73
|
|
|
color + top * width + reset, |
74
|
|
|
*[cl(line) for line in lines], |
75
|
|
|
color + bottom * width + reset, |
76
|
|
|
*pad_bottom, |
77
|
|
|
] |
78
|
|
|
|
79
|
|
|
@property |
80
|
|
|
def _stream(self) -> Writeable: |
81
|
|
|
return self._the_stream |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
class ColorConsoleBuilder: |
|
|
|
|
85
|
|
|
def __init__(self): |
86
|
|
|
self._mapping = {} |
87
|
|
|
self._width = 100 |
88
|
|
|
self._top, self._bottom = "-", "-" |
89
|
|
|
self._left, self._right = "", "" |
90
|
|
|
self._pad_top, self._pad_bottom = 1, 1 |
91
|
|
|
|
92
|
|
|
def add(self, level: MsgLevel, color: int) -> __qualname__: |
|
|
|
|
93
|
|
|
self._mapping[level] = color |
94
|
|
|
return self |
95
|
|
|
|
96
|
|
|
def width(self, w: int) -> __qualname__: |
|
|
|
|
97
|
|
|
self._width = w |
98
|
|
|
return self |
99
|
|
|
|
100
|
|
|
def top(self, top: str) -> __qualname__: |
|
|
|
|
101
|
|
|
self._top = top |
102
|
|
|
return self |
103
|
|
|
|
104
|
|
|
def bottom(self, bottom: str) -> __qualname__: |
|
|
|
|
105
|
|
|
self._bottom = bottom |
106
|
|
|
return self |
107
|
|
|
|
108
|
|
|
def left(self, left: str) -> __qualname__: |
|
|
|
|
109
|
|
|
self._left = left |
110
|
|
|
return self |
111
|
|
|
|
112
|
|
|
def right(self, right: str) -> __qualname__: |
|
|
|
|
113
|
|
|
self._right = right |
114
|
|
|
return self |
115
|
|
|
|
116
|
|
|
def pad_top(self, i: int) -> __qualname__: |
|
|
|
|
117
|
|
|
self._pad_top = i |
118
|
|
|
return self |
119
|
|
|
|
120
|
|
|
def pad_bottom(self, i: int) -> __qualname__: |
|
|
|
|
121
|
|
|
self._pad_bottom = i |
122
|
|
|
return self |
123
|
|
|
|
124
|
|
|
def build(self, sink: Optional[Writeable] = None) -> ColorConsole: |
|
|
|
|
125
|
|
|
if sink is None: |
126
|
|
|
sink = sys.stdout |
127
|
|
|
|
128
|
|
|
def fn(level: MsgLevel): |
|
|
|
|
129
|
|
|
return self._mapping[level] |
130
|
|
|
|
131
|
|
|
return ColorConsole( |
132
|
|
|
sink, |
133
|
|
|
self._top, |
134
|
|
|
self._bottom, |
135
|
|
|
self._left, |
136
|
|
|
self._right, |
137
|
|
|
self._pad_top, |
138
|
|
|
self._pad_bottom, |
139
|
|
|
self._width, |
140
|
|
|
fn, |
141
|
|
|
) |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
class ColorConsoles: |
|
|
|
|
145
|
|
|
@classmethod |
146
|
|
|
def new(cls) -> ColorConsoleBuilder: |
|
|
|
|
147
|
|
|
return ColorConsoleBuilder() |
148
|
|
|
|
149
|
|
|
@classmethod |
150
|
|
|
def default(cls) -> ColorConsoleBuilder: |
|
|
|
|
151
|
|
|
mapping = { |
152
|
|
|
MsgLevel.INFO: Style.BRIGHT, |
153
|
|
|
MsgLevel.NOTICE: Fore.BLUE, |
154
|
|
|
MsgLevel.SUCCESS: Fore.GREEN, |
155
|
|
|
MsgLevel.WARNING: Fore.MAGENTA, |
156
|
|
|
MsgLevel.FAILURE: Fore.RED, |
157
|
|
|
} |
158
|
|
|
builder = ColorConsoleBuilder() |
159
|
|
|
for k, v in mapping.items(): |
|
|
|
|
160
|
|
|
builder.add(k, v) |
161
|
|
|
return builder |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
__all__ = ["AbstractColorConsole", "ColorConsole", "ColorConsoles"] |
165
|
|
|
|