-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalladium
executable file
·519 lines (488 loc) · 18.2 KB
/
palladium
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#!/bin/bash
source ~/Repositories/scripts/essential-functions
loc="$HOME/Athenaeum"
ix="$usdd/athenaeum-index.csv"
bkp="$HOME/Misc/Backups/athenaeum"
logdir="$HOME/.local/logs/palladium"
log="$HOME/.local/logs/palladium/pall-open-book-log.csv"
all_attr=(type title subtitle author series volume edition first_pub year publisher country language trans_p orig_lang transor filename id)
diagnostic_mode=0 # 0 for off, 1 for on
function dup-check {
if [[ "$(xsv search -s "$1" "^$2$" "$3" | xsv select "$1" | sed 1d | wc -l)" -gt 1 ]]
then
return 0
else
return 1
fi
}
function dupper {
# dupper index_file field_already_queried other_field_to_query term fields_to_display_comma_separated order_of_field
# dupper "$ix" title volume XYZ volume,subtitle 1
# e.g. can be run if two books titled XYZ are found (in order to choose by volume).
# $5 is the order in which the chosen variable in $4 is located, so if "subtitle" is to be
# used, $5 would be "2" in the previous example. This is reflected in $best.
dup_file="$1"
dup_field="$2"
alt_field="$3"
dup_term="$4"
view_fields="$5"
alt_field_order="$6"
[[ "$diagnostic_mode" -eq 1 ]] && {
echolor purple ":: dupper values:"
echolor purple "\t:: dup_file ““$dup_file”” "
echolor purple "\t:: dup_field ““$dup_field””"
echolor purple "\t:: alt_field ““$alt_field””"
echolor purple "\t:: dup_term ““$dup_term””"
echolor purple "\t:: view_fields ““$view_fields””"
echolor purple "\t:: alt_field_order ““$alt_field_order””"
}
[[ -z "$view_fields" ]] && view_fields="$alt_field"
[[ -z "$alt_field_order" ]] && alt_field_order=1
dup_tmp="/tmp/pall-dupper-$(random-string).csv"
xsv search -s "$dup_field" "^$dup_term$" "$dup_file" > "$dup_tmp"
}
function dup-filter {
cat "$dup_tmp" \
| xsv select "$view_fields" \
| sed 1d \
| sort -V \
| uniq \
| fzf \
| ifne xsv select "$alt_field_order"
}
function redupper {
[[ "$diagnostic_mode" -eq 1 ]] && echolor purple ":: redupper is called."
[[ ! -e "$dup_tmp" ]] && return 1
[[ "$(cat "$dup_tmp" | wc -l)" -eq 2 ]] && return 0
best-algo "$alt_field" "$dup_out" "$dup_tmp"
dupper "$dup_tmp" "$alt_field" "$chosen_best" "$dup_out" "$chosen_best_config" "$chosen_order_of_field"
[[ "$(cat "$dup_tmp" | wc -l)" -eq 2 ]] && return 0
dup_out=$(dup-filter)
[[ -z "$dup_out" ]] && return 1
redupper
}
function best-algo {
# best methods to deal with duplicates
best=(author author 1 volume series,volume,subtitle 2 language language,trans_p 1 edition edition,year 1 publisher publisher 1 country country,language 1 transor transor,language 1 filename filename 1)
attr_diff=()
for j in $(xsv headers -j "$ix")
do
if [[ "$(xsv search -s "$1" "^$2$" "$3" | xsv select "$j" | sed 1d | sort | uniq | sed '/^""$/d' | wc -l)" -gt 1 ]]
then
attr_diff+=("$j")
fi
done
[[ -z "$attr_diff" ]] && return 1
i=-1
for j in ${best[@]}
do
((i++))
[[ $((i%3)) -eq 0 ]] && {
if $(echo "${attr_diff[@]}" | grep -q "$j")
then
chosen_best=${best[$i]}
chosen_best_config=${best[((i+1))]}
chosen_order_of_field=${best[((i+2))]}
[[ "$diagnostic_mode" -eq 1 ]] && {
echolor purple ":: best-algo values:"
echolor purple "\t:: chosen_best ““$chosen_best””"
echolor purple "\t:: chosen_best_config ““$chosen_best_config””"
echolor purple "\t:: chosen_order_of_field ““$chosen_order_of_field””"
}
break
fi
continue
}
done
}
function choose-book {
[[ ! -z "$sld_fnm" ]] && return 0
[[ -z "$sld_ttl" ]] && sld_ttl="$(xsv select title "$ix" | tr -d '"' | sed 1d | sort | uniq | fzf)"
[[ -z "$sld_ttl" ]] && return 1
[[ "$diagnostic_mode" -eq 1 ]] && {
echolor purple ":: The following command is occurring inside dup-check:"
echolor white "xsv search -s title \"${sld_ttl/\?/\\?}\" \"$ix\" | xsv select title | sed 1d | wc -l\n"
dup-check title "${sld_ttl/\?/\\?}" "$ix" && echolor purple ":: dup-check returned ““0”” (dup found)" || echolor purple ":: dup-check returned ““1”” (no dup)"
}
if $(dup-check title "${sld_ttl/\?/\\?}" "$ix")
then
[[ "$diagnostic_mode" -eq 1 ]] && echolor purple ":: choose-book passing ““${sld_ttl/\?/\\?}”” into best-algo"
best-algo title "${sld_ttl/\?/\\?}" "$ix"
if [[ -z "$chosen_best" ]] \
|| [[ -z "$sld_ttl" ]] \
|| [[ -z "$chosen_best_config" ]] \
|| [[ -z "$chosen_order_of_field" ]]
then
echolor red ":: ERROR: best-algo failed to return a result."
echolor red ":: chosen_best is ““$chosen_best””"
echolor red ":: sld_ttl is ““$sld_ttl””"
echolor red ":: chosen_best_config is ““$chosen_best_config””"
echolor red ":: chosen_order_of_field is ““$chosen_order_of_field””"
fi
dupper "$ix" title "$chosen_best" "${sld_ttl/\?/\\?}" "$chosen_best_config" "$chosen_order_of_field"
dup_out=$(dup-filter)
[[ -z "$dup_out" ]] && return 1
redupper || return 1
sld_fnm="$loc/$(xsv search -s title "^${sld_ttl/\?/\\?}$" "$ix" | xsv search -s "$chosen_best" "^$dup_out" | xsv select filename | sed -n 2p)"
else
sld_fnm="$loc/$(xsv search -s title "^${sld_ttl/\?/\\?}$" "$ix" | xsv select filename | sed -n 2p)"
fi
sld_id="$(switch-param filename id "$(echo "$sld_fnm" | awk -F '/' '{print $NF}')")"
}
function backup-index {
cmp -s "$ix" "$bkp/$(eza -1f "$bkp" | tail -n 1)" || {
show-stats
echolor yellow ":: New entries detected. Backing up index..."
cp -- "$ix" "$bkp"/athenaeum-index-$(date-string).csv
}
}
function search-by {
function search-by-default {
filterer="$(xsv select "$1" "$ix" | sed 1d | sed '/""/d' | tr -d '"' | sort | uniq | fzf)"
[[ -z "$filterer" ]] && return 1
sld_ttl="$(xsv select title,"$1" "$ix" | xsv search -s "$1" "^$filterer$" | xsv select title | tr -d '"' | sed 1d | sort | uniq | fzf)"
[[ -z "$sld_ttl" ]] && return 1
export sld_ttl
open-book
}
function search-by-series {
filterer="$(xsv select series "$ix" | sed 1d | sed '/""/d' | tr -d '"' | sort | uniq | fzf)"
[[ -z "$filterer" ]] && return 1
dupper "$ix" series volume "$filterer" "series,volume,author,title,subtitle" 2
dup_out="$(dup-filter)"
[[ -z "$dup_out" ]] && return 1
redupper || return 1
sld_fnm="$loc/$(xsv search -s series "^$filterer$" "$ix" | xsv search -s volume "^$dup_out" | xsv select filename | sed -n 2p)"
[[ -z "$sld_fnm" ]] && return 1
export sld_fnm
open-book
}
case "$(echo -e "Author\nCountry\nLanguage\nPublisher\nSeries\nType\nYear" | fzf)" in
"Author") search-by-default author ;;
"Country") search-by-default country ;;
"Language") search-by-default language ;;
"Publisher") search-by-default publisher ;;
"Type") search-by-default type ;;
"Year") search-by-default first_pub ;;
"Series") search-by-series ;;
*) echolor red ":: No search parameter chosen."
return
;;
esac
}
function dup-check-in-index {
# check for duplicated lines
cat "$ix" | sort | uniq -c | grep -qv "^ 1" && {
IFS=$'\n'
for j in $(cat "$ix" | sort | uniq -c | grep -v "^ 1" | xsv select 2)
do
echolor red ":: Duplication detected in index file: ““$j””"
done
unset IFS
exit
}
}
function add-entry {
[[ -z "$1" ]] && {
echolor red ":: No filename chosen."
return 1
}
[[ -e "$1" ]] || {
echolor red ":: This file does not exist."
return 1
}
[[ -e "$loc/$(basename "$1")" ]] && {
echolor red ":: This file already exists in index."
return 1
}
nix="/tmp/ath-new-entry-$(date-string).csv"
nixf="$nix.form"
touch "$nix" "$nixf"
IFS=$'\n'
sed -n 1p "$ix" >> "$nix"
[[ -z "$(cat "$logdir/last-add")" ]] && repeat_last_entry_p="n"
if [[ "$repeat_last_entry_p" = "y" ]]
then
cat "$logdir/last-add" | sed '/^filename/d' > "$nixf"
else
for j in $(xsv headers -j "$ix" | sed '/^filename$/d;/^id$/d')
do
echo -e "$j ––– \t" >> "$nixf"
done
fi
repeat_last_entry_p="n"
echo -e "filename ––– \t$(basename "$1")" >> "$nixf"
emacsclient -nw -a emacs "$nixf"
new_filename="$(cat "$nixf" | grep '^filename' | awk -F '–––' '{print $NF}' | sed 's/\t//g;s/^ *//g;s/ *$//g')"
sed -i '/^$/d' "$nixf"
cat "$nixf" | grep -v '–––' && {
echolor red ":: Input error!"
return 1
}
[[ -z "$new_filename" ]] && {
echolor red ":: Input error!"
return 1
}
[[ -e "$HOME/Athenaeum/$new_filename" ]] && {
echolor red ":: You have modified the filename, but such a file already exists!"
return 1
}
for j in $(cat "$nixf")
do
value="$(echo "$j" | awk -F '–––' '{print $NF}' | sed 's/\t//g;s/^ *//g;s/ *$//g' | sed "s/'/’/g;s/ */ /g;s/,/‚/g;s/(/⟮/g;s/)/⟯/g")"
echo "$value" | grep -q ',' && value="\"$value\""
echo -n "$value," >> "$nix"
done
while true
do
identifier="$(random-string 8)"
xsv select id "$ix" | grep -q "$identifier" || break
done
echo -n "$identifier" >> "$nix"
[[ "$(xsv select title "$nix" | sed 1d)" = '""' ]] && {
echolor red ":: Title can't be empty!"
return 1
}
xsv flatten "$nix" | sed 's/,/⸴/g' | csvlens --no-headers
for j in $(xsv headers -j "$nix")
do
[[ "$j" = "title" || "$j" = "subtitle" || "$j" = "volume" || "$j" = "edition" || "$j" = "trans_p" || "$j" = "filename" || "$j" = "id" ]] && continue
nix_term="$(xsv select "$j" "$nix" | sed -n 2p)"
[[ "$nix_term" = '""' ]] && continue
[[ -n "$(xsv search -s "$j" "^$nix_term$" "$ix" | sed 1d)" ]] && continue
echolor pink-mintgreen ":: New $j: ““$nix_term””"
done
echolor yellow ":: Continue? (Y/n) " 1
read -r continue_p
[[ "$continue_p" = "n" ]] && {
echolor red ":: Exiting."
return
}
cp -f -- "$nixf" "$logdir/last-add"
xsv cat rows "$ix" "$nix" | xsv sort -s title > "$ix"-new
rm -f "$ix"
mv "$ix"-new "$ix"
mv "$1" "$HOME/Athenaeum/$new_filename"
[[ "$(basename "$1")" != "$new_filename" ]] && {
echolor pink-mintgreen ":: Filename changed!\n:: Old ““$(basename "$1")””\n:: New ““$new_filename””"
}
continue_p=""
echolor yellow ":: Create symlink? (y/N) " 1
read -r continue_p
[[ "$continue_p" = "y" ]] && {
ln -s ~/Athenaeum/"$new_filename" . && \
echolor green ":: Symlink created."
}
make-cover "$identifier"
backup-index
unset IFS
}
function index-sorter {
# reorder index file by book title
echo
}
function symlinker {
choose-book || return 1
[[ -e "./$(basename "$sld_fnm")" ]] && {
echolor red ":: Book ““$sld_ttl”” already symlinked here."
return 1
}
ln -sf "$sld_fnm" . &&\
echolor green ":: Book ““$sld_ttl”” symlinked here!"
}
function open-book {
choose-book || return 1
[[ -z "$sld_fnm" ]] && return 1
[[ "$diagnostic_mode" -eq 1 ]] && {
echolor purple ":: open-book values:"
echolor purple "\t:: sld_fnm ““$sld_fnm””"
}
[[ "$sld_fnm" = "$loc/" ]] && {
echolor red ":: No PDF filename passed to open-book function."
return 1
}
sioyek "$sld_fnm" >/dev/null 2>/dev/null &
echo "$(date-string ymdhm),\"$sld_ttl\",$sld_fnm" >> "$HOME/.local/logs/palladium/pall-open-book-log.csv"
}
function show-info {
[[ -n "$1" ]] && sld_fnm="$1"
[[ -z "$sld_fnm" ]] && return 1
base_fnm="$(basename -- "$sld_fnm")"
echolor blue "$(stat "$sld_fnm" | sed '2d;4d'; echo -n " MSize: ")" 1
echolor purple "$(du -h "$sld_fnm" | awk '{print $1}') " 1
echolor blue "Pages: " 1
echolor purple "$(pdfinfo "$sld_fnm" | grep 'Pages:' | awk '{print $NF}')"
echolor yellow '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
echolor white "$(xsv search -s filename "^$base_fnm" "$ix" | xsv flatten)"
}
[[ -z "$ix" ]] && {
echolor red ":: Index variable not set!"
exit
}
function show-stats {
total="$(xsv count "$ix")"
echolor violet-orange ":: Found ““$total”” records."
[[ "$1" = "all" ]] || return
function get-totals {
xsv select "$1" "$ix" | sed 1d | sed '/^""$/d' | sort | uniq | wc -l
}
echolor pink-mintgreen ":: Titles \t““$(get-totals title)””"
echolor pink-mintgreen ":: Authors \t““$(get-totals author)””"
echolor pink-mintgreen ":: Years \t““$(get-totals first_pub)””"
echolor pink-mintgreen ":: Series \t““$(get-totals series)””"
echolor pink-mintgreen ":: Countries \t““$(get-totals country)””"
echolor pink-mintgreen ":: Languages \t““$(get-totals language)””"
echolor pink-mintgreen ":: Publishers \t““$(get-totals publisher)””"
echolor pink-mintgreen ":: Translators \t““$(get-totals transor)””"
echolor pink-mintgreen ":: Types\t““$(get-totals type)””"
for j in $(xsv select type "$ix" | sed 1d | sort | uniq)
do
echolor pink-mintgreen " └──── $j \t““$(xsv select type "$ix" | sed 1d | grep -c "^$j$")””"
done
}
function open-history {
function list-24-hour-books {
for j in {1..24}
do
tac "$log" | grep "^$(dateadd -f '%Y%m%d%H' "$(date-string long)" -${j}h)"
done
}
sld_fnm="$(list-24-hour-books | ifne xsv select -n 3 | awk -F '/' '{print $NF}' | sort | uniq | ifne fzf)"
[[ -z "$sld_fnm" ]] && return 1
sioyek "$loc/$sld_fnm" >/dev/null 2>/dev/null &
}
function from-id {
xsv -s id "$1" "$3" | ifne xsv select "$2" | sed 1d
}
function open-from-file {
sld_fnm="$(tac "$usdd/pall-$1" | ifne awk -F '/' '{print $NF}' | sort | uniq | ifne fzf)"
[[ -z "$sld_fnm" ]] && return 1
sioyek "$loc/$sld_fnm" >/dev/null 2>/dev/null &
}
function put-in {
pc="$usdd/pall-current"
pd="$usdd/pall-done"
case "$(echo -e "To current\nTo done" | fzf)" in
"To current") choose-book || return 1
[[ -z "$sld_fnm" ]] && return 1
cat "$pc" | grep -q "^$sld_fnm$" && {
echolor red ":: Book ““$sld_ttl”” is already in ““current””"
return 1
}
echo "$sld_fnm" >> "$pc"
echolor green ":: Book ““$sld_ttl”” added to ““current””"
;;
"To done") choose-book || return 1
[[ -z "$sld_fnm" ]] && return 1
cat "$pc" | grep -q "^$sld_fnm$" \
&& echolor yellow-violet ":: Book ““$sld_ttl”” found in ““current””, removing..."
sed -i "s|^$sld_fnm$||g;/^$/d" "$pc" || return 1
cat "$pd" | grep -q "^$sld_fnm$" && {
echolor red ":: Book ““$sld_ttl”” is already in ““done””"
return 1
}
echo "$sld_fnm" >> "$pd"
echolor green ":: Book ““$sld_ttl”” added to ““done””"
;;
*) return 1 ;;
esac
}
function give-id {
[[ -z "$1" ]] && {
choose-book || return 1
[[ -z "$sld_fnm" ]] && return 1
xsv search -s filename "$(echo "$sld_fnm" | awk -F '/' '{print $NF}')" "$ix" \
| xsv select id \
| sed 1d
return 0
}
xsv search -s id "$1" "$ix" | xsv select filename | sed 1d
}
function give-path {
choose-book || return 1
[[ -z "$sld_fnm" ]] && return 1
[[ "$sld_fnm" = "$loc/" ]] && return 1
realpath "$sld_fnm"
}
function switch-param {
if [[ -z "$1" ]] || [[ -z "$2" ]] || [[ -z "$3" ]]
then
return 1
fi
xsv search -s "$1" "^$3$" "$ix" | xsv select "$2" | sed 1d
}
function make-cover {
# enter id as $1
IFS=$'\n'
[[ -z "$1" ]] && return 1
bfn="$(xsv search -s id "$1" "$ix" | xsv select filename | sed 1d)"
[[ -e "$loc/covers/$1.jxl" ]] && {
echolor red ":: cover ““$1.jxl”” for ““$bfn”” already exists!"
continue
}
echolor yellow ":: Making cover..."
pdftk "$loc/$bfn" cat 1 output /tmp/cover.pdf || {
echolor red ":: Failed to make cover!"
continue
}
magick /tmp/cover.pdf "$loc/covers/$1.jxl"
}
function add-to-stack {
stackdir="$HOME/.local/share/user-scripts/palladium/stacks"
choose-book || return 1
sld_id="$(switch-param filename id "$(echo "$sld_fnm" | awk -F '/' '{print $NF}')")"
sld_stack="$(fd 'csv$' "$stackdir" | awk -F '/' '{print $NF}' | sed 's/\.csv$//g' | fzf )"
grep -q "^$sld_id," "$stackdir/$sld_stack.csv" && {
echolor yellow-aquamarine ":: A book with ID ““$sld_id”” is already in stack ““$sld_stack””"
return 1
}
stacknote="/tmp/stacknote-$sld_stack-$sld_id-$(date-string).csv"
emacsclient -nw -a emacs "$stacknote"
[[ -z "$(cat "$stacknote")" ]] && echo "NO NOTE" >> "$stacknote"
echo "$sld_id,\"$sld_ttl\",\"$(cat "$stacknote" | perl -pe 's/\n/—–/g' | sed 's/—–*$//g')\"" >> "$stackdir/$sld_stack.csv"
}
function see-stack {
stackdir="$HOME/.local/share/user-scripts/palladium/stacks"
sld_stack="$(fd 'csv$' "$stackdir" | awk -F '/' '{print $NF}' | sed 's/\.csv$//g' | fzf )"
IFS=$'\n'
for j in $(cat "$stackdir/$sld_stack.csv")
do
echo -n "$j" | xsv select -n 1 | tr -d '\n'
echo -n ' // '
echo -n "$j" | xsv select -n 2
done | fzf
# | fzf --preview "source ~/Repositories/scripts/palladium; whole_line="{}"; this_id=\"$(echo $this_id | awk -F ' // ' '{print $1}')\"; grep \"^$this_id\" \"$stackdir/$sld_stack.csv\" | xsv select 3 "
# | fzf --preview "source ~/Repositories/scripts/palladium; this_id="{}"; show-info \"$(switch-param id filename \"$(echo $this_id | awk -F ' // ' '{print $1}')\")\""
unset IFS
# make this work somehow
# fzf preview loves to destroy itself
}
function open-current {
cd "$HOME/Projects/reading/"
local sld_fnm_current="$(fd -d 1 pdf | ifne fzf)"
[[ -z "$sld_fnm_current" ]] && return 1
sioyek "$sld_fnm_current" >/dev/null 2>/dev/null &
}
backup-index
dup-check-in-index
case "$1" in
"by") search-by ;;
"link") symlinker ;;
"add") add-entry "$2" ;;
"again") repeat_last_entry_p=y add-entry "$2";;
"info") choose-book || exit 1
show-info ;;
"stats") show-stats all ;;
"hist") open-history ;;
"curr") open-from-file current ;;
"done") open-from-file done ;;
"put") put-in ;;
"id") give-id "$2" ;;
"path") give-path ;;
# "stack") add-to-stack ;;
# "sees") see-stack ;;
"") open-book ;;
"cover") make-cover "$2" ;;
"now" ) open-current ;;
*) echolor red ":: Unknown command." ;;
esac