add function epylibre

This commit is contained in:
Tomasz Kapias 2023-03-30 14:09:28 +07:00
parent b5d54ad879
commit 6d415cc060
Signed by: tkapias
SSH key fingerprint: SHA256:bsmasrX7y0xxAHa/x1x8zAgHInO4nPpKMk5JIQ0Vsbw
2 changed files with 77 additions and 1 deletions

View file

@ -145,6 +145,10 @@ else
export MARKPATH="$HOME/.marks"
fi
if command -v calibre 1> /dev/null; then
export CALIBRELIBRARY=$HOME/Livres/calibre
fi
# ==============================
# DISPLAY
# ==============================

View file

@ -673,7 +673,7 @@ mans() {
$inside_option = 0;
}
if (!$inside_option) {
if (/^(\s*)\Q$option\E\b/p) {
if (/^(\s*)(-+\w*,\s)*\Q$option\E\b[^-]/p) {
# start of this option
$option_indentation = $1;
$inside_option = 1;
@ -713,3 +713,75 @@ if command -v br 1> /dev/null; then
fi
}
fi
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# epycalibrary - Select a book in your calibre library and open it in epy
if command -v epy 1> /dev/null; then
epylibre() {
Help() {
cat <<- HEREDOC 1>&2
Calibre Book Selector for "epy" (epub terminal reader).
Usage: epylibre "<calibre/folder/path>"
- The path is optional if you have defined it
in an env variable named 'CALIBRELIBRARY'.
HEREDOC
}
while getopts ":h" option; do
case $option in
h) Help; return 0 ;;
\?) echo -e "Unknown option: -$OPTARG \n" >&2; Help; return 1;;
: ) echo -e "Missing argument for -$OPTARG \n" >&2; Help; return 1;;
* ) echo -e "Unimplemented option: -$option \n" >&2; Help; return 1;;
esac
done
array=( epy fzf )
for cmd in "${array[@]}"; do
if [[ -z $(command -v "$cmd") ]]; then
echo "Requirements: at least $cmd could not be found:"
return 1
fi
done
if [[ -z $1 ]] && [[ -n $CALIBRELIBRARY ]]; then
local _CALIBREPATH=$(realpath --canonicalize-existing "$CALIBRELIBRARY" 2> /dev/null)
else
local _CALIBREPATH=$(realpath --canonicalize-existing "$1" 2> /dev/null)
fi
if [[ -z $_CALIBREPATH ]]; then
echo "Error: Calibre directory not found."
Help
return 1
fi
local _EPUB=$(find "$_CALIBREPATH" -type f -iname '*.epub' \
| while read _FILE; do
_REALFILE=$(realpath "$_FILE")
readarray -t _METADATA < <(sed -En \
-e '/dc:title/{s/^.+<dc:title>|<\/dc:title>$//gp}' \
-e '/calibre:series/{s/^.+\scontent=\"|\"\/>//g;H}' \
-e '/dc:creator/{s/^.+opf:role=//g;H}' \
-e '${x;{s/<\/dc:creator>\n\"aut\">/, /g};{s/^\n|\"aut\">|<\/dc:creator>//g};p}' \
"$(dirname "$_REALFILE")"/metadata.opf)
if [[ -z ${_METADATA[2]} ]]; then
_METADATA[2]="-"
_METADATA[3]="-"
fi
echo "${_METADATA[2]}|${_METADATA[3]}|${_METADATA[1]}|${_METADATA[0]}|${_REALFILE}"
done \
| sort --field-separator '|' --key=3 --key=2 --key=1 --ignore-case --ignore-leading-blanks \
| column --output-separator ' ' --separator '|' --table --table-columns 'Serie-------------->,Index,Author-------------->,Title,File' --table-columns-limit 5 --table-truncate 1,3 \
| fzf --no-mouse --cycle --reverse --no-hscroll --header-lines=1 --prompt "Choose an EPUB book to read with epy > " \
| awk -F '\t' '{print $NF}')
if [[ -n $_EPUB ]]; then
epy "$_EPUB"
else
return 1
fi
}
fi