简体中文 | English
richuru is a lightweight dependency that provides rich
's powerful terminal rendering support for loguru
.
This project is sourced and independent from the component graia.amnesia.log
of Graia Amnesia
,
and is modified from the original implementation of @BlueGlassBlock.
Install richuru
:
elaina@localhost $ pip install richuru
elaina@localhost $ pdm add richuru
elaina@localhost $ poetry add richuru
Try it in REPL:
>>> import richuru
>>> richuru.install()
>>>
>>> from loguru import logger
>>> logger.info("Hello World!", alt="Hello, [bold magenta]World[/bold magenta]!")
richuru
is designed as an optional dependency, which means that you need to provide a fallback pattern when using richuru
wherever possible, to be compatible with end-users who do not have richuru
enabled.
Provide alt
in logger.<method>(...)
with the alt
option to make richuru
recognize and enable rich
's rich text functionality.
Of course, you can also specify an entire entry's rich text style without using alt
:
logger.info("i'm in rich!", style="bold red")
which is equivalent to :
logger.info("i'm in rich!", alt="[bold red]i'm in rich![/]")
# without richuru: no, you are not in rich :P
Print the ConsoleRenderable
object:
from rich.markdown import Markdown
with open("README.md") as readme:
markdown = Markdown(readme.read())
logger.info("fallback msg", rich=markdown)
This works with static elements like Table
, Markdown
When you need to use dynamic elements, you need to pass the rich Console
object you are using to install
. In fact, if you need to print any ConsoleRenderable
to standard output, it is recommended to use a singleton Console
.
from richuru import install
from rich import Console
console = Console(
theme=Theme( # required, otherwise the color will be incorrect
{
'logging.level.success': 'green',
'logging.level.trace': 'bright_black',
}
)
)
install(rich_console=console)
with Progress(
TextColumn('[bold green]{task.description}'),
BarColumn(bar_width=None),
MofNCompleteColumn(),
'•',
TimeElapsedColumn(),
console=console,
) as progress:
task = progress.add_task('[cyan]Working...', total=10)
while not progress.finished:
logger.success('Hello, World!')
progress.update(task, advance=1)
progress.refresh()
sleep(0.2)
This project is open source under the MIT License.