Skip to content

Commit d6af09c

Browse files
authored
Add example for setting a theme (#1497)
* Draft example for setting a theme * Fix typos * Add some more elements to the custom theme * Added in a custom self.doc_leader * Add in setting background color for one style to make it discoverable that this is an option * Change some styles to show off different options * Add comments about how colors can be defined * Fix typo * Overrode "traceback.exc_type"
1 parent b9c6369 commit d6af09c

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

examples/rich_theme.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python3
2+
"""A simple example of setting a custom theme for a cmd2 application."""
3+
4+
from rich.style import Style
5+
6+
import cmd2
7+
import cmd2.rich_utils as ru
8+
from cmd2 import Cmd2Style, Color
9+
10+
11+
class ThemedApp(cmd2.Cmd):
12+
"""A simple cmd2 application with a custom theme."""
13+
14+
def __init__(self, *args, **kwargs):
15+
"""Initialize the application."""
16+
super().__init__(*args, **kwargs)
17+
self.intro = "This is a themed application. Try the 'theme_show' command."
18+
19+
# Set text which prints right before all of the help tables are listed.
20+
self.doc_leader = "Welcome to this glorious help ..."
21+
22+
# Create a custom theme
23+
# Colors can come from the cmd2.color.Color StrEnum class, be RGB hex values, or
24+
# be any of the rich standard colors: https://rich.readthedocs.io/en/stable/appendix/colors.html
25+
custom_theme = {
26+
Cmd2Style.SUCCESS: Style(color=Color.GREEN), # Use color from cmd2 Color class
27+
Cmd2Style.WARNING: Style(color=Color.ORANGE1),
28+
Cmd2Style.ERROR: Style(color=Color.PINK1),
29+
Cmd2Style.HELP_HEADER: Style(color=Color.CYAN, bgcolor="#44475a"),
30+
Cmd2Style.HELP_LEADER: Style(color="#f8f8f2", bgcolor="#282a36"), # use RGB hex colors
31+
Cmd2Style.TABLE_BORDER: Style(color="turquoise2"), # use a rich standard color
32+
"traceback.exc_type": Style(color=Color.RED, bgcolor=Color.LIGHT_YELLOW3),
33+
"argparse.args": Style(color=Color.AQUAMARINE3, underline=True),
34+
"inspect.attr": Style(color=Color.DARK_GOLDENROD, bold=True),
35+
}
36+
ru.set_theme(custom_theme)
37+
38+
@cmd2.with_category("Theme Commands")
39+
def do_theme_show(self, _: cmd2.Statement):
40+
"""Showcases the custom theme by printing messages with different styles."""
41+
# NOTE: Using soft_wrap=False will ensure display looks correct when background colors are part of the style
42+
self.poutput("This is a basic output message.")
43+
self.psuccess("This is a success message.", soft_wrap=False)
44+
self.pwarning("This is a warning message.", soft_wrap=False)
45+
self.perror("This is an error message.", soft_wrap=False)
46+
self.pexcept(ValueError("This is a dummy ValueError exception."))
47+
48+
49+
if __name__ == "__main__":
50+
import sys
51+
52+
app = ThemedApp()
53+
sys.exit(app.cmdloop())

0 commit comments

Comments
 (0)