opener.ksh: script nuevo

This commit is contained in:
Tuxliban Torvalds 2023-08-15 23:02:42 -06:00
parent a42407f10d
commit 12161dc362
1 changed files with 188 additions and 0 deletions

188
varios/ksh/opener.ksh Executable file
View File

@ -0,0 +1,188 @@
#!/bin/ksh
#
# v0.3 - 15/08/2023
# Opener personalizado inspirado en el plugin preview-tabbed de nnn.
#
# Dependencias:
# pdf: zathura (GUI), pdftotext
# 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>
set -ef -o noclobber
if [[ ${#} == 0 ]]; then
print "Uso: ${0##*/} <filepath>"
exit 1
fi
IFS="$(printf '%b_' '\n')"; IFS="${IFS%_}"
EDITOR="${VISUAL:-${EDITOR:-vi}}"
FPATH="$1"
FNAME="${1##*/}"
ext="${FNAME##*.}"
if [[ -n $ext ]]; then
typeset -l ext
ext="$(print "$ext")"
fi
function open_with_viewer {
typeset viewer="$1"
shift
if type "$viewer" >/dev/null 2>&1; then
"$viewer" "${FPATH}" >/dev/null 2>&1 &
fi
}
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 &
fi
}
function open_with_editor {
"$EDITOR" "${FPATH}"
}
# Funciones para gestionar los archivos
function handle_pdf {
if [[ -n $DISPLAY ]]; then
open_with_viewer zathura
else
pdftotext -l 10 -nopgbrk -q -- "${FPATH}" - | eval "$PAGER"
fi
return 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
fi
return 0
}
function handle_video {
if [[ -n $DISPLAY ]]; then
open_with_viewer ffplay -autoexit -fs
else
open_with_converter exiftool
fi
return 0
}
function handle_image {
if [[ -n $DISPLAY ]]; then
open_with_viewer nsxiv -a
elif type viu >/dev/null 2>&1; then
open_with_converter viu -n
fi
return 0
}
function handle_html {
if [[ -n $DISPLAY ]]; then
open_with_converter w3m -dump
fi
return 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;;
## PDF
pdf)
handle_pdf
exit 1;;
## Audio
aac|flac|m4a|mid|midi|mpa|mp2|mp3|ogg|wav|webm|wma)
handle_audio
exit 1;;
## Video
avi|mkv|mp4)
handle_video
exit 1;;
## Image
bmp|gif|jpeg|jpg|png|tiff|webp|heic|\
postscript|jp2|jxl|avif|heif|eps)
handle_image
exit 1;;
## Log files
log)
open_with_editor
exit 0;;
## OpenDocument
odt|ods|odp|sxw)
if type odt2txt >/dev/null 2>&1; then
open_with_converter odt2txt
exit 0
fi
exit 1;;
## Markdown
md)
if type glow >/dev/null 2>&1; then
open_with_converter glow -sdark
exit 0
fi
exit 1;;
## HTML
htm|html|xhtml)
handle_html
exit 1;;
esac
function handle_mime {
typeset mimetype="${1}"
case "${mimetype}" in
## Manpages
text/troff)
man -a "${FPATH}"
exit 0;;
## Text
text/* | */xml)
open_with_editor
exit 0;;
esac
}
MIMETYPE="$( file -bL --mime-type -- "${FPATH}" )"
handle_mime "${MIMETYPE}"
exit 1