Commits

Geert Jansen  committed 308df73

Implement auto paging

  • Participants
  • Parent commits 0d5a739

Comments (0)

Files changed (3)

File lib/cli/context.py

 from StringIO import StringIO
 from subprocess import Popen, PIPE
 
+from cli import platform
 from cli.error import ParseError, CommandError
 from cli.object import create
 from cli.settings import Settings
             self._execute_shell_command(parsed[1])
             return
         name, args, opts, redirections, pipeline = parsed
+        if self.settings.get('cli:autopage'):
+            pager = self.settings.get('cli:pager', platform.get_pager())
+            if pipeline:
+                pipeline += '| %' % pager
+            else:
+                pipeline = pager
         command = self._create_command(name, args, opts)
         self.command = command
         self._setup_pipeline(pipeline)

File lib/cli/platform/posix/util.py

     name = name.replace('-', '').replace('_', '')
     fname = os.path.join(home, '.%src' % name)
     return fname
+
+def which(cmd):
+    """Find a command `cmd' in the path."""
+    if cmd.startswith('/') and os.access(cmd, os.X_OK):
+        return cmd
+    path = os.environ.get('PATH')
+    path = path.split(os.pathsep)
+    for dir in path:
+        fname = os.path.join(dir, cmd)
+        if os.access(fname, os.X_OK):
+            return fname
+
+def get_pager():
+    """Return the platform specific pager."""
+    pager = os.environ.get('PAGER')
+    if pager is None and which('less'):
+        pager = 'less -FSRX'
+    if pager is None and which('more'):
+        pager = 'more'
+    return pager

File lib/cli/settings.py

         ('cli:ps2', str, '> '),
         ('cli:debug', boolean, False),
         ('cli:verbosity', int, 0)
+        ('cli:autopage', boolean, True),
+        ('cli:pager', str, None)
     ]
 
     def __init__(self, name):