From c7a2de5da00e0e5cd7452187118a196fcc010e20 Mon Sep 17 00:00:00 2001 From: Alexander Bocken Date: Mon, 20 Apr 2020 00:19:00 +0200 Subject: [PATCH] history in dmenu, execute commands in new terminal windows with ! --- dmenu_run | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100755 dmenu_run diff --git a/dmenu_run b/dmenu_run new file mode 100755 index 0000000..dcaa8dd --- /dev/null +++ b/dmenu_run @@ -0,0 +1,58 @@ +#!/bin/sh +aliases=$HOME/.config/aliasrc + +source $aliases +termcmd="st -e" +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 + +cmd=$(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 + } + ' ) + +case $cmd in + *\! ) ${termcmd} "$(printf "%s" "${cmd}" | cut -d'!' -f1)";; + * ) ${cmd} ;; +esac +exit