|
11 | 11 | from rich.console import Console |
12 | 12 | from rich.style import Style |
13 | 13 | from rich.theme import Theme |
| 14 | +from rich_argparse import RichHelpFormatter |
14 | 15 |
|
15 | 16 | # Default styles for printing strings of various types. |
16 | 17 | # These can be altered to suit an application's needs and only need to be a |
@@ -44,19 +45,29 @@ def __repr__(self) -> str: |
44 | 45 | # Controls when ANSI style sequences are allowed in output |
45 | 46 | allow_style = AllowStyle.TERMINAL |
46 | 47 |
|
| 48 | +# Rich theme used by Cmd2Console |
| 49 | +THEME: Optional[Theme] = None |
47 | 50 |
|
48 | | -# Default Rich theme used by Cmd2Console |
49 | | -DEFAULT_THEME: Optional[Theme] = None |
| 51 | +# Backup of default rich-argparse styles |
| 52 | +DEFAULT_RICH_ARGPARSE_STYLES = RichHelpFormatter.styles.copy() |
50 | 53 |
|
51 | 54 |
|
52 | | -def set_default_theme(theme: Optional[Theme]) -> None: |
| 55 | +def set_theme(theme: Optional[Theme]) -> None: |
53 | 56 | """ |
54 | | - Set the default Rich theme used by Cmd2Console. |
| 57 | + Set the Rich theme used by Cmd2Console and rich-argparse. |
55 | 58 |
|
56 | | - :param theme: new default theme or None if you want to use Rich's default |
| 59 | + :param theme: new theme or None if you want to use Rich and rich-argparse defaults. |
57 | 60 | """ |
58 | | - global DEFAULT_THEME |
59 | | - DEFAULT_THEME = theme |
| 61 | + global THEME |
| 62 | + THEME = theme |
| 63 | + |
| 64 | + # Update rich-argparse styles |
| 65 | + if theme is None: |
| 66 | + RichHelpFormatter.styles = DEFAULT_RICH_ARGPARSE_STYLES.copy() |
| 67 | + else: |
| 68 | + for name, style in theme.styles.items(): |
| 69 | + if name in RichHelpFormatter.styles: |
| 70 | + RichHelpFormatter.styles[name] = style |
60 | 71 |
|
61 | 72 |
|
62 | 73 | class Cmd2Console(Console): |
@@ -86,7 +97,7 @@ def __init__(self, file: IO[str]) -> None: |
86 | 97 | markup=False, |
87 | 98 | emoji=False, |
88 | 99 | highlight=False, |
89 | | - theme=DEFAULT_THEME, |
| 100 | + theme=THEME, |
90 | 101 | **kwargs, |
91 | 102 | ) |
92 | 103 |
|
|
0 commit comments