rich/README.md

148 lines
5.3 KiB
Markdown
Raw Normal View History

2019-12-23 16:21:51 +00:00
# Rich
2019-12-23 19:29:52 +00:00
Rich is a Python library for _rich_ text and high level formatting in the terminal.
2019-12-23 16:21:51 +00:00
2019-12-23 19:29:52 +00:00
The Rich API make it extremely easy to add colored text (up to 16.7million colors) and styles (bold, italic, underline etc.) to your script or application. Rich can also render pretty tables, markdown and source code with syntax highlighting.
2019-12-23 16:21:51 +00:00
## Installing
Rich may be installed with pip or your favorite PyPi package manager.
```
pip install rich
```
2019-12-23 18:00:28 +00:00
## Console Printing
2019-12-23 16:21:51 +00:00
The first step to using the rich console is to import and construct the `Console` object.
```python
from rich.console import Console
console = Console()
```
Most applications will require one `Console` instance. The easiest way to manage your console instance would be to construct an instance at the module level and import it where needed.
The Console object has a `print` method which has an intentionally similar interface to the builtin `print` function. Here's an example of use:
```
console.print("Hello", "World!")
```
2019-12-23 19:33:59 +00:00
As you might expect, this will print `"Hello World!"` to the terminal. Not that unlike the `print` function, Rich will word-wrap your test to fit within the terminal width.
2019-12-23 16:21:51 +00:00
2019-12-23 19:29:52 +00:00
There are a few ways of adding color and style to your output. You can set a style for the entire output by adding a `style` keyword argument. Here's an example:
2019-12-23 16:21:51 +00:00
```
console.print("Hello", "World!", style="bold red")
```
The output will be something like the following:
2019-12-23 16:32:46 +00:00
![Hello World](./imgs/hello_world.png)
2019-12-23 16:21:51 +00:00
That's fine for styling a line of text at a time. For more finely grained styling, Rich renders a special markup which is similar in syntax to [bbcode](https://en.wikipedia.org/wiki/BBCode). Here's an example:
2019-12-23 19:29:52 +00:00
```python
console.print("Where there is a [bold cyan]Will[/bold cyan] there [u]is[/u] a [i]way[/i].")
```
2019-12-23 16:21:51 +00:00
2019-12-23 19:29:52 +00:00
![Console Markup](./imgs/where_there_is_a_will.png)
2019-12-23 16:21:51 +00:00
2019-12-23 19:29:52 +00:00
### Defining Styles
2019-12-23 17:16:44 +00:00
Rich expects styles to be specified with a _style definition_ string, which is a intuitive syntax that reads almost like English.
There are a few ways of specifying colors:
2019-12-23 19:29:52 +00:00
- The word `"default"` to select the default foreground or background color.
2019-12-23 17:16:44 +00:00
- The name of the color (one of 256 possible constants) e.g. `"magenta"`. To see the full range of colors run `python -m rich.color` from the console.
- A number between 0 and 255 (inclusive) which corresponds to one of the 256 possible constants above.
- A CSS hex style color, e.g. `#ff0000` or `#d75faf`
- A CSS rbg style color, e.g. `rgb(215,95,175)`
By itself, a color will set the _foreground_ color. To set a _background_ color, precede the color with the word `"on"`. For example `"red on white"`.
2019-12-23 19:29:52 +00:00
Note that the CSS hex and RGB style of color lets you chose one of 16.7 million colors, but some terminals (notably OSX terminal) only support 256 colors. If Rich detects that only 256 colors are supported it will pick the closest color available. In practice this means that you may not get exactly the color you ask for, but it is generally _close enough_.
2019-12-23 18:00:28 +00:00
2019-12-23 17:16:44 +00:00
To set a style or attribute add one or more of the following words:
- `"bold"` for bold text.
- `"dim"` for dim text.
- `"italic"` for italic text.
- `"underline"` for underlined text.
- `"blink"` for text that blinks.
- `"reverse"` to swap foreground and background text.
- `"conceal"` for concealed text (not supported on most terminals).
2019-12-23 19:29:52 +00:00
- `"strike"` for text with a line through it (not supported on all terminals).
2019-12-23 17:16:44 +00:00
2019-12-23 19:33:59 +00:00
Style attributes and colors may appear in any order, i.e. `"bold magenta on yellow"` has the same effect as `"on yellow magenta bold"`. The latter may be preferred by Yoda.
2019-12-23 18:00:28 +00:00
## Console Logging
2019-12-23 17:16:44 +00:00
2019-12-23 19:29:52 +00:00
TODO
2019-12-23 16:21:51 +00:00
## Emoji
2019-12-23 19:29:52 +00:00
To insert an emoji in to console output place the name between two colons. Here's an example:
2019-12-23 16:21:51 +00:00
```python
2019-12-23 17:16:44 +00:00
>>> console.print(":smiley: :vampire: :pile_of_poo: :thumbs_up: :raccoon:")
😃 🧛 💩 👍 🦝
2019-12-23 16:21:51 +00:00
```
Please use this feature wisely.
2019-12-23 18:00:28 +00:00
## Markdown
2019-12-23 19:33:59 +00:00
Rich can render markdown and does a reasonable job of translating the formatting to the terminal.
2019-12-23 18:00:28 +00:00
2019-12-23 19:29:52 +00:00
To render markdown import the `Markdown` class and construct it with a string containing markdown code. Then print it to the console. Here's an example:
2019-12-23 18:00:28 +00:00
```python
from rich.console import Console
from rich.markdown import Markdown
console = Console()
with open("README.md") as readme:
markdown = Markdown(readme.read())
console.print(markdown)
```
This will produce output something like the following:
![markdown](./imgs/markdown.png)
## Syntax Highlighting
Rich uses the [pygments](https://pygments.org/) library to implement syntax highlighting. Usage is similar to rendering markdown; construct a `Syntax` object and print it to the console. Here's an example:
```python
from rich.console import Console
from rich.syntax import Syntax
my_code = '''
def iter_first_last(values: Iterable[T]) -> Iterable[Tuple[bool, bool, T]]:
"""Iterate and generate a tuple with a flag for first and last value."""
iter_values = iter(values)
try:
previous_value = next(iter_values)
except StopIteration:
return
first = True
for value in iter_values:
yield first, False, previous_value
first = False
previous_value = value
yield first, True, previous_value
'''
syntax = Syntax(my_code, "python", theme="monokai", line_numbers=True)
console = Console()
console.print(syntax)
```
This will produce the following output:
![syntax](./imgs/syntax.png)