Skip to content

Commit 3892724

Browse files
committed
Simplified color example.
1 parent 44d82ed commit 3892724

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

examples/color.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44
Execute the taste_the_rainbow command to see the colors available.
55
"""
66

7+
import argparse
8+
79
from rich.style import Style
810

911
import cmd2
10-
from cmd2.colors import Color
12+
from cmd2 import (
13+
Color,
14+
stylize,
15+
)
1116

1217

1318
class CmdLineApp(cmd2.Cmd):
@@ -19,15 +24,16 @@ def __init__(self) -> None:
1924
self.intro = 'Run the taste_the_rainbow command to see all of the colors available to you in cmd2.'
2025

2126
rainbow_parser = cmd2.Cmd2ArgumentParser()
22-
rainbow_parser.add_argument('-b', '--background', action='store_true', help='Show background colors as well')
27+
rainbow_parser.add_argument('-b', '--background', action='store_true', help='show background colors as well')
2328

2429
@cmd2.with_argparser(rainbow_parser)
25-
def do_taste_the_rainbow(self, args) -> None:
30+
def do_taste_the_rainbow(self, args: argparse.Namespace) -> None:
2631
"""Show all of the colors available within cmd2's Color StrEnum class."""
2732

2833
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, soft_wrap=False)
34+
style = Style(bgcolor=color_member) if args.background else Style(color=color_member)
35+
styled_name = stylize(color_member.name, style=style)
36+
self.poutput(styled_name)
3137

3238

3339
if __name__ == '__main__':

0 commit comments

Comments
 (0)