summaryrefslogtreecommitdiff
path: root/examples/paged_output.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2018-06-16 12:44:36 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2018-06-16 12:44:36 -0400
commite3672dfec9c7f0919a726b3e105f06b9474b630f (patch)
tree5c8f6a5a37688a20f78d230745b273c66f888007 /examples/paged_output.py
parenteb32a860d442f6990971bf9ab96db576c939587c (diff)
downloadcmd2-git-e3672dfec9c7f0919a726b3e105f06b9474b630f.tar.gz
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
Diffstat (limited to 'examples/paged_output.py')
-rwxr-xr-xexamples/paged_output.py42
1 files changed, 30 insertions, 12 deletions
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__':