mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Using console.get_style, adding test ensuring style applies
This commit is contained in:
@@ -2,7 +2,7 @@ from __future__ import annotations
|
||||
|
||||
from rich.console import ConsoleOptions, Console, RenderResult
|
||||
from rich.segment import Segment
|
||||
from rich.style import Style, StyleType
|
||||
from rich.style import StyleType
|
||||
|
||||
|
||||
class UnderlineBar:
|
||||
@@ -23,19 +23,16 @@ class UnderlineBar:
|
||||
width: int | None = None,
|
||||
) -> None:
|
||||
self.highlight_range = highlight_range
|
||||
if isinstance(highlight_style, str):
|
||||
self.highlight_style = Style.parse(highlight_style)
|
||||
else:
|
||||
self.highlight_style = highlight_style
|
||||
if isinstance(background_style, str):
|
||||
self.background_style = Style.parse(background_style)
|
||||
else:
|
||||
self.background_style = background_style
|
||||
self.highlight_style = highlight_style
|
||||
self.background_style = background_style
|
||||
self.width = width
|
||||
|
||||
def __rich_console__(
|
||||
self, console: Console, options: ConsoleOptions
|
||||
) -> RenderResult:
|
||||
highlight_style = console.get_style(self.highlight_style)
|
||||
background_style = console.get_style(self.background_style)
|
||||
|
||||
half_bar_right = "╸"
|
||||
half_bar_left = "╺"
|
||||
bar = "━"
|
||||
@@ -47,7 +44,7 @@ class UnderlineBar:
|
||||
end = min(end, width)
|
||||
|
||||
if start == end == 0 or end < 0 or start > end:
|
||||
yield Segment(bar * width, style=self.background_style)
|
||||
yield Segment(bar * width, style=background_style)
|
||||
return
|
||||
|
||||
# Round start and end to nearest half
|
||||
@@ -59,25 +56,23 @@ class UnderlineBar:
|
||||
half_end = end - int(end) > 0
|
||||
|
||||
# Initial non-highlighted portion of bar
|
||||
yield Segment(bar * (int(start - 0.5)), style=self.background_style)
|
||||
yield Segment(bar * (int(start - 0.5)), style=background_style)
|
||||
if not half_start and start > 0:
|
||||
yield Segment(half_bar_right, style=self.background_style)
|
||||
yield Segment(half_bar_right, style=background_style)
|
||||
|
||||
# The highlighted portion
|
||||
bar_width = int(end) - int(start)
|
||||
if half_start:
|
||||
yield Segment(
|
||||
half_bar_left + bar * (bar_width - 1), style=self.highlight_style
|
||||
)
|
||||
yield Segment(half_bar_left + bar * (bar_width - 1), style=highlight_style)
|
||||
else:
|
||||
yield Segment(bar * bar_width, style=self.highlight_style)
|
||||
yield Segment(bar * bar_width, style=highlight_style)
|
||||
if half_end:
|
||||
yield Segment(half_bar_right, style=self.highlight_style)
|
||||
yield Segment(half_bar_right, style=highlight_style)
|
||||
|
||||
# The non-highlighted tail
|
||||
if not half_end and end - width != 0:
|
||||
yield Segment(half_bar_left, style=self.background_style)
|
||||
yield Segment(bar * (int(width) - int(end) - 1), style=self.background_style)
|
||||
yield Segment(half_bar_left, style=background_style)
|
||||
yield Segment(bar * (int(width) - int(end) - 1), style=background_style)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
from rich.style import Style
|
||||
|
||||
from tests.utilities.render import render
|
||||
from textual.renderables.underline_bar import UnderlineBar
|
||||
|
||||
MAGENTA = "\x1b[35m"
|
||||
GREY = "\x1b[38;5;59m"
|
||||
STOP = "\x1b[0m"
|
||||
GREEN = "\x1b[32m"
|
||||
RED = "\x1b[31m"
|
||||
|
||||
|
||||
def test_no_highlight():
|
||||
@@ -110,13 +110,17 @@ def test_highlight_full_range_out_of_bounds_start():
|
||||
assert render(bar) == f"{GREY}━━━━━━{STOP}"
|
||||
|
||||
|
||||
def test_init_with_str_style():
|
||||
bar = UnderlineBar(background_style="green", highlight_style="yellow")
|
||||
assert bar.background_style == Style(color="green")
|
||||
assert bar.highlight_style == Style(color="yellow")
|
||||
|
||||
|
||||
def test_init_with_object_style():
|
||||
bar = UnderlineBar(background_style=Style(color="green"), highlight_style=Style(color="yellow"))
|
||||
assert bar.background_style == Style(color="green")
|
||||
assert bar.highlight_style == Style(color="yellow")
|
||||
def test_custom_styles():
|
||||
bar = UnderlineBar(
|
||||
highlight_range=(2, 4),
|
||||
highlight_style="red",
|
||||
background_style="green",
|
||||
width=6
|
||||
)
|
||||
assert render(bar) == (
|
||||
f"{GREEN}━{STOP}"
|
||||
f"{GREEN}╸{STOP}"
|
||||
f"{RED}━━{STOP}"
|
||||
f"{GREEN}╺{STOP}"
|
||||
f"{GREEN}━{STOP}"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user