Heyhey

On Thu, Dec 03, 2015 at 02:57:52AM -0800, Xarchus wrote:
> [...]
>
> - improved the history/cache parsing/de-duplication awk one-liner in
>   dmenu_path; the former 'NR==FNR' test was not enough: in case of a not
> supplied or empty history file it attempted to remove a count followed by a
> tab from the file names in the cache. Obviously to find such an occurrence
> would be crazy rare, so it was quite harmless (I suppose that you don't
> have any executables in your path of the form "count\tname", do you ?
> Anyway, now you can :) )
> 
> New patch attached.

I have tested it briefly and it works for me. I would remove all the
checks as done in the attached version of the patch.

If you are happy with this version we should put it on the website. The
question is whether we should replace the current version that does not
apply to tip or just add it as an alternate history patch.


Cheers,

Silvan

diff --git a/dmenu_path b/dmenu_path
old mode 100644
new mode 100755
index 338bac4..c928173
--- a/dmenu_path
+++ b/dmenu_path
@@ -1,4 +1,6 @@
 #!/bin/sh
+HISTORY="$1"
+
 cachedir=${XDG_CACHE_HOME:-"$HOME/.cache"}
 if [ -d "$cachedir" ]; then
 	cache=$cachedir/dmenu_run
@@ -7,7 +9,6 @@ else
 fi
 IFS=:
 if stest -dqr -n "$cache" $PATH; then
-	stest -flx $PATH | sort -u | tee "$cache"
-else
-	cat "$cache"
+	stest -flx $PATH | sort -u > "$cache"
 fi
+awk '!cache { sub("^[0-9]+\t","") } !x[$0]++' "$HISTORY" cache=1 "$cache"
diff --git a/dmenu_run b/dmenu_run
index 834ede5..cfb8323 100755
--- a/dmenu_run
+++ b/dmenu_run
@@ -1,2 +1,32 @@
 #!/bin/sh
-dmenu_path | dmenu "$@" | ${SHELL:-"/bin/sh"} &
+
+historyfile=~/.cache/dmenu/history
+
+dmenu_path $historyfile | dmenu "$@" \
+	| awk -v histfile=$historyfile '
+		BEGIN {
+			FS=OFS="\t"
+			if(histfile) {
+				while ( (getline < histfile) > 0 ) {
+					count=$1
+					sub("^[0-9]+\t","")
+					fname=$0
+					history[fname]=count
+				}
+				close(histfile)
+			}
+		}
+
+		{
+			history[$0]++
+			print
+		}
+
+		END {
+			if(!histfile)
+				exit
+			for (f in history)
+				print history[f],f | "sort -t '\t' -k1rn >" histfile
+		}
+	' \
+	| while read cmd; do ${SHELL:-"/bin/sh"} -c "$cmd" & done

Reply via email to