diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-03-20 16:25:08 -0400 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-03-20 16:25:08 -0400 |
commit | 0faf8959dbc4bd2175683939305e1926590d36ed (patch) | |
tree | 90caec077aaf22000e9b59d6ff890a87de6b8068 | |
parent | 328b408fa755a17268c33f0fae0301bb85d1f102 (diff) | |
download | cmd2-git-0faf8959dbc4bd2175683939305e1926590d36ed.tar.gz |
Added an extra check to ppaged() to make sure cmd2 app is running in a real terminal before attempting to use a pager
-rwxr-xr-x | cmd2.py | 13 | ||||
-rwxr-xr-x | examples/paged_output.py | 4 |
2 files changed, 15 insertions, 2 deletions
@@ -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) |