diff textshaper/main.py @ 34:88a69d587326

round1 of commands
author Jeff Hammel <k0scist@gmail.com>
date Sun, 02 Mar 2014 15:33:07 -0800
parents 929a5e97af3e
children 55e0579e2143
line wrap: on
line diff
--- a/textshaper/main.py	Sun Feb 23 16:48:09 2014 -0800
+++ b/textshaper/main.py	Sun Mar 02 15:33:07 2014 -0800
@@ -10,8 +10,11 @@
 import subprocess
 import sys
 import time
+from .commands import Commands
+from .indent import deindent, indent
 from which import which
 
+HR = '--'
 
 def info(content):
     """gathers info about the content and returns a dict"""
@@ -22,7 +25,7 @@
             'columns': max([len(line) for line in lines])}
 
 
-def display(content, keys=('lines', 'chars', 'columns'), hr='--'):
+def display(content, keys=('lines', 'chars', 'columns'), hr=HR):
     """displays the content"""
     print (content)
     if keys:
@@ -31,6 +34,13 @@
     print ('; '.join(['{}: {}'.format(key, _info[key])
                       for key in keys]))
 
+def add_commands():
+    # TODO: do this dynamically
+    commands = Commands()
+    commands.add(indent, 'i')
+    commands.add(deindent, 'd')
+    return commands
+
 def add_options(parser):
     """add options to the parser instance"""
 
@@ -42,13 +52,16 @@
                         help="do not strip whitespace before processing")
 
     if which('xclip'): # TODO: support e.g. xsel or python native
-        parser.add_argument('-c', '--clip', '--copy', dest='copy_to_clipboard',
+        parser.add_argument('-c', '-o', '--clip', '--copy', dest='copy_to_clipboard',
                             action='store_true', default=False,
                             help="copy to clipboard")
-
+        parser.add_argument('-i', dest='input_from_clipboard',
+                            action='store_true', default=False,
+                            help="copy from clipboard")
 
 
 def main(args=sys.argv[1:]):
+    """CLI"""
 
     # TODO: read ~/.textshaper
 
@@ -58,24 +71,38 @@
     options = parser.parse_args(args)
 
     # read input
-    content = options.input.read()
+    if getattr(options, 'input_from_clipboard', False):
+        content = subprocess.check_output(['xclip', '-o'])
+    else:
+        content = options.input.read()
+
+    # get formatting commands
+    commands = add_commands()
 
     # pre-process output
     if options.strip:
         content = content.strip()
 
-    # print formatted content
-    display(content)
-
     # main display loop
     # TODO: read input + commands
-    while True:
-        time.sleep(1) # XXX
+    try:
+        while True:
+            # print formatted content
+            display(content)
+            print (commands.display())
+            choice = raw_input('? ')
+            new_content = commands(choice, content)
+            if new_content is None:
+                print ("Choice '{}' not recognized".format(choice))
+                continue
+            content = new_content
 
-    if options.copy_to_clipboard:
-        # copy content to X clipboard
-        process = subprocess.Popen(['xclip', '-i'], stdin=subprocess.PIPE)
-        _, _ = process.communicate(content)
+            if options.copy_to_clipboard:
+                # copy content to X clipboard
+                process = subprocess.Popen(['xclip', '-i'], stdin=subprocess.PIPE)
+                _, _ = process.communicate(content)
+    except KeyboardInterrupt:
+        sys.exit(0)
 
 if __name__ == '__main__':
   main()