Skip to content

Commit b6c3936

Browse files
authored
Merge pull request #595 from ethanmcc/strip-color-when-redirecting-ppaged
Strip color codes when redirecting w/ ppaged()
2 parents eaf2918 + dd44ed2 commit b6c3936

File tree

5 files changed

+46
-25
lines changed

5 files changed

+46
-25
lines changed

cmd2/cmd2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ def ppaged(self, msg: str, end: str='\n', chop: bool=False) -> None:
704704
break
705705
self.pipe_proc = None
706706
else:
707-
self.stdout.write(msg_str)
707+
self.decolorized_write(self.stdout, msg_str)
708708
except BrokenPipeError:
709709
# This occurs if a command's output is being piped to another process and that process closes before the
710710
# command is finished. If you would like your application to print a warning message, then set the

docs/unfreefeatures.rst

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,19 +146,20 @@ The output methods in the previous section all honor the ``colors`` setting,
146146
which has three possible values:
147147

148148
Never
149-
poutput() and pfeedback() strip all ANSI escape sequences
149+
poutput(), pfeedback(), and ppaged() strip all ANSI escape sequences
150150
which instruct the terminal to colorize output
151151

152152
Terminal
153-
(the default value) poutput() and pfeedback() do not strip any ANSI escape
154-
sequences when the output is a terminal, but if the output is a pipe or a
155-
file the escape sequences are stripped. If you want colorized output you
156-
must add ANSI escape sequences, preferably using some python color library
157-
like `plumbum.colors`, `colorama`, `blessings`, or `termcolor`.
153+
(the default value) poutput(), pfeedback(), and ppaged() do not strip any
154+
ANSI escape sequences when the output is a terminal, but if the output is
155+
a pipe or a file the escape sequences are stripped. If you want colorized
156+
output you must add ANSI escape sequences, preferably using some python
157+
color library like `plumbum.colors`, `colorama`, `blessings`, or
158+
`termcolor`.
158159

159160
Always
160-
poutput() and pfeedback() never strip ANSI escape sequences, regardless of
161-
the output destination
161+
poutput(), pfeedback(), and ppaged() never strip ANSI escape sequences,
162+
regardless of the output destination
162163

163164

164165
.. _quiet:

examples/colors.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@
99
The colors setting has three possible values:
1010
1111
Never
12-
poutput() and pfeedback() strip all ANSI escape sequences
12+
poutput(), pfeedback(), and ppaged() strip all ANSI escape sequences
1313
which instruct the terminal to colorize output
1414
1515
Terminal
16-
(the default value) poutput() and pfeedback() do not strip any ANSI escape
17-
sequences when the output is a terminal, but if the output is a pipe or a
18-
file the escape sequences are stripped. If you want colorized output you
19-
must add ANSI escape sequences, preferably using some python color library
20-
like `plumbum.colors`, `colorama`, `blessings`, or `termcolor`.
16+
(the default value) poutput(), pfeedback(), and ppaged() do not strip any
17+
ANSI escape sequences when the output is a terminal, but if the output is
18+
a pipe or a file the escape sequences are stripped. If you want colorized
19+
output you must add ANSI escape sequences, preferably using some python
20+
color library like `plumbum.colors`, `colorama`, `blessings`, or
21+
`termcolor`.
2122
2223
Always
23-
poutput() and pfeedback() never strip ANSI escape sequences, regardless of
24-
the output destination
24+
poutput(), pfeedback(), and ppaged() never strip ANSI escape sequences,
25+
regardless of the output destination
2526
"""
2627

2728
import random

examples/plumbum_colors.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@
99
The colors setting has three possible values:
1010
1111
Never
12-
poutput() and pfeedback() strip all ANSI escape sequences
12+
poutput(), pfeedback(), and ppaged() strip all ANSI escape sequences
1313
which instruct the terminal to colorize output
1414
1515
Terminal
16-
(the default value) poutput() and pfeedback() do not strip any ANSI escape
17-
sequences when the output is a terminal, but if the output is a pipe or a
18-
file the escape sequences are stripped. If you want colorized output you
19-
must add ANSI escape sequences, preferably using some python color library
20-
like `plumbum.colors`, `colorama`, `blessings`, or `termcolor`.
16+
(the default value) poutput(), pfeedback(), and ppaged() do not strip any
17+
ANSI escape sequences when the output is a terminal, but if the output is
18+
a pipe or a file the escape sequences are stripped. If you want colorized
19+
output you must add ANSI escape sequences, preferably using some python
20+
color library like `plumbum.colors`, `colorama`, `blessings`, or
21+
`termcolor`.
2122
2223
Always
23-
poutput() and pfeedback() never strip ANSI escape sequences, regardless of
24-
the output destination
24+
poutput(), pfeedback(), and ppaged() never strip ANSI escape sequences,
25+
regardless of the output destination
2526
2627
WARNING: This example requires the plumbum package, which isn't normally required by cmd2.
2728
"""

tests/test_cmd2.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,6 +2109,24 @@ def test_ppaged(base_app):
21092109
out = base_app.stdout.getvalue()
21102110
assert out == msg + end
21112111

2112+
def test_ppaged_strips_color_when_redirecting(base_app):
2113+
msg = 'testing...'
2114+
end = '\n'
2115+
base_app.colors = cmd2.constants.COLORS_TERMINAL
2116+
base_app.redirecting = True
2117+
base_app.ppaged(Fore.RED + msg)
2118+
out = base_app.stdout.getvalue()
2119+
assert out == msg + end
2120+
2121+
def test_ppaged_strips_color_when_redirecting_if_always(base_app):
2122+
msg = 'testing...'
2123+
end = '\n'
2124+
base_app.colors = cmd2.constants.COLORS_ALWAYS
2125+
base_app.redirecting = True
2126+
base_app.ppaged(Fore.RED + msg)
2127+
out = base_app.stdout.getvalue()
2128+
assert out == Fore.RED + msg + end
2129+
21122130
# we override cmd.parseline() so we always get consistent
21132131
# command parsing by parent methods we don't override
21142132
# don't need to test all the parsing logic here, because

0 commit comments

Comments
 (0)