Skip to content

Commit 3800261

Browse files
authored
Merge pull request #324 from python-cmd2/ppaged_terminal
Added an extra check to ppaged() to make sure in a real terminal
2 parents 328b408 + 0faf895 commit 3800261

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

cmd2.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,7 +1262,8 @@ def pfeedback(self, msg):
12621262
def ppaged(self, msg, end='\n'):
12631263
"""Print output using a pager if it would go off screen and stdout isn't currently being redirected.
12641264
1265-
Never uses a pager inside of a script (Python or text) or when output is being redirected or piped.
1265+
Never uses a pager inside of a script (Python or text) or when output is being redirected or piped or when
1266+
stdout or stdin are not a fully functional terminal.
12661267
12671268
:param msg: str - message to print to current stdout - anything convertible to a str with '{}'.format() is OK
12681269
:param end: str - string appended after the end of the message if not already present, default a newline
@@ -1273,8 +1274,16 @@ def ppaged(self, msg, end='\n'):
12731274
if not msg_str.endswith(end):
12741275
msg_str += end
12751276

1277+
# Attempt to detect if we are not running within a fully functional terminal.
1278+
# Don't try to use the pager when being run by a continuous integration system like Jenkins + pexpect.
1279+
functional_terminal = False
1280+
if self.stdin.isatty() and self.stdout.isatty():
1281+
if sys.platform.startswith('win') or os.environ.get('TERM') is not None:
1282+
functional_terminal = True
1283+
12761284
# Don't attempt to use a pager that can block if redirecting or running a script (either text or Python)
1277-
if not self.redirecting and not self._in_py and not self._script_dir:
1285+
# Also only attempt to use a pager if actually running in a real fully functional terminal
1286+
if functional_terminal and not self.redirecting and not self._in_py and not self._script_dir:
12781287
if sys.platform.startswith('win'):
12791288
pager_cmd = 'more'
12801289
else:

examples/paged_output.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ def __init__(self):
1717
@with_argument_list
1818
def do_page_file(self, args):
1919
"""Read in a text file and display its output in a pager."""
20+
if not args:
21+
self.perror('page_file requires a path to a file as an argument', traceback_war=False)
22+
return
23+
2024
with open(args[0], 'r') as f:
2125
text = f.read()
2226
self.ppaged(text)

0 commit comments

Comments
 (0)