| 1 |
.. _highlighting: |
| 2 |
|
| 3 |
Highlighting |
| 4 |
============ |
| 5 |
|
| 6 |
Rich will automatically highlight patterns in text, such as numbers, strings, collections, booleans, None, and a few more exotic patterns such as file paths, URLs and UUIDs. |
| 7 |
|
| 8 |
You can disable highlighting either by setting ``highlight=False`` on :meth:`~rich.console.Console.print` or :meth:`~rich.console.Console.log`, or by setting ``highlight=False`` on the :class:`~rich.console.Console` constructor which disables it everywhere. If you disable highlighting on the constructor, you can still selectively *enable* highlighting with ``highlight=True`` on print / log. |
| 9 |
|
| 10 |
Custom Highlighters |
| 11 |
------------------- |
| 12 |
|
| 13 |
If the default highlighting doesn't fit your needs, you can define a custom highlighter. The easiest way to do this is to extend the :class:`~rich.highlighter.RegexHighlighter` class which applies a style to any text matching a list of regular expressions. |
| 14 |
|
| 15 |
Here's an example which highlights text that looks like an email address:: |
| 16 |
|
| 17 |
from rich.console import Console |
| 18 |
from rich.highlighter import RegexHighlighter |
| 19 |
from rich.theme import Theme |
| 20 |
|
| 21 |
class EmailHighlighter(RegexHighlighter): |
| 22 |
"""Apply style to anything that looks like an email.""" |
| 23 |
|
| 24 |
base_style = "example." |
| 25 |
highlights = [r"(?P<email>[\w-]+@([\w-]+\.)+[\w-]+)"] |
| 26 |
|
| 27 |
|
| 28 |
theme = Theme({"example.email": "bold magenta"}) |
| 29 |
console = Console(highlighter=EmailHighlighter(), theme=theme) |
| 30 |
console.print("Send funds to [email protected]") |
| 31 |
|
| 32 |
|
| 33 |
The ``highlights`` class variable should contain a list of regular expressions. The group names of any matching expressions are prefixed with the ``base_style`` attribute and used as styles for matching text. In the example above, any email addresses will have the style "example.email" applied, which we've defined in a custom :ref:`Theme <themes>`. |
| 34 |
|
| 35 |
Setting the highlighter on the Console will apply highlighting to all text you print (if enabled). You can also use a highlighter on a more granular level by using the instance as a callable and printing the result. For example, we could use the email highlighter class like this:: |
| 36 |
|
| 37 |
|
| 38 |
console = Console(theme=theme) |
| 39 |
highlight_emails = EmailHighlighter() |
| 40 |
console.print(highlight_emails("Send funds to [email protected]")) |
| 41 |
|
| 42 |
|
| 43 |
While :class:`~rich.highlighter.RegexHighlighter` is quite powerful, you can also extend its base class :class:`~rich.highlighter.Highlighter` to implement a custom scheme for highlighting. It contains a single method :class:`~rich.highlighter.Highlighter.highlight` which is passed the :class:`~rich.text.Text` to highlight. |
| 44 |
|
| 45 |
Here's a silly example that highlights every character with a different color:: |
| 46 |
|
| 47 |
from random import randint |
| 48 |
|
| 49 |
from rich import print |
| 50 |
from rich.highlighter import Highlighter |
| 51 |
|
| 52 |
|
| 53 |
class RainbowHighlighter(Highlighter): |
| 54 |
def highlight(self, text): |
| 55 |
for index in range(len(text)): |
| 56 |
text.stylize(f"color({randint(16, 255)})", index, index + 1) |
| 57 |
|
| 58 |
|
| 59 |
rainbow = RainbowHighlighter() |
| 60 |
print(rainbow("I must not fear. Fear is the mind-killer.")) |
| 61 |
|
| 62 |
Builtin Highlighters |
| 63 |
-------------------- |
| 64 |
|
| 65 |
The following builtin highlighters are available. |
| 66 |
|
| 67 |
* :class:`~rich.highlighter.ISO8601Highlighter` Highlights ISO8601 date time strings. |
| 68 |
* :class:`~rich.highlighter.JSONHighlighter` Highlights JSON formatted strings. |