aboutsummaryrefslogtreecommitdiff
path: root/menu_run
blob: 16e4453ec1f0f610b21de6048b574dc05b421e13 (plain) (blame)
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

menu="bemenu"
cache_dir="${XDG_CACHE_HOME:-"$HOME/.cache"}"
cache="$cache_dir/bemenu_run"

uptodate()
{
	IFS=:
	for path  in $PATH
	do
		test "$path" -nt "$cache" &&
			return 1
	done

	return 0
}

get_bin()
{
	IFS=:
	for path in $PATH
	do
		for bin in "$path"/*
		do
			[ -x "$bin" ] &&
				echo "${bin##*/}"
		done
	done
}

main()
{
	[ -d "$cache_dir" ] ||
		mkdir -p "$cache_dir"
	[ -z "$WAYLAND_DISPLAY" ] &&
		menu="dmenu"

	if [ -f "$cache" ] && uptodate
	then
		"$menu" -p ' ' "$@" < "$cache" | ${SHELL:-"/bin/sh"} &
	else
		get_bin | sort -u | tee "$cache" | "$menu" -p ' ' "$@" | ${SHELL:-"/bin/sh"} &
	fi
}

main "$@"