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
Cmd2GeneralConsole
Bases: Cmd2BaseConsole
Rich console for general-purpose printing.
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
Cmd2RichArgparseConsole
Bases: Cmd2BaseConsole
Rich console for rich-argparse output.
This class ensures long lines in help text are not truncated by avoiding soft_wrap, which conflicts with rich-argparse's explicit no_wrap and overflow settings.
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
Cmd2ExceptionConsole
Bases: Cmd2BaseConsole
Rich console for printing exceptions.
Ensures that long exception messages word wrap for readability by keeping soft_wrap disabled.
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
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. |