Mercurial > hg > TextShaper
annotate textshaper/main.py @ 26:c23782a7b7ba
more hookups, yo
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 23 Feb 2014 11:41:37 -0800 |
parents | 586631375670 |
children | 929a5e97af3e |
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 |
19
70dde00a4df0
selectable, albeit single, input
Jeff Hammel <k0scist@gmail.com>
parents:
18
diff
changeset
|
15 |
12 | 16 def info(content): |
17 """gathers info about the content and returns a dict""" | |
18 | 18 |
12 | 19 lines = content.splitlines() |
20 return {'lines': len(lines), | |
21 'chars': len(content), | |
18 | 22 'columns': max([len(line) for line in lines])} |
23 | |
12 | 24 |
25 def display(content, keys=('lines', 'chars', 'columns'), hr='--'): | |
18 | 26 """displays the content""" |
27 print (content) | |
12 | 28 if keys: |
29 _info = info(content) | |
30 print (hr) | |
31 print ('; '.join(['{}: {}'.format(key, _info[key]) | |
13 | 32 for key in keys])) |
4 | 33 |
0 | 34 def add_options(parser): |
19
70dde00a4df0
selectable, albeit single, input
Jeff Hammel <k0scist@gmail.com>
parents:
18
diff
changeset
|
35 """add options to the parser instance""" |
70dde00a4df0
selectable, albeit single, input
Jeff Hammel <k0scist@gmail.com>
parents:
18
diff
changeset
|
36 |
22
586631375670
STUB: README.txt textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents:
19
diff
changeset
|
37 parser.add_argument('-f', '--file', dest='input', |
586631375670
STUB: README.txt textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents:
19
diff
changeset
|
38 type=argparse.FileType('r'), default=sys.stdin, |
586631375670
STUB: README.txt textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents:
19
diff
changeset
|
39 help="file to read from [DEFAULT: stdin]") |
0 | 40 |
18 | 41 if which('xclip'): # TODO: support e.g. xsel or python native |
22
586631375670
STUB: README.txt textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents:
19
diff
changeset
|
42 parser.add_argument('-c', '--clip', '--copy', dest='copy_to_clipboard', |
586631375670
STUB: README.txt textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents:
19
diff
changeset
|
43 action='store_true', default=False, |
586631375670
STUB: README.txt textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents:
19
diff
changeset
|
44 help="copy to clipboard") |
18 | 45 |
46 | |
9
71fb16088d54
add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
47 |
0 | 48 def main(args=sys.argv[1:]): |
49 | |
50 # parse command line options | |
22
586631375670
STUB: README.txt textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents:
19
diff
changeset
|
51 parser = argparse.ArgumentParser(description=__doc__) |
586631375670
STUB: README.txt textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents:
19
diff
changeset
|
52 add_options(parser) |
18 | 53 options = parser.parse_args(args) |
0 | 54 |
22
586631375670
STUB: README.txt textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents:
19
diff
changeset
|
55 # read input |
586631375670
STUB: README.txt textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents:
19
diff
changeset
|
56 content = options.input.read() |
3 | 57 |
58 # print formatted content | |
4 | 59 display(content) |
3 | 60 |
18 | 61 # main display loop |
62 # TODO: read input + commands | |
3 | 63 while True: |
64 time.sleep(1) # XXX | |
65 | |
18 | 66 if options.copy_to_clipboard: |
67 # copy content to X clipboard | |
68 process = subprocess.Popen(['xclip', '-i'], stdin=subprocess.PIPE) | |
69 _, _ = process.communicate(content) | |
70 | |
0 | 71 if __name__ == '__main__': |
72 main() | |
73 |