aboutsummaryrefslogtreecommitdiff
path: root/misc/.local/bin/dmenu_omnibar
blob: 41d0b1f03d021ce0d799038b83e608a90e764809 (plain) (blame)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/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 <path>] [-B|--bookmarks <path>] [-s|--search-engine <url>]"
    echo
    echo "where:"
    echo "    -h | --help:"
    echo "        prints this help message."
    echo "    -b | --browser <path>"
    echo "        path to the executable used to open the selected link."
    echo "    -B | --bookmarks <path>"
    echo "        path to a file containing a list of bookmarks and"
    echo "        omnibar commands. See FORMAT below."
    echo "    -s | --search-engine <url>"
    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" &