Skip to content

Commit 1d47c09

Browse files
committed
Revise allow_style and colored output documentation
1 parent ec8e743 commit 1d47c09

File tree

2 files changed

+64
-88
lines changed

2 files changed

+64
-88
lines changed

docs/features/generating_output.rst

Lines changed: 51 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,34 @@ methods::
77
print("Greetings, Professor Falken.", file=self.stdout)
88
self.stdout.write("Shall we play a game?\n")
99

10-
While you could send output directly to ``sys.stdout``, ``cmd`` can be
11-
initialized with a ``stdin`` and ``stdout`` variables, which it stores
12-
as ``self.stdin`` and ``self.stdout``. By using these variables every
13-
time you produce output, you can trivially change where all the output
14-
goes by changing how you initialize your class.
10+
While you could send output directly to ``sys.stdout``, :mod:`cmd2.cmd2.Cmd`
11+
can be initialized with a ``stdin`` and ``stdout`` variables, which it stores
12+
as ``self.stdin`` and ``self.stdout``. By using these variables every time you
13+
produce output, you can trivially change where all the output goes by changing
14+
how you initialize your class.
1515

16-
``cmd2`` extends this approach in a number of convenient ways. See
16+
:mod:`cmd2.cmd2.Cmd` extends this approach in a number of convenient ways. See
1717
:ref:`features/redirection:Output Redirection And Pipes` for information on how
1818
users can change where the output of a command is sent. In order for those
1919
features to work, the output you generate must be sent to ``self.stdout``. You
20-
can use the methods described above, and everything will work fine. ``cmd2``
21-
also adds a number of output related methods to ``Cmd2.Cmd`` which you may use
22-
to enhance the output your application produces.
20+
can use the methods described above, and everything will work fine.
21+
:mod:`cmd2.cmd2.Cmd` also includes a number of output related methods which you
22+
may use to enhance the output your application produces.
2323

2424

2525
Ordinary Output
2626
---------------
2727

28-
The :meth:`.cmd2.Cmd.poutput` method is similar to the Python
29-
`built-in print function <https://docs.python.org/3/library/functions.html#print>`_. :meth:`~cmd2.cmd2.Cmd.poutput` adds two
30-
conveniences.
28+
The :meth:`~.cmd2.Cmd.poutput` method is similar to the Python
29+
`built-in print function <https://docs.python.org/3/library/functions.html#print>`_. :meth:`~.cmd2.Cmd.poutput` adds two
30+
conveniences:
3131

3232
1. Since users can pipe output to a shell command, it catches
3333
``BrokenPipeError`` and outputs the contents of
3434
``self.broken_pipe_warning`` to ``stderr``. ``self.broken_pipe_warning``
3535
defaults to an empty string so this method will just swallow the exception.
3636
If you want to show an error message, put it in
37-
``self.broken_pipe_warning`` when you initialize ``Cmd2.cmd``.
37+
``self.broken_pipe_warning`` when you initialize :mod:`.cmd2.Cmd`.
3838

3939
2. It examines and honors the :ref:`features/settings:allow_style` setting.
4040
See :ref:`features/generating_output:Colored Output` below for more details.
@@ -46,75 +46,12 @@ Here's a simple command that shows this method in action::
4646
self.poutput(args)
4747

4848

49-
Colored Output
50-
--------------
51-
The output methods in the previous section all honor the ``allow_style``
52-
setting, which has three possible values:
53-
54-
Never
55-
poutput(), pfeedback(), and ppaged() strip all ANSI style sequences
56-
which instruct the terminal to colorize output
57-
58-
Terminal
59-
(the default value) poutput(), pfeedback(), and ppaged() do not strip any
60-
ANSI style sequences when the output is a terminal, but if the output is a
61-
pipe or a file the style sequences are stripped. If you want colorized
62-
output you must add ANSI style sequences using either cmd2's internal ansi
63-
module or another color library such as `plumbum.colors`, `colorama`, or
64-
`colored`.
65-
66-
Always
67-
poutput(), pfeedback(), and ppaged() never strip ANSI style sequences,
68-
regardless of the output destination
69-
70-
Colored and otherwise styled output can be generated using the `ansi.style()`
71-
function:
72-
73-
.. automethod:: cmd2.ansi.style
74-
:noindex:
75-
76-
You may want to generate output in different colors, which is typically done by
77-
adding `ANSI escape sequences
78-
<https://en.wikipedia.org/wiki/ANSI_escape_code#Colors>`_ which tell the
79-
terminal to change the foreground and background colors. If you want to give
80-
yourself a headache, you can generate these by hand. You could also use another
81-
Python color library like `plumbum.colors
82-
<https://plumbum.readthedocs.io/en/latest/colors.html>`_, `colored
83-
<https://gitlab.com/dslackw/colored>`_, or `colorama
84-
<https://github.com/tartley/colorama>`_. Colorama is unique because when it's
85-
running on Windows, it wraps ``stdout``, looks for ANSI escape sequences, and
86-
converts them into the appropriate ``win32`` calls to modify the state of the
87-
terminal.
88-
89-
``cmd2`` imports and uses Colorama and provides a number of convenience methods
90-
for generating colorized output, measuring the screen width of colorized
91-
output, setting the window title in the terminal, and removing ANSI escape
92-
codes from a string. These functions are all documentated in
93-
:mod:`cmd2.ansi`.
94-
95-
:mod:`cmd2.cmd2.Cmd` includes an :ref:`features/settings:allow_style` setting,
96-
which controls whether ANSI escape sequences that instruct the terminal to
97-
colorize output are stripped from the output. The recommended approach is to
98-
construct your application so that it generates colorized output, and then
99-
allow your users to use this setting to remove the colorization if they do not
100-
want it.
101-
102-
Output generated by any of these
103-
methods will honor the :ref:`features/settings:allow_style` setting:
104-
105-
- :meth:`~.cmd2.Cmd.poutput`
106-
- :meth:`~.cmd2.Cmd.perror`
107-
- :meth:`~.cmd2.Cmd.pwarning`
108-
- :meth:`~.cmd2.Cmd.pexcept`
109-
- :meth:`~.cmd2.Cmd.pfeedback`
110-
- :meth:`~.cmd2.Cmd.ppaged`
111-
112-
11349
Error Messages
11450
--------------
11551

