-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41cc14d
commit 3617dc4
Showing
25 changed files
with
383 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
Higlighting | ||
=========== | ||
|
||
Rich can apply styles to patterns in text which you :meth:`~rich.console.Console.print` or :meth:`~rich.console.Console.log`. With the default settings, Rich will highlight things such as numbers, strings, collections, booleans, None, and a few more exotic patterns such as URLs and UUIDs. | ||
|
||
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. | ||
|
||
|
||
Custom Highlighters | ||
------------------- | ||
|
||
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. | ||
|
||
Here's an example which highlights text that looks like an email address:: | ||
|
||
|
||
from rich.highlighter import RegexHighlighter | ||
|
||
|
||
class EmailHighlighter(RegexHighlighter): | ||
"""Apply style to anything that looks like an email.""" | ||
|
||
base_style = "example." | ||
highlights = [r"(?P<email>[\w-]+@([\w-]+\.)+[\w-]+)"] | ||
|
||
|
||
from rich.console import Console | ||
from rich.style import Style | ||
from rich.theme import Theme | ||
|
||
theme = Theme({"example.email": "bold magenta"}) | ||
console = Console(highlighter=EmailHighlighter(), theme=theme) | ||
|
||
console.print("Send funds to [email protected]") | ||
|
||
|
||
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>`. | ||
|
||
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:: | ||
|
||
|
||
console = Console(theme=theme) | ||
highlight_emails = EmailHighlighter() | ||
console.print(highlight_emails("Send funds to [email protected]")) | ||
|
||
|
||
While :class:`~rich.highlighter.RegexHighlighter` is quite powerful, you can also extend its base class :class:`~rich.highlighter.Highlighter` which you can use to implement any scheme for highlighting. It contains a single method :class:`~rich.highlighter.Highlighter.highlight` which is passed the :class:`~rich.text.Text` to highlight. | ||
|
||
Here's an silly example that highlights every character with a different color:: | ||
|
||
from random import randint | ||
|
||
from rich import print | ||
from rich.highlighter import Highlighter | ||
|
||
|
||
class RainbowHighlighter(Highlighter): | ||
def highlight(self, text): | ||
for index in range(len(text)): | ||
text.stylize(index, index + 1, str(randint(16, 255))) | ||
|
||
|
||
rainbow = RainbowHighlighter() | ||
print(rainbow("I must not fear. Fear is the mind-killer.")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from rich.highlighter import RegexHighlighter | ||
|
||
|
||
class EmailHighlighter(RegexHighlighter): | ||
"""Apply style to anything that looks like an email.""" | ||
|
||
base_style = "example." | ||
highlights = [r"(?P<email>[\w-]+@([\w-]+\.)+[\w-]+)"] | ||
|
||
|
||
from rich.console import Console | ||
from rich.style import Style | ||
from rich.theme import Theme | ||
|
||
theme = Theme({"example.email": "bold magenta"}) | ||
console = Console(highlighter=EmailHighlighter(), theme=theme) | ||
|
||
console.print("Send funds to [email protected]") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from random import randint | ||
|
||
from rich import print | ||
from rich.highlighter import Highlighter | ||
|
||
|
||
class RainbowHighlighter(Highlighter): | ||
def highlight(self, text): | ||
for index in range(len(text)): | ||
text.stylize(index, index + 1, str(randint(16, 255))) | ||
|
||
|
||
rainbow = RainbowHighlighter() | ||
print(rainbow("I must not fear. Fear is the mind-killer.")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,13 @@ | |
name = "rich" | ||
homepage = "https://github.com/willmcgugan/rich" | ||
documentation = "https://rich.readthedocs.io/en/latest/" | ||
version = "1.0.0" | ||
version = "1.0.1" | ||
description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal" | ||
authors = ["Will McGugan <[email protected]>"] | ||
license = "MIT" | ||
readme = "README.md" | ||
classifiers = [ | ||
"Development Status :: 3 - Alpha", | ||
"Development Status :: 5 - Production/Stable", | ||
"Environment :: Console", | ||
"Intended Audience :: Developers", | ||
"Operating System :: Microsoft :: Windows", | ||
|
@@ -26,9 +26,9 @@ python = "^3.6" | |
pprintpp = "^0.4.0" | ||
typing-extensions = "^3.7.4" | ||
dataclasses = {version="^0.7", python = "~3.6"} | ||
pygments = "^2.6.1" | ||
pygments = "^2.6.0" | ||
commonmark = "^0.9.0" | ||
colorama = "^0.4.3" | ||
colorama = "^0.4.0" | ||
|
||
|
||
[tool.poetry.dev-dependencies] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.