-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdmenu-runner
executable file
·88 lines (70 loc) · 2.61 KB
/
dmenu-runner
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/sh
# POSIX-compliant (bashism-free) shell script to use a dmenu-like program
# to select/run executables found in $PATH, saving and prioritizing previous
# commands. An alternative to the "dmenu_run" command shipped with dmenu and the
# dmenu_run_history script available from the suckless website:
# https://tools.suckless.org/dmenu/scripts/dmenu_run_with_command_history/
# Based on:
# https://gitlab.com/FlyingWombat/my-scripts/blob/a0cb5717777c1587af381004aa8fb048206bee55/sway-launcher
# configuration
# set your preferred dmenu-like program in the environment variable $DMENU_PROGRAM
# examples: "dmenu", "rofi -dmenu", "term-dmenu", or just "fzf" to run in your current terminal
[ -z "$DMENU_PROGRAM" ] && DMENU_PROGRAM='term-dmenu --no-extended --print-query --no-sort'
if [ -z "$RUNNER_HISTFILE" ]; then
cachedir=${XDG_CACHE_HOME:-"$HOME/.cache"}
mkdir -p "$cachedir"
RUNNER_HISTFILE="$cachedir/dmenu_runner_history"
fi
# Get shell command list
# This may include the occasional non-executable file
# shellcheck disable=SC2046 # word-splitting isn't an issue here.
command_list=$(stest -flx $(echo "$PATH" | tr : '\n' | sort -u) | sort -u)
# read existing command history
if [ -f "$RUNNER_HISTFILE" ]; then
command_history=$(cat "$RUNNER_HISTFILE")
else
command_history=""
fi
# search command list
command_str=$(printf "%s\n" "${command_history}" "${command_list}" \
| sed -E 's/^[0-9]+ (.+)$/\1/' \
| $DMENU_PROGRAM \
| tail -n1) || exit 1
if [ "$command_str" = "" ]; then
exit 1
fi
# using \E flag from perl regex
test "${command_str#*\\E}" != "$command_str" && echo "command can't contain '\E'"
test "${command_str#*\\E}" != "$command_str" && exit 1
update_history() {
# get full line from history (with count number)
hist_line=$(echo "$command_history" | grep -Pe "^[0-9]+ \Q$command_str\E$")
if [ "$hist_line" = "" ]; then
hist_count=1
else
# Increment usage count
hist_count=$(echo "$hist_line" | sed -E 's/^([0-9]+) .+$/\1/')
hist_count=$((hist_count + 1))
# delete line, to add updated later
command_history=$(echo "$command_history" \
| grep --invert-match -Pe "^[0-9]+ \Q$command_str\E$")
fi
# update history
update_line="${hist_count} ${command_str}"
printf "%s\n" "${update_line}" "${command_history}" \
| sort --numeric-sort --reverse >"$RUNNER_HISTFILE"
}
update_history &
# execute command
# shellcheck disable=SC2154
if [ -n "$SWAYSOCK" ]; then
swaymsg -t command exec "$command_str" &
else
eval "$command_str" &
# "disown" is a bashism; use it if only if the shell supports job control
if command -v disown >/dev/null; then
# shellcheck disable=SC2039
disown
fi
fi
# vi:ft=sh