diff options
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | cmd2/cmd2.py | 24 | ||||
-rwxr-xr-x | examples/paged_output.py | 21 | ||||
-rw-r--r-- | tests/conftest.py | 8 | ||||
-rw-r--r-- | tests/transcripts/regex_set.txt | 1 |
5 files changed, 37 insertions, 19 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 973ed6c6..86a8f51a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ * Fixed issue where piping and redirecting did not work correctly with paths that had spaces * Enhancements * Added ability to print a header above tab-completion suggestions using `completion_header` member + * **pager** is now a settable parameter which controls pager command used by ``cmd2.Cmd.ppaged`` method + * **pager** looks for *PAGER* environment variable if present or uses sane defaults if not ## 0.8.8 (TBD, 2018) * Bug Fixes diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 75946764..80449a34 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -385,6 +385,17 @@ class Cmd(cmd.Cmd): break feedback_to_output = False # Do not include nonessentials in >, | output by default (things like timing) locals_in_py = False + pager = os.environ.get('PAGER') + if not pager: + if sys.platform.startswith('win'): + pager = 'more' + else: + # Here is the meaning of the various flags we are using with the less command: + # -S causes lines longer than the screen width to be chopped (truncated) rather than wrapped + # -R causes ANSI "color" escape sequences to be output in raw form (i.e. colors are displayed) + # -X disables sending the termcap initialization and deinitialization strings to the terminal + # -F causes less to automatically exit if the entire file can be displayed on the first screen + pager = 'less -SRXF' quiet = False # Do not suppress nonessential output timing = False # Prints elapsed time for each command @@ -397,6 +408,7 @@ class Cmd(cmd.Cmd): 'editor': 'Program used by ``edit``', 'feedback_to_output': 'Include nonessentials in `|`, `>` results', 'locals_in_py': 'Allow access to your application in py via self', + 'pager': 'Command used for displaying paged output', 'prompt': 'The prompt issued to solicit input', 'quiet': "Don't print nonessential feedback", 'timing': 'Report execution times'} @@ -635,17 +647,7 @@ class Cmd(cmd.Cmd): # Don't attempt to use a pager that can block if redirecting or running a script (either text or Python) # 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: - # Here is the meaning of the various flags we are using with the less command: - # -S causes lines longer than the screen width to be chopped (truncated) rather than wrapped - # -R causes ANSI "color" escape sequences to be output in raw form (i.e. colors are displayed) - # -X disables sending the termcap initialization and deinitialization strings to the terminal - # -F causes less to automatically exit if the entire file can be displayed on the first screen - pager_cmd = 'less -SRXF' - self.pipe_proc = subprocess.Popen(pager_cmd, shell=True, stdin=subprocess.PIPE) + self.pipe_proc = subprocess.Popen(self.pager, shell=True, stdin=subprocess.PIPE) try: self.pipe_proc.stdin.write(msg_str.encode('utf-8', 'replace')) self.pipe_proc.stdin.close() diff --git a/examples/paged_output.py b/examples/paged_output.py index c56dcb89..72efd4f5 100755 --- a/examples/paged_output.py +++ b/examples/paged_output.py @@ -2,26 +2,35 @@ # coding=utf-8 """A simple example demonstrating the using paged output via the ppaged() method. """ +import os +from typing import List import cmd2 class PagedOutput(cmd2.Cmd): - """ Example cmd2 application where we create commands that just print the arguments they are called with.""" + """ Example cmd2 application which shows how to display output using a pager.""" def __init__(self): super().__init__() @cmd2.with_argument_list - def do_page_file(self, args): - """Read in a text file and display its output in a pager.""" + def do_page_file(self, args: List[str]): + """Read in a text file and display its output in a pager. + + Usage: page_file <file_path> + """ 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) + filename = os.path.expanduser(args[0]) + try: + with open(filename, 'r') as f: + text = f.read() + self.ppaged(text) + except FileNotFoundError as ex: + self.perror('ERROR: file {!r} not found'.format(filename), traceback_war=False) complete_page_file = cmd2.Cmd.path_complete diff --git a/tests/conftest.py b/tests/conftest.py index 90d45bd9..0027ac7d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -80,8 +80,10 @@ SHORTCUTS_TXT = """Shortcuts for other commands: """ expect_colors = True +pager = 'less -SRXF' if sys.platform.startswith('win'): expect_colors = False + pager = 'more ' # Output from the show command with default settings SHOW_TXT = """colors: {} continuation_prompt: > @@ -90,10 +92,11 @@ echo: False editor: vim feedback_to_output: False locals_in_py: False +pager: {} prompt: (Cmd) quiet: False timing: False -""".format(expect_colors) +""".format(expect_colors, pager) if expect_colors: color_str = 'True ' @@ -107,10 +110,11 @@ echo: False # Echo command issued into output editor: vim # Program used by ``edit`` feedback_to_output: False # Include nonessentials in `|`, `>` results locals_in_py: False # Allow access to your application in py via self +pager: {} # Command used for displaying paged output prompt: (Cmd) # The prompt issued to solicit input quiet: False # Don't print nonessential feedback timing: False # Report execution times -""".format(color_str) +""".format(color_str, pager) class StdOut(object): diff --git a/tests/transcripts/regex_set.txt b/tests/transcripts/regex_set.txt index b818c464..eba5b105 100644 --- a/tests/transcripts/regex_set.txt +++ b/tests/transcripts/regex_set.txt @@ -12,6 +12,7 @@ editor: /.*/ feedback_to_output: False locals_in_py: False maxrepeats: 3 +pager: /.*/ prompt: (Cmd)/ / quiet: False timing: False |