cmd2.rich_utils
cmd2.rich_utils
Provides common utilities to support Rich in cmd2-based applications.
AllowStyle
RichPrintKwargs
Bases: TypedDict
Keyword arguments that can be passed to rich.console.Console.print() via cmd2's print methods.
See Rich's Console.print() documentation for full details on these parameters. https://rich.readthedocs.io/en/stable/reference/console.html#rich.console.Console.print
Note: All fields are optional (total=False). If a key is not present in the dictionary, Rich's default behavior for that argument will apply.
Cmd2BaseConsole
Bases: Console
Base class for all cmd2 Rich consoles.
This class handles the core logic for managing Rich behavior based on
cmd2's global settings, such as ALLOW_STYLE and APP_THEME.
Cmd2BaseConsole initializer.
| PARAMETER | DESCRIPTION |
|---|---|
file
|
optional file object where the console should write to. Defaults to sys.stdout.
TYPE:
|
kwargs
|
keyword arguments passed to the parent Console class.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
if disallowed keyword argument is passed in. |
Source code in cmd2/rich_utils.py
on_broken_pipe
render_str
Override to ensure formatting overrides passed to print() and log() are respected.
Source code in cmd2/rich_utils.py
print
print(*objects, sep=' ', end='\n', style=None, justify=None, overflow=None, no_wrap=None, emoji=None, markup=None, highlight=None, width=None, height=None, crop=True, soft_wrap=None, new_line_start=False)
Override to support ANSI sequences and address a bug in Rich.
This method calls cmd2.rich_utils.prepare_objects_for_rendering on the objects being printed. This ensures that strings containing ANSI style sequences are converted to Rich Text objects, so that Rich can correctly calculate their display width.
Additionally, it works around a bug in Rich where complex renderables (like Table and Rule) may not receive formatting settings passed to print(). By temporarily injecting these settings into thread-local storage, we ensure that all internal rendering calls within the print() operation respect the requested overrides.
There is an issue on Rich to fix the latter: https://github.com/Textualize/rich/issues/4028
Source code in cmd2/rich_utils.py
log
log(*objects, sep=' ', end='\n', style=None, justify=None, emoji=None, markup=None, highlight=None, log_locals=False, _stack_offset=1)
Override to support ANSI sequences and address a bug in Rich.
This method calls cmd2.rich_utils.prepare_objects_for_rendering on the objects being logged. This ensures that strings containing ANSI style sequences are converted to Rich Text objects, so that Rich can correctly calculate their display width.
Additionally, it works around a bug in Rich where complex renderables (like Table and Rule) may not receive formatting settings passed to log(). By temporarily injecting these settings into thread-local storage, we ensure that all internal rendering calls within the log() operation respect the requested overrides.
There is an issue on Rich to fix the latter: https://github.com/Textualize/rich/issues/4028
Source code in cmd2/rich_utils.py
Cmd2GeneralConsole
Bases: Cmd2BaseConsole
Rich console for general-purpose printing.
It enables soft wrap and disables Rich's automatic detection for markup, emoji, and highlighting. These defaults can be overridden in calls to the console's or cmd2's print methods.
Cmd2GeneralConsole initializer.
| PARAMETER | DESCRIPTION |
|---|---|
file
|
optional file object where the console should write to. Defaults to sys.stdout.
TYPE:
|
Source code in cmd2/rich_utils.py
on_broken_pipe
render_str
Override to ensure formatting overrides passed to print() and log() are respected.
Source code in cmd2/rich_utils.py
print
print(*objects, sep=' ', end='\n', style=None, justify=None, overflow=None, no_wrap=None, emoji=None, markup=None, highlight=None, width=None, height=None, crop=True, soft_wrap=None, new_line_start=False)
Override to support ANSI sequences and address a bug in Rich.
This method calls cmd2.rich_utils.prepare_objects_for_rendering on the objects being printed. This ensures that strings containing ANSI style sequences are converted to Rich Text objects, so that Rich can correctly calculate their display width.
Additionally, it works around a bug in Rich where complex renderables (like Table and Rule) may not receive formatting settings passed to print(). By temporarily injecting these settings into thread-local storage, we ensure that all internal rendering calls within the print() operation respect the requested overrides.
There is an issue on Rich to fix the latter: https://github.com/Textualize/rich/issues/4028
Source code in cmd2/rich_utils.py
log
log(*objects, sep=' ', end='\n', style=None, justify=None, emoji=None, markup=None, highlight=None, log_locals=False, _stack_offset=1)
Override to support ANSI sequences and address a bug in Rich.
This method calls cmd2.rich_utils.prepare_objects_for_rendering on the objects being logged. This ensures that strings containing ANSI style sequences are converted to Rich Text objects, so that Rich can correctly calculate their display width.
Additionally, it works around a bug in Rich where complex renderables (like Table and Rule) may not receive formatting settings passed to log(). By temporarily injecting these settings into thread-local storage, we ensure that all internal rendering calls within the log() operation respect the requested overrides.
There is an issue on Rich to fix the latter: https://github.com/Textualize/rich/issues/4028
Source code in cmd2/rich_utils.py
Cmd2RichArgparseConsole
Bases: Cmd2BaseConsole
Rich console for rich-argparse output.
Ensures long lines in help text are not truncated by disabling soft_wrap, which conflicts with rich-argparse's explicit no_wrap and overflow settings.
Since this console is used to print error messages which may not be intended for Rich formatting, it disables Rich's automatic detection for markup, emoji, and highlighting. Because rich-argparse does markup and highlighting without involving the console, disabling these settings does not affect the library's internal functionality.
Cmd2RichArgparseConsole initializer.
| PARAMETER | DESCRIPTION |
|---|---|
file
|
optional file object where the console should write to. Defaults to sys.stdout.
TYPE:
|
Source code in cmd2/rich_utils.py
on_broken_pipe
render_str
Override to ensure formatting overrides passed to print() and log() are respected.
Source code in cmd2/rich_utils.py
print
print(*objects, sep=' ', end='\n', style=None, justify=None, overflow=None, no_wrap=None, emoji=None, markup=None, highlight=None, width=None, height=None, crop=True, soft_wrap=None, new_line_start=False)
Override to support ANSI sequences and address a bug in Rich.
This method calls cmd2.rich_utils.prepare_objects_for_rendering on the objects being printed. This ensures that strings containing ANSI style sequences are converted to Rich Text objects, so that Rich can correctly calculate their display width.
Additionally, it works around a bug in Rich where complex renderables (like Table and Rule) may not receive formatting settings passed to print(). By temporarily injecting these settings into thread-local storage, we ensure that all internal rendering calls within the print() operation respect the requested overrides.
There is an issue on Rich to fix the latter: https://github.com/Textualize/rich/issues/4028
Source code in cmd2/rich_utils.py
log
log(*objects, sep=' ', end='\n', style=None, justify=None, emoji=None, markup=None, highlight=None, log_locals=False, _stack_offset=1)
Override to support ANSI sequences and address a bug in Rich.
This method calls cmd2.rich_utils.prepare_objects_for_rendering on the objects being logged. This ensures that strings containing ANSI style sequences are converted to Rich Text objects, so that Rich can correctly calculate their display width.
Additionally, it works around a bug in Rich where complex renderables (like Table and Rule) may not receive formatting settings passed to log(). By temporarily injecting these settings into thread-local storage, we ensure that all internal rendering calls within the log() operation respect the requested overrides.
There is an issue on Rich to fix the latter: https://github.com/Textualize/rich/issues/4028
Source code in cmd2/rich_utils.py
Cmd2ExceptionConsole
Bases: Cmd2BaseConsole
Rich console for printing exceptions and Rich Tracebacks.
Ensures that output is always word-wrapped for readability and disables Rich's automatic detection for markup, emoji, and highlighting to prevent interference with raw error data.
Cmd2ExceptionConsole initializer.
| PARAMETER | DESCRIPTION |
|---|---|
file
|
optional file object where the console should write to. Defaults to sys.stdout.
TYPE:
|
Source code in cmd2/rich_utils.py
on_broken_pipe
render_str
Override to ensure formatting overrides passed to print() and log() are respected.
Source code in cmd2/rich_utils.py
print
print(*objects, sep=' ', end='\n', style=None, justify=None, overflow=None, no_wrap=None, emoji=None, markup=None, highlight=None, width=None, height=None, crop=True, soft_wrap=None, new_line_start=False)
Override to support ANSI sequences and address a bug in Rich.
This method calls cmd2.rich_utils.prepare_objects_for_rendering on the objects being printed. This ensures that strings containing ANSI style sequences are converted to Rich Text objects, so that Rich can correctly calculate their display width.
Additionally, it works around a bug in Rich where complex renderables (like Table and Rule) may not receive formatting settings passed to print(). By temporarily injecting these settings into thread-local storage, we ensure that all internal rendering calls within the print() operation respect the requested overrides.
There is an issue on Rich to fix the latter: https://github.com/Textualize/rich/issues/4028
Source code in cmd2/rich_utils.py
log
log(*objects, sep=' ', end='\n', style=None, justify=None, emoji=None, markup=None, highlight=None, log_locals=False, _stack_offset=1)
Override to support ANSI sequences and address a bug in Rich.
This method calls cmd2.rich_utils.prepare_objects_for_rendering on the objects being logged. This ensures that strings containing ANSI style sequences are converted to Rich Text objects, so that Rich can correctly calculate their display width.
Additionally, it works around a bug in Rich where complex renderables (like Table and Rule) may not receive formatting settings passed to log(). By temporarily injecting these settings into thread-local storage, we ensure that all internal rendering calls within the log() operation respect the requested overrides.
There is an issue on Rich to fix the latter: https://github.com/Textualize/rich/issues/4028
Source code in cmd2/rich_utils.py
set_theme
Set the Rich theme used by cmd2.
Call set_theme() with no arguments to reset to the default theme. This will clear any custom styles that were previously applied.
| PARAMETER | DESCRIPTION |
|---|---|
styles
|
optional mapping of style names to styles
TYPE:
|
Source code in cmd2/rich_utils.py
console_width
rich_text_to_string
Convert a Rich Text object to a string.
This function's purpose is to render a Rich Text object, including any styles (e.g., color, bold),
to a plain Python string with ANSI style sequences. It differs from text.plain, which strips
all formatting.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
the text object to convert
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
the resulting string with ANSI styles preserved. |
Source code in cmd2/rich_utils.py
indent
Indent a Rich renderable.
When soft-wrapping is enabled, a Rich console is unable to properly print a Padding object of indented text, as it truncates long strings instead of wrapping them. This function provides a workaround for this issue, ensuring that indented text is printed correctly regardless of the soft-wrap setting.
For non-text objects, this function merely serves as a convenience wrapper around Padding.indent().
| PARAMETER | DESCRIPTION |
|---|---|
renderable
|
a Rich renderable to indent.
TYPE:
|
level
|
number of characters to indent.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Padding
|
a Padding object containing the indented content. |
Source code in cmd2/rich_utils.py
prepare_objects_for_rendering
Prepare a tuple of objects for printing by Rich's Console.print().
This function processes objects to ensure they are rendered correctly by Rich. It inspects each object and, if its string representation contains ANSI style sequences, it converts the object to a Rich Text object. This ensures Rich can properly parse the non-printing codes for accurate display width calculation.
Objects that already implement the Rich console protocol or are expandable by its pretty printer are left untouched, as they can be handled directly by Rich's native renderers.
| PARAMETER | DESCRIPTION |
|---|---|
objects
|
objects to prepare
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Any, ...]
|
a tuple containing the processed objects. |