summaryrefslogtreecommitdiff
path: root/scripts/dmenu-filepicker
blob: 288cac9fedaf9837419efe5533323630d53467c1 (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
39
40
41
42
43
44
45
46
47
#!/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"

usage() {
	echo "Usage: dmenu-filepicker [-m MENU] [PATH]"
}

while getopts "m:" opt; do
	case "$opt" in
		m)
			menu="$OPTARG"
			;;
		*)
			usage
			exit 1
			;;
	esac
done
shift $((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