summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xcmd2.py13
-rwxr-xr-xexamples/paged_output.py4
2 files changed, 15 insertions, 2 deletions
diff --git a/cmd2.py b/cmd2.py
index 9f026571..525c0dac 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -1262,7 +1262,8 @@ class Cmd(cmd.Cmd):
def ppaged(self, msg, end='\n'):
"""Print output using a pager if it would go off screen and stdout isn't currently being redirected.
- Never uses a pager inside of a script (Python or text) or when output is being redirected or piped.
+ Never uses a pager inside of a script (Python or text) or when output is being redirected or piped or when
+ stdout or stdin are not a fully functional terminal.
:param msg: str - message to print to current stdout - anything convertible to a str with '{}'.format() is OK
:param end: str - string appended after the end of the message if not already present, default a newline
@@ -1273,8 +1274,16 @@ class Cmd(cmd.Cmd):
if not msg_str.endswith(end):
msg_str += end
+ # Attempt to detect if we are not running within a fully functional terminal.
+ # Don't try to use the pager when being run by a continuous integration system like Jenkins + pexpect.
+ functional_terminal = False
+ if self.stdin.isatty() and self.stdout.isatty():
+ if sys.platform.startswith('win') or os.environ.get('TERM') is not None:
+ functional_terminal = True
+
# Don't attempt to use a pager that can block if redirecting or running a script (either text or Python)
- if not self.redirecting and not self._in_py and not self._script_dir:
+ # Also only attempt to use a pager if actually running in a real fully functional terminal
+ if functional_terminal and not self.redirecting and not self._in_py and not self._script_dir:
if sys.platform.startswith('win'):
pager_cmd = 'more'
else:
diff --git a/examples/paged_output.py b/examples/paged_output.py
index 171c1b3e..9005a4da 100755
--- a/examples/paged_output.py
+++ b/examples/paged_output.py
@@ -17,6 +17,10 @@ class PagedOutput(cmd2.Cmd):
@with_argument_list
def do_page_file(self, args):
"""Read in a text file and display its output in a pager."""
+ if not args:
+ self.perror('page_file requires a path to a file as an argument', traceback_war=False)
+ return
+
with open(args[0], 'r') as f:
text = f.read()
self.ppaged(text)