comparison 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
comparison
equal deleted inserted replaced
289:e1a861bbb559 290:03d62a2cbe62
3 import optparse 3 import optparse
4 import os 4 import os
5 import subprocess 5 import subprocess
6 import sys 6 import sys
7 7
8 def choose_file(directory, dmenu='dmenu'): 8 def choose_file(directory, dmenu='dmenu',
9 args=('-i', '-nb', 'black', '-nf', 'white')):
9 """choose a file in the directory with dmenu""" 10 """choose a file in the directory with dmenu"""
10 directory = os.path.abspath(directory) 11 directory = os.path.abspath(directory)
11 files = os.listdir(directory) 12 files = os.listdir(directory)
12 string = '\n'.join(files) 13 string = '\n'.join(files)
13 14
15 if isinstance(dmenu, basestring):
16 dmenu = [dmenu]
17 dmenu = list(dmenu)
18 dmenu.extend(args)
14 19
15 process = subprocess.Popen([dmenu, '-i'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) 20 process = subprocess.Popen(dmenu, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
16 stdout, _ = process.communicate(input=string) 21 stdout, _ = process.communicate(input=string)
17 if process.returncode: 22 if process.returncode:
18 return 23 return
19 chosen = os.path.join(directory, stdout) 24 chosen = os.path.join(directory, stdout)
20 if os.path.isdir(chosen): 25 if os.path.isdir(chosen):
21 return choose_file(chosen) 26 return choose_file(chosen)
22 return chosen 27 return chosen
23 28
24 def main(args=sys.argv[1:]): 29 def main(args=sys.argv[1:]):
25 parser = optparse.OptionParser() 30 parser = optparse.OptionParser()
26 print choose_file(os.getcwd()) 31 parser.add_option('-d', '--directory', dest='directory',
32 default=os.getcwd(),
33 help="call on this directory [Default: current directory]")
34 parser.add_option('-e', '--exec', dest='executable',
35 help="call this proram with the result")
36 options, args = parser.parse_args(args)
37 chosen = choose_file(options.directory)
38 if chosen:
39 if options.executable:
40 pass
41 print chosen
42 else:
43 sys.exit(1)
27 44
28 if __name__ == '__main__': 45 if __name__ == '__main__':
29 main() 46 main()