Set the Rich theme used by cmd2. This function performs an in-place update of the existing theme's styles. This ensures that any Console objects already using the theme will reflect the changes immediately without needing to be recreated. Call set_theme() with no arguments to reset
(styles: Mapping[str, StyleType] | None = None)
| 319 | |
| 320 | |
| 321 | def set_theme(styles: Mapping[str, StyleType] | None = None) -> None: |
| 322 | """Set the Rich theme used by cmd2. |
| 323 | |
| 324 | This function performs an in-place update of the existing theme's |
| 325 | styles. This ensures that any Console objects already using the theme |
| 326 | will reflect the changes immediately without needing to be recreated. |
| 327 | |
| 328 | Call set_theme() with no arguments to reset to the default theme. |
| 329 | This will clear any custom styles that were previously applied. |
| 330 | |
| 331 | :param styles: optional mapping of style names to styles |
| 332 | """ |
| 333 | theme = get_theme() |
| 334 | |
| 335 | # Start with a fresh copy of the default styles. |
| 336 | unparsed_styles: dict[str, StyleType] = {} |
| 337 | unparsed_styles.update(_create_default_theme().styles) |
| 338 | |
| 339 | # Add the custom styles, which may contain unparsed strings |
| 340 | if styles is not None: |
| 341 | unparsed_styles.update(styles) |
| 342 | |
| 343 | # Use Rich's Theme class to perform the parsing |
| 344 | parsed_styles = Theme(unparsed_styles).styles |
| 345 | |
| 346 | # Perform the in-place update with the results |
| 347 | theme.styles.clear() |
| 348 | theme.styles.update(parsed_styles) |
| 349 | |
| 350 | # Synchronize rich-argparse styles with the main application theme. |
| 351 | for name in Cmd2HelpFormatter.styles.keys() & theme.styles.keys(): |
| 352 | Cmd2HelpFormatter.styles[name] = theme.styles[name] |
| 353 | |
| 354 | |
| 355 | def _create_default_theme() -> Theme: |
nothing calls this directly
no test coverage detected
searching dependent graphs…