annotate textshaper/main.py @ 20:09f4c09d9617

remove cruft
author Jeff Hammel <k0scist@gmail.com>
date Sun, 23 Feb 2014 00:31:09 -0800
parents 70dde00a4df0
children 586631375670
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 """
4
26bb5e99f76c textshaper/main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
5 package to shape text blocks
0
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 """
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7
18
df52326aa08d start filling out options
Jeff Hammel <k0scist@gmail.com>
parents: 13
diff changeset
8 import argparse
0
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 import os
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 import subprocess
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 import sys
3
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
12 import time
18
df52326aa08d start filling out options
Jeff Hammel <k0scist@gmail.com>
parents: 13
diff changeset
13 from which import which
0
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14
19
70dde00a4df0 selectable, albeit single, input
Jeff Hammel <k0scist@gmail.com>
parents: 18
diff changeset
15
12
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
16 def info(content):
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
17 """gathers info about the content and returns a dict"""
18
df52326aa08d start filling out options
Jeff Hammel <k0scist@gmail.com>
parents: 13
diff changeset
18
12
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
19 lines = content.splitlines()
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
20 return {'lines': len(lines),
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
21 'chars': len(content),
18
df52326aa08d start filling out options
Jeff Hammel <k0scist@gmail.com>
parents: 13
diff changeset
22 'columns': max([len(line) for line in lines])}
df52326aa08d start filling out options
Jeff Hammel <k0scist@gmail.com>
parents: 13
diff changeset
23
12
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
24
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
25 def display(content, keys=('lines', 'chars', 'columns'), hr='--'):
18
df52326aa08d start filling out options
Jeff Hammel <k0scist@gmail.com>
parents: 13
diff changeset
26 """displays the content"""
df52326aa08d start filling out options
Jeff Hammel <k0scist@gmail.com>
parents: 13
diff changeset
27 print (content)
12
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
28 if keys:
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
29 _info = info(content)
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
30 print (hr)
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
31 print ('; '.join(['{}: {}'.format(key, _info[key])
13
c3df7dccb02b STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 12
diff changeset
32 for key in keys]))
4
26bb5e99f76c textshaper/main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
33
0
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 def add_options(parser):
19
70dde00a4df0 selectable, albeit single, input
Jeff Hammel <k0scist@gmail.com>
parents: 18
diff changeset
35 """add options to the parser instance"""
70dde00a4df0 selectable, albeit single, input
Jeff Hammel <k0scist@gmail.com>
parents: 18
diff changeset
36
70dde00a4df0 selectable, albeit single, input
Jeff Hammel <k0scist@gmail.com>
parents: 18
diff changeset
37 parser.add_option('-f', '--file', dest='input',
70dde00a4df0 selectable, albeit single, input
Jeff Hammel <k0scist@gmail.com>
parents: 18
diff changeset
38 type=argparse.FileType('r'), default=sys.stdin,
70dde00a4df0 selectable, albeit single, input
Jeff Hammel <k0scist@gmail.com>
parents: 18
diff changeset
39 help="file to read from [DEFAULT: stdin]")
0
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40
18
df52326aa08d start filling out options
Jeff Hammel <k0scist@gmail.com>
parents: 13
diff changeset
41 if which('xclip'): # TODO: support e.g. xsel or python native
df52326aa08d start filling out options
Jeff Hammel <k0scist@gmail.com>
parents: 13
diff changeset
42 parser.add_option('-c', '--clip', '--copy', dest='copy_to_clipboard',
df52326aa08d start filling out options
Jeff Hammel <k0scist@gmail.com>
parents: 13
diff changeset
43 action='store_true', default=False,
df52326aa08d start filling out options
Jeff Hammel <k0scist@gmail.com>
parents: 13
diff changeset
44 help="copy to clipboard")
df52326aa08d start filling out options
Jeff Hammel <k0scist@gmail.com>
parents: 13
diff changeset
45
df52326aa08d start filling out options
Jeff Hammel <k0scist@gmail.com>
parents: 13
diff changeset
46
9
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
47
0
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 def main(args=sys.argv[1:]):
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 # parse command line options
18
df52326aa08d start filling out options
Jeff Hammel <k0scist@gmail.com>
parents: 13
diff changeset
51 parser = argparse.ArgumentParser(description=__doc__, formatter=PlainDescriptionFormatter())
df52326aa08d start filling out options
Jeff Hammel <k0scist@gmail.com>
parents: 13
diff changeset
52 options = parser.parse_args(args)
0
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53
3
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
54 # read from stdin
19
70dde00a4df0 selectable, albeit single, input
Jeff Hammel <k0scist@gmail.com>
parents: 18
diff changeset
55 content = options.read()
3
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
56
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
57 # print formatted content
4
26bb5e99f76c textshaper/main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
58 display(content)
3
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
59
18
df52326aa08d start filling out options
Jeff Hammel <k0scist@gmail.com>
parents: 13
diff changeset
60 # main display loop
df52326aa08d start filling out options
Jeff Hammel <k0scist@gmail.com>
parents: 13
diff changeset
61 # TODO: read input + commands
3
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
62 while True:
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
63 time.sleep(1) # XXX
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
64
18
df52326aa08d start filling out options
Jeff Hammel <k0scist@gmail.com>
parents: 13
diff changeset
65 if options.copy_to_clipboard:
df52326aa08d start filling out options
Jeff Hammel <k0scist@gmail.com>
parents: 13
diff changeset
66 # copy content to X clipboard
df52326aa08d start filling out options
Jeff Hammel <k0scist@gmail.com>
parents: 13
diff changeset
67 process = subprocess.Popen(['xclip', '-i'], stdin=subprocess.PIPE)
df52326aa08d start filling out options
Jeff Hammel <k0scist@gmail.com>
parents: 13
diff changeset
68 _, _ = process.communicate(content)
df52326aa08d start filling out options
Jeff Hammel <k0scist@gmail.com>
parents: 13
diff changeset
69
0
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
70 if __name__ == '__main__':
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
71 main()
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
72