MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / set_theme

Function set_theme

cmd2/rich_utils.py:321–352  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

319
320
321def 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
355def _create_default_theme() -> Theme:

Callers

nothing calls this directly

Calls 3

get_themeFunction · 0.85
_create_default_themeFunction · 0.85
clearMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…