Debian Codegraph @

examples/color.py

raw symbols SCIP
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 import argparse
8
9 from rich.style import Style
10 from rich.text import Text
11
12 import cmd2
13 from cmd2 import Color
14
15
16 class CmdLineApp(cmd2.Cmd):
17 """Example cmd2 application demonstrating colorized output."""
18
19 def __init__(self) -> None:
20 # Set include_ipy to True to enable the "ipy" command which runs an interactive IPython shell
21 super().__init__(include_ipy=True)
22 self.intro = "Run the taste_the_rainbow command to see all of the colors available to you in cmd2."
23
24 rainbow_parser = cmd2.Cmd2ArgumentParser()
25 rainbow_parser.add_argument("-b", "--background", action="store_true", help="show background colors as well")
26 rainbow_parser.add_argument("-p", "--paged", action="store_true", help="display output using a pager")
27
28 @cmd2.with_argparser(rainbow_parser)
29 def do_taste_the_rainbow(self, args: argparse.Namespace) -> None:
30 """Show all of the colors available within cmd2's Color StrEnum class."""
31
32 def create_style(color: Color) -> Style:
33 """Create a foreground or background color Style."""
34 if args.background:
35 return Style(bgcolor=color)
36 return Style(color=color)
37
38 styled_names = [Text(color.name, style=create_style(color)) for color in Color]
39 output = Text("\n").join(styled_names)
40
41 if args.paged:
42 self.ppaged(output)
43 else:
44 self.poutput(output)
45
46
47 if __name__ == "__main__":
48 import sys
49
50 c = CmdLineApp()
51 sys.exit(c.cmdloop())