tidy, remove deprecated code

This commit is contained in:
Will McGugan
2022-03-24 14:44:08 +00:00
parent d64abeed2b
commit 73a90083e4
4 changed files with 28 additions and 137 deletions

View File

@@ -194,20 +194,15 @@ class Border:
box1, box2, box3 = get_box(top, style, outer_style, top_style)[0]
if has_left:
yield box1 if top == left else _Segment(" ", box2.style)
# yield _Segment(box1 if top == left else " ", top_style)
yield _Segment(box2.text * width, box2.style)
# yield _Segment(box2 * width, top_style)
if has_right:
yield box3 if top == left else _Segment(" ", box3.style)
# yield _Segment(box3 if top == right else " ", top_style)
yield new_line
left_segment = get_box(left, style, outer_style, left_style)[1][0]
_right_segment = get_box(right, style, outer_style, right_style)[1][2]
right_segment = _Segment(_right_segment.text + "\n", _right_segment.style)
# box_right = BOX[right][1][2]
# left_segment = _Segment(box_left, left_style)
# right_segment = _Segment(box_right + "\n", right_style)
if has_left and has_right:
for line in lines:
yield left_segment
@@ -231,12 +226,9 @@ class Border:
box1, box2, box3 = get_box(top, style, outer_style, bottom_style)[2]
if has_left:
yield box1 if bottom == left else _Segment(" ", box1.style)
# yield _Segment(box1 if bottom == left else " ", bottom_style)
# yield _Segment(box2 * width, bottom_style)
yield _Segment(box2.text * width, box2.style)
if has_right:
yield box3 if bottom == right else _Segment(" ", box3.style)
# yield _Segment(box3 if bottom == right else " ", bottom_style)
yield new_line

View File

@@ -1,116 +0,0 @@
from __future__ import annotations
import sys
if sys.version_info >= (3, 8):
from typing import Literal
else:
from typing_extensions import Literal
from rich.color import Color
from rich.console import Console, ConsoleOptions, RenderResult, RenderableType
from rich.segment import Segment
from rich.style import Style
BOX_STYLES: dict[str, tuple[str, str, str]] = {
"": (" ", " ", " "),
"rounded": ("╭─╮", "│ │", "╰─╯"),
"solid": ("┌─┐", "│ │", "└─┘"),
"double": ("╔═╗", "║ ║", "╚═╝"),
"dashed": ("┏╍┓", "╏ ╏", "┗╍┛"),
"heavy": ("┏━┓", "┃ ┃", "┗━┛"),
"inner": ("▗▄▖", "▐ ▌", "▝▀▘"),
"outer": ("▛▀▜", "▌ ▐", "▙▄▟"),
}
BoxType = Literal["", "rounded", "solid", "double", "dashed", "heavy", "inner", "outer"]
class Box:
def __init__(
self,
renderable: RenderableType,
*,
sides: tuple[str, str, str, str],
colors: tuple[Color, Color, Color, Color],
):
self.renderable = renderable
self.sides = sides
self.colors = colors
@property
def styles(self) -> tuple[Style, Style, Style, Style]:
color1, color2, color3, color4 = self.colors
from_color = Style.from_color
return (
from_color(color1),
from_color(color2),
from_color(color3),
from_color(color4),
)
def __rich_console__(
self, console: "Console", options: "ConsoleOptions"
) -> "RenderResult":
width = options.max_width
top, right, bottom, left = (
side if side != "none" else "" for side in self.sides
)
top_style, right_style, bottom_style, left_style = map(
console.get_style, self.styles
)
BOX = BOX_STYLES
renderable = self.renderable
render_width = width - bool(left) - bool(right)
lines = console.render_lines(renderable, options.update_width(render_width))
new_line = Segment.line()
if top != "none":
char_left, char_mid, char_right = iter(BOX[top][0])
row = f"{char_left if left else ''}{char_mid * render_width}{char_right if right else ''}"
yield Segment(row, top_style)
yield new_line
if not left and not right:
for line in lines:
yield from line
yield new_line
elif left and right:
left_segment = Segment(BOX[left][1][0], left_style)
right_segment = Segment(BOX[right][1][2] + "\n", right_style)
for line in lines:
yield left_segment
yield from line
yield right_segment
elif left:
left_segment = Segment(BOX[left][1][0], left_style)
for line in lines:
yield left_segment
yield from line
yield new_line
elif right:
right_segment = Segment(BOX[right][1][2] + "\n", right_style)
for line in lines:
yield from line
yield right_segment
if bottom:
char_left, char_mid, char_right = iter(BOX[bottom][2])
row = f"{char_left if left else ''}{char_mid * render_width}{char_right if right else ''}"
yield Segment(row, bottom_style)
yield new_line
if __name__ == "__main__":
from rich import print
green = Color.parse("green")
box = Box(
"foo",
sides=("rounded", "rounded", "rounded", "rounded"),
colors=(green, green, green, green),
)
print(box)

