aboutsummaryrefslogtreecommitdiff
path: root/dmenu-filepicker
blob: e2642b2c2e2b7a51335b50e027127ba330fcd58a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/bin/sh

# A dmenu-based file picker (prints selected file to stdout)
# Requires: dmenu (or similar)
# Usage: dmenu-filepicker [-m menu] [path]

menu="dmenu -i -l 15"

while getopts "m:" opt; do
	case "$opt" in
		m)
			menu="$OPTARG"
			;;
	esac
done
shift `expr $OPTIND - 1`

fullpath=$(realpath ${@:-"$(pwd)"})

while true; do
	if [ "$sel" = "." ]; then
		echo "$fullpath" | sed 's|/\.||'
		break
	elif [ -d "$fullpath" ] || [ -z "$fullpath" ]; then
		if [ -z "$fullpath" ]; then fp="/"; else fp="$fullpath"; fi
		sel=$(ls -a "$fp" | sed 's|.*/||' | $menu)
		if [ "$sel" = ".." ]; then
			fullpath=$(echo "$fullpath" | sed "s|/[^/]*$||")
		else
			fullpath=$(echo "$sel" | sed "s|^|$fullpath/|")
		fi
	else
		echo "$fullpath"
		break
	fi

	[ -z "$sel" ] && break
done

Generated with cgit - Back to sebastiano.tronto.net