commit d9a44a5fd379d23969f0e666041f1a5a976f05a2
parent 7fbaf58bebe72a87d9822590ec5434e052bbd343
Author: Xarchus <xarchus@comcast.net>
Date: Tue, 19 Jan 2016 04:25:48 -0800
Alternative to dmenu_run adding command history
- drop-in replacement for dmenu_run, no dmenu patching necessary
- when doing multiselect, runs command immediately upon selection
- added note to the current 'history' patch pointing to this
Diffstat:
4 files changed, 83 insertions(+), 0 deletions(-)
diff --git a/tools.suckless.org/dmenu/patches/history.md b/tools.suckless.org/dmenu/patches/history.md
@@ -22,3 +22,11 @@ Author
------
* Peter John Hartman (wart_) <[http://antiopus.trilidun.org/durandus/](http://antiopus.trilidun.org/durandus/)>
+
+Note
+----
+
+As an alternative to the patch above, the following script is meant to replace
+`dmenu_run` : it handles the command history in a similar way as the patch and
+can be used with an unpatched dmenu 4.6.
+[dmenu_run_history](http://tools.suckless.org/dmenu/scripts/dmenu_run_with_command_history)
diff --git a/tools.suckless.org/dmenu/scripts/dmenu_run_with_command_history/dmenu_run_history b/tools.suckless.org/dmenu/scripts/dmenu_run_with_command_history/dmenu_run_history
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+cachedir=${XDG_CACHE_HOME:-"$HOME/.cache"}
+if [ -d "$cachedir" ]; then
+ cache=$cachedir/dmenu_run
+ historyfile=$cachedir/dmenu_history
+else # if no xdg dir, fall back to dotfiles in ~
+ cache=$HOME/.dmenu_cache
+ historyfile=$HOME/.dmenu_history
+fi
+
+IFS=:
+if stest -dqr -n "$cache" $PATH; then
+ stest -flx $PATH | sort -u > "$cache"
+fi
+unset IFS
+
+awk -v histfile=$historyfile '
+ BEGIN {
+ while( (getline < histfile) > 0 ) {
+ sub("^[0-9]+\t","")
+ print
+ x[$0]=1
+ }
+ } !x[$0]++ ' "$cache" \
+ | 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 {
+ if(!NR) exit
+ for (f in history)
+ print history[f],f | "sort -t '\t' -k1rn >" histfile
+ }
+ ' \
+ | while read cmd; do ${SHELL:-"/bin/sh"} -c "$cmd" & done
diff --git a/tools.suckless.org/dmenu/scripts/dmenu_run_with_command_history/index.md b/tools.suckless.org/dmenu/scripts/dmenu_run_with_command_history/index.md
@@ -0,0 +1,23 @@
+dmenu_run with command history
+==============================
+
+A self-contained alternative to dmenu_run which also handles history.
+
+History is saved in a file in `$XDG_CACHE_HOME`, with fallback to a dot
+file in `$HOME`. Change as necessary.
+
+In addition to the above, dmenu_run_history will launch each entry
+immediately on `Ctrl-Return` (multiselect).
+
+The script can be used with the 4.6 version of dmenu.
+
+Download
+========
+
+[dmenu_run_history](dmenu_run_history) (20151217)
+
+Authors
+=======
+
+* Xarchus
+* Silvan Jegen (initial idea, code contributor, feedback)
diff --git a/tools.suckless.org/dmenu/scripts/index.md b/tools.suckless.org/dmenu/scripts/index.md
@@ -22,3 +22,5 @@ Download
the terminal.
* [browse](https://github.com/clamiax/scripts/blob/master/src/browse):
little files navigator
+* [dmenu_run_history](http://tools.suckless.org/dmenu/scripts/dmenu_run_with_command_history) :
+ dmenu_run alternative with command history