@@ -26,16 +26,17 @@ complex output.
26
26
27
27
## Ordinary Output
28
28
29
- The ` cmd2.Cmd.poutput ` method is similar to the Python
30
- [ built-in print function ] ( https://docs.python.org/3/library/functions.html#print ) .
31
- ` cmd2.Cmd.poutput ` adds two conveniences:
29
+ The [ poutput ] [ cmd2.Cmd.poutput ] method is similar to the Python built-in
30
+ [ print] ( https://docs.python.org/3/library/functions.html#print ) function. ` poutput ` adds a few
31
+ conveniences:
32
32
33
33
1 . Since users can pipe output to a shell command, it catches ` BrokenPipeError ` and outputs the
34
34
contents of ` self.broken_pipe_warning ` to ` stderr ` . ` self.broken_pipe_warning ` defaults to an
35
35
empty string so this method will just swallow the exception. If you want to show an error
36
36
message, put it in ` self.broken_pipe_warning ` when you initialize ` cmd2.Cmd ` .
37
37
2 . It examines and honors the [ allow_style] ( ./settings.md#allow_style ) setting. See
38
38
[ Colored Output] ( #colored-output ) below for more details.
39
+ 3 . It allows printing arbitrary ` rich ` renderable objects which can get visually quite complex.
39
40
40
41
Here's a simple command that shows this method in action:
41
42
@@ -48,20 +49,22 @@ def do_echo(self, args):
48
49
## Error Messages
49
50
50
51
When an error occurs in your program, you can display it on ` sys.stderr ` by calling the
51
- ` .cmd2.Cmd.perror ` method. By default this method applies ` Cmd2Style.ERROR ` to the output.
52
+ [ perror] [ cmd2.Cmd.perror ] method. By default this method applies
53
+ [ Cmd2Style.ERROR] [ cmd2.Cmd2Style.ERROR ] to the output.
52
54
53
55
## Warning Messages
54
56
55
- ` cmd2.Cmd.pwarning ` is just like ` cmd2.Cmd.perror ` but applies ` Cmd2Style.WARNING ` to the output.
57
+ [ pwarning] [ cmd2.Cmd.pwarning ] is just like ` cmd2.Cmd.perror ` but applies ` Cmd2Style.WARNING ` to the
58
+ output.
56
59
57
60
## Feedback
58
61
59
62
You may have the need to display information to the user which is not intended to be part of the
60
63
generated output. This could be debugging information or status information about the progress of
61
64
long running commands. It's not output, it's not error messages, it's feedback. If you use the
62
65
[ Timing] ( ./settings.md#timing ) setting, the output of how long it took the command to run will be
63
- output as feedback. You can use the ` cmd2.Cmd.pfeedback ` method to produce this type of output, and
64
- several [ Settings] ( ./settings.md ) control how it is handled.
66
+ output as feedback. You can use the [ pfeedback ] [ cmd2.Cmd.pfeedback ] method to produce this type of
67
+ output, and several [ Settings] ( ./settings.md ) control how it is handled.
65
68
66
69
If the [ quiet] ( ./settings.md#quiet ) setting is ` True ` , then calling ` cmd2.Cmd.pfeedback ` produces no
67
70
output. If [ quiet] ( ./settings.md#quiet ) is ` False ` , the
@@ -71,17 +74,19 @@ send the output to `stdout` or `stderr`.
71
74
## Exceptions
72
75
73
76
If your app catches an exception and you would like to display the exception to the user, the
74
- ` cmd2.Cmd.pexcept ` method can help. The default behavior is to just display the message contained
75
- within the exception. However, if the [ debug] ( ./settings.md#debug ) setting is ` True ` , then the
76
- entire stack trace will be displayed.
77
+ [ pexcept ] [ cmd2.Cmd.pexcept ] method can help. The default behavior is to just display the message
78
+ contained within the exception. However, if the [ debug] ( ./settings.md#debug ) setting is ` True ` , then
79
+ the entire stack trace will be displayed.
77
80
78
81
## Paging Output
79
82
80
83
If you know you are going to generate a lot of output, you may want to display it in a way that the
81
84
user can scroll forwards and backwards through it. If you pass all of the output to be displayed in
82
- a single call to ` .cmd2.Cmd.ppaged ` , it will be piped to an operating system appropriate shell
83
- command to page the output. On Windows, the output is piped to ` more ` ; on Unix-like operating
84
- systems like MacOS and Linux, it is piped to ` less ` .
85
+ a single call to [ ppaged] [ cmd2.Cmd.ppaged ] , it will be piped to an operating system appropriate
86
+ shell command to page the output. On Windows, the output is piped to
87
+ [ more] ( https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/more ) ; on
88
+ Unix-like operating systems like MacOS and Linux, it is piped to
89
+ [ less] ( https://man7.org/linux/man-pages/man1/less.1.html ) .
85
90
86
91
## Colored Output
87
92
@@ -102,20 +107,21 @@ all colors available to your `cmd2` application.
102
107
103
108
### Custom Themes
104
109
105
- ` cmd2 ` uses a ` rich ` ` Theme ` object to define styles for various UI elements. You can define your
106
- own custom theme using ` cmd2.rich_utils.set_theme ` . See the
110
+ ` cmd2 ` uses a ` rich ` [ Theme] ( https://rich.readthedocs.io/en/stable/reference/theme.html ) object to
111
+ define styles for various UI elements. You can define your own custom theme using
112
+ [ cmd2.rich_utils.set_theme] [ ] . See the
107
113
[ rich_theme.py] ( https://github.com/python-cmd2/cmd2/blob/main/examples/rich_theme.py ) example for
108
114
more information.
109
115
110
116
After adding the desired escape sequences to your output, you should use one of these methods to
111
117
present the output to the user:
112
118
113
- - ` cmd2.Cmd.poutput `
114
- - ` cmd2.Cmd.perror `
115
- - ` cmd2.Cmd.pwarning `
116
- - ` cmd2.Cmd.pexcept `
117
- - ` cmd2.Cmd.pfeedback `
118
- - ` cmd2.Cmd.ppaged `
119
+ - [ cmd2.Cmd.poutput] [ ]
120
+ - [ cmd2.Cmd.perror] [ ]
121
+ - [ cmd2.Cmd.pwarning] [ ]
122
+ - [ cmd2.Cmd.pexcept] [ ]
123
+ - [ cmd2.Cmd.pfeedback] [ ]
124
+ - [ cmd2.Cmd.ppaged] [ ]
119
125
120
126
These methods all honor the [ allow_style] ( ./settings.md#allow_style ) setting, which users can modify
121
127
to control whether these escape codes are passed through to the terminal or not.
@@ -125,9 +131,9 @@ to control whether these escape codes are passed through to the terminal or not.
125
131
If you would like to generate output which is left, center, or right aligned within a specified
126
132
width or the terminal width, the following functions can help:
127
133
128
- - ` cmd2.string_utils.align_left `
129
- - ` cmd2.string_utils.align_center `
130
- - ` cmd2.string_utils.align_right `
134
+ - [ cmd2.string_utils.align_left] [ ]
135
+ - [ cmd2.string_utils.align_center] [ ]
136
+ - [ cmd2.string_utils.align_right] [ ]
131
137
132
138
These functions differ from Python's string justifying functions in that they support characters
133
139
with display widths greater than 1. Additionally, ANSI style sequences are safely ignored and do not
@@ -141,6 +147,6 @@ you can pad it appropriately with spaces. However, there are categories of Unico
141
147
occupy 2 cells, and other that occupy 0. To further complicate matters, you might have included ANSI
142
148
escape sequences in the output to generate colors on the terminal.
143
149
144
- The ` cmd2.string_utils.str_width ` function solves both of these problems. Pass it a string, and
150
+ The [ cmd2.string_utils.str_width] [ ] function solves both of these problems. Pass it a string, and
145
151
regardless of which Unicode characters and ANSI text style escape sequences it contains, it will
146
152
tell you how many characters on the screen that string will consume when printed.
0 commit comments