-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBookMarkIt
More file actions
executable file
·118 lines (102 loc) · 3.51 KB
/
BookMarkIt
File metadata and controls
executable file
·118 lines (102 loc) · 3.51 KB
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
#!/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.
## add [command] [key] Add a new bash command git a given key. The "key" will be requested if not provided, the "command" instead will be taken from the clipboard if not provided.
## find [query] Find a URL/command from the bookmarks. You can provide an initial query string to limit
## the search
dependencies=(yad)
if command -v notify-send >/dev/null; then
NOTIFY="notify-send --app-name BookMarkIt -i dialog-information"
else
NOTIFY="echo"
fi
for d in "${dependencies[@]}"; do
if ! command -v "$d" >/dev/null 2>&1; then
$NOTIFY "a dependency is missing: $d. Please install it"
exit 1
fi
done
BOOKMARKS="$HOME/Documents/bookmarks.yml"
action=${1:-'find'}
add_new_bookmark() {
local book
local key
if ! book=$(wl-paste); then
$NOTIFY "could not get bookmark from clipboard"
exit 1
fi
exists_already=$(yq '.bookmarks[].link' "${BOOKMARKS}" | grep "$book" -c)
if [[ $exists_already -gt 0 ]]; then
$NOTIFY "This URL was already bookmarked"
return 1
fi
#if ! key=$(kdialog --title BookMarkIt --inputbox "Give a name to bookmark"); then
#exit 0
#fi
# --- Define Variables ---
DIALOG_TITLE="BookMarkIt"
DIALOG_MAIN_TEXT="Please enter the Bookmark and Key to find it later"
SEPARATOR="\x1F"
if ! INPUT_RAW=$(yad --form \
--title="$DIALOG_TITLE" \
--text="$DIALOG_MAIN_TEXT" \
--width=650 --height=150 \
--separator="$SEPARATOR" \
--field="Key" "$key" \
--field="Bookmark" "$book" \
); then
# allow to exit from the form without complaining
exit 0
fi
# shellcheck disable=SC2016
key=$(echo "$INPUT_RAW" | awk -F"$SEPARATOR" '{print $1}')
# shellcheck disable=SC2016
book=$(echo "$INPUT_RAW" | awk -F"$SEPARATOR" '{print $2}')
if [ -z "$key" ] || [ -z "$book" ]; then
$NOTIFY "failed: key:$key: book:$book:"
exit 1
fi
# replace spaces with underscore for the key
key="${key// /_}"
if out=$(yq --inplace ".bookmarks += [{\"key\": \"${key}\", \"link\": \"${book}\"}]" "${BOOKMARKS}"); then
$NOTIFY "$key stored in Bookmark"
else
$NOTIFY "BookMarkIt failed: error $? $out"
fi
return 0
}
find_bookmark() {
QUERY="--query=$*"
selection=$(\
yq 'with(.bookmarks[]; .key = .key + " => " + .link)' "${BOOKMARKS}" \
| yq '[ .bookmarks[].key]' \
| sort \
| fzf \
--prompt "Search bookmark > " \
--layout reverse --height=70% "$QUERY"\
--bind 'ctrl-y:execute(readlink -f {} | echo {} | cut -d">" -f2 | tr -d "\n" | xclip -selection clipboard)+abort'\
)
if [[ -z ${selection} ]]; then
exit 0
fi
selection=$(echo "${selection}" | awk -F ' => ' '{print $2}')
sanitized_selection=$(printf "%s" "$selection" | xargs)
if [[ "$sanitized_selection" = https://* ]]; then
xdg-open "$selection" > /dev/null 2>&1
else
echo "$selection" | wl-copy
$NOTIFY "added to clipboard: '${selection}'"
fi
}
if [[ $action == "add" ]]; then
shift
add_new_bookmark "$@"
exit $?
fi
if [ "$action" == "find" ]; then
find_bookmark "$2"
exit $?
fi
echo "[!] unsupported action \"$action\""
exit 1