| 1 |
.. _rich_text: |
| 2 |
|
| 3 |
Rich Text |
| 4 |
========= |
| 5 |
|
| 6 |
Rich has a :class:`~rich.text.Text` class you can use to mark up strings with color and style attributes. You can use a Text instance anywhere a string is accepted, which gives you a lot of control over presentation. |
| 7 |
|
| 8 |
You can consider this class to be like a string with marked up regions of text. Unlike a built-in ``str``, a Text instance is mutable, and most methods operate in-place rather than returning a new instance. |
| 9 |
|
| 10 |
One way to add a style to Text is the :meth:`~rich.text.Text.stylize` method which applies a style to a start and end offset. Here is an example:: |
| 11 |
|
| 12 |
from rich.console import Console |
| 13 |
from rich.text import Text |
| 14 |
|
| 15 |
console = Console() |
| 16 |
text = Text("Hello, World!") |
| 17 |
text.stylize("bold magenta", 0, 6) |
| 18 |
console.print(text) |
| 19 |
|
| 20 |
This will print "Hello, World!" to the terminal, with the first word in bold magenta. |
| 21 |
|
| 22 |
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:: |
| 23 |
|
| 24 |
text = Text() |
| 25 |
text.append("Hello", style="bold magenta") |
| 26 |
text.append(" World!") |
| 27 |
console.print(text) |
| 28 |
|
| 29 |
If you would like to use text that is already formatted with ANSI codes, call :meth:`~rich.text.Text.from_ansi` to convert it to a ``Text`` object:: |
| 30 |
|
| 31 |
text = Text.from_ansi("\033[1;35mHello\033[0m, World!") |
| 32 |
console.print(text.spans) |
| 33 |
|
| 34 |
Since building Text instances from parts is a common requirement, Rich offers :meth:`~rich.text.Text.assemble` which will combine strings or pairs of string and Style, and return a Text instance. The following example is equivalent to the ANSI example above:: |
| 35 |
|
| 36 |
text = Text.assemble(("Hello", "bold magenta"), ", World!") |
| 37 |
console.print(text) |
| 38 |
|
| 39 |
You can apply a style to given words in the text with :meth:`~rich.text.Text.highlight_words` or for ultimate control call :meth:`~rich.text.Text.highlight_regex` to highlight text matching a *regular expression*. |
| 40 |
|
| 41 |
|
| 42 |
Text attributes |
| 43 |
~~~~~~~~~~~~~~~ |
| 44 |
|
| 45 |
The Text class has a number of parameters you can set on the constructor to modify how the text is displayed. |
| 46 |
|
| 47 |
- ``justify`` should be "left", "center", "right", or "full", and will override default justify behavior. |
| 48 |
- ``overflow`` should be "fold", "crop", or "ellipsis", and will override default overflow. |
| 49 |
- ``no_wrap`` prevents wrapping if the text is longer then the available width. |
| 50 |
- ``tab_size`` Sets the number of characters in a tab. |
| 51 |
|
| 52 |
A Text instance may be used in place of a plain string virtually everywhere in the Rich API, which gives you a lot of control in how text renders within other Rich renderables. For instance, the following example right aligns text within a :class:`~rich.panel.Panel`:: |
| 53 |
|
| 54 |
from rich import print |
| 55 |
from rich.panel import Panel |
| 56 |
from rich.text import Text |
| 57 |
panel = Panel(Text("Hello", justify="right")) |
| 58 |
print(panel) |
| 59 |
|
| 60 |
|