dmenu

Mahdi's build of dmenu
git clone git://mahdi.pw/dmenu.git
Log | Files | Refs | README | LICENSE

dmenu_run_history (936B)


      1 #!/bin/sh
      2 
      3 cachedir=${XDG_CACHE_HOME:-"$HOME/.cache"}
      4 if [ -d "$cachedir" ]; then
      5 	cache=$cachedir/dmenu_run
      6 	historyfile=$cachedir/dmenu_history
      7 else			# if no xdg dir, fall back to dotfiles in ~
      8 	cache=$HOME/.dmenu_cache
      9 	historyfile=$HOME/.dmenu_history
     10 fi
     11 
     12 IFS=:
     13 if stest -dqr -n "$cache" $PATH; then
     14 	stest -flx $PATH | sort -u > "$cache"
     15 fi
     16 unset IFS
     17 
     18 awk -v histfile=$historyfile '
     19 	BEGIN {
     20 		while( (getline < histfile) > 0 ) {
     21 			sub("^[0-9]+\t","")
     22 			print
     23 			x[$0]=1
     24 		}
     25 	} !x[$0]++ ' "$cache" \
     26 	| dmenu -i "$@" \
     27 	| awk -v histfile=$historyfile '
     28 		BEGIN {
     29 			FS=OFS="\t"
     30 			while ( (getline < histfile) > 0 ) {
     31 				count=$1
     32 				sub("^[0-9]+\t","")
     33 				fname=$0
     34 				history[fname]=count
     35 			}
     36 			close(histfile)
     37 		}
     38 
     39 		{
     40 			history[$0]++
     41 			print
     42 		}
     43 
     44 		END {
     45 			if(!NR) exit
     46 			for (f in history)
     47 				print history[f],f | "sort -t '\t' -k1rn >" histfile
     48 		}
     49 	' \
     50 	| while read cmd; do ${SHELL:-"/bin/sh"} -c "$cmd" & done