Skip to content

Commit 0d4e633

Browse files
committed
Added pager settable parameter to control the command used to display paged output
Also: - ppaged() now uses the pager settable parameter - By default the pager settable parameter uses the PAGER environment variable if present - If PAGER environment variable is not present, pager gets set to: - "less -SXRF" on POSIX OSes (macOS and Linux) - "more" on Windows
1 parent 3ae71e8 commit 0d4e633

File tree

5 files changed

+37
-19
lines changed

5 files changed

+37
-19
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Fixed issue where piping and redirecting did not work correctly with paths that had spaces
44
* Enhancements
55
* Added ability to print a header above tab-completion suggestions using `completion_header` member
6+
* **pager** is now a settable parameter which controls pager command used by ``cmd2.Cmd.ppaged`` method
7+
* **pager** looks for *PAGER* environment variable if present or uses sane defaults if not
68

79
## 0.8.8 (TBD, 2018)
810
* Bug Fixes

cmd2/cmd2.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,17 @@ class Cmd(cmd.Cmd):
385385
break
386386
feedback_to_output = False # Do not include nonessentials in >, | output by default (things like timing)
387387
locals_in_py = False
388+
pager = os.environ.get('PAGER')
389+
if not pager:
390+
if sys.platform.startswith('win'):
391+
pager = 'more'
392+
else:
393+
# Here is the meaning of the various flags we are using with the less command:
394+
# -S causes lines longer than the screen width to be chopped (truncated) rather than wrapped
395+
# -R causes ANSI "color" escape sequences to be output in raw form (i.e. colors are displayed)
396+
# -X disables sending the termcap initialization and deinitialization strings to the terminal
397+
# -F causes less to automatically exit if the entire file can be displayed on the first screen
398+
pager = 'less -SRXF'
388399
quiet = False # Do not suppress nonessential output
389400
timing = False # Prints elapsed time for each command
390401

@@ -397,6 +408,7 @@ class Cmd(cmd.Cmd):
397408
'editor': 'Program used by ``edit``',
398409
'feedback_to_output': 'Include nonessentials in `|`, `>` results',
399410
'locals_in_py': 'Allow access to your application in py via self',
411+
'pager': 'Command used for displaying paged output',
400412
'prompt': 'The prompt issued to solicit input',
401413
'quiet': "Don't print nonessential feedback",
402414
'timing': 'Report execution times'}
@@ -635,17 +647,7 @@ def ppaged(self, msg: str, end: str='\n') -> None:
635647
# Don't attempt to use a pager that can block if redirecting or running a script (either text or Python)
636648
# Also only attempt to use a pager if actually running in a real fully functional terminal
637649
if functional_terminal and not self.redirecting and not self._in_py and not self._script_dir:
638-
639-
if sys.platform.startswith('win'):
640-
pager_cmd = 'more'
641-
else:
642-
# Here is the meaning of the various flags we are using with the less command:
643-
# -S causes lines longer than the screen width to be chopped (truncated) rather than wrapped
644-
# -R causes ANSI "color" escape sequences to be output in raw form (i.e. colors are displayed)
645-
# -X disables sending the termcap initialization and deinitialization strings to the terminal
646-
# -F causes less to automatically exit if the entire file can be displayed on the first screen
647-
pager_cmd = 'less -SRXF'
648-
self.pipe_proc = subprocess.Popen(pager_cmd, shell=True, stdin=subprocess.PIPE)
650+
self.pipe_proc = subprocess.Popen(self.pager, shell=True, stdin=subprocess.PIPE)
649651
try:
650652
self.pipe_proc.stdin.write(msg_str.encode('utf-8', 'replace'))
651653
self.pipe_proc.stdin.close()

examples/paged_output.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,35 @@
22
# coding=utf-8
33
"""A simple example demonstrating the using paged output via the ppaged() method.
44
"""
5+
import os
6+
from typing import List
57

68
import cmd2
79

810

911
class PagedOutput(cmd2.Cmd):
10-
""" Example cmd2 application where we create commands that just print the arguments they are called with."""
12+
""" Example cmd2 application which shows how to display output using a pager."""
1113

1214
def __init__(self):
1315
super().__init__()
1416

1517
@cmd2.with_argument_list
16-
def do_page_file(self, args):
17-
"""Read in a text file and display its output in a pager."""
18+
def do_page_file(self, args: List[str]):
19+
"""Read in a text file and display its output in a pager.
20+
21+
Usage: page_file <file_path>
22+
"""
1823
if not args:
1924
self.perror('page_file requires a path to a file as an argument', traceback_war=False)
2025
return
2126

22-
with open(args[0], 'r') as f:
23-
text = f.read()
24-
self.ppaged(text)
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)
2534

2635
complete_page_file = cmd2.Cmd.path_complete
2736

tests/conftest.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@
8080
"""
8181

8282
expect_colors = True
83+
pager = 'less -SRXF'
8384
if sys.platform.startswith('win'):
8485
expect_colors = False
86+
pager = 'more'
8587
# Output from the show command with default settings
8688
SHOW_TXT = """colors: {}
8789
continuation_prompt: >
@@ -90,10 +92,11 @@
9092
editor: vim
9193
feedback_to_output: False
9294
locals_in_py: False
95+
pager: {}
9396
prompt: (Cmd)
9497
quiet: False
9598
timing: False
96-
""".format(expect_colors)
99+
""".format(expect_colors, pager)
97100

98101
if expect_colors:
99102
color_str = 'True '
@@ -107,10 +110,11 @@
107110
editor: vim # Program used by ``edit``
108111
feedback_to_output: False # Include nonessentials in `|`, `>` results
109112
locals_in_py: False # Allow access to your application in py via self
113+
pager: {} # Command used for displaying paged output
110114
prompt: (Cmd) # The prompt issued to solicit input
111115
quiet: False # Don't print nonessential feedback
112116
timing: False # Report execution times
113-
""".format(color_str)
117+
""".format(color_str, pager)
114118

115119

116120
class StdOut(object):

tests/transcripts/regex_set.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ editor: /.*/
1212
feedback_to_output: False
1313
locals_in_py: False
1414
maxrepeats: 3
15+
pager: /.*/
1516
prompt: (Cmd)/ /
1617
quiet: False
1718
timing: False

0 commit comments

Comments
 (0)