Skip to content

Commit e3672df

Browse files
committed
Altered behavior further to lean towards giving the developer more power over the end user
Specifically: - PAGER environment variable is not used by default to set cmd2.Cmd.pager - This is done to ensure a consistent behavior of cmd2 applications across users by default Developers are free to set pager and pager_chop in the __init__() for their class derived from cmd2.Cmd differently to ensure whatever behavior they desire. Also: - Updated the paged_output.py example to demonstrate using ppaged() with both wrapped and chopped/truncated text
1 parent eb32a86 commit e3672df

File tree

3 files changed

+41
-29
lines changed

3 files changed

+41
-29
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* Enhancements
55
* Added ability to print a header above tab-completion suggestions using `completion_header` member
66
* Added ``pager`` and ``pager_chop`` attributes to the ``cmd2.Cmd`` class
7-
* ``pager`` looks for *PAGER* environment variable if present or uses sane defaults if not
8-
* ``pager_chop`` appends a **-S** flag if ``pager`` starts with **less**
7+
* ``pager`` defaults to **less -RXF** on POSIX and **more** on Windows
8+
* ``pager_chop`` defaults to **less -SRXF** on POSIX and **more** on Windows
99
* Added ``chop`` argument to ``cmd2.Cmd.ppaged()`` method for displaying output using a pager
1010
* If ``chop`` is ``False``, then ``self.pager`` is used as the pager
1111
* Otherwise ``self.pager_chop`` is used as the pager

cmd2/cmd2.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -535,22 +535,16 @@ def __init__(self, completekey: str='tab', stdin=None, stdout=None, persistent_h
535535
self.matches_delimited = False
536536

537537
# Set the pager(s) for use with the ppaged() method for displaying output using a pager
538-
self.pager = os.environ.get('PAGER')
539-
if not self.pager:
540-
if sys.platform.startswith('win'):
541-
self.pager = self.pager_chop = 'more'
542-
else:
543-
# Here is the meaning of the various flags we are using with the less command:
544-
# -S causes lines longer than the screen width to be chopped (truncated) rather than wrapped
545-
# -R causes ANSI "color" escape sequences to be output in raw form (i.e. colors are displayed)
546-
# -X disables sending the termcap initialization and deinitialization strings to the terminal
547-
# -F causes less to automatically exit if the entire file can be displayed on the first screen
548-
self.pager = 'less -RXF'
549-
self.pager_chop = 'less -SRXF'
538+
if sys.platform.startswith('win'):
539+
self.pager = self.pager_chop = 'more'
550540
else:
551-
self.pager_chop = self.pager
552-
if self.pager_chop.startswith('less'):
553-
self.pager_chop += ' -S'
541+
# Here is the meaning of the various flags we are using with the less command:
542+
# -S causes lines longer than the screen width to be chopped (truncated) rather than wrapped
543+
# -R causes ANSI "color" escape sequences to be output in raw form (i.e. colors are displayed)
544+
# -X disables sending the termcap initialization and deinitialization strings to the terminal
545+
# -F causes less to automatically exit if the entire file can be displayed on the first screen
546+
self.pager = 'less -RXF'
547+
self.pager_chop = 'less -SRXF'
554548

555549
# ----- Methods related to presenting output to the user -----
556550

examples/paged_output.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,43 @@ class PagedOutput(cmd2.Cmd):
1414
def __init__(self):
1515
super().__init__()
1616

17+
def page_file(self, file_path: str, chop: bool=False):
18+
"""Helper method to prevent having too much duplicated code."""
19+
filename = os.path.expanduser(file_path)
20+
try:
21+
with open(filename, 'r') as f:
22+
text = f.read()
23+
self.ppaged(text, chop=chop)
24+
except FileNotFoundError as ex:
25+
self.perror('ERROR: file {!r} not found'.format(filename), traceback_war=False)
26+
1727
@cmd2.with_argument_list
18-
def do_page_file(self, args: List[str]):
19-
"""Read in a text file and display its output in a pager.
28+
def do_page_wrap(self, args: List[str]):
29+
"""Read in a text file and display its output in a pager, wrapping long lines if they don't fit.
2030
21-
Usage: page_file <file_path>
31+
Usage: page_wrap <file_path>
2232
"""
2333
if not args:
24-
self.perror('page_file requires a path to a file as an argument', traceback_war=False)
34+
self.perror('page_wrap requires a path to a file as an argument', traceback_war=False)
2535
return
36+
self.page_file(args[0], chop=False)
2637

27-
filename = os.path.expanduser(args[0])
28-
try:
29-
with open(filename, 'r') as f:
30-
text = f.read()
31-
self.ppaged(text)
32-
except FileNotFoundError as ex:
33-
self.perror('ERROR: file {!r} not found'.format(filename), traceback_war=False)
38+
complete_page_wrap = cmd2.Cmd.path_complete
39+
40+
@cmd2.with_argument_list
41+
def do_page_truncate(self, args: List[str]):
42+
"""Read in a text file and display its output in a pager, truncating long lines if they don't fit.
43+
44+
Truncated lines can still be accessed by scrolling to the right using the arrow keys.
45+
46+
Usage: page_chop <file_path>
47+
"""
48+
if not args:
49+
self.perror('page_truncate requires a path to a file as an argument', traceback_war=False)
50+
return
51+
self.page_file(args[0], chop=True)
3452

35-
complete_page_file = cmd2.Cmd.path_complete
53+
complete_page_truncate = cmd2.Cmd.path_complete
3654

3755

3856
if __name__ == '__main__':

0 commit comments

Comments
 (0)