box substitution

This commit is contained in:
Will McGugan 2020-06-27 22:50:31 +01:00
parent cfeb34743c
commit f4e5c7cdfc
3 changed files with 35 additions and 38 deletions

View File

@ -1,4 +1,4 @@
from typing import Iterable, List
from typing import Iterable, List, Optional
from typing_extensions import Literal
from ._loop import loop_last
@ -143,24 +143,6 @@ class Box:
return "".join(parts)
class PlatformDefaultBox(Box):
"""A sentinel to be replaced with an appropriate box for the platform."""
PLATFORM_DEFAULT: Box = PlatformDefaultBox(
"""\
+--+
| ||
|-+|
| ||
|-+|
|-+|
| ||
+--+
"""
)
ASCII: Box = Box(
"""\
+--+
@ -358,6 +340,14 @@ LEGACY_WINDOWS_SUBSTITUTIONS = {
}
def get_safe_box(box: Optional[Box], legacy_windows: bool) -> Box:
"""Substitute Box constants that don't render on windows legacy."""
if legacy_windows:
return LEGACY_WINDOWS_SUBSTITUTIONS.get(box, box) if box else None
else:
return box
if __name__ == "__main__": # pragma: no cover
from rich.columns import Columns
@ -389,9 +379,11 @@ if __name__ == "__main__": # pragma: no cover
console.print(Panel("[bold green]Box Constants", style="green"), justify="center")
console.print()
columns = Columns(expand=False, padding=2)
columns = Columns(expand=True, padding=2)
for box_name in BOXES:
table = Table(width=80, show_footer=True, style="dim", border_style="not dim")
table = Table(
width=80, show_footer=True, style="dim", border_style="not dim", expand=True
)
spaces = " " * 10
table.add_column("Header 1", "Footer 1")
table.add_column("Header 2", "Footer 2")
@ -403,4 +395,3 @@ if __name__ == "__main__": # pragma: no cover
console.print(columns)
# console.save_html("box.html", inline_styles=True)

View File

@ -1,6 +1,6 @@
from typing import Optional, Union
from .box import Box, SQUARE, ROUNDED
from .box import get_safe_box, Box, SQUARE, ROUNDED
from .console import (
Console,
@ -36,11 +36,12 @@ class Panel(JupyterMixin):
def __init__(
self,
renderable: RenderableType,
box: Box = None,
box: Box = ROUNDED,
expand: bool = True,
style: Union[str, Style] = "none",
width: Optional[int] = None,
padding: PaddingDimensions = 0,
safe_box: bool = True,
) -> None:
self.renderable = renderable
self.box = box
@ -48,6 +49,7 @@ class Panel(JupyterMixin):
self.style = style
self.width = width
self.padding = padding
self.safe_box = safe_box
def __rich_console__(
self, console: Console, options: ConsoleOptions
@ -70,7 +72,12 @@ class Panel(JupyterMixin):
width = child_width + 2
child_options = options.update(width=child_width)
lines = console.render_lines(renderable, child_options)
box = SQUARE if console.legacy_windows else (self.box or ROUNDED)
print(repr(self.box))
box = (
get_safe_box(self.box, console.legacy_windows)
if self.safe_box
else self.box
)
line_start = Segment(box.mid_left, style)
line_end = Segment(f"{box.mid_right}\n", style)
yield Segment(box.get_top([width - 2]), style)
@ -100,6 +107,7 @@ if __name__ == "__main__": # pragma: no cover
Padding(Text.from_markup("[bold magenta]Hello World!"), (1, 8)),
box=ROUNDED,
expand=False,
safe_box=True,
)
)

View File

@ -96,7 +96,7 @@ class Table(JupyterMixin):
title (Union[str, Text], optional): The title of the table rendered at the top. Defaults to None.
caption (Union[str, Text], optional): The table caption rendered below. Defaults to None.
width (int, optional): The width in characters of the table, or ``None`` to automatically fit. Defaults to None.
box (Optional[box.Box], optional): One of the constants in box.py used to draw the edges (See :ref:`appendix_box`). Defaults to box.HEAVY_HEAD.
box (box.Box, optional): One of the constants in box.py used to draw the edges (See :ref:`appendix_box`). Defaults to box.HEAVY_HEAD.
padding (PaddingDimensions, optional): Padding for cells (top, right, bottom, left). Defaults to (0, 1).
collapse_padding (bool, optional): Enable collapsing of padding around cells. Defaults to False.
pad_edge (bool, optional): Enable padding of edge cells. Defaults to True.
@ -123,7 +123,7 @@ class Table(JupyterMixin):
title: TextType = None,
caption: TextType = None,
width: int = None,
box: Optional[box.Box] = box.PLATFORM_DEFAULT,
box: Optional[box.Box] = box.HEAVY_HEAD,
padding: PaddingDimensions = (0, 1),
collapse_padding: bool = False,
pad_edge: bool = True,
@ -226,15 +226,16 @@ class Table(JupyterMixin):
max_width -= 2
extra_width = self._extra_width
table_width = (
sum(self._calculate_column_widths(console, max_width)) + extra_width
)
_measure_column = self._measure_column
minimum_width = max(
minimum_width = sum(
_measure_column(console, column, max_width).minimum
for column in self.columns
)
return Measurement(minimum_width, table_width)
maximum_width = sum(
_measure_column(console, column, max_width).maximum
for column in self.columns
)
return Measurement(minimum_width + extra_width, maximum_width + extra_width)
@property
def padding(self) -> Tuple[int, int, int, int]:
@ -489,7 +490,7 @@ class Table(JupyterMixin):
_, pad_right, _, pad_left = self.padding
if self.collapse_padding:
if column_index > 0:
pad_left = max(0, pad_right - pad_left)
pad_left = max(0, pad_left - pad_right)
return pad_left + pad_right
def _measure_column(
@ -534,14 +535,11 @@ class Table(JupyterMixin):
)
)
)
_box = (
(box.SQUARE if console.legacy_windows else box.HEAVY_HEAD)
if isinstance(self.box, box.PlatformDefaultBox)
box.get_safe_box(self.box, console.legacy_windows)
if self.safe_box
else self.box
)
if self.safe_box and _box is not None and console.legacy_windows:
_box = box.LEGACY_WINDOWS_SUBSTITUTIONS.get(_box, _box)
# _box = self.box
new_line = Segment.line()