changeset 289:e1a861bbb559

sample file picker
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 10 May 2013 14:25:29 -0700
parents 498210b6e1a9
children 03d62a2cbe62
files python/dmenu.py
diffstat 1 files changed, 29 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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()