summaryrefslogtreecommitdiff
path: root/examples/paged_output.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2018-06-27 06:05:59 -0700
committerGitHub <noreply@github.com>2018-06-27 06:05:59 -0700
commit842788675e9c4f0d416626ea3d8a9dac5e00b595 (patch)
tree15c42850f2d624ed60565876d20889ca8b7cf8ff /examples/paged_output.py
parentf0c98ac10e60995bbf2dff9848931109cac2f96e (diff)
parent3c8880e40bf653e098577de94be7eb2171dbc6b4 (diff)
downloadcmd2-git-842788675e9c4f0d416626ea3d8a9dac5e00b595.tar.gz
Merge branch 'master' into autocompleter
Diffstat (limited to 'examples/paged_output.py')
-rwxr-xr-xexamples/paged_output.py43
1 files changed, 35 insertions, 8 deletions
diff --git a/examples/paged_output.py b/examples/paged_output.py
index c56dcb89..d1b1b2c2 100755
--- a/examples/paged_output.py
+++ b/examples/paged_output.py
@@ -2,28 +2,55 @@
# 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__()
+ 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):
- """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_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)
+
+ 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.
- with open(args[0], 'r') as f:
- text = f.read()
- self.ppaged(text)
+ 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__':