Enrich (Rich Extensions)
The `enrich` library extends the functionality of the popular `rich` library, providing additional features not natively included in `rich`. It offers a custom `Console` class with support for redirecting `sys.stdout`/`sys.stderr` and implicit soft wrapping, as well as an alternative `RichHandler` for logging that prioritizes soft wrapping. The current stable version is 1.2.7, with releases occurring periodically to maintain compatibility with `rich` and add minor enhancements.
Warnings
- breaking Future versions (v1.3.0+) will require Python 3.9 or newer. Version 1.2.7 (and earlier) supports Python 3.6+.
- gotcha The `Console` and `RichHandler` classes from `enrich` are distinct from their `rich` counterparts. Using `from rich.console import Console` will *not* provide the `enrich`-specific features like `redirect` or `soft_wrap` parameters.
- gotcha The `enrich` library is built upon `rich`, and breaking changes or incompatibilities in `rich` itself can affect `enrich` users. Specific versions of `enrich` might pin compatible `rich` versions.
Install
-
pip install enrich
Imports
- Console
from enrich.console import Console
- RichHandler
from enrich.logging import RichHandler
Quickstart
import logging
from enrich.console import Console
from enrich.logging import RichHandler
import sys
# Example 1: Console with redirect support
console_redirect = Console(redirect=True, record=True)
original_stdout = sys.stdout
sys.stdout = console_redirect.fileproxy
print("This output is redirected and recorded.")
sys.stdout = original_stdout # Restore stdout
# print(f"Captured output: {console_redirect.export_text()}") # Use this to see recorded text
# Example 2: Console with implicit soft wrapping
console_soft_wrap = Console(soft_wrap=True)
console_soft_wrap.print("This is a very long line of text that will be soft-wrapped by the console instance, ensuring it fits within any terminal width without explicit print arguments.")
# Example 3: Soft-wrapping logger
FORMAT = "%(message)s"
logging.basicConfig(
level="INFO", format=FORMAT, handlers=[RichHandler(soft_wrap=True)]
)
log = logging.getLogger("my_app")
log.info("This log message will also be soft-wrapped.")
log.info("Another long message demonstrating the soft-wrapping logger functionality.")