diff textshaper/main.py @ 18:df52326aa08d

start filling out options
author Jeff Hammel <k0scist@gmail.com>
date Sat, 22 Feb 2014 22:49:18 -0800
parents c3df7dccb02b
children 70dde00a4df0
line wrap: on
line diff
--- a/textshaper/main.py	Sat Feb 22 17:00:08 2014 -0800
+++ b/textshaper/main.py	Sat Feb 22 22:49:18 2014 -0800
@@ -5,22 +5,25 @@
 package to shape text blocks
 """
 
-import optparse
+import argparse
 import os
 import subprocess
 import sys
 import time
+from which import which
 
 def info(content):
     """gathers info about the content and returns a dict"""
+
     lines = content.splitlines()
     return {'lines': len(lines),
             'chars': len(content),
-            'columns': max([len(line) for line in lines])
-        }
+            'columns': max([len(line) for line in lines])}
+
 
 def display(content, keys=('lines', 'chars', 'columns'), hr='--'):
-    print content
+    """displays the content"""
+    print (content)
     if keys:
         _info = info(content)
         print (hr)
@@ -30,23 +33,18 @@
 def add_options(parser):
     """add options to the OptionParser instance"""
 
-    # TODO
-    #    parser.add_option('-c', '--clip', '--copy', dest='copy_to_clipboard',
-    #                  help="copy to given program on exit")
+    if which('xclip'): # TODO: support e.g. xsel or python native
+        parser.add_option('-c', '--clip', '--copy', dest='copy_to_clipboard',
+                          action='store_true', default=False,
+                          help="copy to clipboard")
+
+
 
 def main(args=sys.argv[1:]):
 
     # parse command line options
-    usage = '%prog [options] ...'
-    class PlainDescriptionFormatter(optparse.IndentedHelpFormatter):
-        """description formatter for console script entry point"""
-        def format_description(self, description):
-            if description:
-                return description.strip() + '\n'
-            else:
-                return ''
-    parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter())
-    options, args = parser.parse_args(args)
+    parser = argparse.ArgumentParser(description=__doc__, formatter=PlainDescriptionFormatter())
+    options = parser.parse_args(args)
 
     # read from stdin
     content = sys.stdin.read()
@@ -54,9 +52,16 @@
     # print formatted content
     display(content)
 
+    # main display loop
+    # TODO: read input + commands
     while True:
         time.sleep(1) # XXX
 
+    if options.copy_to_clipboard:
+        # copy content to X clipboard
+        process = subprocess.Popen(['xclip', '-i'], stdin=subprocess.PIPE)
+        _, _ = process.communicate(content)
+
 if __name__ == '__main__':
   main()