Skip to content

Commit fca3b95

Browse files
Add a simple example to demonstrate the colors available in cmd2.Color (#1482)
Also: - Deleted old examples/colors.py and updated documentation --------- Co-authored-by: Kevin Van Brunt <[email protected]>
1 parent cab433d commit fca3b95

File tree

4 files changed

+54
-93
lines changed

4 files changed

+54
-93
lines changed

docs/features/completion.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ Tab completion of argument values can be configured by using one of three parame
9696
- `completer`
9797

9898
See the [arg_decorators](https://github.com/python-cmd2/cmd2/blob/main/examples/arg_decorators.py)
99-
or [colors](https://github.com/python-cmd2/cmd2/blob/main/examples/colors.py) example for a
100-
demonstration of how to use the `choices` parameter. See the
99+
example for a demonstration of how to use the `choices` parameter. See the
101100
[argparse_completion](https://github.com/python-cmd2/cmd2/blob/main/examples/argparse_completion.py)
102101
example for a demonstration of how to use the `choices_provider` parameter. See the
103102
[arg_decorators](https://github.com/python-cmd2/cmd2/blob/main/examples/arg_decorators.py) or

examples/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ each:
3232
- Show how to enable custom tab completion by assigning a completer function to `do_*` commands
3333
- [cmd2_as_argument.py](https://github.com/python-cmd2/cmd2/blob/main/examples/cmd_as_argument.py)
3434
- Demonstrates how to accept and parse command-line arguments when invoking a cmd2 application
35-
- [colors.py](https://github.com/python-cmd2/cmd2/blob/main/examples/colors.py)
36-
- Show various ways of using colorized output within a cmd2 application
35+
- [color.py](https://github.com/python-cmd2/cmd2/blob/main/examples/color.py)
36+
- Show the numerous colors available to use in your cmd2 applications
3737
- [custom_parser.py](https://github.com/python-cmd2/cmd2/blob/main/examples/custom_parser.py)
3838
- Demonstrates how to create your own custom `Cmd2ArgumentParser`
3939
- [decorator_example.py](https://github.com/python-cmd2/cmd2/blob/main/examples/decorator_example.py)

examples/color.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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())

examples/colors.py

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)