mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Use render instead of render_lines in Opacity, add tests
This commit is contained in:
@@ -1,13 +1,9 @@
|
|||||||
import functools
|
import functools
|
||||||
from time import sleep
|
|
||||||
|
|
||||||
from rich.color import Color
|
from rich.color import Color
|
||||||
from rich.console import ConsoleOptions, Console, RenderResult, RenderableType
|
from rich.console import ConsoleOptions, Console, RenderResult, RenderableType
|
||||||
from rich.live import Live
|
|
||||||
from rich.panel import Panel
|
|
||||||
from rich.segment import Segment
|
from rich.segment import Segment
|
||||||
from rich.style import Style
|
from rich.style import Style
|
||||||
from rich.text import Text
|
|
||||||
|
|
||||||
from textual.renderables.utilities import blend_colors
|
from textual.renderables.utilities import blend_colors
|
||||||
|
|
||||||
@@ -27,10 +23,9 @@ class Opacity:
|
|||||||
def __rich_console__(
|
def __rich_console__(
|
||||||
self, console: Console, options: ConsoleOptions
|
self, console: Console, options: ConsoleOptions
|
||||||
) -> RenderResult:
|
) -> RenderResult:
|
||||||
lines = console.render_lines(self.renderable, options)
|
segments = console.render(self.renderable, options)
|
||||||
opacity = self.value
|
opacity = self.value
|
||||||
for line in lines:
|
for segment in segments:
|
||||||
for segment in line:
|
|
||||||
style = segment.style
|
style = segment.style
|
||||||
if not style:
|
if not style:
|
||||||
yield segment
|
yield segment
|
||||||
@@ -46,7 +41,6 @@ class Opacity:
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
yield segment
|
yield segment
|
||||||
yield ""
|
|
||||||
|
|
||||||
|
|
||||||
@functools.lru_cache(maxsize=1024)
|
@functools.lru_cache(maxsize=1024)
|
||||||
@@ -60,6 +54,12 @@ def _get_blended_style_cached(
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
from rich.live import Live
|
||||||
|
from rich.panel import Panel
|
||||||
|
from rich.text import Text
|
||||||
|
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
console = Console()
|
console = Console()
|
||||||
|
|
||||||
panel = Panel.fit(
|
panel = Panel.fit(
|
||||||
|
|||||||
@@ -4,28 +4,47 @@ from rich.text import Text
|
|||||||
from tests.utilities.render import render
|
from tests.utilities.render import render
|
||||||
from textual.renderables.opacity import Opacity
|
from textual.renderables.opacity import Opacity
|
||||||
|
|
||||||
|
STOP = "\x1b[0m"
|
||||||
@pytest.mark.skip
|
|
||||||
def test_simple_opacity():
|
|
||||||
text = Text("Hello, world!", style="#ff0000 on #00ff00")
|
|
||||||
assert render(Opacity(text, value=.5)) == ""
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skip
|
@pytest.fixture
|
||||||
def test_ansi_colors_ignored():
|
def text():
|
||||||
pass
|
return Text("Hello, world!", style="#ff0000 on #00ff00", end="")
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skip
|
def test_simple_opacity(text):
|
||||||
def test_opacity_no_style():
|
blended_red_on_green = "\x1b[38;2;127;127;0;48;2;0;255;0m"
|
||||||
pass
|
assert render(Opacity(text, value=.5)) == (
|
||||||
|
f"{blended_red_on_green}Hello, world!{STOP}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skip
|
def test_value_zero_sets_foreground_color_to_background_color(text):
|
||||||
def test_opacity_only_fg():
|
foreground = background = "0;255;0"
|
||||||
pass
|
assert render(Opacity(text, value=0)) == (
|
||||||
|
f"\x1b[38;2;{foreground};48;2;{background}mHello, world!{STOP}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skip
|
def test_opacity_value_of_one_noop(text):
|
||||||
def test_opacity_only_bg():
|
assert render(Opacity(text, value=1)) == render(text)
|
||||||
pass
|
|
||||||
|
|
||||||
|
def test_ansi_colors_noop():
|
||||||
|
ansi_colored_text = Text("Hello, world!", style="red on green", end="")
|
||||||
|
assert render(Opacity(ansi_colored_text, value=.5)) == render(ansi_colored_text)
|
||||||
|
|
||||||
|
|
||||||
|
def test_opacity_no_style_noop():
|
||||||
|
text_no_style = Text("Hello, world!", end="")
|
||||||
|
assert render(Opacity(text_no_style, value=.2)) == render(text_no_style)
|
||||||
|
|
||||||
|
|
||||||
|
def test_opacity_only_fg_noop():
|
||||||
|
text_only_fg = Text("Hello, world!", style="#ff0000", end="")
|
||||||
|
assert render(Opacity(text_only_fg, value=.5)) == render(text_only_fg)
|
||||||
|
|
||||||
|
|
||||||
|
def test_opacity_only_bg_noop():
|
||||||
|
text_only_bg = Text("Hello, world!", style="on #ff0000", end="")
|
||||||
|
assert render(Opacity(text_only_bg, value=.5)) == render(text_only_bg)
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ def render(renderable: RenderableType, no_wrap: bool = False) -> str:
|
|||||||
console = Console(
|
console = Console(
|
||||||
width=100, file=io.StringIO(), color_system="truecolor", legacy_windows=False
|
width=100, file=io.StringIO(), color_system="truecolor", legacy_windows=False
|
||||||
)
|
)
|
||||||
console.print(renderable, no_wrap=no_wrap)
|
with console.capture() as capture:
|
||||||
output = replace_link_ids(console.file.getvalue())
|
console.print(renderable, no_wrap=no_wrap, end="")
|
||||||
|
output = replace_link_ids(capture.get())
|
||||||
return output
|
return output
|
||||||
|
|||||||
Reference in New Issue
Block a user