summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md4
-rw-r--r--cmd2/cmd2.py24
-rwxr-xr-xexamples/paged_output.py42
3 files changed, 41 insertions, 29 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 300dc732..078af8d8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,8 +4,8 @@
* Enhancements
* Added ability to print a header above tab-completion suggestions using `completion_header` member
* Added ``pager`` and ``pager_chop`` attributes to the ``cmd2.Cmd`` class
- * ``pager`` looks for *PAGER* environment variable if present or uses sane defaults if not
- * ``pager_chop`` appends a **-S** flag if ``pager`` starts with **less**
+ * ``pager`` defaults to **less -RXF** on POSIX and **more** on Windows
+ * ``pager_chop`` defaults to **less -SRXF** on POSIX and **more** on Windows
* Added ``chop`` argument to ``cmd2.Cmd.ppaged()`` method for displaying output using a pager
* If ``chop`` is ``False``, then ``self.pager`` is used as the pager
* Otherwise ``self.pager_chop`` is used as the pager
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 5244f73e..cff73f13 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -535,22 +535,16 @@ class Cmd(cmd.Cmd):
self.matches_delimited = False
# Set the pager(s) for use with the ppaged() method for displaying output using a pager
- self.pager = os.environ.get('PAGER')
- if not self.pager:
- if sys.platform.startswith('win'):
- self.pager = self.pager_chop = '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
- self.pager = 'less -RXF'
- self.pager_chop = 'less -SRXF'
+ if sys.platform.startswith('win'):
+ self.pager = self.pager_chop = 'more'
else:
- self.pager_chop = self.pager
- if self.pager_chop.startswith('less'):
- self.pager_chop += ' -S'
+ # 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
+ self.pager = 'less -RXF'
+ self.pager_chop = 'less -SRXF'
# ----- Methods related to presenting output to the user -----
diff --git a/examples/paged_output.py b/examples/paged_output.py
index 72efd4f5..d1b1b2c2 100755
--- a/examples/paged_output.py
+++ b/examples/paged_output.py
@@ -14,25 +14,43 @@ class PagedOutput(cmd2.Cmd):
def __init__(self):
super().__init__()
+ def page_file(self, file_path: str, chop: bool=False):
+ """Helper method to prevent having too much duplicated code."""
+ filename = os.path.expanduser(file_path)
+ try:
+ with open(filename, 'r') as f:
+ text = f.read()
+ self.ppaged(text, chop=chop)
+ except FileNotFoundError as ex:
+ self.perror('ERROR: file {!r} not found'.format(filename), traceback_war=False)
+
@cmd2.with_argument_list
- def do_page_file(self, args: List[str]):
- """Read in a text file and display its output in a pager.
+ def do_page_wrap(self, args: List[str]):
+ """Read in a text file and display its output in a pager, wrapping long lines if they don't fit.
- Usage: page_file <file_path>
+ Usage: page_wrap <file_path>
"""
if not args:
- self.perror('page_file requires a path to a file as an argument', traceback_war=False)
+ self.perror('page_wrap requires a path to a file as an argument', traceback_war=False)
return
+ self.page_file(args[0], chop=False)
- 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_wrap = cmd2.Cmd.path_complete
+
+ @cmd2.with_argument_list
+ def do_page_truncate(self, args: List[str]):
+ """Read in a text file and display its output in a pager, truncating long lines if they don't fit.
+
+ Truncated lines can still be accessed by scrolling to the right using the arrow keys.
+
+ Usage: page_chop <file_path>
+ """
+ if not args:
+ self.perror('page_truncate requires a path to a file as an argument', traceback_war=False)
+ return
+ self.page_file(args[0], chop=True)
- complete_page_file = cmd2.Cmd.path_complete
+ complete_page_truncate = cmd2.Cmd.path_complete
if __name__ == '__main__':