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
#
# 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.
#
# Dependencias:
# pdf: zathura (GUI), pdftotext
# pdf: zathura (GUI), pdftotext (CLI)
# bsdtar: listar archivos comprimidos
# audio: mocp (plugin nnn usando moc), mpv, exiftool
# avi|mkv|mp4: ffplay (GUI), exiftool
# log: $EDITOR
# odt|ods|odp|sxw: odt2txt
# Markdown: glow
# htm|html|xhtml: w3m
# Imágenes: nsxiv, viu
# text/troff: man
# text/* | */xml: $EDITOR
# Autor: O. Sánchez <o-sanchez@linuxmail.org> 2023
# audio: mocp (plugin nnn usando moc), mpv, exiftool (CLI)
# avi|mkv|mp4: ffplay (GUI), exiftool (CLI)
# log: $EDITOR (CLI)
# csv|ods|xls|xlsx: sc-im (GUI), libreoffice (CLI)
# odp|sxw|doc|ppt|pptx: libreoffice (CLI)
# Markdown: glow (CLI)
# htm|html|xhtml: w3m (CLI)
# Imágenes: nsxiv (GUI), fbv (CLI)
# text/troff: man (CLI)
# text/* | */xml: $EDITOR (CLI)
#
# Códigos de salida
NO_ARGS=10
SUCCESS=15
FAILED_VIEWER=20
FAILED_EXTENSION=25
set -ef -o noclobber
if [[ ${#} == 0 ]]; then
print "Uso: ${0##*/} <filepath>"
exit 1
exit $NO_ARGS
fi
IFS="$(printf '%b_' '\n')"; IFS="${IFS%_}"
EDITOR="${VISUAL:-${EDITOR:-vi}}"
FPATH="$1"
FNAME="${1##*/}"
ext="${FNAME##*.}"
FILE_PATH="$1"
FILE_NAME="${1##*/}"
ext="${FILE_NAME##*.}"
if [[ -n $ext ]]; then
typeset -l ext
ext="$(print "$ext")"
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"
shift
if type "$viewer" >/dev/null 2>&1; then
"$viewer" "$@" "${FPATH}" >/dev/null 2>&1 &
"$viewer" "$@" "${FILE_PATH}" >/dev/null 2>&1 &
fi
}
@ -47,134 +59,122 @@ function open_with_converter {
typeset converter="$1"
shift
if type "$converter" >/dev/null 2>&1; then
"$converter" "$@" "${FPATH}" | eval "$PAGER"
fi
}
function open_with_browser {
typeset browser="$1"
shift
if type "$browser" >/dev/null 2>&1; then
"$browser" "$@" "${FPATH}" >/dev/null 2>&1 &
"$converter" "$@" "${FILE_PATH}" | "$PAGER"
fi
}
function open_with_editor {
"$EDITOR" "${FPATH}"
"$EDITOR" "${FILE_PATH}"
}
# Funciones para gestionar los archivos
function handle_pdf {
check_tool zathura
if [[ -n $DISPLAY ]]; then
open_with_viewer zathura
open_file zathura
else
pdftotext -l 10 -nopgbrk -q -- "${FPATH}" - | eval "$PAGER"
pdftotext -l 10 -nopgbrk -q -- "${FILE_PATH}" - | "$PAGER"
fi
exit 0
}
function handle_audio {
if type mocp >/dev/null 2>&1 && type mocq >/dev/null 2>&1; then
open_with_viewer mocq opener
elif type mpv >/dev/null 2>&1; then
open_with_viewer mpv
elif type exiftool >/dev/null 2>&1; then
open_with_converter exiftool
check_tool mocp
check_tool mocq
check_tool mpv
check_tool exiftool
if [[ -n $DISPLAY ]]; then
mocq "${FILE_PATH}" opener || open_file mpv
else
print && open_with_converter exiftool
fi
exit 0
}
function handle_video {
check_tool ffplay
check_tool exiftool
if [[ -n $DISPLAY ]]; then
open_with_viewer ffplay -autoexit -fs
open_file ffplay -autoexit -fs
else
open_with_converter exiftool
print && open_with_converter exiftool
fi
exit 0
}
function handle_image {
check_tool nsxiv
check_tool fbv
if [[ -n $DISPLAY ]]; then
open_with_viewer nsxiv -a
elif type viu >/dev/null 2>&1; then
open_with_converter viu -n
open_file nsxiv -a
else
open_with_converter fbv -f
fi
exit 0
}
function handle_html {
check_tool w3m
if [[ -n $DISPLAY ]]; then
open_with_converter w3m -dump
else
w3m -H -graph -no-cookie "${FILE_PATH}"
fi
exit 0
}
case "${ext}" in
## Archive
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)
if type bsdtar >/dev/null 2>&1; then
bsdtar --list --file "${FPATH}" | eval "$PAGER"
exit 0
fi
exit 1;;
check_tool bsdtar
open_with_converter bsdtar --list --file && exit $SUCCESS && exit $FAILED_VIEWER;;
## PDF
pdf)
handle_pdf
exit 1;;
handle_pdf && exit $SUCCESS || exit $FAILED_VIEWER;;
## Audio
aac|flac|m4a|mid|midi|mpa|mp2|mp3|ogg|wav|webm|wma)
handle_audio
exit 1;;
handle_audio && exit $SUCCESS || exit $FAILED_VIEWER;;
## Video
avi|mkv|mp4|mov)
handle_video
exit 1;;
handle_video && exit $SUCCESS || exit $FAILED_VIEWER;;
## Image
bmp|gif|jpeg|jpg|png|tiff|webp|heic|\
postscript|jp2|jxl|avif|heif|eps)
handle_image
exit 1;;
handle_image && exit $SUCCESS || exit $FAILED_VIEWER;;
## Log files
log)
open_with_editor
exit 0;;
open_with_editor && exit $SUCCESS || exit $FAILED_VIEWER;;
## OpenDocument
odt|ods|odp|sxw)
if type odt2txt >/dev/null 2>&1; then
open_with_converter odt2txt
exit 0
fi
exit 1;;
## Hojas de cálculo
sc|csv|ods|xls|xlsx)
check_tool sc-im
if [[ -n $DISPLAY ]]; then
open_file urxvtc -T "Terminal Sheets" -e sc-im && exit $SUCCESS || exit $FAILED_VIEWER
else
sc-im ${FILE_PATH} && exit $SUCCESS || exit $FAILED_VIEW
fi;;
## Winbugs documents
doc|docx|xls|xlsx|ppt|pptx)
if type soffice >/dev/null 2>&1; then
soffice --nologo "${@}" >/dev/null 2>&1 &
exit 0
fi
exit 1;;
## Documentos de texto
odt|docx)
check_tool pandoc
open_with_converter pandoc -t plain && exit $SUCCESS || exit $FAILED_VIEWER;;
## Winbugs documents y OpenDocument
odp|sxw|doc|ppt|pptx)
check_tool soffice
soffice --nologo "${FILE_PATH}" >/dev/null 2>&1 && exit $SUCCESS || exit $FAILED_VIEWER;;
## Markdown
md)
if type glow >/dev/null 2>&1; then
open_with_converter glow -sdark
exit 0
fi
exit 1;;
check_tool glow
open_with_converter glow -sdark && exit $SUCCESS || exit $FAILED_VIEWER;;
## HTML
htm|html|xhtml)
handle_html
exit 1;;
handle_html && exit $SUCCESS || exit $FAILED_VIEWER;;
esac
function handle_mime {
@ -182,16 +182,17 @@ function handle_mime {
case "${mimetype}" in
## Manpages
text/troff)
man -a "${FPATH}"
exit 0;;
man -a "${FILE_PATH}" && exit $SUCCESS || exit $FAILED_VIEWER;;
## Text
text/* | */xml)
open_with_editor
exit 0;;
esac
text/* | */xml | */javascript)
open_with_editor && exit $SUCCESS || exit $FAILED_VIEWER;;
*)
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}"
exit 1