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