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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
#!/bin/sh
book_conf="${XDG_CONFIG_HOME:-$HOME/.config}/dbook/dbook.conf"
book_data="${XDG_DATA_HOME:-$HOME/.local/share}/dbook"
menu="wmenu"
note()
{
: "${1:?}"
command -v notify-send > /dev/null &&
notify-send " dbook" "$1"
}
die()
{
: "${1:?}"
note "$1"
printf "\033[31;1merr: %b\033[0m\n" "$1" 1>&2
exit "${2:-1}"
}
dep_check()
{
: "${1:?}"
for dep; do
command -v "$dep" 1>/dev/null ||
die "$dep not found, please install it" 127
done
unset dep
}
parse_name()
{
while read -r line
do
line=${line%%|*}
# trim leading and trailing white spaces
line=${line#"${line%%[![:space:]]*}"}
line=${line%"${line##*[![:space:]]}"}
[ -z "${line##\#*}" ] &&
continue
echo "$line"
done < "$book_conf"
}
parse_data()
{
read -r book_name
read_name=
[ -z "$book_name" ] || [ ! -f "$book_conf" ] &&
return 1
while read -r line
do
# trim leading and trailing white spaces
line=${line#"${line%%[![:space:]]*}"}
line=${line%"${line##*[![:space:]]}"}
# skip trailing lines till match
case "$line" in
"$book_name"*)
# make sure read_name fully matches book_name
read_name=${line%%|*}
read_name=${read_name%"${read_name##*[![:space:]]}"}
[ "$book_name" != "$read_name" ] &&
continue
;;
*)
continue
;;
esac
# extract date from string
line="${line##*|}"
line=${line#"${line%%[![:space:]]*}"}
echo "$line"
return 0
done < "$book_conf"
unset read_name
return 1
}
rm_data()
{
# usage rm_data [data_name]
: "${1:?}"
read_name=
data=
cl=
file=
old_ifs="$IFS"
IFS=
while read -r line
do
cl="$line\n"
# trim leading and trailing white spaces
line=${line#"${line%%[![:space:]]*}"}
line=${line%"${line##*[![:space:]]}"}
# catch match
case "$line" in
"$book_name"*)
read_name=${line%%|*}
read_name=${read_name%"${read_name##*[![:space:]]}"}
# make sure read_name fully matches
if [ "$1" = "$read_name" ]
then
# remove saved data
data="$(echo "$read_name" | parse_data)"
[ -e "$data" ] && [ -z "${data##"${book_data}"/*}" ] &&
rm "$data"
continue
fi
;;
esac
file="${file}${cl}"
done < "$book_conf"
IFS="$old_ifs"
# shellcheck disable=SC2059
printf "$file" > "$book_conf"
unset read_name data file cl old_ifs
}
entry()
{
printf "" | "$menu" -p " ${1:?} " ||
die "input empty"
}
sh_realpath()
{
# usage: sh_realpath [path]
: "${1:?}"
if [ -z "${1##/*}" ]
then
echo "$1"
else
echo "${PWD:-$(pwd)}/$1"
fi
}
clip()
{
# usage: clip [data]
: "${1:?}"
if [ -z "$WAYLAND_DISPLAY" ]
then
dep_check "xclip"
echo "$1" | xclip -selection clipboard
else
dep_check "wl-copy"
echo "$1" | wl-copy
fi &&
note "data coppied to clipboard"
}
clip_file()
{
# usage: clip [data]
: "${1:?}"
if [ -z "$WAYLAND_DISPLAY" ]
then
dep_check "xclip"
echo "$1" | xclip -selection clipboard "$1" -t text/uri-list
else
dep_check "wl-copy"
echo "$1" | wl-copy -t text/uri-list
fi &&
note "data coppied to clipboard"
}
main()
{
name=
data=
[ -z "$WAYLAND_DISPLAY" ] &&
menu="dmenu"
[ -d "$book_data" ] ||
mkdir -p "$book_data"
[ -d "$book_conf" ] ||
mkdir -p "${book_conf%/*}"
case "$1" in
-h|--help)
cat <<- EOF
Usage: dbook command
a bookmark manager using dmenu
Commands:
-h show this help cruft
-i [bookmark string] [bookmark name], inset a new entry
-s [bookmark string] [bookmark name], inset a new entry and make a copy
-d [bookmark name], delete an entry
-t [bookmark name], type the data
-c [bookmark name], copy the date to clipboard
EOF
;;
-i)
data="${2:-$(entry name)}"
shift > /dev/null 2>&1
shift > /dev/null 2>&1
name="${*:-$(entry name)}"
echo "$name" | parse_data > /dev/null &&
die "name already in use"
[ -e "$data" ] &&
data="$(sh_realpath "$data")"
printf "%s\t|\t%s\n" "$name" "$data" >> "$book_conf"
;;
-s)
data="${2:-$(entry name)}"
shift > /dev/null 2>&1
shift > /dev/null 2>&1
name="${*:-$(entry name)}"
echo "$name" | parse_data > /dev/null &&
die "name already in use"
[ -d "$data" ] &&
die "$data is a directory, use -i instead"
if [ -e "$data" ]
then
cp "$data" "$book_data"
data="${book_data}/${data##*/}"
fi
printf "%s\t|\t%s\n" "$name" "$data" >> "$book_conf"
;;
-d)
rm_data "${2:-"$(parse_name | "$menu" -p " " -l 25)"}"
;;
-t)
shift > /dev/null 2>&1
data="${*:-"$(parse_name | "$menu" -p " " -l 25 | parse_data)"}"
[ -z "$data" ] &&
die "empty, use -i to add an entry"
if [ -z "$WAYLAND_DISPLAY" ]
then
dep_check "xdotool"
xdotool type --delay 20 "$data"
else
dep_check "wtype"
wtype -d 20 "$data"
fi
;;
-c)
shift > /dev/null 2>&1
data="${*:-"$(parse_name | "$menu" -p " " -l 25 | parse_data)"}"
[ -z "$data" ] &&
die "empty, use -i to add an entry"
case "$(file --brief "$data")" in
*ASCII\ text*)
clip "$(cat "$data")"
;;
*No\ such\ file\ or\ directory\))
clip "$data"
;;
*)
clip_file "$(printf "file://%s" "$data")"
;;
esac
;;
"")
dep_check "xdg-open"
[ -s "$book_conf" ] ||
die "no bookmarks, try dbook -h"
data="$(parse_name | "$menu" -p " " -l 25 | parse_data)"
[ -z "$data" ] &&
die "empty, use -i to add an entry"
xdg-open "$data" 2> /dev/null ||
case "$(file --brief "$data")" in
*ASCII\ text*)
clip "$(cat "$data")"
;;
*)
clip "$data"
;;
esac
;;
*)
die "$1, invalid usage"
esac
}
main "$@"
|