# HG changeset patch # User Jeff Hammel # Date 1393138158 28800 # Node ID df52326aa08d3558e21a023d697deef591c6ac95 # Parent 30c534d3db666724c937a83170bde8f549b1e654 start filling out options diff -r 30c534d3db66 -r df52326aa08d setup.py --- a/setup.py Sat Feb 22 17:00:08 2014 -0800 +++ b/setup.py Sat Feb 22 22:49:18 2014 -0800 @@ -5,7 +5,7 @@ import os version = "0.0" -dependencies = ['webob'] +dependencies = ['webob', 'which'] # allow use of setuptools/distribute or distutils kw = {} diff -r 30c534d3db66 -r df52326aa08d textshaper/main.py --- 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()