rich/docs/source/text.rst

56 lines
2.6 KiB
ReStructuredText
Raw Normal View History

2020-04-13 11:37:53 +00:00
.. _rich_text:
2020-04-29 14:21:10 +00:00
2020-01-12 11:43:23 +00:00
Rich Text
=========
2020-10-08 13:35:12 +00:00
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.
You can consider this class to be like a string with marked up regions of text. Unlike a builtin ``str``, a Text instance is mutable, and most methods operate in-place rather than returning a new instance.
2020-01-12 15:54:47 +00:00
2020-03-02 17:32:47 +00:00
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::
2020-01-12 15:54:47 +00:00
2021-01-09 15:28:04 +00:00
from rich.console import Console
2020-01-12 15:54:47 +00:00
from rich.text import Text
2021-01-09 15:28:04 +00:00
console = Console()
2020-01-12 15:54:47 +00:00
text = Text("Hello, World!")
2020-07-23 16:51:00 +00:00
text.stylize("bold magenta", 0, 6)
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)
2020-04-13 11:37:53 +00:00
2020-06-03 08:39:56 +00:00
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 follow example is equivalent to the code above::
2020-05-19 12:38:13 +00:00
text = Text.assemble(("Hello", "bold magenta"), " World!")
console.print(text)
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*.
2020-08-01 16:45:40 +00:00
Text attributes
~~~~~~~~~~~~~~~
2020-08-02 14:05:42 +00:00
The Text class has a number of parameters you can set on the constructor to modify how the text is displayed.
2020-08-01 16:45:40 +00:00
2020-08-25 16:09:56 +00:00
- ``justify`` should be "left", "center", "right", or "full", and will override default justify behavior.
2020-08-02 14:05:42 +00:00
- ``overflow`` should be "fold", "crop", or "ellipsis", and will override default overflow.
2020-08-01 16:45:40 +00:00
- ``no_wrap`` prevents wrapping if the text is longer then the available width.
- ``tab_size`` Sets the number of characters in a tab.
2021-03-30 17:02:05 +00:00
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`::
2020-08-01 16:45:40 +00:00
from rich import print
from rich.panel import Panel
from rich.text import Text
panel = Panel(Text("Hello", justify="right"))
print(panel)