cmd2.theme
cmd2.theme
Provides a centralized theming system for cmd2.
This module manages the global theme used for both Rich terminal output and prompt-toolkit interactive components. It ensures that styling is consistent and synchronized across the entire application when the theme is updated using the following strategy.
- Rich consoles use a persistent Theme object that is updated in-place.
- prompt-toolkit integration in cmd2 uses a DynamicStyle wrapper around a callable that returns the current prompt-toolkit theme.
To prevent Rich's built-in styles (like 'bold', or 'italic') from polluting the prompt-toolkit namespace or colliding with its own internal names, only styles starting with registered prefixes (e.g., 'cmd2.') or those explicitly mapped to UI elements are synchronized to the prompt-toolkit theme.
get_theme
get_pt_theme
Get the application-wide prompt-toolkit style. Initializes it on the first call.
reset_theme
Reset the application-wide theme to its initial state.
This function performs an in-place reset of the existing Rich theme's styles. This ensures that any Console objects already using the theme will reflect the changes immediately without needing to be recreated.
Changes are automatically propagated to all synchronized components.
Source code in cmd2/theme.py
update_theme
Update the existing theme.
This function performs an in-place update of the existing Rich theme's styles. This ensures that any Console objects already using the theme will reflect the changes immediately without needing to be recreated.
Changes are automatically propagated to all synchronized components.
| PARAMETER | DESCRIPTION |
|---|---|
styles
|
mapping of style names to styles
TYPE:
|
Source code in cmd2/theme.py
register_pt_mapping
Map a Rich theme style name to one or more prompt-toolkit UI components.
This enables styling of prompt-toolkit's internal elements (such as the completion menu) using styles in the application's Rich theme.
Registering a mapping also flags the style for synchronization to the prompt-toolkit theme, making it accessible via 'class:style_name'.
| PARAMETER | DESCRIPTION |
|---|---|
style_name
|
The style name used in the Rich theme.
TYPE:
|
pt_ui_names
|
One or more prompt-toolkit UI component names (e.g., 'completion-menu').
TYPE:
|
Source code in cmd2/theme.py
register_synchronized_style
Register a Rich theme style for synchronization with prompt-toolkit.
This ensures that the style is synchronized to the prompt-toolkit theme (accessible via 'class:style_name') even if it does not begin with a registered prefix.
| PARAMETER | DESCRIPTION |
|---|---|
style_name
|
The style name used in the Rich theme.
TYPE:
|
Source code in cmd2/theme.py
unregister_pt_mapping
Remove one or more prompt-toolkit UI component mappings.
The style itself remains in the synchronization mapping (even if no UI component mappings remain), ensuring it continues to be synchronized to the prompt-toolkit theme.
To completely remove a style from synchronization, use unregister_synchronized_style().
| PARAMETER | DESCRIPTION |
|---|---|
style_name
|
The style name used in the Rich theme.
TYPE:
|
pt_ui_names
|
Specific UI component(s) to unmap.
TYPE:
|
Source code in cmd2/theme.py
unregister_synchronized_style
Stop synchronizing a Rich theme style with prompt-toolkit.
This removes the style and all its mappings entirely from the prompt-toolkit theme synchronization.
| PARAMETER | DESCRIPTION |
|---|---|
style_name
|
The style name to unregister.
TYPE:
|
Source code in cmd2/theme.py
register_synchronized_prefix
Register a prefix whose styles will be synchronized to the prompt-toolkit theme.
The prefix must include any desired delimiters (e.g., 'myapp.' or 'plugin-').
| PARAMETER | DESCRIPTION |
|---|---|
prefix
|
The prefix string. Must be at least 1 character.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the prefix is empty. |
Source code in cmd2/theme.py
unregister_synchronized_prefix
Stop synchronizing styles starting with the given prefix.
| PARAMETER | DESCRIPTION |
|---|---|
prefix
|
The prefix string to remove.
TYPE:
|