aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/dmenu_run_history
blob: ba9b2afaf2899fb601c90cc54fccb4bb0be153b9 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/sh

# https://tools.suckless.org/dmenu/scripts/

[ "$1" = "privilege" ] &&
	export cmd_prefix='rofi-askpass ' &&
	export dmenu_args='-nhf #ff7070 -sf #ff7070 -p ⠀:::⠀privilege⠀:::' &&
	shift 1

[ "$1" = "terminal" ] &&
	export cmd_prefix='urxvt -hold -e ' &&
	export dmenu_args='-nhf #9e9e9e -sf #9e9e9e -p ⠀:::⠀terminal⠀:::' &&
	shift 1

cachedir=${XDG_CACHE_HOME:-"$HOME/.cache"}
cache=$cachedir/dmenu_run
historyfile=$XDG_DATA_HOME/dmenu_history
mkdir --parents "$cachedir"

update() {
	IFS=:
	stest -flx $PATH | sort -u >"$cache"
	unset IFS
}

[ ! -f "$cache" ] && update

awk -v histfile="$historyfile" '
	BEGIN {
		while( (getline < histfile) > 0 ) {
			sub("^[0-9]+\t","")
			print
			x[$0]=1
		}
	} !x[$0]++ ' "$cache" \
	| dmenu -i -b ${dmenu_args:--p ⠀:::⠀execute⠀:::} "$@" \
	| awk -v histfile="$historyfile" '
		BEGIN {
			FS=OFS="\t"
			while ( (getline < histfile) > 0 ) {
				count=$1
				sub("^[0-9]+\t","")
				fname=$0
				history[fname]=count
			}
			close(histfile)
		}

		{
			history[$0]++
			print
		}

		END {
			if(!NR) exit
			for (f in history)
				print history[f],f | "sort -t '\t' -k1rn >" histfile
		}
	' | while read -r cmd; do ${SHELL:-"/bin/sh"} -c "$cmd_prefix $cmd" & done

update