comparison textshaper/main.py @ 34:88a69d587326

round1 of commands
author Jeff Hammel <k0scist@gmail.com>
date Sun, 02 Mar 2014 15:33:07 -0800
parents 929a5e97af3e
children 55e0579e2143
comparison
equal deleted inserted replaced
33:de3148412191 34:88a69d587326
8 import argparse 8 import argparse
9 import os 9 import os
10 import subprocess 10 import subprocess
11 import sys 11 import sys
12 import time 12 import time
13 from .commands import Commands
14 from .indent import deindent, indent
13 from which import which 15 from which import which
14 16
17 HR = '--'
15 18
16 def info(content): 19 def info(content):
17 """gathers info about the content and returns a dict""" 20 """gathers info about the content and returns a dict"""
18 21
19 lines = content.splitlines() 22 lines = content.splitlines()
20 return {'lines': len(lines), 23 return {'lines': len(lines),
21 'chars': len(content), 24 'chars': len(content),
22 'columns': max([len(line) for line in lines])} 25 'columns': max([len(line) for line in lines])}
23 26
24 27
25 def display(content, keys=('lines', 'chars', 'columns'), hr='--'): 28 def display(content, keys=('lines', 'chars', 'columns'), hr=HR):
26 """displays the content""" 29 """displays the content"""
27 print (content) 30 print (content)
28 if keys: 31 if keys:
29 _info = info(content) 32 _info = info(content)
30 print (hr) 33 print (hr)
31 print ('; '.join(['{}: {}'.format(key, _info[key]) 34 print ('; '.join(['{}: {}'.format(key, _info[key])
32 for key in keys])) 35 for key in keys]))
36
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
33 43
34 def add_options(parser): 44 def add_options(parser):
35 """add options to the parser instance""" 45 """add options to the parser instance"""
36 46
37 parser.add_argument('-f', '--file', dest='input', 47 parser.add_argument('-f', '--file', dest='input',
40 parser.add_argument('-n', '--no-strip', dest='strip', 50 parser.add_argument('-n', '--no-strip', dest='strip',
41 action='store_false', default=True, 51 action='store_false', default=True,
42 help="do not strip whitespace before processing") 52 help="do not strip whitespace before processing")
43 53
44 if which('xclip'): # TODO: support e.g. xsel or python native 54 if which('xclip'): # TODO: support e.g. xsel or python native
45 parser.add_argument('-c', '--clip', '--copy', dest='copy_to_clipboard', 55 parser.add_argument('-c', '-o', '--clip', '--copy', dest='copy_to_clipboard',
46 action='store_true', default=False, 56 action='store_true', default=False,
47 help="copy to clipboard") 57 help="copy to clipboard")
48 58 parser.add_argument('-i', dest='input_from_clipboard',
59 action='store_true', default=False,
60 help="copy from clipboard")
49 61
50 62
51 def main(args=sys.argv[1:]): 63 def main(args=sys.argv[1:]):
64 """CLI"""
52 65
53 # TODO: read ~/.textshaper 66 # TODO: read ~/.textshaper
54 67
55 # parse command line options 68 # parse command line options
56 parser = argparse.ArgumentParser(description=__doc__) 69 parser = argparse.ArgumentParser(description=__doc__)
57 add_options(parser) 70 add_options(parser)
58 options = parser.parse_args(args) 71 options = parser.parse_args(args)
59 72
60 # read input 73 # read input
61 content = options.input.read() 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()
62 81
63 # pre-process output 82 # pre-process output
64 if options.strip: 83 if options.strip:
65 content = content.strip() 84 content = content.strip()
66 85
67 # print formatted content
68 display(content)
69
70 # main display loop 86 # main display loop
71 # TODO: read input + commands 87 # TODO: read input + commands
72 while True: 88 try:
73 time.sleep(1) # XXX 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
74 99
75 if options.copy_to_clipboard: 100 if options.copy_to_clipboard:
76 # copy content to X clipboard 101 # copy content to X clipboard
77 process = subprocess.Popen(['xclip', '-i'], stdin=subprocess.PIPE) 102 process = subprocess.Popen(['xclip', '-i'], stdin=subprocess.PIPE)
78 _, _ = process.communicate(content) 103 _, _ = process.communicate(content)
104 except KeyboardInterrupt:
105 sys.exit(0)
79 106
80 if __name__ == '__main__': 107 if __name__ == '__main__':
81 main() 108 main()
82 109