Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/rich/box.py: 79%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from typing import TYPE_CHECKING, Iterable, Literal
3from ._loop import loop_last
5if TYPE_CHECKING:
6 from rich.console import ConsoleOptions
9class Box:
10 """Defines characters to render boxes.
12 ┌─┬┐ top
13 │ ││ head
14 ├─┼┤ head_row
15 │ ││ mid
16 ├─┼┤ row
17 ├─┼┤ foot_row
18 │ ││ foot
19 └─┴┘ bottom
21 Args:
22 box (str): Characters making up box.
23 ascii (bool, optional): True if this box uses ascii characters only. Default is False.
24 """
26 def __init__(self, box: str, *, ascii: bool = False) -> None:
27 self._box = box
28 self.ascii = ascii
29 line1, line2, line3, line4, line5, line6, line7, line8 = box.splitlines()
30 # top
31 self.top_left, self.top, self.top_divider, self.top_right = iter(line1)
32 # head
33 self.head_left, _, self.head_vertical, self.head_right = iter(line2)
34 # head_row
35 (
36 self.head_row_left,
37 self.head_row_horizontal,
38 self.head_row_cross,
39 self.head_row_right,
40 ) = iter(line3)
42 # mid
43 self.mid_left, _, self.mid_vertical, self.mid_right = iter(line4)
44 # row
45 self.row_left, self.row_horizontal, self.row_cross, self.row_right = iter(line5)
46 # foot_row
47 (
48 self.foot_row_left,
49 self.foot_row_horizontal,
50 self.foot_row_cross,
51 self.foot_row_right,
52 ) = iter(line6)
53 # foot
54 self.foot_left, _, self.foot_vertical, self.foot_right = iter(line7)
55 # bottom
56 self.bottom_left, self.bottom, self.bottom_divider, self.bottom_right = iter(
57 line8
58 )
60 def __repr__(self) -> str:
61 return "Box(...)"
63 def __str__(self) -> str:
64 return self._box
66 def substitute(self, options: "ConsoleOptions", safe: bool = True) -> "Box":
67 """Substitute this box for another if it won't render due to platform issues.
69 Args:
70 options (ConsoleOptions): Console options used in rendering.
71 safe (bool, optional): Substitute this for another Box if there are known problems
72 displaying on the platform (currently only relevant on Windows). Default is True.
74 Returns:
75 Box: A different Box or the same Box.
76 """
77 box = self
78 if options.legacy_windows and safe:
79 box = LEGACY_WINDOWS_SUBSTITUTIONS.get(box, box)
80 if options.ascii_only and not box.ascii:
81 box = ASCII
82 return box
84 def get_plain_headed_box(self) -> "Box":
85 """If this box uses special characters for the borders of the header, then
86 return the equivalent box that does not.
88 Returns:
89 Box: The most similar Box that doesn't use header-specific box characters.
90 If the current Box already satisfies this criterion, then it's returned.
91 """
92 return PLAIN_HEADED_SUBSTITUTIONS.get(self, self)
94 def get_top(self, widths: Iterable[int]) -> str:
95 """Get the top of a simple box.
97 Args:
98 widths (list[int]): Widths of columns.
100 Returns:
101 str: A string of box characters.
102 """
104 parts: list[str] = []
105 append = parts.append
106 append(self.top_left)
107 for last, width in loop_last(widths):
108 append(self.top * width)
109 if not last:
110 append(self.top_divider)
111 append(self.top_right)
112 return "".join(parts)
114 def get_row(
115 self,
116 widths: Iterable[int],
117 level: Literal["head", "row", "foot", "mid"] = "row",
118 edge: bool = True,
119 ) -> str:
120 """Get the top of a simple box.
122 Args:
123 width (list[int]): Widths of columns.
125 Returns:
126 str: A string of box characters.
127 """
128 if level == "head":
129 left = self.head_row_left
130 horizontal = self.head_row_horizontal
131 cross = self.head_row_cross
132 right = self.head_row_right
133 elif level == "row":
134 left = self.row_left
135 horizontal = self.row_horizontal
136 cross = self.row_cross
137 right = self.row_right
138 elif level == "mid":
139 left = self.mid_left
140 horizontal = " "
141 cross = self.mid_vertical
142 right = self.mid_right
143 elif level == "foot":
144 left = self.foot_row_left
145 horizontal = self.foot_row_horizontal
146 cross = self.foot_row_cross
147 right = self.foot_row_right
148 else:
149 raise ValueError("level must be 'head', 'row' or 'foot'")
151 parts: list[str] = []
152 append = parts.append
153 if edge:
154 append(left)
155 for last, width in loop_last(widths):
156 append(horizontal * width)
157 if not last:
158 append(cross)
159 if edge:
160 append(right)
161 return "".join(parts)
163 def get_bottom(self, widths: Iterable[int]) -> str:
164 """Get the bottom of a simple box.
166 Args:
167 widths (list[int]): Widths of columns.
169 Returns:
170 str: A string of box characters.
171 """
173 parts: list[str] = []
174 append = parts.append
175 append(self.bottom_left)
176 for last, width in loop_last(widths):
177 append(self.bottom * width)
178 if not last:
179 append(self.bottom_divider)
180 append(self.bottom_right)
181 return "".join(parts)
184# fmt: off
185ASCII: Box = Box(
186 "+--+\n"
187 "| ||\n"
188 "|-+|\n"
189 "| ||\n"
190 "|-+|\n"
191 "|-+|\n"
192 "| ||\n"
193 "+--+\n",
194 ascii=True,
195)
197ASCII2: Box = Box(
198 "+-++\n"
199 "| ||\n"
200 "+-++\n"
201 "| ||\n"
202 "+-++\n"
203 "+-++\n"
204 "| ||\n"
205 "+-++\n",
206 ascii=True,
207)
209ASCII_DOUBLE_HEAD: Box = Box(
210 "+-++\n"
211 "| ||\n"
212 "+=++\n"
213 "| ||\n"
214 "+-++\n"
215 "+-++\n"
216 "| ||\n"
217 "+-++\n",
218 ascii=True,
219)
221SQUARE: Box = Box(
222 "┌─┬┐\n"
223 "│ ││\n"
224 "├─┼┤\n"
225 "│ ││\n"
226 "├─┼┤\n"
227 "├─┼┤\n"
228 "│ ││\n"
229 "└─┴┘\n"
230)
232SQUARE_DOUBLE_HEAD: Box = Box(
233 "┌─┬┐\n"
234 "│ ││\n"
235 "╞═╪╡\n"
236 "│ ││\n"
237 "├─┼┤\n"
238 "├─┼┤\n"
239 "│ ││\n"
240 "└─┴┘\n"
241)
243MINIMAL: Box = Box(
244 " ╷ \n"
245 " │ \n"
246 "╶─┼╴\n"
247 " │ \n"
248 "╶─┼╴\n"
249 "╶─┼╴\n"
250 " │ \n"
251 " ╵ \n"
252)
255MINIMAL_HEAVY_HEAD: Box = Box(
256 " ╷ \n"
257 " │ \n"
258 "╺━┿╸\n"
259 " │ \n"
260 "╶─┼╴\n"
261 "╶─┼╴\n"
262 " │ \n"
263 " ╵ \n"
264)
266MINIMAL_DOUBLE_HEAD: Box = Box(
267 " ╷ \n"
268 " │ \n"
269 " ═╪ \n"
270 " │ \n"
271 " ─┼ \n"
272 " ─┼ \n"
273 " │ \n"
274 " ╵ \n"
275)
278SIMPLE: Box = Box(
279 " \n"
280 " \n"
281 " ── \n"
282 " \n"
283 " \n"
284 " ── \n"
285 " \n"
286 " \n"
287)
289SIMPLE_HEAD: Box = Box(
290 " \n"
291 " \n"
292 " ── \n"
293 " \n"
294 " \n"
295 " \n"
296 " \n"
297 " \n"
298)
301SIMPLE_HEAVY: Box = Box(
302 " \n"
303 " \n"
304 " ━━ \n"
305 " \n"
306 " \n"
307 " ━━ \n"
308 " \n"
309 " \n"
310)
313HORIZONTALS: Box = Box(
314 " ── \n"
315 " \n"
316 " ── \n"
317 " \n"
318 " ── \n"
319 " ── \n"
320 " \n"
321 " ── \n"
322)
324ROUNDED: Box = Box(
325 "╭─┬╮\n"
326 "│ ││\n"
327 "├─┼┤\n"
328 "│ ││\n"
329 "├─┼┤\n"
330 "├─┼┤\n"
331 "│ ││\n"
332 "╰─┴╯\n"
333)
335HEAVY: Box = Box(
336 "┏━┳┓\n"
337 "┃ ┃┃\n"
338 "┣━╋┫\n"
339 "┃ ┃┃\n"
340 "┣━╋┫\n"
341 "┣━╋┫\n"
342 "┃ ┃┃\n"
343 "┗━┻┛\n"
344)
346HEAVY_EDGE: Box = Box(
347 "┏━┯┓\n"
348 "┃ │┃\n"
349 "┠─┼┨\n"
350 "┃ │┃\n"
351 "┠─┼┨\n"
352 "┠─┼┨\n"
353 "┃ │┃\n"
354 "┗━┷┛\n"
355)
357HEAVY_HEAD: Box = Box(
358 "┏━┳┓\n"
359 "┃ ┃┃\n"
360 "┡━╇┩\n"
361 "│ ││\n"
362 "├─┼┤\n"
363 "├─┼┤\n"
364 "│ ││\n"
365 "└─┴┘\n"
366)
368DOUBLE: Box = Box(
369 "╔═╦╗\n"
370 "║ ║║\n"
371 "╠═╬╣\n"
372 "║ ║║\n"
373 "╠═╬╣\n"
374 "╠═╬╣\n"
375 "║ ║║\n"
376 "╚═╩╝\n"
377)
379DOUBLE_EDGE: Box = Box(
380 "╔═╤╗\n"
381 "║ │║\n"
382 "╟─┼╢\n"
383 "║ │║\n"
384 "╟─┼╢\n"
385 "╟─┼╢\n"
386 "║ │║\n"
387 "╚═╧╝\n"
388)
390MARKDOWN: Box = Box(
391 " \n"
392 "| ||\n"
393 "|-||\n"
394 "| ||\n"
395 "|-||\n"
396 "|-||\n"
397 "| ||\n"
398 " \n",
399 ascii=True,
400)
401# fmt: on
403# Map Boxes that don't render with raster fonts on to equivalent that do
404LEGACY_WINDOWS_SUBSTITUTIONS = {
405 ROUNDED: SQUARE,
406 MINIMAL_HEAVY_HEAD: MINIMAL,
407 SIMPLE_HEAVY: SIMPLE,
408 HEAVY: SQUARE,
409 HEAVY_EDGE: SQUARE,
410 HEAVY_HEAD: SQUARE,
411}
413# Map headed boxes to their headerless equivalents
414PLAIN_HEADED_SUBSTITUTIONS = {
415 HEAVY_HEAD: SQUARE,
416 SQUARE_DOUBLE_HEAD: SQUARE,
417 MINIMAL_DOUBLE_HEAD: MINIMAL,
418 MINIMAL_HEAVY_HEAD: MINIMAL,
419 ASCII_DOUBLE_HEAD: ASCII2,
420}
423if __name__ == "__main__": # pragma: no cover
424 from rich.columns import Columns
425 from rich.panel import Panel
427 from . import box as box
428 from .console import Console
429 from .table import Table
430 from .text import Text
432 console = Console(record=True)
434 BOXES = [
435 "ASCII",
436 "ASCII2",
437 "ASCII_DOUBLE_HEAD",
438 "SQUARE",
439 "SQUARE_DOUBLE_HEAD",
440 "MINIMAL",
441 "MINIMAL_HEAVY_HEAD",
442 "MINIMAL_DOUBLE_HEAD",
443 "SIMPLE",
444 "SIMPLE_HEAD",
445 "SIMPLE_HEAVY",
446 "HORIZONTALS",
447 "ROUNDED",
448 "HEAVY",
449 "HEAVY_EDGE",
450 "HEAVY_HEAD",
451 "DOUBLE",
452 "DOUBLE_EDGE",
453 "MARKDOWN",
454 ]
456 console.print(Panel("[bold green]Box Constants", style="green"), justify="center")
457 console.print()
459 columns = Columns(expand=True, padding=2)
460 for box_name in sorted(BOXES):
461 table = Table(
462 show_footer=True, style="dim", border_style="not dim", expand=True
463 )
464 table.add_column("Header 1", "Footer 1")
465 table.add_column("Header 2", "Footer 2")
466 table.add_row("Cell", "Cell")
467 table.add_row("Cell", "Cell")
468 table.box = getattr(box, box_name)
469 table.title = Text(f"box.{box_name}", style="magenta")
470 columns.add_renderable(table)
471 console.print(columns)
473 # console.save_svg("box.svg")