Mercurial > hg > TextShaper
annotate 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 |
| rev | line source |
|---|---|
| 0 | 1 #!/usr/bin/env python |
| 2 # -*- coding: utf-8 -*- | |
| 3 | |
| 4 """ | |
| 4 | 5 package to shape text blocks |
| 0 | 6 """ |
| 7 | |
| 18 | 8 import argparse |
| 0 | 9 import os |
| 10 import subprocess | |
| 11 import sys | |
| 3 | 12 import time |
| 18 | 13 from which import which |
| 0 | 14 |
| 12 | 15 def info(content): |
| 16 """gathers info about the content and returns a dict""" | |
| 18 | 17 |
| 12 | 18 lines = content.splitlines() |
| 19 return {'lines': len(lines), | |
| 20 'chars': len(content), | |
| 18 | 21 'columns': max([len(line) for line in lines])} |
| 22 | |
| 12 | 23 |
| 24 def display(content, keys=('lines', 'chars', 'columns'), hr='--'): | |
| 18 | 25 """displays the content""" |
| 26 print (content) | |
| 12 | 27 if keys: |
| 28 _info = info(content) | |
| 29 print (hr) | |
| 30 print ('; '.join(['{}: {}'.format(key, _info[key]) | |
| 13 | 31 for key in keys])) |
| 4 | 32 |
| 0 | 33 def add_options(parser): |
| 34 """add options to the OptionParser instance""" | |
| 35 | |
| 18 | 36 if which('xclip'): # TODO: support e.g. xsel or python native |
| 37 parser.add_option('-c', '--clip', '--copy', dest='copy_to_clipboard', | |
| 38 action='store_true', default=False, | |
| 39 help="copy to clipboard") | |
| 40 | |
| 41 | |
|
9
71fb16088d54
add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
42 |
| 0 | 43 def main(args=sys.argv[1:]): |
| 44 | |
| 45 # parse command line options | |
| 18 | 46 parser = argparse.ArgumentParser(description=__doc__, formatter=PlainDescriptionFormatter()) |
| 47 options = parser.parse_args(args) | |
| 0 | 48 |
| 3 | 49 # read from stdin |
| 50 content = sys.stdin.read() | |
| 51 | |
| 52 # print formatted content | |
| 4 | 53 display(content) |
| 3 | 54 |
| 18 | 55 # main display loop |
| 56 # TODO: read input + commands | |
| 3 | 57 while True: |
| 58 time.sleep(1) # XXX | |
| 59 | |
| 18 | 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) | |
| 64 | |
| 0 | 65 if __name__ == '__main__': |
| 66 main() | |
| 67 |
