mirror of https://github.com/Textualize/rich.git
Calculate cell_length in Segment __init__. Segment now plain class.
This commit is contained in:
parent
bc82a15c55
commit
7d09020caa
|
@ -5,10 +5,11 @@ from logging import getLogger
|
||||||
from operator import attrgetter
|
from operator import attrgetter
|
||||||
from typing import (
|
from typing import (
|
||||||
TYPE_CHECKING,
|
TYPE_CHECKING,
|
||||||
|
Any,
|
||||||
Dict,
|
Dict,
|
||||||
Iterable,
|
Iterable,
|
||||||
|
Iterator,
|
||||||
List,
|
List,
|
||||||
NamedTuple,
|
|
||||||
Optional,
|
Optional,
|
||||||
Sequence,
|
Sequence,
|
||||||
Tuple,
|
Tuple,
|
||||||
|
@ -57,22 +58,45 @@ ControlCode = Union[
|
||||||
|
|
||||||
|
|
||||||
@rich_repr()
|
@rich_repr()
|
||||||
class Segment(NamedTuple):
|
class Segment:
|
||||||
"""A piece of text with associated style. Segments are produced by the Console render process and
|
"""A piece of text with associated style. Segments are produced by the Console render process and
|
||||||
are ultimately converted in to strings to be written to the terminal.
|
are ultimately converted in to strings to be written to the terminal.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
text (str): A piece of text.
|
text (str): A piece of text.
|
||||||
style (:class:`~rich.style.Style`, optional): An optional style to apply to the text.
|
style (:class:`~rich.style.Style`, optional): An optional style to apply to the text.
|
||||||
control (Sequence[ControlCode], optional): Optional sequence of control codes.
|
control (Tuple[ControlCode], optional): Optional sequence of control codes.
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
cell_length (int): The cell length of this Segment.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
text: str = ""
|
__slots__ = ["text", "style", "control", "cell_length", "_tuple"]
|
||||||
"""Raw text."""
|
|
||||||
style: Optional[Style] = None
|
def __init__(
|
||||||
"""An optional style."""
|
self,
|
||||||
control: Optional[Sequence[ControlCode]] = None
|
text: str = "",
|
||||||
"""Optional sequence of control codes."""
|
style: Optional[Style] = None,
|
||||||
|
control: Optional[Sequence[ControlCode]] = None,
|
||||||
|
):
|
||||||
|
self.text = text
|
||||||
|
self.style = style
|
||||||
|
self.control = control
|
||||||
|
|
||||||
|
self.cell_length = 0 if self.control else cell_len(self.text)
|
||||||
|
|
||||||
|
self._tuple = text, style, control
|
||||||
|
|
||||||
|
def __iter__(self) -> Iterator[Any]:
|
||||||
|
return iter(self._tuple)
|
||||||
|
|
||||||
|
def __eq__(self, other: object) -> bool:
|
||||||
|
if isinstance(other, Segment):
|
||||||
|
return self._tuple == other._tuple
|
||||||
|
return self._tuple == other
|
||||||
|
|
||||||
|
def __hash__(self) -> int:
|
||||||
|
return hash(self._tuple)
|
||||||
|
|
||||||
def __rich_repr__(self) -> Result:
|
def __rich_repr__(self) -> Result:
|
||||||
yield self.text
|
yield self.text
|
||||||
|
@ -87,11 +111,6 @@ class Segment(NamedTuple):
|
||||||
"""Check if the segment contains text."""
|
"""Check if the segment contains text."""
|
||||||
return bool(self.text)
|
return bool(self.text)
|
||||||
|
|
||||||
@property
|
|
||||||
def cell_length(self) -> int:
|
|
||||||
"""Get cell length of segment."""
|
|
||||||
return 0 if self.control else cell_len(self.text)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_control(self) -> bool:
|
def is_control(self) -> bool:
|
||||||
"""Check if the segment contains control codes."""
|
"""Check if the segment contains control codes."""
|
||||||
|
|
Loading…
Reference in New Issue