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