-
Notifications
You must be signed in to change notification settings - Fork 0
/
buku
executable file
·147 lines (125 loc) · 2.91 KB
/
buku
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
#!/usr/bin/env bash
VERSION="v0.2"
actions=('add' 'open' 'edit' 'del' 'version')
buku_cmd="buku --nostdin --tacit --np --nc"
url_regexp='^(http(s)?:\/\/)?(www.)?([a-zA-Z0-9])+([\-\.]{1}[a-zA-Z0-9]+)*\.[a-zA-Z]{2,5}(:[0-9]{1,5})?(\/[^\s]*)?$'
declare -A flags=(
["add"]="--add"
["open"]="--open"
["edit"]="--update"
["del"]="--delete"
)
log() {
echo "message-info '$1'" >>"$QUTE_FIFO"
}
error_log() {
echo "message-error '$1'" >>"$QUTE_FIFO"
exit
}
run_buku() {
# shellcheck disable=SC2068
if $buku_cmd "${flags["$1"]}" ${@:2}; then
log "Success"
else
error_log "Something wrong happened"
fi
exit
}
search() {
open_count="$1"
search_flag="$2"
query="${3//\*/\\*}"
# shellcheck disable=SC2068
result_str=$($buku_cmd --format 1 "$search_flag" ${query[@]} | grep -Eo '^[0-9]+' | head -n "$open_count")
echo "${result_str//\\n/ }"
}
parse_query() {
query=""
while [[ "$1" =~ [^-][[:alpha:]]* ]]; do
query+="$1 "
shift
done
echo "$query"
}
# shellcheck disable=SC2076
if [[ " ${actions[*]} " =~ " $1 " ]]; then
action="$1"
shift
else
action="add"
fi
# Argument Parsing
while : ; do
case "$1" in
-n*)
if [[ "$1" =~ -n[[:digit:]]+ ]]; then
limit=$( echo "$1" | grep -Eo '[[:digit:]]+' )
shift
elif [[ "$1" == "-n" ]] && [[ "$2" =~ [[:digit:]]+ ]]; then
limit="$2"
shift 2
else
error_log "The -n flag only accepts a number"
fi
;;
-s)
search=true
search_type="--sany"
search_query=$(parse_query "$@")
shift "$#"
;;
-S)
search=true
search_type="--sall"
search_query=$(parse_query "$@")
shift "$#"
;;
*)
break
;;
esac
done
case $action in
add)
if echo "$1" | grep -qP "$url_regexp"; then
run_buku add "$@"
fi
run_buku add "$QUTE_URL" "$@"
;;
open)
if [[ -z "$1" ]] && [[ ! $search ]]; then
error_log "At least 1 argument required: index | query | tag"
fi
if [[ -n "$1" ]]; then
if [[ "$1" =~ ^[[:digit:]]+$ ]] || [[ "$1" =~ ^[[:digit:]]+-[[:digit:]]+$ ]]; then
index="$1"
else
search=true
search_type="--sany"
search_query=$(parse_query "$@")
shift "$#"
fi
shift
fi
if [[ ! $search ]]; then
log "OK"
run_buku open "$index"
fi
result=$(search "${limit:-1}" "$search_type" "$search_query")
run_buku open "${result[@]}"
;;
edit)
if [[ -z "$1" ]]; then
error_log "argument required: tag"
fi
run_buku edit "$@"
;;
del)
if [[ -z "$1" ]]; then
error_log "argument required: tag | tag range"
fi
run_buku del "$@"
;;
version)
log "Buku Version: v$($buku_cmd -v), userscript version: ${VERSION}"
esac