diff --git a/.bashrc.d/01-env.bashrc b/.bashrc.d/01-env.bashrc index b860b84..f650cb7 100644 --- a/.bashrc.d/01-env.bashrc +++ b/.bashrc.d/01-env.bashrc @@ -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 # ============================== diff --git a/.bashrc.d/11-functions.bashrc b/.bashrc.d/11-functions.bashrc index af04198..3d37f0a 100644 --- a/.bashrc.d/11-functions.bashrc +++ b/.bashrc.d/11-functions.bashrc @@ -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 "" + + - 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>$//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