# HG changeset patch # User Jeff Hammel # Date 1368221129 25200 # Node ID e1a861bbb5593d1dff5e76d9a6690db7708570d4 # Parent 498210b6e1a90f76d9ad4249ac0ca23bf36b11a7 sample file picker diff -r 498210b6e1a9 -r e1a861bbb559 python/dmenu.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/dmenu.py Fri May 10 14:25:29 2013 -0700 @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +import optparse +import os +import subprocess +import sys + +def choose_file(directory, dmenu='dmenu'): + """choose a file in the directory with dmenu""" + directory = os.path.abspath(directory) + files = os.listdir(directory) + string = '\n'.join(files) + + + process = subprocess.Popen([dmenu, '-i'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) + stdout, _ = process.communicate(input=string) + if process.returncode: + return + chosen = os.path.join(directory, stdout) + if os.path.isdir(chosen): + return choose_file(chosen) + return chosen + +def main(args=sys.argv[1:]): + parser = optparse.OptionParser() + print choose_file(os.getcwd()) + +if __name__ == '__main__': + main()