summaryrefslogtreecommitdiff
path: root/dmenu/dmenu_run
blob: c22468bd46a3cd1a86b77c9d07c307d14ed67041 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/sh
# end a command with ; to run in a terminal

term="${TERMINAL:-st} -e"
cachedir=${XDG_CACHE_HOME:-"$HOME/.cache"}
cache="$cachedir/dmenu_recent"

touch "$cache"

# cleaning
sort -u "$cache" | while read cmd; do
  command -v "${cmd%;}" >/dev/null 2>&1 || sed -i "/$cmd/d" "$cache"
done

most_used=$(sort "$cache" | uniq -c | sort -rh | sed 's/\s*//' | cut -d' ' -f2-)
run=$( (
  echo "$most_used"
  dmenu_path | grep -vxF "$most_used"
) | dmenu -i "$@")

[ -z "$run" ] && exit 1

(
  echo "$run"
  head -n 99 "$cache"
) >"$cache.$$"
mv "$cache.$$" "$cache"

case "$run" in
*\;) exec $(echo $term ${run%;}) ;;
*) exec "$run" ;;
esac