Skip to content

Commit 7ad9ceb

Browse files
committed
Merge branch 'main' into prompt-toolkit
2 parents e8337b9 + e4fdece commit 7ad9ceb

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ time reading the [rich documentation](https://rich.readthedocs.io/).
7575
- `_set_parser_prog` renamed to `set_parser_prog` (without the leading underscore) and moved
7676
to `argparse_custom` module
7777
- Renamed history `--output_file` to `--output-file` to follow common command-line practices
78+
- `cmd2.Cmd.ppretty` method removed - `rich` has more and better options for pretty printing,
79+
see the
80+
[pretty_print.py](https://github.com/python-cmd2/cmd2/blob/main/examples/pretty_print.py)
81+
example for a demonstration of pretty-printing JSON data
7882

7983
- Enhancements
8084
- Enhanced all print methods (`poutput()`, `perror()`, `ppaged()`, etc.) to natively render

docs/features/settings.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ Output generated by `cmd2` programs may contain ANSI escape sequences which inst
1919
apply colors or text styling (i.e. bold) to the output. The `allow_style` setting controls the
2020
behavior of these escape sequences in output generated with any of the following methods:
2121

22-
- **`cmd2.Cmd.poutput`**
2322
- **`cmd2.Cmd.perror`**
24-
- **`cmd2.Cmd.pwarning`**
2523
- **`cmd2.Cmd.pexcept`**
2624
- **`cmd2.Cmd.pfeedback`**
25+
- **`cmd2.Cmd.poutput`**
2726
- **`cmd2.Cmd.ppaged`**
28-
- **`cmd2.Cmd.ppretty`**
27+
- **`cmd2.Cmd.print_to`**
28+
- **`cmd2.Cmd.psuccess`**
29+
- **`cmd2.Cmd.pwarning`**
2930

3031
This setting can be one of three values:
3132

examples/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ each:
6969
- [persistent_history.py](https://github.com/python-cmd2/cmd2/blob/main/examples/persistent_history.py)
7070
- Shows how to enable persistent history in your `cmd2` application
7171
- [pretty_print.py](https://github.com/python-cmd2/cmd2/blob/main/examples/pretty_print.py)
72-
- Demonstrates use of cmd2.Cmd.ppretty() for pretty-printing arbitrary Python data structures
73-
like dictionaries.
72+
- Demonstrates use of rich within cmd2 for pretty-printing arbitrary Python data structures like
73+
dictionaries.
7474
- [python_scripting.py](https://github.com/python-cmd2/cmd2/blob/main/examples/python_scripting.py)
7575
- Shows how cmd2's built-in `run_pyscript` command can provide advanced Python scripting of cmd2
7676
applications

examples/pretty_print.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#!/usr/bin/env python3
2-
"""A simple example demonstrating use of cmd2.Cmd.ppretty()."""
2+
"""A simple example demonstrating how to pretty print JSON data in a cmd2 app using rich."""
3+
4+
from rich.json import JSON
35

46
import cmd2
57

6-
data = {
8+
EXAMPLE_DATA = {
79
"name": "John Doe",
810
"age": 30,
911
"address": {"street": "123 Main St", "city": "Anytown", "state": "CA"},
@@ -14,14 +16,27 @@
1416
class Cmd2App(cmd2.Cmd):
1517
def __init__(self) -> None:
1618
super().__init__()
19+
self.data = EXAMPLE_DATA
1720

1821
def do_normal(self, _) -> None:
1922
"""Display the data using the normal poutput method."""
20-
self.poutput(data)
23+
self.poutput(self.data)
2124

2225
def do_pretty(self, _) -> None:
23-
"""Display the data using the ppretty method."""
24-
self.ppretty(data)
26+
"""Display the JSON data in a pretty way using rich."""
27+
28+
json_renderable = JSON.from_data(
29+
self.data,
30+
indent=2,
31+
highlight=True,
32+
skip_keys=False,
33+
ensure_ascii=False,
34+
check_circular=True,
35+
allow_nan=True,
36+
default=None,
37+
sort_keys=False,
38+
)
39+
self.poutput(json_renderable)
2540

2641

2742
if __name__ == '__main__':

0 commit comments

Comments
 (0)