annotate python/dmenu.py @ 562:8d28fce0a6d6

detect scripts; namely x-tile
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 11 Dec 2013 02:52:53 -0800
parents 618db1b1f34b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
289
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 import optparse
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 import os
297
618db1b1f34b dmenu + emacs, baby
Jeff Hammel <jhammel@mozilla.com>
parents: 291
diff changeset
5 import shlex
289
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 import subprocess
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 import sys
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8
290
03d62a2cbe62 update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 289
diff changeset
9 def choose_file(directory, dmenu='dmenu',
291
8f92aa15406f pick a font
Jeff Hammel <jhammel@mozilla.com>
parents: 290
diff changeset
10 args=('-i', '-nb', 'black', '-nf', 'white',
8f92aa15406f pick a font
Jeff Hammel <jhammel@mozilla.com>
parents: 290
diff changeset
11 '-fn', '-*-lucidatypewriter-medium-r-*-*-*-120-*-*-*-*-*-*')):
289
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 """choose a file in the directory with dmenu"""
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 directory = os.path.abspath(directory)
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 files = os.listdir(directory)
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 string = '\n'.join(files)
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16
290
03d62a2cbe62 update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 289
diff changeset
17 if isinstance(dmenu, basestring):
03d62a2cbe62 update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 289
diff changeset
18 dmenu = [dmenu]
03d62a2cbe62 update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 289
diff changeset
19 dmenu = list(dmenu)
03d62a2cbe62 update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 289
diff changeset
20 dmenu.extend(args)
289
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21
290
03d62a2cbe62 update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 289
diff changeset
22 process = subprocess.Popen(dmenu, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
289
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 stdout, _ = process.communicate(input=string)
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 if process.returncode:
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 return
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 chosen = os.path.join(directory, stdout)
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 if os.path.isdir(chosen):
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 return choose_file(chosen)
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 return chosen
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 def main(args=sys.argv[1:]):
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 parser = optparse.OptionParser()
290
03d62a2cbe62 update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 289
diff changeset
33 parser.add_option('-d', '--directory', dest='directory',
03d62a2cbe62 update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 289
diff changeset
34 default=os.getcwd(),
03d62a2cbe62 update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 289
diff changeset
35 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
36 parser.add_option('-e', '--exec', dest='executable',
03d62a2cbe62 update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 289
diff changeset
37 help="call this proram with the result")
03d62a2cbe62 update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 289
diff changeset
38 options, args = parser.parse_args(args)
03d62a2cbe62 update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 289
diff changeset
39 chosen = choose_file(options.directory)
03d62a2cbe62 update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 289
diff changeset
40 if chosen:
03d62a2cbe62 update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 289
diff changeset
41 if options.executable:
297
618db1b1f34b dmenu + emacs, baby
Jeff Hammel <jhammel@mozilla.com>
parents: 291
diff changeset
42 # TODO: extract this pattern (see ims journal.txt)
618db1b1f34b dmenu + emacs, baby
Jeff Hammel <jhammel@mozilla.com>
parents: 291
diff changeset
43 command = shlex.split(options.executable)
618db1b1f34b dmenu + emacs, baby
Jeff Hammel <jhammel@mozilla.com>
parents: 291
diff changeset
44 executable = command[0]
618db1b1f34b dmenu + emacs, baby
Jeff Hammel <jhammel@mozilla.com>
parents: 291
diff changeset
45 command.append(chosen)
618db1b1f34b dmenu + emacs, baby
Jeff Hammel <jhammel@mozilla.com>
parents: 291
diff changeset
46 os.execlp(executable, *command)
618db1b1f34b dmenu + emacs, baby
Jeff Hammel <jhammel@mozilla.com>
parents: 291
diff changeset
47 else:
618db1b1f34b dmenu + emacs, baby
Jeff Hammel <jhammel@mozilla.com>
parents: 291
diff changeset
48 print chosen
290
03d62a2cbe62 update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 289
diff changeset
49 else:
03d62a2cbe62 update demnu to almost a real program
Jeff Hammel <jhammel@mozilla.com>
parents: 289
diff changeset
50 sys.exit(1)
289
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 if __name__ == '__main__':
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53 main()