rich/docs/source/text.rst

21 lines
957 B
ReStructuredText
Raw Normal View History

2020-01-12 11:43:23 +00:00
Rich Text
=========
2020-01-12 15:54:47 +00:00
Rich has a :class:`~rich.text.Text` class for text that may be marked up with color and style attributes. You can consider this class to be like a mutable string with style information. The methods on the Text() instance are similar to a Python ``str`` but are designed to preserve any style information.
One way to add a style to Text is the :meth:`~tich.text.Text.styleize` method which applies a style to a start and end offset. Here is an example::
from rich.text import Text
text = Text("Hello, World!")
text.styleize(0, 6, "bold magenta")
console.print(text)
This will print "Hello, World!" to the terminal, with the first word in bold.
Alternatively, you can construct style text by calling :meth:`~rich.text.Text.append` to add a string and style to the end of the Text. Here's an example::
text = Text()
text.append("Hello", style="bold magenta")
text.append(" World!")
console.print(text)