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