View File

@@ -35,10 +35,11 @@ if TYPE_CHECKING:
from .styles import Styles, StylesBase
from .styles import DockGroup
from .._box import BoxType
from .types import EdgeType
BorderDefinition = (
"Sequence[tuple[BoxType, str | Color] | None] | tuple[BoxType, str | Color]"
"Sequence[tuple[EdgeType, str | Color] | None] | tuple[EdgeType, str | Color]"
)
@@ -125,7 +126,7 @@ class BoxProperty:
def __get__(
self, obj: StylesBase, objtype: type[StylesBase] | None = None
) -> tuple[BoxType, Color]:
) -> tuple[EdgeType, Color]:
"""Get the box property
Args:
@@ -133,18 +134,18 @@ class BoxProperty:
objtype (type[Styles]): The ``Styles`` class
Returns:
A ``tuple[BoxType, Style]`` containing the string type of the box and
A ``tuple[EdgeType, Style]`` containing the string type of the box and
it's style. Example types are "rounded", "solid", and "dashed".
"""
box_type, color = obj.get_rule(self.name) or self.DEFAULT
return (box_type, color)
def __set__(self, obj: Styles, border: tuple[BoxType, str | Color] | None):
def __set__(self, obj: Styles, border: tuple[EdgeType, str | Color] | None):
"""Set the box property
Args:
obj (Styles): The ``Styles`` object.
value (tuple[BoxType, str | Color | Style], optional): A 2-tuple containing the type of box to use,
value (tuple[EdgeType, str | Color | Style], optional): A 2-tuple containing the type of box to use,
e.g. "dashed", and the ``Style`` to be used. You can supply the ``Style`` directly, or pass a
``str`` (e.g. ``"blue on #f0f0f0"`` ) or ``Color`` instead.
@@ -169,10 +170,10 @@ class BoxProperty:
class Edges(NamedTuple):
"""Stores edges for border / outline."""
top: tuple[BoxType, Color]
right: tuple[BoxType, Color]
bottom: tuple[BoxType, Color]
left: tuple[BoxType, Color]
top: tuple[EdgeType, Color]
right: tuple[EdgeType, Color]
bottom: tuple[EdgeType, Color]
left: tuple[EdgeType, Color]
def __bool__(self) -> bool:
(top, _), (right, _), (bottom, _), (left, _) = self
@@ -248,8 +249,8 @@ class BorderProperty:
Args:
obj (Styles): The ``Styles`` object.
border (Sequence[tuple[BoxType, str | Color | Style] | None] | tuple[BoxType, str | Color | Style] | None):
A ``tuple[BoxType, str | Color | Style]`` representing the type of box to use and the ``Style`` to apply
border (Sequence[tuple[EdgeType, str | Color | Style] | None] | tuple[EdgeType, str | Color | Style] | None):
A ``tuple[EdgeType, str | Color | Style]`` representing the type of box to use and the ``Style`` to apply
to the box.
Alternatively, you can supply a sequence of these tuples and they will be applied per-edge.
If the sequence is of length 1, all edges will be decorated according to the single element.

View File

@@ -13,6 +13,20 @@ else:
Edge = Literal["top", "right", "bottom", "left"]
EdgeType = Literal[
"none",
"round",
"solid",
"double",
"dashed",
"heavy",
"inner",
"outer",
"hkey",
"vkey",
"tall",
"wide",
]
Visibility = Literal["visible", "hidden", "initial", "inherit"]
Display = Literal["block", "none"]
BoxSizing = Literal["border-box", "content-box"]