Mercurial > hg > TextShaper
annotate textshaper/main.py @ 39:986f8a20c234
STUB: textshaper/url2txt.py
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Thu, 03 Jul 2014 13:46:30 -0700 |
parents | 8cf9a26b9aaa |
children | 21b6a9569f21 |
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 |
34 | 13 from .commands import Commands |
14 from .indent import deindent, indent | |
37
8cf9a26b9aaa
STUB: textshaper/main.py textshaper/tablify.py
Jeff Hammel <k0scist@gmail.com>
parents:
36
diff
changeset
|
15 from .tablify import make_csv |
36
55e0579e2143
STUB: textshaper/main.py textshaper/whitespace.py
Jeff Hammel <k0scist@gmail.com>
parents:
34
diff
changeset
|
16 from .whitespace import underscore |
18 | 17 from which import which |
0 | 18 |
34 | 19 HR = '--' |
19
70dde00a4df0
selectable, albeit single, input
Jeff Hammel <k0scist@gmail.com>
parents:
18
diff
changeset
|
20 |
12 | 21 def info(content): |
22 """gathers info about the content and returns a dict""" | |
18 | 23 |
12 | 24 lines = content.splitlines() |
25 return {'lines': len(lines), | |
26 'chars': len(content), | |
18 | 27 'columns': max([len(line) for line in lines])} |
28 | |
12 | 29 |
34 | 30 def display(content, keys=('lines', 'chars', 'columns'), hr=HR): |
18 | 31 """displays the content""" |
32 print (content) | |
12 | 33 if keys: |
34 _info = info(content) | |
35 print (hr) | |
36 print ('; '.join(['{}: {}'.format(key, _info[key]) | |
13 | 37 for key in keys])) |
4 | 38 |
34 | 39 def add_commands(): |
40 # TODO: do this dynamically | |
41 commands = Commands() | |
42 commands.add(indent, 'i') | |
43 commands.add(deindent, 'd') | |
36
55e0579e2143
STUB: textshaper/main.py textshaper/whitespace.py
Jeff Hammel <k0scist@gmail.com>
parents:
34
diff
changeset
|
44 commands.add(underscore, 'u') |
37
8cf9a26b9aaa
STUB: textshaper/main.py textshaper/tablify.py
Jeff Hammel <k0scist@gmail.com>
parents:
36
diff
changeset
|
45 commands.add(make_csv, 'c') |
34 | 46 return commands |
47 | |
36
55e0579e2143
STUB: textshaper/main.py textshaper/whitespace.py
Jeff Hammel <k0scist@gmail.com>
parents:
34
diff
changeset
|
48 |
0 | 49 def add_options(parser): |
19
70dde00a4df0
selectable, albeit single, input
Jeff Hammel <k0scist@gmail.com>
parents:
18
diff
changeset
|
50 """add options to the parser instance""" |
70dde00a4df0
selectable, albeit single, input
Jeff Hammel <k0scist@gmail.com>
parents:
18
diff
changeset
|
51 |
22
586631375670
STUB: README.txt textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents:
19
diff
changeset
|
52 parser.add_argument('-f', '--file', dest='input', |
586631375670
STUB: README.txt textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents:
19
diff
changeset
|
53 type=argparse.FileType('r'), default=sys.stdin, |
586631375670
STUB: README.txt textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents:
19
diff
changeset
|
54 help="file to read from [DEFAULT: stdin]") |
32
929a5e97af3e
prestrip text unless specified
Jeff Hammel <k0scist@gmail.com>
parents:
22
diff
changeset
|
55 parser.add_argument('-n', '--no-strip', dest='strip', |
929a5e97af3e
prestrip text unless specified
Jeff Hammel <k0scist@gmail.com>
parents:
22
diff
changeset
|
56 action='store_false', default=True, |
929a5e97af3e
prestrip text unless specified
Jeff Hammel <k0scist@gmail.com>
parents:
22
diff
changeset
|
57 help="do not strip whitespace before processing") |
0 | 58 |
18 | 59 if which('xclip'): # TODO: support e.g. xsel or python native |
34 | 60 parser.add_argument('-c', '-o', '--clip', '--copy', dest='copy_to_clipboard', |
22
586631375670
STUB: README.txt textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents:
19
diff
changeset
|
61 action='store_true', default=False, |
586631375670
STUB: README.txt textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents:
19
diff
changeset
|
62 help="copy to clipboard") |
34 | 63 parser.add_argument('-i', dest='input_from_clipboard', |
64 action='store_true', default=False, | |
65 help="copy from clipboard") | |
18 | 66 |
9
71fb16088d54
add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
67 |
0 | 68 def main(args=sys.argv[1:]): |
34 | 69 """CLI""" |
0 | 70 |
32
929a5e97af3e
prestrip text unless specified
Jeff Hammel <k0scist@gmail.com>
parents:
22
diff
changeset
|
71 # TODO: read ~/.textshaper |
929a5e97af3e
prestrip text unless specified
Jeff Hammel <k0scist@gmail.com>
parents:
22
diff
changeset
|
72 |
0 | 73 # parse command line options |
22
586631375670
STUB: README.txt textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents:
19
diff
changeset
|
74 parser = argparse.ArgumentParser(description=__doc__) |
586631375670
STUB: README.txt textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents:
19
diff
changeset
|
75 add_options(parser) |
18 | 76 options = parser.parse_args(args) |
0 | 77 |
22
586631375670
STUB: README.txt textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents:
19
diff
changeset
|
78 # read input |
34 | 79 if getattr(options, 'input_from_clipboard', False): |
80 content = subprocess.check_output(['xclip', '-o']) | |
81 else: | |
82 content = options.input.read() | |
83 | |
84 # get formatting commands | |
85 commands = add_commands() | |
3 | 86 |
32
929a5e97af3e
prestrip text unless specified
Jeff Hammel <k0scist@gmail.com>
parents:
22
diff
changeset
|
87 # pre-process output |
929a5e97af3e
prestrip text unless specified
Jeff Hammel <k0scist@gmail.com>
parents:
22
diff
changeset
|
88 if options.strip: |
929a5e97af3e
prestrip text unless specified
Jeff Hammel <k0scist@gmail.com>
parents:
22
diff
changeset
|
89 content = content.strip() |
929a5e97af3e
prestrip text unless specified
Jeff Hammel <k0scist@gmail.com>
parents:
22
diff
changeset
|
90 |
18 | 91 # main display loop |
92 # TODO: read input + commands | |
34 | 93 try: |
94 while True: | |
95 # print formatted content | |
96 display(content) | |
97 print (commands.display()) | |
98 choice = raw_input('? ') | |
99 new_content = commands(choice, content) | |
100 if new_content is None: | |
101 print ("Choice '{}' not recognized".format(choice)) | |
102 continue | |
103 content = new_content | |
3 | 104 |
34 | 105 if options.copy_to_clipboard: |
106 # copy content to X clipboard | |
107 process = subprocess.Popen(['xclip', '-i'], stdin=subprocess.PIPE) | |
108 _, _ = process.communicate(content) | |
109 except KeyboardInterrupt: | |
110 sys.exit(0) | |
18 | 111 |
0 | 112 if __name__ == '__main__': |
113 main() | |
114 |