Skip to content

Commit 5081947

Browse files
committed
Add a simple example to demonstrate the colors available in cmd2.Color
1 parent 28729a2 commit 5081947

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

examples/color.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
"""A sample application for cmd2. Demonstrating colors available in the cmd2.colors.Color enum.
3+
4+
Execute the taste_the_rainbow command to see the colors available.
5+
"""
6+
7+
from rich.style import Style
8+
9+
import cmd2
10+
from cmd2.colors import Color
11+
12+
13+
class CmdLineApp(cmd2.Cmd):
14+
"""Example cmd2 application demonstrating colorized output."""
15+
16+
def __init__(self) -> None:
17+
# Set include_ipy to True to enable the "ipy" command which runs an interactive IPython shell
18+
super().__init__(include_ipy=True)
19+
self.intro = 'Run the taste_the_rainbow command to see all of the colors available to you in cmd2.'
20+
21+
rainbow_parser = cmd2.Cmd2ArgumentParser()
22+
rainbow_parser.add_argument('-b', '--background', action='store_true', help='Show background colors as well')
23+
24+
@cmd2.with_argparser(rainbow_parser)
25+
def do_taste_the_rainbow(self, args) -> None:
26+
"""Show all of the colors available within cmd2's Color StrEnum class."""
27+
28+
for color_member in Color:
29+
style = Style(bgcolor=color_member.value) if args.background else Style(color=color_member.value)
30+
self.poutput(f"{color_member.name}", style=style)
31+
32+
33+
if __name__ == '__main__':
34+
import sys
35+
36+
c = CmdLineApp()
37+
sys.exit(c.cmdloop())

0 commit comments

Comments
 (0)