Enrich (Rich Extensions)

1.2.7 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates key features of the `enrich` library: a `Console` instance configured to redirect `sys.stdout` and capture output, another `Console` instance with implicit soft-wrapping enabled for print statements, and the `RichHandler` configured for soft-wrapping in standard Python logging.

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.")

view raw JSON →