mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
respect ansi theme
This commit is contained in:
@@ -40,6 +40,7 @@ import rich.repr
|
||||
from rich.color import Color as RichColor
|
||||
from rich.color import ColorType
|
||||
from rich.color_triplet import ColorTriplet
|
||||
from rich.terminal_theme import TerminalTheme
|
||||
from typing_extensions import Final
|
||||
|
||||
from textual._color_constants import ANSI_COLORS, COLOR_NAME_TO_RGB
|
||||
@@ -176,7 +177,9 @@ class Color(NamedTuple):
|
||||
return cls(0, 0, 0, alpha_percentage / 100.0, auto=True)
|
||||
|
||||
@classmethod
|
||||
def from_rich_color(cls, rich_color: RichColor | None) -> Color:
|
||||
def from_rich_color(
|
||||
cls, rich_color: RichColor | None, theme: TerminalTheme | None = None
|
||||
) -> Color:
|
||||
"""Create a new color from Rich's Color class.
|
||||
|
||||
Args:
|
||||
@@ -187,7 +190,7 @@ class Color(NamedTuple):
|
||||
"""
|
||||
if rich_color is None:
|
||||
return TRANSPARENT
|
||||
r, g, b = rich_color.get_truecolor()
|
||||
r, g, b = rich_color.get_truecolor(theme)
|
||||
return cls(r, g, b)
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -182,11 +182,16 @@ class Content(Visual):
|
||||
"""
|
||||
if isinstance(text, str):
|
||||
text = Text.from_markup(text)
|
||||
app = active_app.get()
|
||||
spans = [
|
||||
Span(
|
||||
start,
|
||||
end,
|
||||
style if isinstance(style, str) else Style.from_rich_style(style),
|
||||
(
|
||||
style
|
||||
if isinstance(style, str)
|
||||
else Style.from_rich_style(style, app.ansi_theme)
|
||||
),
|
||||
)
|
||||
for start, end, style in text._spans
|
||||
]
|
||||
@@ -683,12 +688,12 @@ class Content(Visual):
|
||||
return
|
||||
|
||||
if parse_style is None:
|
||||
console = active_app.get().console
|
||||
app = active_app.get()
|
||||
# TODO: Update when we add Content.from_markup
|
||||
|
||||
def get_style(style: str, /) -> Style:
|
||||
return (
|
||||
Style.from_rich_style(console.get_style(style))
|
||||
Style.from_rich_style(app.console.get_style(style), app.ansi_theme)
|
||||
if isinstance(style, str)
|
||||
else style
|
||||
)
|
||||
|
||||
@@ -13,6 +13,7 @@ from rich.measure import Measurement
|
||||
from rich.protocol import is_renderable, rich_cast
|
||||
from rich.segment import Segment
|
||||
from rich.style import Style as RichStyle
|
||||
from rich.terminal_theme import TerminalTheme
|
||||
from rich.text import Text
|
||||
|
||||
from textual._context import active_app
|
||||
@@ -141,7 +142,9 @@ class Style:
|
||||
return new_style
|
||||
|
||||
@classmethod
|
||||
def from_rich_style(cls, rich_style: RichStyle) -> Style:
|
||||
def from_rich_style(
|
||||
cls, rich_style: RichStyle, theme: TerminalTheme | None = None
|
||||
) -> Style:
|
||||
"""Build a Style from a (Rich) Style.
|
||||
|
||||
Args:
|
||||
@@ -151,8 +154,8 @@ class Style:
|
||||
New Style.
|
||||
"""
|
||||
return Style(
|
||||
Color.from_rich_color(rich_style.bgcolor),
|
||||
Color.from_rich_color(rich_style.color),
|
||||
Color.from_rich_color(rich_style.bgcolor, theme),
|
||||
Color.from_rich_color(rich_style.color, theme),
|
||||
bold=rich_style.bold,
|
||||
dim=rich_style.dim,
|
||||
italic=rich_style.italic,
|
||||
|
||||
Reference in New Issue
Block a user