dotfiles/.local/bin/rofi-bang
Marty Oehme 264e095224 Add initial rofi-bangs script
Can be executed with rofi-bang. It will take a comma-separated list of
commands (for now statically sourced from rofi config dir), show labels
and 'bangs' for them (in the manner of !c or !yt) which, if typed will
instantly execute their command. In this manner, it becomes possible to
create a quickly accessible menu using rofi.

E.g. we could have !b to search bookmarks. As soon as !b is typed the
'bang' instance of rofi exits and executes the associated command. If
the command starts up another, visually identical, rofi instance with a
preselection of different user bookmarks, it appears as if we just
jumped into a bookmark list in the same rofi instance.

Of course, the commands can be anything. They don't have to invoke more
rofi instances.
2019-09-04 23:04:07 +02:00

48 lines
1.5 KiB
Bash
Executable file

#!/bin/bash
#
#
# rofi-bang
#
# Allows execution of arbitrary commands through rofi by
# invoking shell scripts. These shell scripts can in turn
# of course be other rofi commands, which allows the creation
# of chained rofi executions (e.g. !c 1+2) to run a
# calculator script within rofi and immediately execute it.
# Hence, rofi-bang.
cwd="$XDG_CONFIG_HOME"/rofi
if [ ! -d "$cwd" ]; then
echo "The necessary directory at $HOME/.config/rofi is not set up correctly."
exit 1
fi
cmdfile="$cwd/rofi-bang-commands.csv"
if [ ! -f "$cmdfile" ]; then
echo "The nessesary file at $cwd/rofi-bang-commands.csv has not been found."
exit 1
fi
# Present the initial selection screen - but as soon as only one candidate is left exit out
# since bangs are unique (!text will not be matched by anything else) it should automatically exit
# whenever a bang has been typed
selection=$(
cut <"$cmdfile" -s -d ',' -f 1,2 |
sed 's/,/: /' |
printf "%s\n%s\n%s" "$(cat -)" "suffix that is not in cmd file" "one last entry" |
rofi -dmenu -i -p "Run> " -theme "Arc-Dark" -auto-select |
sed 's/: /,/' |
head -n 1
)
# we did not select anything, just exit
if [ -z "$selection" ]; then exit 0; fi
# we selected something, check if it is in the bang commands file
# if it is, we should execute the bang
is_bang=$(grep "$selection" "$cmdfile")
if [ -n "$is_bang" ]; then
cmd=$(echo "$is_bang" | cut -s -d ',' -f 4)
echo "$cmd" | xargs --no-run-if-empty -I "{}" /bin/bash -c "{}"
else
echo "not implemented yet, should re-run rofi with the exact same settings"
fi