Skip to content

Commit e0d69f7

Browse files
committed
Updated initialization.py and python_scripting.py examples for cmd2 3.0.
Deleted basic.py and pirate.py examples.
1 parent 6219f76 commit e0d69f7

File tree

12 files changed

+87
-229
lines changed

12 files changed

+87
-229
lines changed

.github/CODEOWNERS

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727

2828
# cmd2 code
2929
cmd2/__init__.py @kmvanbrunt @tleonhardt
30-
cmd2/ansi.py @kmvanbrunt @tleonhardt
3130
cmd2/argparse_*.py @kmvanbrunt @anselor
3231
cmd2/clipboard.py @tleonhardt
3332
cmd2/cmd2.py @tleonhardt @kmvanbrunt
33+
cmd2/colors.py @tleonhardt @kmvanbrunt
3434
cmd2/command_definition.py @anselor
3535
cmd2/constants.py @tleonhardt @kmvanbrunt
3636
cmd2/decorators.py @kmvanbrunt @anselor
@@ -39,8 +39,11 @@ cmd2/history.py @tleonhardt
3939
cmd2/parsing.py @kmvanbrunt
4040
cmd2/plugin.py @anselor
4141
cmd2/py_bridge.py @kmvanbrunt
42+
cmd2/rich_utils.py @kmvanbrunt
4243
cmd2/rl_utils.py @kmvanbrunt
43-
cmd2/table_creator.py @kmvanbrunt
44+
cmd2/string_utils.py @kmvanbrunt
45+
cmd2/styles.py @tleonhardt @kmvanbrunt
46+
cmd2/terminal_utils.py @kmvanbrunt
4447
cmd2/transcript.py @tleonhardt
4548
cmd2/utils.py @tleonhardt @kmvanbrunt
4649

.github/CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ uv venv --python 3.12
269269
Then you can run commands in this isolated virtual environment using `uv` like so:
270270

271271
```sh
272-
uv run examples/basic.py
272+
uv run examples/hello_cmd2.py
273273
```
274274

275275
Alternatively you can activate the virtual environment using the OS-specific command such as this on

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,6 @@ examples.
105105

106106
## Tutorials
107107

