Mercurial > hg > config
annotate python/dmenu.py @ 290:03d62a2cbe62
update demnu to almost a real program
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 10 May 2013 19:43:47 -0700 |
parents | e1a861bbb559 |
children | 8f92aa15406f |
rev | line source |
---|---|
289 | 1 #!/usr/bin/env python |
2 | |
3 import optparse | |
4 import os | |
5 import subprocess | |
6 import sys | |
7 | |
290
03d62a2cbe62
update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
289
diff
changeset
|
8 def choose_file(directory, dmenu='dmenu', |
03d62a2cbe62
update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
289
diff
changeset
|
9 args=('-i', '-nb', 'black', '-nf', 'white')): |
289 | 10 """choose a file in the directory with dmenu""" |
11 directory = os.path.abspath(directory) | |
12 files = os.listdir(directory) | |
13 string = '\n'.join(files) | |
14 | |
290
03d62a2cbe62
update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
289
diff
changeset
|
15 if isinstance(dmenu, basestring): |
03d62a2cbe62
update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
289
diff
changeset
|
16 dmenu = [dmenu] |
03d62a2cbe62
update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
289
diff
changeset
|
17 dmenu = list(dmenu) |
03d62a2cbe62
update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
289
diff
changeset
|
18 dmenu.extend(args) |
289 | 19 |
290
03d62a2cbe62
update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
289
diff
changeset
|
20 process = subprocess.Popen(dmenu, stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
289 | 21 stdout, _ = process.communicate(input=string) |
22 if process.returncode: | |
23 return | |
24 chosen = os.path.join(directory, stdout) | |
25 if os.path.isdir(chosen): | |
26 return choose_file(chosen) | |
27 return chosen | |
28 | |
29 def main(args=sys.argv[1:]): | |
30 parser = optparse.OptionParser() | |
290
03d62a2cbe62
update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
289
diff
changeset
|
31 parser.add_option('-d', '--directory', dest='directory', |
03d62a2cbe62
update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
289
diff
changeset
|
32 default=os.getcwd(), |
03d62a2cbe62
update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
289
diff
changeset
|
33 help="call on this directory [Default: current directory]") |
03d62a2cbe62
update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
289
diff
changeset
|
34 parser.add_option('-e', '--exec', dest='executable', |
03d62a2cbe62
update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
289
diff
changeset
|
35 help="call this proram with the result") |
03d62a2cbe62
update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
289
diff
changeset
|
36 options, args = parser.parse_args(args) |
03d62a2cbe62
update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
289
diff
changeset
|
37 chosen = choose_file(options.directory) |
03d62a2cbe62
update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
289
diff
changeset
|
38 if chosen: |
03d62a2cbe62
update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
289
diff
changeset
|
39 if options.executable: |
03d62a2cbe62
update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
289
diff
changeset
|
40 pass |
03d62a2cbe62
update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
289
diff
changeset
|
41 print chosen |
03d62a2cbe62
update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
289
diff
changeset
|
42 else: |
03d62a2cbe62
update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents:
289
diff
changeset
|
43 sys.exit(1) |
289 | 44 |
45 if __name__ == '__main__': | |
46 main() |