rich/docs/source/text.rst

21 lines
964 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.
2020-03-02 17:29:32 +00:00
One way to add a style to Text is the :meth:`~tich.text.Text.stylize` method which applies a style to a start and end offset. Here is an example::
2020-01-12 15:54:47 +00:00
from rich.text import Text
text = Text("Hello, World!")
2020-03-02 17:29:32 +00:00
text.stylize(0, 6, "bold magenta")
2020-01-12 15:54:47 +00:00
console.print(text)
2020-01-19 17:18:05 +00:00
This will print "Hello, World!" to the terminal, with the first word in bold magenta.
2020-01-12 15:54:47 +00:00
2020-01-19 17:18:05 +00:00
Alternatively, you can construct styled text by calling :meth:`~rich.text.Text.append` to add a string and style to the end of the Text. Here's an example::
2020-01-12 15:54:47 +00:00
text = Text()
text.append("Hello", style="bold magenta")
text.append(" World!")
console.print(text)