Skip to content

Commit 37cb694

Browse files
committed
Adds apply_style to ppaged and pfeedback
1 parent 3f208a8 commit 37cb694

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

cmd2/cmd2.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,20 +1151,22 @@ def pexcept(self, msg: Any, *, end: str = '\n', apply_style: bool = True) -> Non
11511151

11521152
self.perror(final_msg, end=end, apply_style=False)
11531153

1154-
def pfeedback(self, msg: Any, *, end: str = '\n') -> None:
1154+
def pfeedback(self, msg: Any, *, end: str = '\n', apply_style: bool = True) -> None:
11551155
"""For printing nonessential feedback. Can be silenced with `quiet`.
11561156
Inclusion in redirected output is controlled by `feedback_to_output`.
11571157
11581158
:param msg: object to print
11591159
:param end: string appended after the end of the message, default a newline
1160+
:param apply_style: If True, then ansi.style_output will be applied to the message text. Set to False in cases
1161+
where the message text already has the desired style. Defaults to True.
11601162
"""
11611163
if not self.quiet:
11621164
if self.feedback_to_output:
1163-
self.poutput(msg, end=end)
1165+
self.poutput(msg, end=end, apply_style=apply_style)
11641166
else:
11651167
self.perror(msg, end=end, apply_style=False)
11661168

1167-
def ppaged(self, msg: Any, *, end: str = '\n', chop: bool = False) -> None:
1169+
def ppaged(self, msg: Any, *, end: str = '\n', chop: bool = False, apply_style: bool = True) -> None:
11681170
"""Print output using a pager if it would go off screen and stdout isn't currently being redirected.
11691171
11701172
Never uses a pager inside of a script (Python or text) or when output is being redirected or piped or when
@@ -1177,6 +1179,8 @@ def ppaged(self, msg: Any, *, end: str = '\n', chop: bool = False) -> None:
11771179
- chopping is ideal for displaying wide tabular data as is done in utilities like pgcli
11781180
False -> causes lines longer than the screen width to wrap to the next line
11791181
- wrapping is ideal when you want to keep users from having to use horizontal scrolling
1182+
:param apply_style: If True, then ansi.style_output will be applied to the message text. Set to False in cases
1183+
where the message text already has the desired style. Defaults to True.
11801184
11811185
WARNING: On Windows, the text always wraps regardless of what the chop argument is set to
11821186
"""
@@ -1215,7 +1219,7 @@ def ppaged(self, msg: Any, *, end: str = '\n', chop: bool = False) -> None:
12151219
pipe_proc = subprocess.Popen(pager, shell=True, stdin=subprocess.PIPE)
12161220
pipe_proc.communicate(msg_str.encode('utf-8', 'replace'))
12171221
else:
1218-
self.poutput(msg_str, end=end)
1222+
self.poutput(msg_str, end=end, apply_style=apply_style)
12191223
except BrokenPipeError:
12201224
# This occurs if a command's output is being piped to another process and that process closes before the
12211225
# command is finished. If you would like your application to print a warning message, then set the

tests/test_cmd2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,7 +1854,7 @@ def test_poutput_none(outsim_app):
18541854
def test_poutput_ansi_always(outsim_app):
18551855
msg = 'Hello World'
18561856
colored_msg = ansi.style(msg, fg=ansi.Fg.CYAN)
1857-
outsim_app.poutput(colored_msg)
1857+
outsim_app.poutput(colored_msg, apply_style=False)
18581858
out = outsim_app.stdout.getvalue()
18591859
expected = colored_msg + '\n'
18601860
assert colored_msg != msg
@@ -1865,7 +1865,7 @@ def test_poutput_ansi_always(outsim_app):
18651865
def test_poutput_ansi_never(outsim_app):
18661866
msg = 'Hello World'
18671867
colored_msg = ansi.style(msg, fg=ansi.Fg.CYAN)
1868-
outsim_app.poutput(colored_msg)
1868+
outsim_app.poutput(colored_msg, apply_style=False)
18691869
out = outsim_app.stdout.getvalue()
18701870
expected = msg + '\n'
18711871
assert colored_msg != msg

0 commit comments

Comments
 (0)