aboutsummaryrefslogtreecommitdiff
path: root/misc/.local/bin/dmenu_omnibar
blob: 775a4575890e550791dc7093f3a6fa65a5474c3d (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/bin/bash

NC='\033[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'

BROWSER=`which firefox-bin`
BOOKMARKS=$HOME/.local/share/misc/bookmarks
HISTORY=$HOME/.cache/omnibar_hist.txt
ENGINE="https://search.dyamon.me/search?q=%s"

#URL_PATTERN='^(https?://)?([A-Za-z0-9-]+\.)+[A-Za-z0-9-]+(:[0-9]+)?(/[A-Za-z0-9%#.=-]*)*(\?([A-Za-z0-9-]+(=[A-Za-z0-9-]+)?&?)+)?$'
URL_PATTERN='^(https?://)?([A-Za-z0-9-]+\.)+[A-Za-z0-9-]+(:[0-9]+)?(/.*)?'

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 "COMMAND HISTORY"
    echo "    Rudimental support for command history is provided. Up to"
    echo "    10 unique entries will appear at the top of the dmenu"
    echo "    selection list."
    echo 
    echo "    The following internal commands are also supported:"
    echo "      !clear    clears the command history"
    echo 
    echo "    Prepending your dmenu entry with one or more whitespaces"
    echo "    (space or tab) disables saving the entry to history."
    echo
    echo "BOOKMARK 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 these tables
#
#   https://docs.microfocus.com/OMi/10.62/Content/OMi/ExtGuide/ExtApps/URL_encoding.htm
#   https://en.wikipedia.org/wiki/Percent-encoding#Reserved_characters
#
# 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'
}

# Manage history
save_to_history() {
    # File might not exist
    touch "$HISTORY"
    # Add new entry at the top
    echo "🔍 $1" | cat - "$HISTORY" > "$HISTORY.tmp"
    # Remove duplicates and truncate file to 10 entries
    uniq "$HISTORY.tmp" | sed 11q > "$HISTORY"
}

clear_history() {
    shred -u "$HISTORY" "$HISTORY.tmp"
}

omnisearch() {
    [[ "$1" =~ ^[[:blank:]]+ ]] && anon=true || anon=false
    # Clean input
    input=$(echo -n "$1" | sed 's/^\s*//' | sed 's/^🔍\s*//' | sed 's/📋 \(primary\|clipboard\)\s*//')
    # Process input
    if [[ "$input" == "!clear" ]]
    then
        clear_history
    elif grep -qs "^$input$" "$BOOKMARKS"
    then
        # Bookmark
        echo "$input" | awk '{ print $(NF) }'
    elif [[ "$input" =~ $URL_PATTERN ]]
    then
        # URL
        echo "$input"
        $anon || save_to_history "$input" 
    elif [[ "$input" =~ :[a-z]+\ .* ]]
    then
        # Omnisearch command
        CMD=$(echo "$input" | cut -s -d' ' -f1 -)
        SEARCH=$(echo "$input" | cut -s --complement -d' ' -f1 - | urlencode)
        # NOTE: the space after `$CMD` in the following `grep` is
        # important to avoid multiple matches when having one command
        # being a leading substring of another.
        URL=$(grep "$CMD " "$BOOKMARKS" | awk '{ print $(NF) }' | sed "s/%s/$SEARCH/g")
        echo "$URL"
        $anon || save_to_history "$input" 
    else
        # Default search query
        SEARCH=$(echo "$input" | urlencode)
        echo "$ENGINE" | sed "s/%s/$SEARCH/g"
        $anon || save_to_history "$input" 
    fi
}

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

OPTIONS=""

# Primary selection
PRIMARY=$(xclip -out -selection primary 2>/dev/null | tr -d '\n')
[ -n "$PRIMARY" ] && \
    [[ "$PRIMARY" =~ $URL_PATTERN ]] && \
    OPTIONS="📋 primary               $PRIMARY\\n"

# Clipboard selection
CLIPBOARD=$(xclip -out -selection clipboard 2>/dev/null | tr -d '\n')
[ -n "$CLIPBOARD" ] && \
    [[ "$CLIPBOARD" =~ $URL_PATTERN ]] && \
    OPTIONS="${OPTIONS}📋 clipboard             $CLIPBOARD\\n"

OPTIONS=$(cat "$HISTORY" ; echo -ne "$OPTIONS" ; grep -v ^: "$BOOKMARKS")

URL=$(echo "$OPTIONS"  | dmenu -l 18 -h 23)
[ -z "$URL" ] && exit 0 || OMNIURL=$(omnisearch "$URL")

# Starts browser in background
[ -z "$OMNIURL" ] || "$BROWSER" "$OMNIURL" &