#!/bin/bash # Allows extended glob syntax in case statements shopt -s extglob NC='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[0;33m' msg_info() { echo -e "${GREEN}[INFO] $1${NC}" } msg_warn() { echo -e "${YELLOW}[WARN] $1${NC}" } msg_error() { echo -e "${RED}[ERROR] $1${NC}" } print_help() { echo echo "dmenu_omnibar - a poor man's omnibar and bookmark manager (using dmenu)" echo echo "USAGE:" echo " dmenu_omnibar -h|--help" echo " dmenu_omnibar [-b|--browser ] [-B|--bookmarks ] [-s|--search-engine ]" echo echo "where:" echo " -h | --help:" echo " prints this help message." echo " -b | --browser " echo " path to the executable used to open the selected link." echo " -B | --bookmarks " echo " path to a file containing a list of bookmarks and" echo " omnibar commands. See FORMAT below." echo " -s | --search-engine " echo " default URL used when the input does not match any other" echo " bookmark, URL or omnibar command. It should contain the" echo " placeholder \"%s\" to be substituted with the query" echo " provided by dmenu." echo echo "DMENU" echo " dmenu will prompt for a bookmark from the provided list." echo " If a match is found, the URL is selected. If a match is" echo " not found the input is interpreted as follows:" echo " 1) as an URL if the provided input is an URL;" echo " 2) as a command if the input starts with \":\". Anything" echo " following the command is interpreted as a query;" echo " 3) as a query to be submitted to the default search engine." echo echo " Note: sometimes an input partially matches an entry in the" echo " bookmarks when you are aiming to one of the other behaviours" echo " (e.g., opening an URL directly). You can submit only the" echo " input text (without autoselection from the list of bookmarks)" echo " with Shift-Enter." echo echo "FORMAT" echo " The bookmarks file is assumed to have the following syntax." echo echo " Lines containing a simple bookmark start with a sequence of" echo " keywords, separated by one or more spaces, followed by the" echo " URL of the bookmark." echo echo " Lines containing omnibar commands start with a command" echo " with a leading colon (e.g., \":s\", \":yt\") followed by" echo " the URL containing the placeholder \"%s\" that will be" echo " substituted with the provided query." echo } # Simple URL encoder # The conversions are performed according to this table # # https://docs.microfocus.com/OMi/10.62/Content/OMi/ExtGuide/ExtApps/URL_encoding.htm # # Consider using something like this: # # https://gist.github.com/cdown/1163649 # # for a more reliable approach. urlencode() { sed 's|+|%2b|g' | sed 's|/|%2f|g' } omnisearch() { if grep -qs "^$1$" "$BOOKMARKS" then # Bookmark echo "$1" | awk '{ print $(NF) }' elif [[ "$1" =~ (https?://)?[A-Za-z0-9]+(\.[A-Za-z0-9]+)*\.[A-Za-z0-9]+(:[0-9]+)? ]] then # URL echo "$1" elif [[ "$1" =~ :[a-z]+\ .* ]] then # Omnisearch command CMD=$(echo "$1" | cut -s -d' ' -f1 -) SEARCH=$(echo "$1" | cut -s --complement -d' ' -f1 - | urlencode) URL=$(grep "$CMD" "$BOOKMARKS" | awk '{ print $(NF) }' | sed "s/%s/$SEARCH/g") echo "$URL" else # Default search query SEARCH=$(echo "$1" | urlencode) echo "$ENGINE" | sed "s/%s/$SEARCH/g" fi } BROWSER=`which firefox-bin` BOOKMARKS=$HOME/.local/share/misc/bookmarks ENGINE="https://search.dyamon.me/search?q=%s" while [[ $# -gt 0 ]]; do arg="$1" case $arg in -h|--help) print_help exit 0 ;; -b|--browser) if [[ $# -ge 2 ]]; then BROWSER=$2 shift ; shift else msg_error "No value found for flag $arg!" print_help exit 2 fi ;; -B|--bookmarks) if [[ $# -ge 2 ]]; then BOOKMARKS=$2 shift ; shift else msg_error "No value found for flag $arg!" print_help exit 3 fi ;; -s|--search-engine) if [[ $# -ge 2 ]]; then ENGINE="$2" shift ; shift else msg_error "No value found for flag $arg!" print_help exit 4 fi ;; *) msg_error "$arg is not a valid flag!" print_help exit 1 ;; esac done URL=$(grep -v ^: "$BOOKMARKS" | dmenu -l 10 -h 23) [ -z "$URL" ] && exit 0 OMNIURL=$(omnisearch "$URL") # Starts browser in background "$BROWSER" "$OMNIURL" &