-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathfigure.py
More file actions
65 lines (46 loc) · 1.72 KB
/
Copy pathfigure.py
File metadata and controls
65 lines (46 loc) · 1.72 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
from typing import Optional
from .barh import barh
from .helpers import create_padding_tuple
from .hist import hist
from .plot import plot
def figure(*args, **kwargs):
return Figure(*args, **kwargs)
class Figure:
def __init__(self, width: Optional[int] = None, padding: int = 0):
self._content = []
self._width = width
self._subfigures = None
self._padding = create_padding_tuple(padding)
def __rich_console__(self, *args):
yield self.get_string()
def aprint(self, string):
self._content.append(string.split("\n"))
def show(self):
print(self.get_string())
def get_string(self, remove_trailing_whitespace=True):
lines = []
padding_lr = self._padding[1] + self._padding[3]
if self._width is None:
line_lengths = [len(line) for c in self._content for line in c]
width = max(line_lengths) if line_lengths else 0
width += padding_lr
else:
width = self._width
# Top padding
lines += self._padding[0] * [" " * width]
pr = " " * self._padding[1]
pl = " " * self._padding[3]
lines += [
pl + line[: width - padding_lr] + pr for c in self._content for line in c
]
# Bottom padding
lines += self._padding[2] * [" " * width]
if remove_trailing_whitespace:
lines = [line.rstrip() for line in lines]
return "\n".join(lines)
def hist(self, *args, **kwargs):
self._content.append(hist(*args, **kwargs))
def barh(self, *args, **kwargs):
self._content.append(barh(*args, **kwargs))
def plot(self, *args, **kwargs):
self._content.append(plot(*args, **kwargs))