-
Notifications
You must be signed in to change notification settings - Fork 0
/
gm.sh
executable file
·64 lines (51 loc) · 1.37 KB
/
gm.sh
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
#!/usr/bin/env bash
PROG="${0##*/}"
CMD=gm
DEPS=("$CMD" dunstify xclip fzf)
declare -a CMD_ARGS=(--list --oneline --color=always)
declare -a FZF_ARGS=(--ansi --no-preview --tac --layout=default --cycle)
FZF_ARGS+=(--prompt=' Gomarks> ' --print-query --multi)
# result data
URL=""
TITLE=""
# shellcheck source=/dev/null
[[ -f "$HOME/.config/shell/fzf.sh" ]] && source "$HOME/.config/shell/fzf.sh"
function log_err {
printf "%s: %s\n" "$PROG" "$*" >&2
delay
exit 1
}
function delay {
read -p "Press ENTER to continue..." -r _
}
function ddg {
local query="$1"
local url="https://duckduckgo.com/?q=${query// /%20}&t=ffab&atb=v1-1"
setsid -f xdg-open "$url"
exit 0
}
function get_bookmark {
local bookmark retcode id
bookmark=$("$CMD" "${CMD_ARGS[@]}" | fzf "${FZF_ARGS[@]}")
retcode="$?"
if [[ "$retcode" -ne 0 ]] && [[ -z "$bookmark" ]]; then
exit "$retcode"
fi
if [[ "$retcode" -ne 0 ]]; then
ddg "$bookmark"
fi
id=$(echo "$bookmark" | awk '{print $1}')
URL=$("$CMD" "$id" -F url)
TITLE=$("$CMD" "$id" -F title)
}
function main {
for dep in "${DEPS[@]}"; do
if ! command -v "$dep" >/dev/null; then
log_err "dependency '$dep' not found"
fi
done
get_bookmark
setsid -f xdg-open "$URL"
dunstify -r 696 -i "do-not-exists" "⭐ Gomarks" "$TITLE"
}
main "$@"