Skip to content

Instantly share code, notes, and snippets.

@kousu
Created February 29, 2016 02:50
Show Gist options
  • Save kousu/4fe951bab45811095c42 to your computer and use it in GitHub Desktop.
Save kousu/4fe951bab45811095c42 to your computer and use it in GitHub Desktop.
#!/bin/sh
# this wraps an arbitrary program in dmenu
# usage: dmenu_do "<dmenu arguments>" -- "<command>"
# dmenu will run with no options, and if the user gives input, <command> will be run with input as trailing arguments
# #
# Example: dmenu_do "-p \"Web Search:\"" -- "sr duckduckgo -j"
# becomes sr duckduckgo -j `dmenu -p "Web Search:"`, except it only runs if dmenu succeeds
#
# This is not a perfect wrapper:
# ideally the API would be: dmenu_do [dmenu args] -- command [args]
# But that involves learning how to use getopt with bash
# For now, you will just have to make liberal use of quoting.
# TIP: if you're running this from *within* .i3/config, you will need many layers of extra quotes, because it sucks
DMENU_ARGS="$1"; shift
DASH="$1"; shift
COMMAND="$1"; shift
if [ "$DASH" != "--" -o ! -z "$1" ]; then
echo "Malformed arguments" >&2
zenity --error --text "Malformed arguments: dmenu=$DMENU_ARGS; dash=$DASH; command=$COMMAND;"
exit 1
fi
#DEBUG
#zenity --info --text "dmenu $DMENU_ARGS" #DEBUG
#zenity --info --text "$COMMAND"
if q=$(eval dmenu $DMENU_ARGS </dev/null); then
# quoting rules are subtle here!
# and errors won't get reported to the user bleh
#echo $COMMAND "$q"
$COMMAND "$q"
fi
# do-ing
bindsym $mod+H exec dmenu_do "-p Hashapass:" -- "hashapass"
#these excessive quotes are because dmenu_do doesn't use getopt, but it should
# XXX sr needs a patch to behave itself here!!!
# quote_ifs () {
#+ echo "$1"
#- if [ -z "$1" ]; then return; fi
#- perl -e '$ifs=$ENV{IFS} || " "; $arg=shift;if($arg =~/[$ifs]/) { $arg="\"$arg\""; } print "$arg\n"; ' -- "$1"
# }
# otherwise it fucking quotes your searches, which means "find this entire string" to Google/DuckDuckGo, but only when you have multiple words
# but if we *don't* so patch then embedded quotes and other syntax characters will run into bash and crash the thing in unpredictable ways.
#XXX is there an elvi that combines W and S, the way the omnibar works in Chrome?
bindsym $mod+W exec dmenu_do "-p \\\\"Web:\\\\"" -- "sr W"
bindsym $mod+S exec dmenu_do "-p \\\\"Web Search:\\\\"" -- "sr S"
bindsym $mod+T exec dmenu_do "-p \\\\"Translate:\\\\"" -- "sr translate"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment