Skip to content

Commit 564adf4

Browse files
committed
Simplified ppaged().
1 parent d35aff2 commit 564adf4

File tree

2 files changed

+27
-47
lines changed

2 files changed

+27
-47
lines changed

Diff for: cmd2/cmd2.py

+27-33
Original file line numberDiff line numberDiff line change
@@ -1403,49 +1403,43 @@ def ppaged(self, msg: Any, *, end: str = '\n', chop: bool = False, dest: Optiona
14031403
14041404
WARNING: On Windows, the text always wraps regardless of what the chop argument is set to
14051405
"""
1406-
# msg can be any type, so convert to string before checking if it's blank
1407-
msg_str = str(msg)
14081406
dest = self.stdout if dest is None else dest
14091407

1410-
# Consider None to be no data to print
1411-
if msg is None or msg_str == '':
1412-
return
1413-
1414-
try:
1415-
import subprocess
1408+
# Attempt to detect if we are not running within a fully functional terminal.
1409+
# Don't try to use the pager when being run by a continuous integration system like Jenkins + pexpect.
1410+
functional_terminal = False
14161411

1417-
# Attempt to detect if we are not running within a fully functional terminal.
1418-
# Don't try to use the pager when being run by a continuous integration system like Jenkins + pexpect.
1419-
functional_terminal = False
1412+
if self.stdin.isatty() and dest.isatty():
1413+
if sys.platform.startswith('win') or os.environ.get('TERM') is not None:
1414+
functional_terminal = True
14201415

1421-
if self.stdin.isatty() and dest.isatty():
1422-
if sys.platform.startswith('win') or os.environ.get('TERM') is not None:
1423-
functional_terminal = True
1416+
# Don't attempt to use a pager that can block if redirecting or running a script (either text or Python).
1417+
# Also only attempt to use a pager if actually running in a real fully functional terminal.
1418+
if functional_terminal and not self._redirecting and not self.in_pyscript() and not self.in_script():
1419+
final_msg = f"{msg}{end}"
1420+
if ansi.allow_style == ansi.AllowStyle.NEVER:
1421+
final_msg = ansi.strip_style(final_msg)
14241422

1425-
# Don't attempt to use a pager that can block if redirecting or running a script (either text or Python)
1426-
# Also only attempt to use a pager if actually running in a real fully functional terminal
1427-
if functional_terminal and not self._redirecting and not self.in_pyscript() and not self.in_script():
1428-
if ansi.allow_style == ansi.AllowStyle.NEVER:
1429-
msg_str = ansi.strip_style(msg_str)
1430-
msg_str += end
1431-
1432-
pager = self.pager
1433-
if chop:
1434-
pager = self.pager_chop
1423+
pager = self.pager
1424+
if chop:
1425+
pager = self.pager_chop
14351426

1427+
try:
14361428
# Prevent KeyboardInterrupts while in the pager. The pager application will
14371429
# still receive the SIGINT since it is in the same process group as us.
14381430
with self.sigint_protection:
1431+
import subprocess
1432+
14391433
pipe_proc = subprocess.Popen(pager, shell=True, stdin=subprocess.PIPE, stdout=dest)
1440-
pipe_proc.communicate(msg_str.encode('utf-8', 'replace'))
1441-
else:
1442-
ansi.style_aware_write(dest, f'{msg_str}{end}')
1443-
except BrokenPipeError:
1444-
# This occurs if a command's output is being piped to another process and that process closes before the
1445-
# command is finished. If you would like your application to print a warning message, then set the
1446-
# broken_pipe_warning attribute to the message you want printed.`
1447-
if self.broken_pipe_warning:
1448-
sys.stderr.write(self.broken_pipe_warning)
1434+
pipe_proc.communicate(final_msg.encode('utf-8', 'replace'))
1435+
except BrokenPipeError:
1436+
# This occurs if a command's output is being piped to another process and that process closes before the
1437+
# command is finished. If you would like your application to print a warning message, then set the
1438+
# broken_pipe_warning attribute to the message you want printed.`
1439+
if self.broken_pipe_warning:
1440+
sys.stderr.write(self.broken_pipe_warning)
1441+
else:
1442+
self.print_to(dest, msg, end=end, paged=False)
14491443

14501444
# ----- Methods related to tab completion -----
14511445

Diff for: tests/test_cmd2.py

-14
Original file line numberDiff line numberDiff line change
@@ -2480,20 +2480,6 @@ def test_ppaged(outsim_app):
24802480
assert out == msg + end
24812481

24822482

2483-
def test_ppaged_blank(outsim_app):
2484-
msg = ''
2485-
outsim_app.ppaged(msg)
2486-
out = outsim_app.stdout.getvalue()
2487-
assert not out
2488-
2489-
2490-
def test_ppaged_none(outsim_app):
2491-
msg = None
2492-
outsim_app.ppaged(msg)
2493-
out = outsim_app.stdout.getvalue()
2494-
assert not out
2495-
2496-
24972483
@with_ansi_style(ansi.AllowStyle.TERMINAL)
24982484
def test_ppaged_strips_ansi_when_redirecting(outsim_app):
24992485
msg = 'testing...'

0 commit comments

Comments
 (0)