rich.text
- class rich.text.Text(text='', style='', *, justify=None, overflow=None, no_wrap=None, end='\n', tab_size=None, spans=None)[source]
Text with color / style.
- Parameters:
text (str, optional) – Default unstyled text. Defaults to “”.
style (Union[str, Style], optional) – Base style for text. Defaults to “”.
justify (str, optional) – Justify method: “left”, “center”, “full”, “right”. Defaults to None.
overflow (str, optional) – Overflow method: “crop”, “fold”, “ellipsis”. Defaults to None.
no_wrap (bool, optional) – Disable text wrapping, or None for default. Defaults to None.
end (str, optional) – Character to end text with. Defaults to “\n”.
tab_size (int) – Number of spaces per tab, or
Noneto useconsole.tab_size. Defaults to None.spans (List[Span], optional)
- append_text(text)[source]
Append another Text instance. This method is more performant that Text.append, but only works for Text.
- append_tokens(tokens)[source]
Append iterable of str and style. Style may be a Style instance or a str style definition.
- classmethod assemble(*parts, style='', justify=None, overflow=None, no_wrap=None, end='\n', tab_size=8, meta=None)[source]
Construct a text instance by combining a sequence of strings with optional styles. The positional arguments should be either strings, or a tuple of string + style.
- Parameters:
style (Union[str, Style], optional) – Base style for text. Defaults to “”.
justify (str, optional) – Justify method: “left”, “center”, “full”, “right”. Defaults to None.
overflow (str, optional) – Overflow method: “crop”, “fold”, “ellipsis”. Defaults to None.
no_wrap (bool, optional) – Disable text wrapping, or None for default. Defaults to None.
end (str, optional) – Character to end text with. Defaults to “\n”.
tab_size (int) – Number of spaces per tab, or
Noneto useconsole.tab_size. Defaults to None.meta (Dict[str, Any], optional)
- Returns:
A new text instance.
- Return type:
- blank_copy(plain='')[source]
Return a new Text instance with copied metadata (but not the string or spans).
- copy_styles(text)[source]
Copy styles from another Text instance.
- Parameters:
text (Text) – A Text instance to copy styles from, must be the same length.
- Return type:
None
- detect_indentation()[source]
Auto-detect indentation of code.
- Returns:
Number of spaces used to indent code.
- Return type:
- divide(offsets)[source]
Divide text in to a number of lines at given offsets.
- Parameters:
offsets (Iterable[int]) – Offsets used to divide text.
- Returns:
New RichText instances between offsets.
- Return type:
Lines
- expand_tabs(tab_size=None)[source]
Converts tabs to spaces.
- Parameters:
tab_size (int, optional) – Size of tabs. Defaults to 8.
- Return type:
None
- extend_style(spaces)[source]
Extend the Text given number of spaces where the spaces have the same style as the last character.
- Parameters:
spaces (int) – Number of spaces to add to the Text.
- Return type:
None
- fit(width)[source]
Fit the text in to given width by chopping in to lines.
- Parameters:
width (int) – Maximum characters in a line.
- Returns:
Lines container.
- Return type:
Lines
- classmethod from_ansi(text, *, style='', justify=None, overflow=None, no_wrap=None, end='\n', tab_size=8)[source]
Create a Text object from a string containing ANSI escape codes.
- Parameters:
text (str) – A string containing escape codes.
style (Union[str, Style], optional) – Base style for text. Defaults to “”.
justify (str, optional) – Justify method: “left”, “center”, “full”, “right”. Defaults to None.
overflow (str, optional) – Overflow method: “crop”, “fold”, “ellipsis”. Defaults to None.
no_wrap (bool, optional) – Disable text wrapping, or None for default. Defaults to None.
end (str, optional) – Character to end text with. Defaults to “\n”.
tab_size (int) – Number of spaces per tab, or
Noneto useconsole.tab_size. Defaults to None.
- Return type:
- classmethod from_markup(text, *, style='', emoji=True, emoji_variant=None, justify=None, overflow=None, end='\n')[source]
Create Text instance from markup.
- Parameters:
text (str) – A string containing console markup.
style (Union[str, Style], optional) – Base style for text. Defaults to “”.
emoji (bool, optional) – Also render emoji code. Defaults to True.
emoji_variant (str, optional) – Optional emoji variant, either “text” or “emoji”. Defaults to None.
justify (str, optional) – Justify method: “left”, “center”, “full”, “right”. Defaults to None.
overflow (str, optional) – Overflow method: “crop”, “fold”, “ellipsis”. Defaults to None.
end (str, optional) – Character to end text with. Defaults to “\n”.
- Returns:
A Text instance with markup rendered.
- Return type:
- highlight_regex(re_highlight, style=None, *, style_prefix='')[source]
Highlight text with a regular expression, where group names are translated to styles.
- Parameters:
re_highlight (Union[re.Pattern, str]) – A regular expression object or string.
style (Union[GetStyleCallable, StyleType]) – Optional style to apply to whole match, or a callable which accepts the matched text and returns a style. Defaults to None.
style_prefix (str, optional) – Optional prefix to add to style group names.
- Returns:
Number of regex matches
- Return type:
- property markup: str
Get console markup to render this Text.
- Returns:
A string potentially creating markup tags.
- Return type:
- on(meta=None, **handlers)[source]
Apply event handlers (used by Textual project).
Example
>>> from rich.text import Text >>> text = Text("hello world") >>> text.on(click="view.toggle('world')")
- remove_suffix(suffix)[source]
Remove a suffix if it exists.
- Parameters:
suffix (str) – Suffix to remove.
- Return type:
None
- right_crop(amount=1)[source]
Remove a number of characters from the end of the text.
- Parameters:
amount (int)
- Return type:
None
- rstrip_end(size)[source]
Remove whitespace beyond a certain width at the end of the text.
- Parameters:
size (int) – The desired size of the text.
- Return type:
None
- set_length(new_length)[source]
Set new length of the text, clipping or padding is required.
- Parameters:
new_length (int)
- Return type:
None
- split(separator='\n', *, include_separator=False, allow_blank=False)[source]
Split rich text in to lines, preserving styles.
- Parameters:
- Returns:
A list of rich text, one per line of the original.
- Return type:
List[RichText]
- classmethod styled(text, style='', *, justify=None, overflow=None)[source]
Construct a Text instance with a pre-applied styled. A style applied in this way won’t be used to pad the text when it is justified.
- Parameters:
text (str) – A string containing console markup.
style (Union[str, Style]) – Style to apply to the text. Defaults to “”.
justify (str, optional) – Justify method: “left”, “center”, “full”, “right”. Defaults to None.
overflow (str, optional) – Overflow method: “crop”, “fold”, “ellipsis”. Defaults to None.
- Returns:
A text instance with a style applied to the entire string.
- Return type:
- stylize_before(style, start=0, end=None)[source]
Apply a style to the text, or a portion of the text. Styles will be applied before other styles already present.
- truncate(max_width, *, overflow=None, pad=False)[source]
Truncate text if it is longer that a given width.
- with_indent_guides(indent_size=None, *, character='│', style='dim green')[source]
Adds indent guide lines to text.
- Parameters:
- Returns:
New text with indentation guides.
- Return type:
- wrap(console, width, *, justify=None, overflow=None, tab_size=8, no_wrap=None)[source]
Word wrap the text.
- Parameters:
console (Console) – Console instance.
width (int) – Number of cells available per line.
justify (str, optional) – Justify method: “default”, “left”, “center”, “full”, “right”. Defaults to “default”.
overflow (str, optional) – Overflow method: “crop”, “fold”, or “ellipsis”. Defaults to None.
tab_size (int, optional) – Default tab size. Defaults to 8.
no_wrap (bool, optional) – Disable wrapping, Defaults to False.
- Returns:
Number of lines.
- Return type:
Lines