summaryrefslogtreecommitdiff
path: root/scripts/dmenu-filepicker
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/dmenu-filepicker')
-rwxr-xr-xscripts/dmenu-filepicker47
1 files changed, 47 insertions, 0 deletions
diff --git a/scripts/dmenu-filepicker b/scripts/dmenu-filepicker
new file mode 100755
index 0000000..288cac9
--- /dev/null
+++ b/scripts/dmenu-filepicker
@@ -0,0 +1,47 @@
1#!/bin/sh
2
3# A dmenu-based file picker (prints selected file to stdout)
4# Requires: dmenu (or similar)
5
6# Usage: dmenu-filepicker [-m menu] [path]
7
8menu="dmenu -i -l 15"
9
10usage() {
11 echo "Usage: dmenu-filepicker [-m MENU] [PATH]"
12}
13
14while getopts "m:" opt; do
15 case "$opt" in
16 m)
17 menu="$OPTARG"
18 ;;
19 *)
20 usage
21 exit 1
22 ;;
23 esac
24done
25shift $((OPTIND - 1))
26
27fullpath=$(realpath "${@:-"$(pwd)"}")
28
29while true; do
30 if [ "$sel" = "." ]; then
31 echo "$fullpath" | sed 's|/\.||'
32 break
33 elif [ -d "$fullpath" ] || [ -z "$fullpath" ]; then
34 if [ -z "$fullpath" ]; then fp="/"; else fp="$fullpath"; fi
35 sel=$(ls -a "$fp" | sed 's|.*/||' | $menu)
36 if [ "$sel" = ".." ]; then
37 fullpath=$(echo "$fullpath" | sed "s|/[^/]*$||")
38 else
39 fullpath=$(echo "$sel" | sed "s|^|$fullpath/|")
40 fi
41 else
42 echo "$fullpath"
43 break
44 fi
45
46 [ -z "$sel" ] && break
47done

Generated with cgit - Back to sebastiano.tronto.net