Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Added

- Added `Content.blank` https://github.com/Textualize/textual/pull/6264

## [6.7.1] - 2025-12-1

### Fixed
Expand Down
48 changes: 44 additions & 4 deletions src/textual/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@ def __init__(
def __str__(self) -> str:
return self._text

@property
def _is_regular(self) -> bool:
"""Check if the line is regular (spans.start > span.end for all spans).

This is a debugging aid, and unlikely to be useful in your app.

Returns:
`True` if the content is regular, `False` if it is not (and broken).
"""
for span in self.spans:
if span.end <= span.start:
return False
return True

@cached_property
def markup(self) -> str:
"""Get content markup to render this Text.
Expand Down Expand Up @@ -374,6 +388,26 @@ def styled(
)
return new_content

@classmethod
def blank(cls, width: int, style: Style | str | None = None) -> Content:
"""Get a Content instance consisting of spaces.

Args:
width: Width of blank content (number of spaces).
style: Style of blank.

Returns:
Content instance.
"""
if not width:
return EMPTY_CONTENT
blank = cls(
" " * width,
[Span(0, width, style)] if style else None,
cell_length=width,
)
return blank

@classmethod
def assemble(
cls,
Expand Down Expand Up @@ -431,7 +465,10 @@ def assemble(
position += len(part.plain)
if end:
text_append(end)
return cls("".join(text), spans, strip_control_codes=strip_control_codes)
assembled_content = cls(
"".join(text), spans, strip_control_codes=strip_control_codes
)
return assembled_content

def simplify(self) -> Content:
"""Simplify spans by joining contiguous spans together.
Expand Down Expand Up @@ -786,19 +823,22 @@ def get_text_at(offset: int) -> "Content":
if stop >= len(self.plain):
return self
text = self.plain[:stop]
return Content(
sliced_content = Content(
text,
self._trim_spans(text, self._spans),
strip_control_codes=False,
)
else:
text = self.plain[start:stop]
spans = [
span._shift(-start) for span in self._spans if span.end > start
span._shift(-start)
for span in self._spans
if span.end - start > 0
]
return Content(
sliced_content = Content(
text, self._trim_spans(text, spans), strip_control_codes=False
)
return sliced_content

else:
# This would be a bit of work to implement efficiently
Expand Down
17 changes: 17 additions & 0 deletions tests/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,3 +570,20 @@ def test_fold(content: Content, width: int, expected: list[Content]) -> None:
assert len(result) == len(expected)
for line, expected_line in zip(result, expected):
assert line.is_same(expected_line)


@pytest.mark.parametrize(
"width,style,text,spans,cell_length",
[
(5, None, " ", [], 5),
(0, None, "", [], 0),
(5, "on red", " ", [Span(0, 5, "on red")], 5),
],
)
def test_blank_method(
width: int, style: str | None, text: str, spans: list[Span], cell_length: int
) -> None:
blank = Content.blank(width, style)
assert blank.plain == text
assert blank.spans == spans
assert blank.cell_length == cell_length
Loading