From 264e095224591254ea0812bb5cb1b94f78f30cc6 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Wed, 4 Sep 2019 23:04:07 +0200 Subject: [PATCH] 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. --- .config/rofi/rofi-bang-commands.csv | 6 ++++ .local/bin/rofi-bang | 48 +++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 .config/rofi/rofi-bang-commands.csv create mode 100755 .local/bin/rofi-bang diff --git a/.config/rofi/rofi-bang-commands.csv b/.config/rofi/rofi-bang-commands.csv new file mode 100644 index 0000000..44aca27 --- /dev/null +++ b/.config/rofi/rofi-bang-commands.csv @@ -0,0 +1,6 @@ +Calculator,!c,source,"rofi -show calc -modi calc -no-show-match -no-sort -theme Arc-Dark" +Clipboard,!y,source,"rofi -modi "clipboard:greenclip print" -show clipboard -run-command '{cmd}' -theme Arc-Dark" +Emojis,!e,source,"rofimoji --rofi-args '-theme sidebar'" +Youtube,!yt,source,"someroficommand" +Wikipedia,!w,source,"someroficommand" +DuckDuckGo,!ddg,source,"someroficommand" diff --git a/.local/bin/rofi-bang b/.local/bin/rofi-bang new file mode 100755 index 0000000..3ccf38f --- /dev/null +++ b/.local/bin/rofi-bang @@ -0,0 +1,48 @@ +#!/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