11652
When an error occurs in your program, you can display it on ``sys.stderr`` by
117-
calling the :meth:`~.cmd2.Cmd.perror` method.
53+
calling the :meth:`~.cmd2.Cmd.perror` method. By default this method applies
54+
:meth:`cmd2.ansi.style_error` to the output.
11855

11956

12057
Warning Messages
@@ -164,6 +101,42 @@ shell command to page the output. On Windows, the output is piped to ``more``;
164101
on Unix-like operating systems like MacOS and Linux, it is piped to ``less``.
165102

166103

104+
Colored Output
105+
--------------
106+
107+
You can add your own `ANSI escape sequences
108+
<https://en.wikipedia.org/wiki/ANSI_escape_code#Colors>`_ to your output which
109+
tell the terminal to change the foreground and background colors. If you want
110+
to give yourself a headache, you can generate these by hand. You could also use
111+
a Python color library like `plumbum.colors
112+
<https://plumbum.readthedocs.io/en/latest/colors.html>`_, `colored
113+
<https://gitlab.com/dslackw/colored>`_, or `colorama
114+
<https://github.com/tartley/colorama>`_. Colorama is unique because when it's
115+
running on Windows, it wraps ``stdout``, looks for ANSI escape sequences, and
116+
converts them into the appropriate ``win32`` calls to modify the state of the
117+
terminal.
118+
119+
``cmd2`` imports and uses Colorama and provides a number of convenience methods
120+
for generating colorized output, measuring the screen width of colorized
121+
output, setting the window title in the terminal, and removing ANSI escape
122+
codes from a string. These functions are all documentated in
123+
:mod:`cmd2.ansi`.
124+
125+
After adding the desired escape sequences to your output, you should use one of
126+
these methods to present the output to the user:
127+
128+
- :meth:`.cmd2.Cmd.poutput`
129+
- :meth:`.cmd2.Cmd.perror`
130+
- :meth:`.cmd2.Cmd.pwarning`
131+
- :meth:`.cmd2.Cmd.pexcept`
132+
- :meth:`.cmd2.Cmd.pfeedback`
133+
- :meth:`.cmd2.Cmd.ppaged`
134+
135+
These methods all honor the :ref:`features/settings:allow_style` setting, which
136+
users can modify to control whether these escape codes are passed through to
137+
the terminal or not.
138+
139+
167140
Centering Text
168141
--------------
169142

docs/features/settings.rst

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,28 @@ show all settings and to modify the value of any setting.
2323
allow_style
2424
~~~~~~~~~~~
2525

26-
The ``allow_style`` setting controls the behavior of ANSI escape sequences
27-
in output generated with any of the following methods:
28-
29-
- ``poutput()``
30-
- ``perror()``
31-
- ``pwarning()``
32-
- ``pfeedback()``
33-
- ``ppaged()``
26+
Output generated by ``cmd2`` programs may contain ANSI escape seqences which
27+
instruct the terminal to apply colors or text styling (i.e. bold) to the
28+
output. The ``allow_style`` setting controls the behavior of these escape
29+
sequences in output generated with any of the following methods:
30+
31+
- :meth:`.cmd2.Cmd.poutput`
32+
- :meth:`.cmd2.Cmd.perror`
33+
- :meth:`.cmd2.Cmd.pwarning`
34+
- :meth:`.cmd2.Cmd.pexcept`
35+
- :meth:`.cmd2.Cmd.pfeedback`
36+
- :meth:`.cmd2.Cmd.ppaged`
3437

3538
This setting can be one of three values:
3639

37-
- ``Never`` - all ANSI escape sequences which instruct the terminal to colorize
40+
- ``Never`` - all ANSI escape sequences which instruct the terminal to style
3841
output are stripped from the output.
3942

4043
- ``Terminal`` - (the default value) pass through ANSI escape sequences when
4144
the output is being sent to the terminal, but if the output is redirected to
4245
a pipe or a file the escape sequences are stripped.
4346

44-
- ``Always`` - ANSI escape sequences are always passed through, regardless
47+
- ``Always`` - ANSI escape sequences are always passed through to the output
4548

4649

4750
continuation_prompt

0 commit comments

Comments
 (0)