On Tue, Dec 8, 2015 at 8:34 PM, Silvan Jegen <[email protected]> wrote:
> 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.


I realized that I am not dealing with the case that the history file
does not exist already. I added a simple check for that (although I
was considering just putting in a comment saying that it has to).

I also missed the opportunity to remove file checks from the awk code.

Find a new patch containing these changes attached.
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..ef7c579 100755
--- a/dmenu_run
+++ b/dmenu_run
@@ -1,2 +1,31 @@
 #!/bin/sh
-dmenu_path | dmenu "$@" | ${SHELL:-"/bin/sh"} &
+
+historyfile=~/.cache/dmenu/history
+if [ ! -e $historyfile ]; then
+   touch $historyfile
+fi
+
+dmenu_path $historyfile | dmenu "$@" \
+	| 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 {
+			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