108-
- PyOhio 2019 presentation:
109-
- [video](https://www.youtube.com/watch?v=pebeWrTqIIw)
110-
- [slides](https://github.com/python-cmd2/talks/blob/master/PyOhio_2019/cmd2-PyOhio_2019.pdf)
111-
- [example code](https://github.com/python-cmd2/talks/tree/master/PyOhio_2019/examples)
112108
- [Cookiecutter](https://github.com/cookiecutter/cookiecutter) Templates from community
113109
- Basic cookiecutter template for cmd2 application :
114110
https://github.com/jayrod/cookiecutter-python-cmd2

docs/features/generating_output.md

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,6 @@ you can pad it appropriately with spaces. However, there are categories of Unico
126126
occupy 2 cells, and other that occupy 0. To further complicate matters, you might have included ANSI
127127
escape sequences in the output to generate colors on the terminal.
128128

129-
The `cmd2.ansi.style_aware_wcswidth` function solves both of these problems. Pass it a string, and
129+
The `cmd2.string_utils.str_width` function solves both of these problems. Pass it a string, and
130130
regardless of which Unicode characters and ANSI text style escape sequences it contains, it will
131131
tell you how many characters on the screen that string will consume when printed.
132-
133-
## Pretty Printing Data Structures
134-
135-
The `cmd2.Cmd.ppretty` method is similar to the Python
136-
[pprint](https://docs.python.org/3/library/pprint.html) function from the standard `pprint` module.
137-
`cmd2.Cmd.pprint` adds the same conveniences as `cmd2.Cmd.poutput`.
138-
139-
This method provides a capability to “pretty-print” arbitrary Python data structures in a form which
140-
can be used as input to the interpreter and is easy for humans to read.
141-
142-
The formatted representation keeps objects on a single line if it can, and breaks them onto multiple
143-
lines if they don’t fit within the allowed width, adjustable by the width parameter defaulting to 80
144-
characters.
145-
146-
Dictionaries are sorted by key before the display is computed.

docs/features/initialization.md

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,32 @@ Here is a basic example `cmd2` application which demonstrates many capabilities
44

55
```py
66
#!/usr/bin/env python3
7-
# coding=utf-8
87
"""A simple example cmd2 application demonstrating the following:
9-
1) Colorizing/stylizing output
10-
2) Using multiline commands
11-
3) Persistent history
12-
4) How to run an initialization script at startup
13-
5) How to group and categorize commands when displaying them in help
14-
6) Opting-in to using the ipy command to run an IPython shell
15-
7) Allowing access to your application in py and ipy
16-
8) Displaying an intro banner upon starting your application
17-
9) Using a custom prompt
18-
10) How to make custom attributes settable at runtime
8+
1) Colorizing/stylizing output
9+
2) Using multiline commands
10+
3) Persistent history
11+
4) How to run an initialization script at startup
12+
5) How to group and categorize commands when displaying them in help
13+
6) Opting-in to using the ipy command to run an IPython shell
14+
7) Allowing access to your application in py and ipy
15+
8) Displaying an intro banner upon starting your application
16+
9) Using a custom prompt
17+
10) How to make custom attributes settable at runtime.
1918
"""
19+
20+
from rich.style import Style
21+
2022
import cmd2
2123
from cmd2 import (
22-
Bg,
23-
Fg,
24-
style,
24+
Color,
25+
stylize,
2526
)
2627

2728

2829
class BasicApp(cmd2.Cmd):
2930
CUSTOM_CATEGORY = 'My Custom Commands'
3031

31-
def __init__(self):
32+
def __init__(self) -> None:
3233
super().__init__(
3334
multiline_commands=['echo'],
3435
persistent_history_file='cmd2_history.dat',
@@ -37,7 +38,10 @@ Here is a basic example `cmd2` application which demonstrates many capabilities
3738
)
3839

3940
# Prints an intro banner once upon application startup
40-
self.intro = style('Welcome to cmd2!', fg=Fg.RED, bg=Bg.WHITE, bold=True)
41+
self.intro = stylize(
42+
'Welcome to cmd2!',
43+
style=Style(color=Color.RED, bgcolor=Color.WHITE, bold=True),
44+
)
4145

4246
# Show this as the prompt when asking for input
4347
self.prompt = 'myapp> '
@@ -52,25 +56,34 @@ Here is a basic example `cmd2` application which demonstrates many capabilities
5256
self.default_category = 'cmd2 Built-in Commands'
5357

5458
# Color to output text in with echo command
55-
self.foreground_color = Fg.CYAN.name.lower()
59+
self.foreground_color = Color.CYAN.value
5660

5761
# Make echo_fg settable at runtime
58-
fg_colors = [c.name.lower() for c in Fg]
62+
fg_colors = [c.value for c in Color]
5963
self.add_settable(
60-
cmd2.Settable('foreground_color', str, 'Foreground color to use with echo command', self,
61-
choices=fg_colors)
64+
cmd2.Settable(
65+
'foreground_color',
66+
str,
67+
'Foreground color to use with echo command',
68+
self,
69+
choices=fg_colors,
70+
)
6271
)
6372

6473
@cmd2.with_category(CUSTOM_CATEGORY)
65-
def do_intro(self, _):
66-
"""Display the intro banner"""
74+
def do_intro(self, _: cmd2.Statement) -> None:
75+
"""Display the intro banner."""
6776
self.poutput(self.intro)
6877

6978
@cmd2.with_category(CUSTOM_CATEGORY)
70-
def do_echo(self, arg):
71-
"""Example of a multiline command"""
72-
fg_color = Fg[self.foreground_color.upper()]
73-
self.poutput(style(arg, fg=fg_color))
79+
def do_echo(self, arg: cmd2.Statement) -> None:
80+
"""Example of a multiline command."""
81+
self.poutput(
82+
stylize(
83+
arg,
84+
style=Style(color=self.foreground_color),
85+
)
86+
)
7487

7588

7689
if __name__ == '__main__':

docs/features/prompt.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,25 @@
66

77
This prompt can be configured by setting the `cmd2.Cmd.prompt` instance attribute. This contains the
88
string which should be printed as a prompt for user input. See the
9-
[Pirate](https://github.com/python-cmd2/cmd2/blob/main/examples/pirate.py#L39) example for the
10-
simple use case of statically setting the prompt.
9+
[Initialization](https://github.com/python-cmd2/cmd2/blob/main/examples/initialization.py) example
10+
for the simple use case of statically setting the prompt.
1111

1212
## Continuation Prompt
1313

1414
When a user types a [Multiline Command](./multiline_commands.md) it may span more than one line of
1515
input. The prompt for the first line of input is specified by the `cmd2.Cmd.prompt` instance
1616
attribute. The prompt for subsequent lines of input is defined by the `cmd2.Cmd.continuation_prompt`
1717
attribute.See the
18-
[Initialization](https://github.com/python-cmd2/cmd2/blob/main/examples/initialization.py#L42)
19-
example for a demonstration of customizing the continuation prompt.
18+
[Initialization](https://github.com/python-cmd2/cmd2/blob/main/examples/initialization.py) example
19+
for a demonstration of customizing the continuation prompt.
2020

2121
## Updating the prompt
2222

2323
If you wish to update the prompt between commands, you can do so using one of the
2424
[Application Lifecycle Hooks](./hooks.md#application-lifecycle-hooks) such as a
2525
[Postcommand hook](./hooks.md#postcommand-hooks). See
26-
[PythonScripting](https://github.com/python-cmd2/cmd2/blob/main/examples/python_scripting.py#L38-L55)
27-
for an example of dynamically updating the prompt.
26+
[PythonScripting](https://github.com/python-cmd2/cmd2/blob/main/examples/python_scripting.py) for an
27+
example of dynamically updating the prompt.
2828

2929
## Asynchronous Feedback
3030

docs/migrating/next_steps.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ to `cmd2.Cmd.poutput`, `cmd2.Cmd.perror`, and `cmd2.Cmd.pfeedback`. These method
4141
of the built in [Settings](../features/settings.md) to allow the user to view or suppress feedback
4242
(i.e. progress or status output). They also properly handle ansi colored output according to user
4343
preference. Speaking of colored output, you can use any color library you want, or use the included
44-
`cmd2.ansi.style` function. These and other related topics are covered in
44+
`cmd2.string_utils.stylize` function. These and other related topics are covered in
4545
[Generating Output](../features/generating_output.md).

examples/README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ each:
2525
- [async_printing.py](https://github.com/python-cmd2/cmd2/blob/main/examples/async_printing.py)
2626
- Shows how to asynchronously print alerts, update the prompt in realtime, and change the window
2727
title
28-
- [basic.py](https://github.com/python-cmd2/cmd2/blob/main/examples/basic.py)
29-
- Shows how to add a command, add help for it, and create persistent command history for your
30-
application
3128
- [basic_completion.py](https://github.com/python-cmd2/cmd2/blob/main/examples/basic_completion.py)
3229
- Show how to enable custom tab completion by assigning a completer function to `do_*` commands
3330
- [cmd2_as_argument.py](https://github.com/python-cmd2/cmd2/blob/main/examples/cmd_as_argument.py)
@@ -81,9 +78,6 @@ each:
8178
- Shows how to use output pagination within `cmd2` apps via the `ppaged` method
8279
- [persistent_history.py](https://github.com/python-cmd2/cmd2/blob/main/examples/persistent_history.py)
8380
- Shows how to enable persistent history in your `cmd2` application
84-
- [pirate.py](https://github.com/python-cmd2/cmd2/blob/main/examples/pirate.py)
85-
- Demonstrates many features including colorized output, multiline commands, shorcuts,
86-
defaulting to shell, etc.
8781
- [pretty_print.py](https://github.com/python-cmd2/cmd2/blob/main/examples/pretty_print.py)
8882
- Demonstrates use of cmd2.Cmd.ppretty() for pretty-printing arbitrary Python data structures
8983
like dictionaries.

examples/basic.py

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

examples/initialization.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
10) How to make custom attributes settable at runtime.
1313
"""
1414

15+
from rich.style import Style
16+
1517
import cmd2
1618
from cmd2 import (
17-
Bg,
18-
Fg,
19-
style,
19+
Color,
20+
stylize,
2021
)
2122

2223

@@ -32,7 +33,10 @@ def __init__(self) -> None:
3233
)
3334

3435
# Prints an intro banner once upon application startup
35-
self.intro = style('Welcome to cmd2!', fg=Fg.RED, bg=Bg.WHITE, bold=True)
36+
self.intro = stylize(
37+
'Welcome to cmd2!',
38+
style=Style(color=Color.RED, bgcolor=Color.WHITE, bold=True),
39+
)
3640

3741
# Show this as the prompt when asking for input
3842
self.prompt = 'myapp> '
@@ -47,24 +51,34 @@ def __init__(self) -> None:
4751
self.default_category = 'cmd2 Built-in Commands'
4852

4953
# Color to output text in with echo command
50-
self.foreground_color = Fg.CYAN.name.lower()
54+
self.foreground_color = Color.CYAN.value
5155

5256
# Make echo_fg settable at runtime
53-
fg_colors = [c.name.lower() for c in Fg]
57+
fg_colors = [c.value for c in Color]
5458
self.add_settable(
55-
cmd2.Settable('foreground_color', str, 'Foreground color to use with echo command', self, choices=fg_colors)
59+
cmd2.Settable(
60+
'foreground_color',
61+
str,
62+
'Foreground color to use with echo command',
63+
self,
64+
choices=fg_colors,
65+
)
5666
)
5767

5868
@cmd2.with_category(CUSTOM_CATEGORY)
59-
def do_intro(self, _) -> None:
69+
def do_intro(self, _: cmd2.Statement) -> None:
6070
"""Display the intro banner."""
6171
self.poutput(self.intro)
6272

6373
@cmd2.with_category(CUSTOM_CATEGORY)
64-
def do_echo(self, arg) -> None:
74+
def do_echo(self, arg: cmd2.Statement) -> None:
6575
"""Example of a multiline command."""
66-
fg_color = Fg[self.foreground_color.upper()]
67-
self.poutput(style(arg, fg=fg_color))
76+
self.poutput(
77+
stylize(
78+
arg,
79+
style=Style(color=self.foreground_color),
80+
)
81+
)
6882

6983

7084
if __name__ == '__main__':

0 commit comments

Comments
 (0)