1
- """Provides common utilities to support Rich in cmd2 applications."""
1
+ """Provides common utilities to support Rich in cmd2-based applications."""
2
2
3
3
from collections .abc import Mapping
4
4
from enum import Enum
17
17
RichCast ,
18
18
)
19
19
from rich .style import (
20
- Style ,
21
20
StyleType ,
22
21
)
23
22
from rich .text import Text
@@ -47,47 +46,41 @@ def __repr__(self) -> str:
47
46
ALLOW_STYLE = AllowStyle .TERMINAL
48
47
49
48
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.
52
51
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 )
63
57
64
- if styles is not None :
65
- cmd2_styles .update (styles )
66
58
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.
69
61
62
+ :param styles: optional mapping of style names to styles
63
+ """
64
+ global APP_THEME # noqa: PLW0603
70
65
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 )
73
69
70
+ # Incorporate custom styles.
71
+ if styles is not None :
72
+ app_styles .update (styles )
74
73
75
- def set_theme (new_theme : Cmd2Theme ) -> None :
76
- """Set the Rich theme used by cmd2.
74
+ APP_THEME = Theme (app_styles )
77
75
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 ]
82
79
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 ()
87
80
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 ()
91
84
92
85
93
86
class RichPrintKwargs (TypedDict , total = False ):
@@ -113,7 +106,7 @@ class RichPrintKwargs(TypedDict, total=False):
113
106
114
107
115
108
class Cmd2Console (Console ):
116
- """Rich console with characteristics appropriate for cmd2 applications."""
109
+ """Rich console with characteristics appropriate for cmd2-based applications."""
117
110
118
111
def __init__ (self , file : IO [str ] | None = None ) -> None :
119
112
"""Cmd2Console initializer.
@@ -147,7 +140,7 @@ def __init__(self, file: IO[str] | None = None) -> None:
147
140
markup = False ,
148
141
emoji = False ,
149
142
highlight = False ,
150
- theme = THEME ,
143
+ theme = APP_THEME ,
151
144
)
152
145
153
146
def on_broken_pipe (self ) -> None :
@@ -178,7 +171,7 @@ def rich_text_to_string(text: Text) -> str:
178
171
markup = False ,
179
172
emoji = False ,
180
173
highlight = False ,
181
- theme = THEME ,
174
+ theme = APP_THEME ,
182
175
)
183
176
with console .capture () as capture :
184
177
console .print (text , end = "" )
0 commit comments