Commits

Nikolaj Sjujskij committed c346f41 Merge

Merging `ino_watch` worklines

"Inline" action registration + pager and firsttimer fix

Comments (0)

Files changed (1)

inotify_watcher.py

 
 from optparse import OptionParser
 import os.path
+import subprocess
 import time
 
 import inotifyx
 
 implemented_actions = {}
 
+PAGER_OPTS = ['less', '-']
+
 
 def register_action(name=None, is_firsttimer=False,
                                catalogue=implemented_actions):
     ''' Registration decorator, maintaining dict of implemented actions'''
 
     def register_closure(f):
+        # it'd be nice to have `nonlocal` here in Python 2.x
+        add_kwargs = {}
         if is_firsttimer:
-            add_kwargs = {'first_time': True}
+            add_kwargs['first_time'] = True
 
         def tmp(*args, **kwargs):
             if add_kwargs.get('first_time', False):
     return register_closure
 
 
+def page_output(f):
+    ''' Pipes output through pager (i.e. `less`) '''
+    def paging_wrapper(*args, **kwargs):
+        import sys
+        output = f(*args, **kwargs)
+        pager = subprocess.Popen(PAGER_OPTS, stdin=subprocess.PIPE)
+        pager.stdin.write(output.encode('utf-8'))
+        pager.stdin = sys.stdin
+        return None
+    return paging_wrapper
+
+
 @register_action('hlite')
+@page_output
 def highlight_watched(filename):
     ''' File contents highlighter '''
     with open(filename, 'r') as file_to_read:
     content = highlight(content,
                         guess_lexer_for_filename(filename, ""),
                         TerminalFormatter(bg='dark'))
-    print(content)
+    return content
 
 
 @register_action('rsthtml', is_firsttimer=True)