|
| 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 | + # Create a custom theme |
| 20 | + custom_theme = { |
| 21 | + Cmd2Style.SUCCESS: Style(color=Color.CYAN), |
| 22 | + Cmd2Style.WARNING: Style(color=Color.MAGENTA), |
| 23 | + Cmd2Style.ERROR: Style(color=Color.BRIGHT_RED), |
| 24 | + "argparse.args": Style(color=Color.AQUAMARINE3, underline=True), |
| 25 | + "inspect.attr": Style(color=Color.DARK_GOLDENROD, bold=True), |
| 26 | + } |
| 27 | + ru.set_theme(custom_theme) |
| 28 | + |
| 29 | + @cmd2.with_category("Theme Commands") |
| 30 | + def do_theme_show(self, _: cmd2.Statement): |
| 31 | + """Showcases the custom theme by printing messages with different styles.""" |
| 32 | + self.poutput("This is a basic output message.") |
| 33 | + self.psuccess("This is a success message.") |
| 34 | + self.warning("This is a warning message.") |
| 35 | + self.error("This is an error message.") |
| 36 | + |
| 37 | + |
| 38 | +if __name__ == "__main__": |
| 39 | + import sys |
| 40 | + |
| 41 | + app = ThemedApp() |
| 42 | + sys.exit(app.cmdloop()) |
0 commit comments