Skip to content

Commit

Permalink
Merge pull request #2080 from Textualize/segment-tune
Browse files Browse the repository at this point in the history
revert segment optimization
  • Loading branch information
willmcgugan committed Mar 18, 2022
2 parents 1e972e3 + cd14f45 commit 7a0310f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 65 deletions.
76 changes: 40 additions & 36 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jupyter = ["ipywidgets"]
[tool.poetry.dev-dependencies]
pytest = "^7.0.0"
black = "^22.1"
mypy = "^0.931"
mypy = "^0.941"
pytest-cov = "^3.0.0"
attrs = "^21.4.0"
types-dataclasses = "^0.6.4"
Expand Down
7 changes: 4 additions & 3 deletions rich/ansi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from contextlib import suppress
import re
import sys
from contextlib import suppress
from typing import Iterable, NamedTuple

from .color import Color
Expand Down Expand Up @@ -198,10 +199,10 @@ def decode_line(self, line: str) -> Text:
return text


if __name__ == "__main__": # pragma: no cover
import pty
if sys.platform != "win32" and __name__ == "__main__": # pragma: no cover
import io
import os
import pty
import sys

decoder = AnsiDecoder()
Expand Down
38 changes: 13 additions & 25 deletions rich/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Iterator,
List,
Optional,
NamedTuple,
Sequence,
Tuple,
Type,
Expand Down Expand Up @@ -58,7 +59,7 @@ class ControlType(IntEnum):


@rich_repr()
class Segment:
class Segment(NamedTuple):
"""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.
Expand All @@ -71,32 +72,19 @@ class Segment:
cell_length (int): The cell length of this Segment.
"""

__slots__ = ["text", "style", "control", "cell_length", "_tuple"]
text: str
style: Optional[Style] = None
control: Optional[Sequence[ControlCode]] = None

def __init__(
self,
text: str = "",
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
@property
def cell_length(self) -> int:
"""The number of terminal cells required to display self.text.
def __hash__(self) -> int:
return hash(self._tuple)
Returns:
int: A number of cells.
"""
text, _style, control = self
return 0 if control else cell_len(text)

def __rich_repr__(self) -> Result:
yield self.text
Expand Down

0 comments on commit 7a0310f

Please sign in to comment.