aboutsummaryrefslogtreecommitdiff
path: root/dmenu-filepicker
blob: 547f2b5d44d8698645ed420fc472b43ec25408e2 (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
#!/bin/sh

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

menu=dmenu
menuopts="-i -l 15"

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 $menuopts)
		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