Mercurial > hg > TextShaper
comparison 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 |
comparison
equal
deleted
inserted
replaced
17:30c534d3db66 | 18:df52326aa08d |
---|---|
3 | 3 |
4 """ | 4 """ |
5 package to shape text blocks | 5 package to shape text blocks |
6 """ | 6 """ |
7 | 7 |
8 import optparse | 8 import argparse |
9 import os | 9 import os |
10 import subprocess | 10 import subprocess |
11 import sys | 11 import sys |
12 import time | 12 import time |
13 from which import which | |
13 | 14 |
14 def info(content): | 15 def info(content): |
15 """gathers info about the content and returns a dict""" | 16 """gathers info about the content and returns a dict""" |
17 | |
16 lines = content.splitlines() | 18 lines = content.splitlines() |
17 return {'lines': len(lines), | 19 return {'lines': len(lines), |
18 'chars': len(content), | 20 'chars': len(content), |
19 'columns': max([len(line) for line in lines]) | 21 'columns': max([len(line) for line in lines])} |
20 } | 22 |
21 | 23 |
22 def display(content, keys=('lines', 'chars', 'columns'), hr='--'): | 24 def display(content, keys=('lines', 'chars', 'columns'), hr='--'): |
23 print content | 25 """displays the content""" |
26 print (content) | |
24 if keys: | 27 if keys: |
25 _info = info(content) | 28 _info = info(content) |
26 print (hr) | 29 print (hr) |
27 print ('; '.join(['{}: {}'.format(key, _info[key]) | 30 print ('; '.join(['{}: {}'.format(key, _info[key]) |
28 for key in keys])) | 31 for key in keys])) |
29 | 32 |
30 def add_options(parser): | 33 def add_options(parser): |
31 """add options to the OptionParser instance""" | 34 """add options to the OptionParser instance""" |
32 | 35 |
33 # TODO | 36 if which('xclip'): # TODO: support e.g. xsel or python native |
34 # parser.add_option('-c', '--clip', '--copy', dest='copy_to_clipboard', | 37 parser.add_option('-c', '--clip', '--copy', dest='copy_to_clipboard', |
35 # help="copy to given program on exit") | 38 action='store_true', default=False, |
39 help="copy to clipboard") | |
40 | |
41 | |
36 | 42 |
37 def main(args=sys.argv[1:]): | 43 def main(args=sys.argv[1:]): |
38 | 44 |
39 # parse command line options | 45 # parse command line options |
40 usage = '%prog [options] ...' | 46 parser = argparse.ArgumentParser(description=__doc__, formatter=PlainDescriptionFormatter()) |
41 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter): | 47 options = parser.parse_args(args) |
42 """description formatter for console script entry point""" | |
43 def format_description(self, description): | |
44 if description: | |
45 return description.strip() + '\n' | |
46 else: | |
47 return '' | |
48 parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter()) | |
49 options, args = parser.parse_args(args) | |
50 | 48 |
51 # read from stdin | 49 # read from stdin |
52 content = sys.stdin.read() | 50 content = sys.stdin.read() |
53 | 51 |
54 # print formatted content | 52 # print formatted content |
55 display(content) | 53 display(content) |
56 | 54 |
55 # main display loop | |
56 # TODO: read input + commands | |
57 while True: | 57 while True: |
58 time.sleep(1) # XXX | 58 time.sleep(1) # XXX |
59 | |
60 if options.copy_to_clipboard: | |
61 # copy content to X clipboard | |
62 process = subprocess.Popen(['xclip', '-i'], stdin=subprocess.PIPE) | |
63 _, _ = process.communicate(content) | |
59 | 64 |
60 if __name__ == '__main__': | 65 if __name__ == '__main__': |
61 main() | 66 main() |
62 | 67 |