Skip to content

Commit aa38e49

Browse files
committed
Corrected theme management functions.
1 parent 44948e5 commit aa38e49

File tree

1 file changed

+29
-36
lines changed

1 file changed

+29
-36
lines changed

cmd2/rich_utils.py

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Provides common utilities to support Rich in cmd2 applications."""
1+
"""Provides common utilities to support Rich in cmd2-based applications."""
22

33
from collections.abc import Mapping
44
from enum import Enum
@@ -17,7 +17,6 @@
1717
RichCast,
1818
)
1919
from rich.style import (
20-
Style,
2120
StyleType,
2221
)
2322
from rich.text import Text
@@ -47,47 +46,41 @@ def __repr__(self) -> str:
4746
ALLOW_STYLE = AllowStyle.TERMINAL
4847

4948

50-
class Cmd2Theme(Theme):
51-
"""Rich theme class used by cmd2."""
49+
def _create_default_theme() -> Theme:
50+
"""Create a default theme for cmd2-based applications.
5251
53-
def __init__(self, styles: Mapping[str, StyleType] | None = None) -> None:
54-
"""Cmd2Theme initializer.
55-
56-
:param styles: optional mapping of style names on to styles.
57-
Defaults to None for a theme with no styles.
58-
"""
59-
cmd2_styles = DEFAULT_CMD2_STYLES.copy()
60-
61-
# Include default styles from rich-argparse
62-
cmd2_styles.update(RichHelpFormatter.styles.copy())
52+
This theme combines the default styles from cmd2, rich-argparse, and Rich.
53+
"""
54+
app_styles = DEFAULT_CMD2_STYLES.copy()
55+
app_styles.update(RichHelpFormatter.styles.copy())
56+
return Theme(app_styles, inherit=True)
6357

64-
if styles is not None:
65-
cmd2_styles.update(styles)
6658

67-
# Set inherit to True to include Rich's default styles
68-
super().__init__(cmd2_styles, inherit=True)
59+
def set_theme(styles: Mapping[str, StyleType] | None = None) -> None:
60+
"""Set the Rich theme used by cmd2.
6961
62+
:param styles: optional mapping of style names to styles
63+
"""
64+
global APP_THEME # noqa: PLW0603
7065

71-
# Current Rich theme used by Cmd2Console
72-
THEME: Cmd2Theme = Cmd2Theme()
66+
# Start with a fresh copy of the default styles.
67+
app_styles: dict[str, StyleType] = {}
68+
app_styles.update(_create_default_theme().styles)
7369

70+
# Incorporate custom styles.
71+
if styles is not None:
72+
app_styles.update(styles)
7473

75-
def set_theme(new_theme: Cmd2Theme) -> None:
76-
"""Set the Rich theme used by cmd2.
74+
APP_THEME = Theme(app_styles)
7775

78-
:param new_theme: new theme to use.
79-
"""
80-
global THEME # noqa: PLW0603
81-
THEME = new_theme
76+
# Synchronize rich-argparse styles with the main application theme.
77+
for name in RichHelpFormatter.styles.keys() & APP_THEME.styles.keys():
78+
RichHelpFormatter.styles[name] = APP_THEME.styles[name]
8279

83-
# Make sure the new theme has all style names included in a Cmd2Theme.
84-
missing_names = Cmd2Theme().styles.keys() - THEME.styles.keys()
85-
for name in missing_names:
86-
THEME.styles[name] = Style()
8780

88-
# Update rich-argparse styles
89-
for name in RichHelpFormatter.styles.keys() & THEME.styles.keys():
90-
RichHelpFormatter.styles[name] = THEME.styles[name]
81+
# The main theme for cmd2-based applications.
82+
# You can change it with set_theme().
83+
APP_THEME = _create_default_theme()
9184

9285

9386
class RichPrintKwargs(TypedDict, total=False):
@@ -113,7 +106,7 @@ class RichPrintKwargs(TypedDict, total=False):
113106

114107

115108
class Cmd2Console(Console):
116-
"""Rich console with characteristics appropriate for cmd2 applications."""
109+
"""Rich console with characteristics appropriate for cmd2-based applications."""
117110

118111
def __init__(self, file: IO[str] | None = None) -> None:
119112
"""Cmd2Console initializer.
@@ -147,7 +140,7 @@ def __init__(self, file: IO[str] | None = None) -> None:
147140
markup=False,
148141
emoji=False,
149142
highlight=False,
150-
theme=THEME,
143+
theme=APP_THEME,
151144
)
152145

153146
def on_broken_pipe(self) -> None:
@@ -178,7 +171,7 @@ def rich_text_to_string(text: Text) -> str:
178171
markup=False,
179172
emoji=False,
180173
highlight=False,
181-
theme=THEME,
174+
theme=APP_THEME,
182175
)
183176
with console.capture() as capture:
184177
console.print(text, end="")

0 commit comments

Comments
 (0)