Skip to content

Commit

Permalink
wrap and overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Jan 26, 2025
1 parent 9f93a35 commit 8224ac8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/textual/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@ def get_height(self, rules: Rules, width: int) -> int:
A height in lines.
"""
lines = self.without_spans._wrap_and_format(
width, no_wrap=rules.get("text_wrap") == "nowrap"
width,
overflow=rules.get("text_overflow"),
no_wrap=rules.get("text_wrap") == "nowrap",
)
return len(lines)

Expand Down Expand Up @@ -368,6 +370,10 @@ def get_span(y: int) -> tuple[int, int] | None:
content_line = FormattedLine(line, width, y=y, align=align)
offsets = divide_line(line.plain, width, fold=overflow == "fold")
divided_lines = content_line.content.divide(offsets)
if overflow == "ellipsis":
divided_lines = [
line.truncate(width, ellipsis=True) for line in divided_lines
]
new_lines = [
FormattedLine(
content.rstrip_end(width), width, offset, y, align=align
Expand Down
28 changes: 28 additions & 0 deletions tests/snapshot_tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -3385,3 +3385,31 @@ def compose(self) -> ComposeResult:
yield Label(TEXT, id="label2")

assert snap_compare(NoWrapApp())


def test_overflow(snap_compare):
"""Test overflow. You should see two labels across 4 lines. The first with overflow clip,
the second with overflow ellipsis."""

TEXT = "FOO " + "FOOBARBAZ" * 100

class OverflowApp(App):
CSS = """
Label {
max-width: 100vw;
}
#label1 {
text-overflow: clip;
background: blue 20%;
}
#label2 {
text-overflow: ellipsis;
background: green 20%;
}
"""

def compose(self) -> ComposeResult:
yield Label(TEXT, id="label1")
yield Label(TEXT, id="label2")

assert snap_compare(OverflowApp())

0 comments on commit 8224ac8

Please sign in to comment.