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