/
__main__.py
181 lines (153 loc) · 5.77 KB
/
__main__.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import io
from time import process_time
from rich.console import Console, ConsoleOptions, RenderGroup, RenderResult
from rich.markdown import Markdown
from rich.measure import Measurement
from rich.padding import Padding
from rich.panel import Panel
from rich.style import Style
from rich.table import Table
from rich.syntax import Syntax
from rich.text import Text
from rich import box
class ColorBox:
def __init__(self, start: int = 16):
self.start = start
def __rich_console__(
self, console: Console, options: ConsoleOptions
) -> RenderResult:
start = self.start
for color_start in range(start, start + 36, 6):
text = Text()
for color_no in range(color_start, color_start + 6):
text.append(" ", Style(bgcolor=str(color_no)))
yield text
def __rich_measure__(self, console: "Console", max_width: int) -> Measurement:
return Measurement(12, 12)
def make_test_card() -> Table:
"""Get a renderable that demonstrates a number of features."""
table = Table.grid(padding=1, pad_edge=True)
table.title = "Rich features"
table.add_column("Feature", no_wrap=True, justify="right", style="bold red")
table.add_column("Demonstration")
color_table = Table(
box=None,
expand=False,
show_header=False,
show_edge=False,
pad_edge=False,
padding=0,
)
color_table.add_row(*(ColorBox(16 + color * 36) for color in range(6)))
table.add_row(
"Colors",
RenderGroup(
"[bold yellow]256[/] colors or [bold green]16.7 million[/] colors [blue](if supported by your terminal)[/].",
Padding(color_table, (1, 0, 0, 0)),
),
)
table.add_row(
"Styles",
"All ansi styles: [bold]bold[/], [dim]dim[/], [italic]italic[/italic], [underline]underline[/], [strike]strikethrough[/], [reverse]reverse[/], and even [blink]blink[/].",
)
lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque in metus sed sapien ultricies pretium a at justo. Maecenas luctus velit et auctor maximus. Donec faucibus vel arcu id pretium."
lorem_table = Table.grid(padding=1, collapse_padding=True)
lorem_table.pad_edge = False
lorem_table.add_row(
Text(lorem, justify="left", style="green"),
Text(lorem, justify="center", style="yellow"),
Text(lorem, justify="right", style="blue"),
Text(lorem, justify="full", style="red"),
)
table.add_row(
"Text",
RenderGroup(
Text.from_markup(
"""Word wrap text. Justify [green]left[/], [yellow]center[/], [blue]right[/] or [red]full[/].\n"""
),
lorem_table,
),
)
def comparison(renderable1, renderable2) -> Table:
table = Table(show_header=False, pad_edge=False, box=None, expand=True)
table.add_column("1", ratio=1)
table.add_column("2", ratio=1)
table.add_row(renderable1, renderable2)
return table
table.add_row(
"CJK support",
Panel(
"该库支持中文,日文和韩文文本!", expand=False, border_style="red", box=box.DOUBLE_EDGE,
),
)
emoji_example = (
"Render emoji code: :+1: :apple: :ant: :bear: :baguette_bread: :bus: "
)
table.add_row("Emoji", comparison(Text(emoji_example), emoji_example))
markup_example = "[bold magenta]Rich[/] supports a simple [i]bbcode[/i] like [b]markup[/b], you can use to insert [yellow]color[/] and [underline]style[/]."
table.add_row(
"Console markup", comparison(Text(markup_example), markup_example),
)
example_table = Table(
title="Star Wars box office", show_header=True, header_style="bold magenta"
)
example_table.add_column("Date", style="dim", no_wrap=True)
example_table.add_column("Title")
example_table.add_column("Production Budget", justify="right", no_wrap=True)
example_table.add_column("Box Office", justify="right", no_wrap=True)
example_table.add_row(
"Dec 20, 2019",
"Star Wars: The Rise of Skywalker",
"$275,000,000",
"$375,126,118",
)
example_table.add_row(
"May 25, 2018",
"[red]Solo[/red]: A Star Wars Story",
"$275,000,000",
"$393,151,347",
)
example_table.add_row(
"Dec 15, 2017",
"Star Wars Ep. VIII: The Last Jedi",
"$262,000,000",
"[bold]$1,332,539,889[/bold]",
)
table.add_row("Tables", example_table)
code = '''\
def iter_last(values: Iterable[T]) -> Iterable[Tuple[bool, T]]:
"""Iterate and generate a tuple with a flag for last value."""
iter_values = iter(values)
try:
previous_value = next(iter_values)
except StopIteration:
return
for value in iter_values:
yield False, previous_value
previous_value = value
yield True, previous_value'''
table.add_row("Syntax highlighting", Syntax(code, "python3", line_numbers=True))
markdown_example = """\
# Markdown
Supports much of the *markdown*, __syntax__!
- Headers
- Basic formatting: **bold**, *italic*, `code`
- Block quotes
- Lists, and more...
"""
table.add_row("Markdown", comparison(markdown_example, Markdown(markdown_example)))
table.add_row(
"And more", """Progress bars, styled logging handler, tracebacks, etc..."""
)
return table
if __name__ == "__main__": # pragma: no cover
console = Console(file=io.StringIO(), force_terminal=True)
test_card = make_test_card()
# Print once to warm cache
console.print(test_card)
console.file = io.StringIO()
start = process_time()
console.print(test_card)
taken = round((process_time() - start) * 1000.0, 1)
print(console.file.getvalue()) # type: ignore
print(f"rendered in {taken}ms")