Skip to content

Commit f080389

Browse files
committed
Updated color.py and argparse_completion.py examples for cmd2 3.0.
1 parent fca3b95 commit f080389

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

examples/argparse_completion.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33

44
import argparse
55

6+
from rich.style import Style
67
from rich.text import Text
78

89
from cmd2 import (
910
Cmd,
1011
Cmd2ArgumentParser,
12+
Color,
1113
CompletionError,
1214
CompletionItem,
1315
with_argparser,
@@ -18,8 +20,8 @@
1820

1921

2022
class ArgparseCompletion(Cmd):
21-
def __init__(self, *args, **kwargs) -> None:
22-
super().__init__(*args, **kwargs)
23+
def __init__(self) -> None:
24+
super().__init__(include_ipy=True)
2325
self.sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball']
2426

2527
def choices_provider(self) -> list[str]:
@@ -39,7 +41,10 @@ def choices_completion_error(self) -> list[str]:
3941

4042
def choices_completion_item(self) -> list[CompletionItem]:
4143
"""Return CompletionItem instead of strings. These give more context to what's being tab completed."""
42-
fancy_item = Text("These things can\ncontain newlines and\n") + Text("styled text!!", style="underline bright_yellow")
44+
fancy_item = Text.assemble(
45+
"These things can\ncontain newlines and\n",
46+
Text("styled text!!", style=Style(color=Color.BRIGHT_YELLOW, underline=True)),
47+
)
4348

4449
items = {1: "My item", 2: "Another item", 3: "Yet another item", 4: fancy_item}
4550
return [CompletionItem(item_id, [description]) for item_id, description in items.items()]

examples/color.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
import argparse
88

99
from rich.style import Style
10+
from rich.text import Text
1011

1112
import cmd2
12-
from cmd2 import (
13-
Color,
14-
stylize,
15-
)
13+
from cmd2 import Color
1614

1715

1816
class CmdLineApp(cmd2.Cmd):
@@ -31,17 +29,19 @@ def __init__(self) -> None:
3129
def do_taste_the_rainbow(self, args: argparse.Namespace) -> None:
3230
"""Show all of the colors available within cmd2's Color StrEnum class."""
3331

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)
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)
4240

4341
if args.paged:
44-
self.ppaged('\n'.join(color_names))
42+
self.ppaged(output)
43+
else:
44+
self.poutput(output)
4545

4646

4747
if __name__ == '__main__':

0 commit comments

Comments
 (0)