textual.content
Content is a container for text, with spans marked up with color / style. If is equivalent to Rich's Text object, with support for more of Textual features.
Unlike Rich Text, Content is immutable so you can't modify it in place, and most methods will return a new Content instance. This is more like the builtin str, and allows Textual to make some significant optimizations.
ANSI_DEFAULT
module-attribute
¶
ANSI_DEFAULT = Style(
background=Color(0, 0, 0, 0, ansi=-1),
foreground=Color(0, 0, 0, 0, ansi=-1),
)
A Style for ansi default background and foreground.
ContentType
module-attribute
¶
Type alias used where content and a str are interchangeable in a function.
Content
¶
Bases: Visual
Text content with marked up spans.
This object can be considered immutable, although it might update its internal state in a way that is consistent with immutability.
spans: Optional list of spans.
cell_length: Cell length of text if known, otherwise `None`.
markup
cached
property
¶
Get Content markup to render this Text.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
A string potentially creating markup tags. |
spans
property
¶
A sequence of spans used to markup regions of the content.
Warning
Never attempt to mutate the spans, as this would certainly break the output--possibly in quite subtle ways!
append_text
¶
assemble
classmethod
¶
Construct new content from string, content, or tuples of (TEXT, STYLE).
This is an efficient way of constructing Content composed of smaller pieces of text and / or other Content objects.
Example
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str | Content | tuple[str, str]
|
Parts to join to gether. A part may be a simple string, another Content |
()
|
|
str
|
Optional end to the Content. |
''
|
center
¶
divide
¶
divide(offsets)
Divide the content at the given offsets.
This will cut the content in to pieces, and return those pieces. Note that the number of pieces return will be one greater than the number of cuts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Sequence[int]
|
Sequence of offsets (in characters) of where to apply the cuts. |
required |
Returns:
Type | Description |
---|---|
list[Content]
|
List of Content instances which combined would be equal to the whole. |
extend_right
¶
from_markup
classmethod
¶
Create content from Textual markup, optionally combined with template variables.
If markup
is already a Content instance, it will be returned unmodified.
See the guide on Content for more details.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str | Content
|
Textual markup, or Content. |
required |
|
object
|
Optional template variables used |
{}
|
Returns:
Type | Description |
---|---|
Content
|
New Content instance. |
get_optimal_width
¶
highlight_regex
¶
highlight_regex(
highlight_regex, *, style, maximum_highlights=None
)
Apply a style to text that matches a regular expression.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Pattern[str] | str
|
Regular expression as a string, or compiled. |
required |
|
Style
|
Style to apply. |
required |
|
int | None
|
Maximum number of matches to highlight, or |
None
|
Returns:
Type | Description |
---|---|
Content
|
new content. |
is_same
¶
is_same(content)
Compare to another Content object.
Two Content objects are the same if their text and spans match.
Note that if you use the ==
operator to compare Content instances, it will only consider
the plain text portion of the content (and not the spans).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Content
|
Content instance. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
join
¶
join(lines)
Join an iterable of content or strings.
This works much like the join method on str
objects.
Self is the separator (which maybe empty) placed between each string or Content.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Iterable[Content | str]
|
An iterable of other Content instances or or strings. |
required |
Returns:
Type | Description |
---|---|
Content
|
A single Content instance, containing all of the lines. |
pad_left
¶
pad_right
¶
render_segments
¶
render_segments(base_style=null(), end='')
Render the Content in to a list of segments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Style
|
Base style for render (style under the content). Defaults to Style.null(). |
null()
|
|
str
|
Character to end the segments with. Defaults to "". |
''
|
Returns:
Type | Description |
---|---|
list[Segment]
|
A list of segments. |
render_strips
¶
Render the visual into an iterable of strips. Part of the Visual protocol.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
RulesMap
|
A mapping of style rules, such as the Widgets |
required |
|
int
|
Width of desired render. |
required |
|
int | None
|
Height of desired render or |
required |
|
Style
|
The base style to render on top of. |
required |
|
Selection | None
|
Selection information, if applicable, otherwise |
None
|
|
Style | None
|
Selection style if |
None
|
Returns:
Type | Description |
---|---|
list[Strip]
|
An list of Strips. |
right
¶
split
¶
split(
separator="\n",
*,
include_separator=False,
allow_blank=False
)
Split rich text into lines, preserving styles.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
String to split on. Defaults to "\n". |
'\n'
|
|
bool
|
Include the separator in the lines. Defaults to False. |
False
|
|
bool
|
Return a blank line if the text ends with a separator. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
list[Content]
|
List[Content]: A list of Content, one per line of the original. |
styled
classmethod
¶
styled(text, style='', cell_length=None)
Create a Content instance from text and an optional style.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
String content. |
required |
|
Style | str
|
Desired style. |
''
|
|
int | None
|
Cell length of text if known, otherwise |
None
|
Returns:
Type | Description |
---|---|
Content
|
New Content instance. |
stylize
¶
Apply a style to the text, or a portion of the text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Union[str, Style]
|
Style instance or style definition to apply. |
required |
|
int
|
Start offset (negative indexing is supported). Defaults to 0. |
0
|
|
Optional[int]
|
End offset (negative indexing is supported), or None for end of text. Defaults to None. |
None
|
stylize_before
¶
Apply a style to the text, or a portion of the text.
Styles applies with this method will be applied before other styles already present.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Union[str, Style]
|
Style instance or style definition to apply. |
required |
|
int
|
Start offset (negative indexing is supported). Defaults to 0. |
0
|
|
Optional[int]
|
End offset (negative indexing is supported), or None for end of text. Defaults to None. |
None
|