-
Notifications
You must be signed in to change notification settings - Fork 1
/
BookMarkIt
executable file
·80 lines (68 loc) · 2.18 KB
/
BookMarkIt
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
#!/usr/bin/env bash
## BookMarkIt
## options
## add [url] [key] Add a new URL to bookmarks with a given key. The "key" will be requested if not
## provided, the URL instead will be taken from the clipboard if not provided.
## find [query] Find a URL from the bookmarks. You can provide an initial query string to limit
## the search
BOOKMARKS="$HOME/Documents/bookmarks.yml"
action=$1
shift
command -v notify-send >/dev/null
if [ $? -eq 0 ]; then
NOTIFY="notify-send --app-name BookMarkIt -i dialog-information"
WARNING="notify-send --app-name BookMarkIt -i dialog-warning"
else
NOTIFY="echo"
WARNING="echo [!]"
fi
add_new_bookmark() {
local url=$1
local key=$2
if [[ -z $url ]]; then
url=`wl-paste`
fi
if [[ ! $url =~ "http" ]]; then
return 1
fi
exists_already=$(yq '.bookmarks[].link' "${BOOKMARKS}" | grep "$url" -c)
if [[ $exists_already -gt 0 ]]; then
$NOTIFY "This URL was already bookmarked"
return 1
fi
if [[ -z $key ]]; then
key=`kdialog --title BookMarkIt --inputbox "Give a name to the URL"`
fi
title=$(wget -q -O - "$url" | tr "\n" " " | sed 's|.*<title>\([^<]*\).*</head>.*|\1|;s|^\s*||;s|\s*$||')
yq --inplace ".bookmarks += [{\"key\": \"${key}\", \"link\": \"${url}\"}]" "${BOOKMARKS}"
$NOTIFY "$key stored in Bookmark"
return 0
}
find_bookmark() {
QUERY="--query=$@"
selection=$(\
yq 'with(.bookmarks[]; .key = .key + " => " + .link)' "${BOOKMARKS}" \
| yq '[ .bookmarks[].key]' \
| column -t -s ' ' \
| sort \
| fzf \
--prompt "Search bookmark > " \
--layout reverse --height=70% $QUERY\
--bind 'ctrl-y:execute(readlink -f {} | echo {} | cut -d">" -f2 | tr -d " " | tr -d "\n" | xclip -selection clipboard)+abort'\
)
if [[ -z ${selection} ]]; then
exit 0
fi
link=$(echo "${selection}" | awk '{print $4}')
xdg-open ${link} 2>&1 > /dev/null
}
if [[ $action == "add" ]]; then
add_new_bookmark $2 $3
exit $?
fi
if [ $action == "find" ]; then
find_bookmark $2
exit $?
fi
echo "[!] unsupported action \"$action\""
exit 1