Skip to content

Instantly share code, notes, and snippets.

@marendowski
Last active October 30, 2023 18:27
Show Gist options
  • Save marendowski/6dcd54bf8ae4fac0b3bb52fa2246daba to your computer and use it in GitHub Desktop.
Save marendowski/6dcd54bf8ae4fac0b3bb52fa2246daba to your computer and use it in GitHub Desktop.
Run dmenu with history of launched programs
#!/bin/sh
# default history path = ~/.cache/
cachedir=${XDG_CACHE_HOME:-"$HOME/.cache"}
histsize=50 # size of the history file
cache=$cachedir/dmenu
hist=$cachedir/dmenu_history
# if there isn't a history file - create it
if [ ! -e "$hist" ]; then
touch "$hist"
echo foo
fi
# this basically gets a history file and goes through it line-by-line and deleting duplicates (because piping history file and list of all executables creates duplicates)
cmd=$(
awk -v histfile=$hist '
BEGIN {
while( (getline < histfile) > 0 ) {
sub("^[0-9]+\t","")
print
x[$0]=1
}
} !x[$0]++ ' "$cache" \
| dmenu "$@")
case $cmd in
* ) ${cmd};
# add cmd to history (in the first line)
sed -i -e "/^${cmd}$/d;${histsize}q" "$hist";
sed -i "1s/^/${cmd}\n/" "$hist" ;;
esac
# delete empty lines if user aborts choosing
sed -i '/^\s*$/d' "$hist"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment