rich
218 строк · 6.4 Кб
1from io import StringIO2
3from benchmarks import snippets4from rich.color import Color, ColorSystem5from rich.console import Console6from rich.pretty import Pretty7from rich.segment import Segment8from rich.style import Style9from rich.syntax import Syntax10from rich.table import Table11from rich.text import Text12
13
14class TextSuite:15def setup(self):16self.console = Console(17file=StringIO(), color_system="truecolor", legacy_windows=False18)19self.len_lorem_ipsum = len(snippets.LOREM_IPSUM)20self.text = Text.from_markup(snippets.MARKUP)21
22def time_wrapping(self):23self.text.wrap(self.console, 12, overflow="fold")24
25def time_indent_guides(self):26Text(snippets.PYTHON_SNIPPET).with_indent_guides()27
28def time_fit(self):29Text(snippets.LOREM_IPSUM).fit(12)30
31def time_split(self):32self.text.split()33
34def time_divide(self):35Text(snippets.LOREM_IPSUM).divide(range(20, 100, 4))36
37def time_align_center(self):38Text(snippets.LOREM_IPSUM).align("center", width=self.len_lorem_ipsum * 3)39
40def time_render(self):41list(self.text.render(self.console))42
43def time_wrapping_unicode_heavy(self):44Text(snippets.UNICODE_HEAVY_TEXT).wrap(self.console, 12, overflow="fold")45
46def time_fit_unicode_heavy(self):47Text(snippets.UNICODE_HEAVY_TEXT).fit(12)48
49def time_split_unicode_heavy(self):50Text(snippets.UNICODE_HEAVY_TEXT).split()51
52def time_divide_unicode_heavy(self):53self.text.divide(range(20, 100, 4))54
55def time_align_center_unicode_heavy(self):56Text(snippets.UNICODE_HEAVY_TEXT).align(57"center", width=self.len_lorem_ipsum * 358)59
60def time_render_unicode_heavy(self):61list(Text(snippets.UNICODE_HEAVY_TEXT).render(self.console))62
63
64class TextHotCacheSuite:65def setup(self):66self.console = Console(67file=StringIO(), color_system="truecolor", legacy_windows=False68)69
70def time_wrapping_unicode_heavy_warm_cache(self):71for _ in range(20):72Text(snippets.UNICODE_HEAVY_TEXT).wrap(self.console, 12, overflow="fold")73
74
75class SyntaxWrappingSuite:76def setup(self):77self.console = Console(78file=StringIO(), color_system="truecolor", legacy_windows=False79)80self.syntax = Syntax(81code=snippets.PYTHON_SNIPPET, lexer="python", word_wrap=True82)83
84def time_text_thin_terminal_heavy_wrapping(self):85self._print_with_width(20)86
87def time_text_thin_terminal_medium_wrapping(self):88self._print_with_width(60)89
90def time_text_wide_terminal_no_wrapping(self):91self._print_with_width(100)92
93def _print_with_width(self, width):94self.console.print(self.syntax, width)95
96
97class TableSuite:98def time_table_no_wrapping(self):99self._print_table(width=100)100
101def time_table_heavy_wrapping(self):102self._print_table(width=30)103
104def _print_table(self, width):105table = Table(title="Star Wars Movies")106console = Console(107file=StringIO(), color_system="truecolor", legacy_windows=False, width=width108)109table.add_column("Released", justify="right", style="cyan", no_wrap=True)110table.add_column("Title", style="magenta")111table.add_column("Box Office", justify="right", style="green")112table.add_row(113"Dec 20, 2019", "[b]Star Wars[/]: The Rise of Skywalker", "$952,110,690"114)115table.add_row(116"May 25, 2018", "Solo: A [red][b]Star Wars[/] Story[/]", "$393,151,347"117)118table.add_row(119"Dec 15, 2017",120"[b red]Star Wars[/] Ep. V111: The Last Jedi",121"$1,332,539,889",122)123table.add_row(124"Dec 16, 2016", "Rogue One: A [blue]Star Wars[/] Story", "$1,332,439,889"125)126console.print(table)127
128
129class PrettySuite:130def setup(self):131self.console = Console(132file=StringIO(), color_system="truecolor", legacy_windows=False, width=100133)134
135def time_pretty(self):136pretty = Pretty(snippets.PYTHON_DICT)137self.console.print(pretty)138
139def time_pretty_indent_guides(self):140pretty = Pretty(snippets.PYTHON_DICT, indent_guides=True)141self.console.print(pretty)142
143def time_pretty_justify_center(self):144pretty = Pretty(snippets.PYTHON_DICT, justify="center")145self.console.print(pretty)146
147
148class StyleSuite:149def setup(self):150self.console = Console(151file=StringIO(), color_system="truecolor", legacy_windows=False, width=100152)153self.style1 = Style.parse("blue on red")154self.style2 = Style.parse("green italic bold")155
156def time_parse_ansi(self):157Style.parse("red on blue")158
159def time_parse_hex(self):160Style.parse("#f0f0f0 on #e2e28a")161
162def time_parse_mixed_complex_style(self):163Style.parse("dim bold reverse #00ee00 on rgb(123,12,50)")164
165def time_style_add(self):166self.style1 + self.style2167
168
169class ColorSuite:170def setup(self):171self.console = Console(172file=StringIO(), color_system="truecolor", legacy_windows=False, width=100173)174self.color = Color.parse("#0d1da0")175
176def time_downgrade_to_eight_bit(self):177self.color.downgrade(ColorSystem.EIGHT_BIT)178
179def time_downgrade_to_standard(self):180self.color.downgrade(ColorSystem.STANDARD)181
182def time_downgrade_to_windows(self):183self.color.downgrade(ColorSystem.WINDOWS)184
185
186class ColorSuiteCached:187def setup(self):188self.console = Console(189file=StringIO(), color_system="truecolor", legacy_windows=False, width=100190)191self.color = Color.parse("#0d1da0")192# Warm cache193self.color.downgrade(ColorSystem.EIGHT_BIT)194self.color.downgrade(ColorSystem.STANDARD)195self.color.downgrade(ColorSystem.WINDOWS)196
197def time_downgrade_to_eight_bit(self):198self.color.downgrade(ColorSystem.EIGHT_BIT)199
200def time_downgrade_to_standard(self):201self.color.downgrade(ColorSystem.STANDARD)202
203def time_downgrade_to_windows(self):204self.color.downgrade(ColorSystem.WINDOWS)205
206
207class SegmentSuite:208def setup(self):209self.line = [210Segment("foo"),211Segment("bar"),212Segment("egg"),213Segment("Where there is a Will"),214Segment("There is a way"),215] * 2216
217def test_divide_complex(self):218list(Segment.divide(self.line, [5, 10, 20, 50, 108, 110, 118]))219