cmd2.rich_utils
cmd2.rich_utils
Provides common utilities to support Rich in cmd2-based applications.
HelpFormatterRenderable
Bases: Protocol
Protocol for objects that require a Cmd2HelpFormatter to render.
AllowStyle
Cmd2HelpFormatter
Cmd2HelpFormatter(
prog,
indent_increment=2,
max_help_position=24,
width=None,
*,
file=None,
console=None,
**kwargs,
)
Bases: RichHelpFormatter
Custom help formatter to configure ordering of help text.
Initialize Cmd2HelpFormatter.
Source code in cmd2/rich_utils.py
add_text
Override to support HelpFormatterRenderable objects.
RawDescriptionCmd2HelpFormatter
RawDescriptionCmd2HelpFormatter(
prog,
indent_increment=2,
max_help_position=24,
width=None,
*,
file=None,
console=None,
**kwargs,
)
Bases: RawDescriptionRichHelpFormatter, Cmd2HelpFormatter
Cmd2 help message formatter which retains any formatting in descriptions and epilogs.
Initialize Cmd2HelpFormatter.
Source code in cmd2/rich_utils.py
add_text
Override to support HelpFormatterRenderable objects.
RawTextCmd2HelpFormatter
RawTextCmd2HelpFormatter(
prog,
indent_increment=2,
max_help_position=24,
width=None,
*,
file=None,
console=None,
**kwargs,
)
Bases: RawTextRichHelpFormatter, Cmd2HelpFormatter
Cmd2 help message formatter which retains formatting of all help text.
Initialize Cmd2HelpFormatter.
Source code in cmd2/rich_utils.py
add_text
Override to support HelpFormatterRenderable objects.
ArgumentDefaultsCmd2HelpFormatter
ArgumentDefaultsCmd2HelpFormatter(
prog,
indent_increment=2,
max_help_position=24,
width=None,
*,
file=None,
console=None,
**kwargs,
)
Bases: ArgumentDefaultsRichHelpFormatter, Cmd2HelpFormatter
Cmd2 help message formatter which adds default values to argument help.
Initialize Cmd2HelpFormatter.
Source code in cmd2/rich_utils.py
add_text
Override to support HelpFormatterRenderable objects.
MetavarTypeCmd2HelpFormatter
MetavarTypeCmd2HelpFormatter(
prog,
indent_increment=2,
max_help_position=24,
width=None,
*,
file=None,
console=None,
**kwargs,
)
Bases: MetavarTypeRichHelpFormatter, Cmd2HelpFormatter
Cmd2 help message formatter which uses the argument 'type' as the default metavar value (instead of the argument 'dest').
Initialize Cmd2HelpFormatter.
Source code in cmd2/rich_utils.py
add_text
Override to support HelpFormatterRenderable objects.
TextGroup
A block of text which is formatted like an argparse argument group, including a title.
Title: Here is the first row of text. Here is yet another row of text.
TextGroup initializer.
| PARAMETER | DESCRIPTION |
|---|---|
title
|
the group's title
TYPE:
|
text
|
the group's text (string or object that may be rendered by Rich)
TYPE:
|
Source code in cmd2/rich_utils.py
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 the application 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
matches_config
Check if this console instance was initialized with the specified settings.
| PARAMETER | DESCRIPTION |
|---|---|
file
|
file stream being checked
TYPE:
|
kwargs
|
other console settings being checked
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the settings match this console's configuration |
Source code in cmd2/rich_utils.py
on_broken_pipe
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.
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.
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.
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.
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.
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
matches_config
Check if this console instance was initialized with the specified settings.
| PARAMETER | DESCRIPTION |
|---|---|
file
|
file stream being checked
TYPE:
|
kwargs
|
other console settings being checked
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the settings match this console's configuration |
Source code in cmd2/rich_utils.py
on_broken_pipe
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.
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.
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.
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.
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
matches_config
Check if this console instance was initialized with the specified settings.
| PARAMETER | DESCRIPTION |
|---|---|
file
|
file stream being checked
TYPE:
|
kwargs
|
other console settings being checked
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the settings match this console's configuration |
Source code in cmd2/rich_utils.py
on_broken_pipe
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.
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.
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.
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.
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
matches_config
Check if this console instance was initialized with the specified settings.
| PARAMETER | DESCRIPTION |
|---|---|
file
|
file stream being checked
TYPE:
|
kwargs
|
other console settings being checked
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the settings match this console's configuration |
Source code in cmd2/rich_utils.py
on_broken_pipe
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.
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.
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.
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.
Source code in cmd2/rich_utils.py
Cmd2SimpleTable
Bases: Table
A clean, lightweight Rich Table tailored for cmd2's internal use.
Cmd2SimpleTable initializer.
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. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
if text is not a rich.text.Text object |
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. |