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