opener.ksh: update to 0.5

Cambios:
- Se modifica sintaxis de los códigos de salida para una mejor lectura
- Se modificó nombre de variables para une mejor comprensión de su función
- Se añadió función para verificar existencia del programa en el sistema
- Se cambió el nombre de la función 'open_with_viewer' por 'open_file'
- En la función 'handle_image' se ha reemplazado 'viu' por 'fbv'
- Ahora los archivos de extensión sc, csv, ods xls y xlsx son abiertos por 'sc-im'
- Los archivos de extensión odt y docx ahora son mostrados en texto plano
- El resto de archivos de OpenDocument y de Offices son manejados por LibreOffice
This commit is contained in:
Tuxliban Torvalds 2023-11-04 15:12:40 -06:00
parent 4d9f6b48f7
commit af455c29e4
1 changed files with 91 additions and 90 deletions

View File

@ -1,45 +1,57 @@
#!/bin/ksh #!/bin/ksh
# #
# v0.4 - 20/09/2023 # v0.5 - 4/11/2023
# Autor: O. Sánchez <o-sanchez@linuxmail.org> 2023
#
# Opener personalizado inspirado en el plugin 'nuke' de nnn. # Opener personalizado inspirado en el plugin 'nuke' de nnn.
# #
# Dependencias: # Dependencias:
# pdf: zathura (GUI), pdftotext # pdf: zathura (GUI), pdftotext (CLI)
# bsdtar: listar archivos comprimidos # bsdtar: listar archivos comprimidos
# audio: mocp (plugin nnn usando moc), mpv, exiftool # audio: mocp (plugin nnn usando moc), mpv, exiftool (CLI)
# avi|mkv|mp4: ffplay (GUI), exiftool # avi|mkv|mp4: ffplay (GUI), exiftool (CLI)
# log: $EDITOR # log: $EDITOR (CLI)
# odt|ods|odp|sxw: odt2txt # csv|ods|xls|xlsx: sc-im (GUI), libreoffice (CLI)
# Markdown: glow # odp|sxw|doc|ppt|pptx: libreoffice (CLI)
# htm|html|xhtml: w3m # Markdown: glow (CLI)
# Imágenes: nsxiv, viu # htm|html|xhtml: w3m (CLI)
# text/troff: man # Imágenes: nsxiv (GUI), fbv (CLI)
# text/* | */xml: $EDITOR # text/troff: man (CLI)
# Autor: O. Sánchez <o-sanchez@linuxmail.org> 2023 # text/* | */xml: $EDITOR (CLI)
# #
# Códigos de salida
NO_ARGS=10
SUCCESS=15
FAILED_VIEWER=20
FAILED_EXTENSION=25
set -ef -o noclobber set -ef -o noclobber
if [[ ${#} == 0 ]]; then if [[ ${#} == 0 ]]; then
print "Uso: ${0##*/} <filepath>" print "Uso: ${0##*/} <filepath>"
exit 1 exit $NO_ARGS
fi fi
IFS="$(printf '%b_' '\n')"; IFS="${IFS%_}" IFS="$(printf '%b_' '\n')"; IFS="${IFS%_}"
EDITOR="${VISUAL:-${EDITOR:-vi}}" EDITOR="${VISUAL:-${EDITOR:-vi}}"
FPATH="$1" FILE_PATH="$1"
FNAME="${1##*/}" FILE_NAME="${1##*/}"
ext="${FNAME##*.}" ext="${FILE_NAME##*.}"
if [[ -n $ext ]]; then if [[ -n $ext ]]; then
typeset -l ext typeset -l ext
ext="$(print "$ext")" ext="$(print "$ext")"
fi fi
function open_with_viewer { function check_tool {
type "$1" >/dev/null 2>&1
}
# Función genérica para abrir archivos
function open_file {
typeset viewer="$1" typeset viewer="$1"
shift shift
if type "$viewer" >/dev/null 2>&1; then if type "$viewer" >/dev/null 2>&1; then
"$viewer" "$@" "${FPATH}" >/dev/null 2>&1 & "$viewer" "$@" "${FILE_PATH}" >/dev/null 2>&1 &
fi fi
} }
@ -47,134 +59,122 @@ function open_with_converter {
typeset converter="$1" typeset converter="$1"
shift shift
if type "$converter" >/dev/null 2>&1; then if type "$converter" >/dev/null 2>&1; then
"$converter" "$@" "${FPATH}" | eval "$PAGER" "$converter" "$@" "${FILE_PATH}" | "$PAGER"
fi
}
function open_with_browser {
typeset browser="$1"
shift
if type "$browser" >/dev/null 2>&1; then
"$browser" "$@" "${FPATH}" >/dev/null 2>&1 &
fi fi
} }
function open_with_editor { function open_with_editor {
"$EDITOR" "${FPATH}" "$EDITOR" "${FILE_PATH}"
} }
# Funciones para gestionar los archivos # Funciones para gestionar los archivos
function handle_pdf { function handle_pdf {
check_tool zathura
if [[ -n $DISPLAY ]]; then if [[ -n $DISPLAY ]]; then
open_with_viewer zathura open_file zathura
else else
pdftotext -l 10 -nopgbrk -q -- "${FPATH}" - | eval "$PAGER" pdftotext -l 10 -nopgbrk -q -- "${FILE_PATH}" - | "$PAGER"
fi fi
exit 0
} }
function handle_audio { function handle_audio {
if type mocp >/dev/null 2>&1 && type mocq >/dev/null 2>&1; then check_tool mocp
open_with_viewer mocq opener check_tool mocq
elif type mpv >/dev/null 2>&1; then check_tool mpv
open_with_viewer mpv check_tool exiftool
elif type exiftool >/dev/null 2>&1; then if [[ -n $DISPLAY ]]; then
open_with_converter exiftool mocq "${FILE_PATH}" opener || open_file mpv
else
print && open_with_converter exiftool
fi fi
exit 0
} }
function handle_video { function handle_video {
check_tool ffplay
check_tool exiftool
if [[ -n $DISPLAY ]]; then if [[ -n $DISPLAY ]]; then
open_with_viewer ffplay -autoexit -fs open_file ffplay -autoexit -fs
else else
open_with_converter exiftool print && open_with_converter exiftool
fi fi
exit 0
} }
function handle_image { function handle_image {
check_tool nsxiv
check_tool fbv
if [[ -n $DISPLAY ]]; then if [[ -n $DISPLAY ]]; then
open_with_viewer nsxiv -a open_file nsxiv -a
elif type viu >/dev/null 2>&1; then else
open_with_converter viu -n open_with_converter fbv -f
fi fi
exit 0
} }
function handle_html { function handle_html {
check_tool w3m
if [[ -n $DISPLAY ]]; then if [[ -n $DISPLAY ]]; then
open_with_converter w3m -dump open_with_converter w3m -dump
else
w3m -H -graph -no-cookie "${FILE_PATH}"
fi fi
exit 0
} }
case "${ext}" in case "${ext}" in
## Archive ## Archive
a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\ a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\
rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zstd|zip) rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zstd|zip)
if type bsdtar >/dev/null 2>&1; then check_tool bsdtar
bsdtar --list --file "${FPATH}" | eval "$PAGER" open_with_converter bsdtar --list --file && exit $SUCCESS && exit $FAILED_VIEWER;;
exit 0
fi
exit 1;;
## PDF ## PDF
pdf) pdf)
handle_pdf handle_pdf && exit $SUCCESS || exit $FAILED_VIEWER;;
exit 1;;
## Audio ## Audio
aac|flac|m4a|mid|midi|mpa|mp2|mp3|ogg|wav|webm|wma) aac|flac|m4a|mid|midi|mpa|mp2|mp3|ogg|wav|webm|wma)
handle_audio handle_audio && exit $SUCCESS || exit $FAILED_VIEWER;;
exit 1;;
## Video ## Video
avi|mkv|mp4|mov) avi|mkv|mp4|mov)
handle_video handle_video && exit $SUCCESS || exit $FAILED_VIEWER;;
exit 1;;
## Image ## Image
bmp|gif|jpeg|jpg|png|tiff|webp|heic|\ bmp|gif|jpeg|jpg|png|tiff|webp|heic|\
postscript|jp2|jxl|avif|heif|eps) postscript|jp2|jxl|avif|heif|eps)
handle_image handle_image && exit $SUCCESS || exit $FAILED_VIEWER;;
exit 1;;
## Log files ## Log files
log) log)
open_with_editor open_with_editor && exit $SUCCESS || exit $FAILED_VIEWER;;
exit 0;;
## OpenDocument ## Hojas de cálculo
odt|ods|odp|sxw) sc|csv|ods|xls|xlsx)
if type odt2txt >/dev/null 2>&1; then check_tool sc-im
open_with_converter odt2txt if [[ -n $DISPLAY ]]; then
exit 0 open_file urxvtc -T "Terminal Sheets" -e sc-im && exit $SUCCESS || exit $FAILED_VIEWER
fi else
exit 1;; sc-im ${FILE_PATH} && exit $SUCCESS || exit $FAILED_VIEW
fi;;
## Winbugs documents ## Documentos de texto
doc|docx|xls|xlsx|ppt|pptx) odt|docx)
if type soffice >/dev/null 2>&1; then check_tool pandoc
soffice --nologo "${@}" >/dev/null 2>&1 & open_with_converter pandoc -t plain && exit $SUCCESS || exit $FAILED_VIEWER;;
exit 0
fi ## Winbugs documents y OpenDocument
exit 1;; odp|sxw|doc|ppt|pptx)
check_tool soffice
soffice --nologo "${FILE_PATH}" >/dev/null 2>&1 && exit $SUCCESS || exit $FAILED_VIEWER;;
## Markdown ## Markdown
md) md)
if type glow >/dev/null 2>&1; then check_tool glow
open_with_converter glow -sdark open_with_converter glow -sdark && exit $SUCCESS || exit $FAILED_VIEWER;;
exit 0
fi
exit 1;;
## HTML ## HTML
htm|html|xhtml) htm|html|xhtml)
handle_html handle_html && exit $SUCCESS || exit $FAILED_VIEWER;;
exit 1;;
esac esac
function handle_mime { function handle_mime {
@ -182,16 +182,17 @@ function handle_mime {
case "${mimetype}" in case "${mimetype}" in
## Manpages ## Manpages
text/troff) text/troff)
man -a "${FPATH}" man -a "${FILE_PATH}" && exit $SUCCESS || exit $FAILED_VIEWER;;
exit 0;;
## Text ## Text
text/* | */xml) text/* | */xml | */javascript)
open_with_editor open_with_editor && exit $SUCCESS || exit $FAILED_VIEWER;;
exit 0;;
esac *)
print "Error: No se puede manejar el mimetype '${MIMETYPE}'"
exit $FAILED_EXTENSION;;
esac
} }
MIMETYPE="$( file -bL --mime-type -- "${FPATH}" )" MIMETYPE="$( file -bL --mime-type -- "${FILE_PATH}" )"
handle_mime "${MIMETYPE}" handle_mime "${MIMETYPE}"
exit 1