mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
fix dim
This commit is contained in:
5
docs/api/content.md
Normal file
5
docs/api/content.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
title: "textual.content"
|
||||
---
|
||||
|
||||
::: textual.content
|
||||
5
docs/api/style.md
Normal file
5
docs/api/style.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
title: "textual.style"
|
||||
---
|
||||
|
||||
::: textual.style
|
||||
@@ -319,4 +319,3 @@ Textual supports Rich renderables, which means you can return any object that wo
|
||||
|
||||
The Content class is generally preferred, as it supports more of Textual's features.
|
||||
If you already have a Text object and your code is working, there is no need to change it -- Textual won't be dropping Rich support.
|
||||
But we recommend the [Content class](#content-class) for newer code.
|
||||
|
||||
@@ -1,125 +0,0 @@
|
||||
from textual._context import active_app
|
||||
from textual.color import Color
|
||||
from textual.css.parse import substitute_references
|
||||
from textual.css.tokenize import tokenize_style, tokenize_values
|
||||
from textual.style import Style
|
||||
|
||||
STYLES = {"bold", "dim", "italic", "underline", "reverse", "strike"}
|
||||
STYLE_ABBREVIATIONS = {
|
||||
"b": "bold",
|
||||
"d": "dim",
|
||||
"i": "italic",
|
||||
"u": "underline",
|
||||
"r": "reverse",
|
||||
"s": "strike",
|
||||
}
|
||||
|
||||
|
||||
def style_parse(style_text: str, variables: dict[str, str] | None) -> Style:
|
||||
"""Parse a Textual style definition.
|
||||
|
||||
Note that variables should be `None` when called within a running app. This is so that
|
||||
this method can use a style cache from the app. Supply a variables dict only for testing.
|
||||
|
||||
Args:
|
||||
style_text: String containing style definition.
|
||||
variables: Variables to use, or `None` for variables from active app.
|
||||
|
||||
Returns:
|
||||
Style instance.
|
||||
"""
|
||||
styles: dict[str, bool | None] = {style: None for style in STYLES}
|
||||
|
||||
color: Color | None = None
|
||||
background: Color | None = None
|
||||
is_background: bool = False
|
||||
style_state: bool = True
|
||||
|
||||
data: dict[str, str] = {}
|
||||
meta: dict[str, object] = {}
|
||||
|
||||
if variables is None:
|
||||
try:
|
||||
app = active_app.get()
|
||||
except LookupError:
|
||||
reference_tokens = {}
|
||||
else:
|
||||
reference_tokens = app.stylesheet._variable_tokens
|
||||
else:
|
||||
reference_tokens = tokenize_values(variables)
|
||||
|
||||
for token in substitute_references(
|
||||
tokenize_style(
|
||||
style_text,
|
||||
read_from=("inline style", ""),
|
||||
),
|
||||
reference_tokens,
|
||||
):
|
||||
name = token.name
|
||||
value = token.value
|
||||
|
||||
if name == "color":
|
||||
token_color = Color.parse(value)
|
||||
if is_background:
|
||||
background = token_color
|
||||
else:
|
||||
color = token_color
|
||||
elif name == "token":
|
||||
if value == "not":
|
||||
style_state = False
|
||||
elif value == "auto":
|
||||
if is_background:
|
||||
background = Color.automatic()
|
||||
else:
|
||||
color = Color.automatic()
|
||||
elif value == "on":
|
||||
is_background = True
|
||||
elif value == "link":
|
||||
data["link"] = style_text[token.end[-1] :]
|
||||
break
|
||||
elif value in STYLES:
|
||||
styles[value] = style_state
|
||||
style_state = True
|
||||
elif value in STYLE_ABBREVIATIONS:
|
||||
styles[STYLE_ABBREVIATIONS[value]] = style_state
|
||||
style_state = True
|
||||
else:
|
||||
if is_background:
|
||||
background = Color.parse(value)
|
||||
else:
|
||||
color = Color.parse(value)
|
||||
elif name in ("key_value", "key_value_quote", "key_value_double_quote"):
|
||||
key, _, value = value.partition("=")
|
||||
if name in ("key_value_quote", "key_value_double_quote"):
|
||||
value = value[1:-1]
|
||||
if key.startswith("@"):
|
||||
meta[key] = value
|
||||
else:
|
||||
data[key] = value
|
||||
elif name == "percent" or (name == "scalar" and value.endswith("%")):
|
||||
percent = int(value.rstrip("%")) / 100.0
|
||||
if is_background:
|
||||
if background is not None:
|
||||
background = background.multiply_alpha(percent)
|
||||
else:
|
||||
if color is not None:
|
||||
color = color.multiply_alpha(percent)
|
||||
|
||||
parsed_style = Style(background, color, link=data.get("link", None), **styles)
|
||||
if meta:
|
||||
parsed_style += Style.from_meta(meta)
|
||||
return parsed_style
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
variables = {"accent": "red"}
|
||||
print(style_parse("blue 20%", None))
|
||||
|
||||
# print(
|
||||
# style_parse(
|
||||
# "bold not underline red $accent rgba(10,20,30,3) on black not italic link=https://www.willmcgugan.com",
|
||||
# variables,
|
||||
# )
|
||||
# )
|
||||
|
||||
# print(style_parse("auto on ansi_red s 20% @click=app.bell('hello')", variables))
|
||||
@@ -153,6 +153,7 @@ class StylesCache:
|
||||
opacity=widget.opacity,
|
||||
ansi_theme=widget.app.ansi_theme,
|
||||
)
|
||||
|
||||
if widget.auto_links:
|
||||
hover_style = widget.hover_style
|
||||
if (
|
||||
|
||||
@@ -12,7 +12,7 @@ from __future__ import annotations
|
||||
import re
|
||||
from functools import lru_cache, total_ordering
|
||||
from operator import itemgetter
|
||||
from typing import TYPE_CHECKING, Callable, Iterable, NamedTuple, Sequence, Union
|
||||
from typing import Callable, Iterable, NamedTuple, Sequence, Union
|
||||
|
||||
import rich.repr
|
||||
from rich._wrap import divide_line
|
||||
@@ -34,9 +34,7 @@ from textual.strip import Strip
|
||||
from textual.style import Style
|
||||
from textual.visual import RulesMap, Visual
|
||||
|
||||
if TYPE_CHECKING:
|
||||
pass
|
||||
|
||||
__all__ = ["ContentType", "Content", "Span"]
|
||||
|
||||
ContentType: TypeAlias = Union["Content", str]
|
||||
"""Type alias used where content and a str are interchangeable in a function."""
|
||||
@@ -959,7 +957,9 @@ class Content(Visual):
|
||||
if end:
|
||||
yield end, base_style
|
||||
|
||||
def render_segments(self, base_style: Style, end: str = "") -> list[Segment]:
|
||||
def render_segments(
|
||||
self, base_style: Style = Style.null(), end: str = ""
|
||||
) -> list[Segment]:
|
||||
_Segment = Segment
|
||||
segments = [
|
||||
_Segment(text, (style.rich_style if style else None))
|
||||
|
||||
@@ -14,7 +14,6 @@ from rich.padding import Padding
|
||||
from rich.panel import Panel
|
||||
from rich.text import Text
|
||||
|
||||
from textual._style_parse import style_parse
|
||||
from textual.cache import LRUCache
|
||||
from textual.css.errors import StylesheetError
|
||||
from textual.css.match import _check_selectors
|
||||
@@ -25,6 +24,7 @@ from textual.css.tokenize import Token, tokenize_values
|
||||
from textual.css.tokenizer import TokenError
|
||||
from textual.css.types import CSSLocation, Specificity3, Specificity6
|
||||
from textual.dom import DOMNode
|
||||
from textual.markup import parse_style
|
||||
from textual.style import Style
|
||||
from textual.widget import Widget
|
||||
|
||||
@@ -232,7 +232,7 @@ class Stylesheet:
|
||||
if style_text in self._style_parse_cache:
|
||||
return self._style_parse_cache[style_text]
|
||||
try:
|
||||
style = style_parse(style_text, None)
|
||||
style = parse_style(style_text)
|
||||
except Exception:
|
||||
style = Style.null()
|
||||
self._style_parse_cache[style_text] = style
|
||||
|
||||
@@ -1,3 +1,10 @@
|
||||
"""
|
||||
The Style class contains all the information needed to generate styled terminal output.
|
||||
|
||||
You won't often need to create Style objects directly, if you are using [Content][textual.content.Content] for output.
|
||||
But you might want to use styles for more customized widgets.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
@@ -9,6 +16,7 @@ import rich.repr
|
||||
from rich.style import Style as RichStyle
|
||||
from rich.terminal_theme import TerminalTheme
|
||||
|
||||
from textual._context import active_app
|
||||
from textual.color import Color
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -18,7 +26,11 @@ if TYPE_CHECKING:
|
||||
@rich.repr.auto(angular=True)
|
||||
@dataclass(frozen=True)
|
||||
class Style:
|
||||
"""Represents a style in the Visual interface (color and other attributes)."""
|
||||
"""Represents a style in the Visual interface (color and other attributes).
|
||||
|
||||
Styles may be added together, which combines their style attributes.
|
||||
|
||||
"""
|
||||
|
||||
background: Color | None = None
|
||||
foreground: Color | None = None
|
||||
@@ -68,6 +80,7 @@ class Style:
|
||||
self.background,
|
||||
self.foreground,
|
||||
self.bold,
|
||||
self.dim,
|
||||
self.italic,
|
||||
self.underline,
|
||||
self.reverse,
|
||||
@@ -108,7 +121,7 @@ class Style:
|
||||
|
||||
return " ".join(output)
|
||||
|
||||
@lru_cache(maxsize=1024)
|
||||
@lru_cache(maxsize=1024 * 4)
|
||||
def __add__(self, other: object | None) -> Style:
|
||||
if isinstance(other, Style):
|
||||
new_style = Style(
|
||||
@@ -117,7 +130,6 @@ class Style:
|
||||
if self.background is None
|
||||
else self.background + other.background
|
||||
),
|
||||
# other.foreground if self.foreground is None else None,
|
||||
self.foreground if other.foreground is None else other.foreground,
|
||||
self.bold if other.bold is None else other.bold,
|
||||
self.dim if other.dim is None else other.dim,
|
||||
@@ -147,9 +159,22 @@ class Style:
|
||||
|
||||
@classmethod
|
||||
def parse(cls, text_style: str, variables: dict[str, str] | None = None) -> Style:
|
||||
"""Parse a style from text.
|
||||
|
||||
Args:
|
||||
text_style: A style encoded in a string.
|
||||
variables: Optional mapping of CSS variables. `None` to get variables from the app.
|
||||
|
||||
Returns:
|
||||
_type_: _description_
|
||||
"""
|
||||
from textual.markup import parse_style
|
||||
|
||||
return parse_style(text_style, variables)
|
||||
try:
|
||||
app = active_app.get()
|
||||
except LookupError:
|
||||
return parse_style(text_style, variables)
|
||||
return app.stylesheet.parse_style(text_style)
|
||||
|
||||
@classmethod
|
||||
def from_rich_style(
|
||||
@@ -245,6 +270,17 @@ class Style:
|
||||
)
|
||||
|
||||
def rich_style_with_offset(self, x: int, y: int) -> RichStyle:
|
||||
"""Get a Rich style with the given offset included in meta.
|
||||
|
||||
This is used in text seleciton.
|
||||
|
||||
Args:
|
||||
x: X coordinate.
|
||||
y: Y coordinate.
|
||||
|
||||
Returns:
|
||||
A Rich Style object.
|
||||
"""
|
||||
color = None if self.foreground is None else self.background + self.foreground
|
||||
return RichStyle(
|
||||
color=None if color is None else color.rich_color,
|
||||
@@ -261,7 +297,7 @@ class Style:
|
||||
|
||||
@cached_property
|
||||
def without_color(self) -> Style:
|
||||
"""The style with no color."""
|
||||
"""The style without any colors."""
|
||||
return Style(
|
||||
bold=self.bold,
|
||||
dim=self.dim,
|
||||
|
||||
@@ -3869,6 +3869,7 @@ class Widget(DOMNode):
|
||||
self._render_content()
|
||||
try:
|
||||
line = self._render_cache.lines[y]
|
||||
print(repr(line))
|
||||
except IndexError:
|
||||
line = Strip.blank(self.size.width, self.rich_style)
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ from typing import Literal
|
||||
|
||||
from rich.console import RenderableType
|
||||
|
||||
from textual.visual import SupportsVisual
|
||||
from textual.widgets._static import Static
|
||||
|
||||
LabelVariant = Literal["success", "error", "warning", "primary", "secondary", "accent"]
|
||||
@@ -49,7 +50,7 @@ class Label(Static):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
renderable: RenderableType = "",
|
||||
renderable: RenderableType | SupportsVisual = "",
|
||||
*,
|
||||
variant: LabelVariant | None = None,
|
||||
expand: bool = False,
|
||||
|
||||
@@ -4,7 +4,6 @@ from typing import TYPE_CHECKING
|
||||
|
||||
from rich.console import RenderableType
|
||||
from rich.protocol import is_renderable
|
||||
from rich.text import Text
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from textual.app import RenderResult
|
||||
@@ -84,13 +83,7 @@ class Static(Widget, inherit_bindings=False):
|
||||
|
||||
@renderable.setter
|
||||
def renderable(self, renderable: RenderableType | SupportsVisual) -> None:
|
||||
if isinstance(renderable, str):
|
||||
if self._render_markup:
|
||||
self._renderable = Text.from_markup(renderable)
|
||||
else:
|
||||
self._renderable = Text(renderable)
|
||||
else:
|
||||
self._renderable = renderable
|
||||
self._renderable = renderable
|
||||
self._visual = None
|
||||
self.clear_cached_dimensions()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user