Skip to content

Commit 71dfcf4

Browse files
authored
Merge pull request #6264 from Textualize/content-blank
added content blank
2 parents 4e0ff24 + 898f180 commit 71dfcf4

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## Unreleased
9+
10+
### Added
11+
12+
- Added `Content.blank` https://github.com/Textualize/textual/pull/6264
13+
814
## [6.7.1] - 2025-12-1
915

1016
### Fixed

src/textual/content.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,20 @@ def __init__(
179179
def __str__(self) -> str:
180180
return self._text
181181

182+
@property
183+
def _is_regular(self) -> bool:
184+
"""Check if the line is regular (spans.start > span.end for all spans).
185+
186+
This is a debugging aid, and unlikely to be useful in your app.
187+
188+
Returns:
189+
`True` if the content is regular, `False` if it is not (and broken).
190+
"""
191+
for span in self.spans:
192+
if span.end <= span.start:
193+
return False
194+
return True
195+
182196
@cached_property
183197
def markup(self) -> str:
184198
"""Get content markup to render this Text.
@@ -374,6 +388,26 @@ def styled(
374388
)
375389
return new_content
376390

391+
@classmethod
392+
def blank(cls, width: int, style: Style | str | None = None) -> Content:
393+
"""Get a Content instance consisting of spaces.
394+
395+
Args:
396+
width: Width of blank content (number of spaces).
397+
style: Style of blank.
398+
399+
Returns:
400+
Content instance.
401+
"""
402+
if not width:
403+
return EMPTY_CONTENT
404+
blank = cls(
405+
" " * width,
406+
[Span(0, width, style)] if style else None,
407+
cell_length=width,
408+
)
409+
return blank
410+
377411
@classmethod
378412
def assemble(
379413
cls,
@@ -431,7 +465,10 @@ def assemble(
431465
position += len(part.plain)
432466
if end:
433467
text_append(end)
434-
return cls("".join(text), spans, strip_control_codes=strip_control_codes)
468+
assembled_content = cls(
469+
"".join(text), spans, strip_control_codes=strip_control_codes
470+
)
471+
return assembled_content
435472

436473
def simplify(self) -> Content:
437474
"""Simplify spans by joining contiguous spans together.
@@ -786,19 +823,22 @@ def get_text_at(offset: int) -> "Content":
786823
if stop >= len(self.plain):
787824
return self
788825
text = self.plain[:stop]
789-
return Content(
826+
sliced_content = Content(
790827
text,
791828
self._trim_spans(text, self._spans),
792829
strip_control_codes=False,
793830
)
794831
else:
795832
text = self.plain[start:stop]
796833
spans = [
797-
span._shift(-start) for span in self._spans if span.end > start
834+
span._shift(-start)
835+
for span in self._spans
836+
if span.end - start > 0
798837
]
799-
return Content(
838+
sliced_content = Content(
800839
text, self._trim_spans(text, spans), strip_control_codes=False
801840
)
841+
return sliced_content
802842

803843
else:
804844
# This would be a bit of work to implement efficiently

tests/test_content.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,3 +570,20 @@ def test_fold(content: Content, width: int, expected: list[Content]) -> None:
570570
assert len(result) == len(expected)
571571
for line, expected_line in zip(result, expected):
572572
assert line.is_same(expected_line)
573+
574+
575+
@pytest.mark.parametrize(
576+
"width,style,text,spans,cell_length",
577+
[
578+
(5, None, " ", [], 5),
579+
(0, None, "", [], 0),
580+
(5, "on red", " ", [Span(0, 5, "on red")], 5),
581+
],
582+
)
583+
def test_blank_method(
584+
width: int, style: str | None, text: str, spans: list[Span], cell_length: int
585+
) -> None:
586+
blank = Content.blank(width, style)
587+
assert blank.plain == text
588+
assert blank.spans == spans
589+
assert blank.cell_length == cell_length

0 commit comments

Comments
 (0)