lf: rewrite preview script
This commit is contained in:
parent
c1537b820b
commit
7bb116da0f
1 changed files with 135 additions and 109 deletions
|
@ -1,32 +1,116 @@
|
|||
#!/bin/sh
|
||||
|
||||
set -C -f
|
||||
f="$(realpath "$1")" w=$2 h=$3 _x=$4 _y=$5
|
||||
|
||||
f="$(realpath "$1")" w=$2 h=$3 x=$4 y=$5
|
||||
|
||||
RED="\033[31m"
|
||||
RESET="\033[0m"
|
||||
RS="$(printf "\037")"
|
||||
CACHE="${XDG_CACHE_HOME}/lf/$(stat --printf "%n %i %F %s %W %Y" -- "$(readlink -f "$1")" | sha256sum | awk '{print $1}')"
|
||||
SIZED_CACHE="${XDG_CACHE_HOME}/lf/$(stat --printf "%n %i %F %s %W %Y ${w} ${h}" -- "$(readlink -f "$1")" | sha256sum | awk '{print $1}')"
|
||||
BAT="bat --style=plain --color=always --italic-text=always --pager=never --tabs=4 --theme=base16 --line-range=:$h --terminal-width=$((w - 2))"
|
||||
CHAFA="chafa -f sixel -s ${w}x${h} --polite on --animate false"
|
||||
|
||||
run() {
|
||||
#shellcheck disable=SC2068
|
||||
if ! command -v "$1" >/dev/null 2>&1; then
|
||||
printf "%b" "${RED}Cannot execute ${1}${RESET}"
|
||||
elif "$@"; then
|
||||
:
|
||||
else
|
||||
printf "%b" "${RED}Failed to run command: ${RESET}"
|
||||
for l in "$@"; do
|
||||
printf "%b" "\n${RED} ${l}${RESET}"
|
||||
done
|
||||
fi
|
||||
# NOTE: preview functions
|
||||
|
||||
archive1() {
|
||||
7z l "$f" | sed 1,11d
|
||||
}
|
||||
|
||||
cached_text() {
|
||||
archive2() {
|
||||
als -- "$f"
|
||||
}
|
||||
|
||||
csv() {
|
||||
mlr --icsv --omd cat "$f" | CLICOLOR_FORCE=1 COLORTERM=truecolor glow -w "$w" -s tokyo-night
|
||||
}
|
||||
|
||||
docx() {
|
||||
docx2txt "$f" -
|
||||
}
|
||||
|
||||
epub() {
|
||||
gnome-epub-thumbnailer -s 1024 "$f" /dev/stdout
|
||||
}
|
||||
|
||||
html() {
|
||||
lynx -dump "$f"
|
||||
}
|
||||
|
||||
image() {
|
||||
$CHAFA "$f"
|
||||
}
|
||||
|
||||
iso() {
|
||||
iso-info --no-header -l "$f"
|
||||
}
|
||||
|
||||
json() {
|
||||
jq -C . "$f"
|
||||
}
|
||||
|
||||
lua() {
|
||||
$BAT --language=lua "$f"
|
||||
}
|
||||
|
||||
markdown() {
|
||||
CLICOLOR_FORCE=1 COLORTERM=truecolor glow -w "$w" -s tokyo-night "$f"
|
||||
}
|
||||
|
||||
word() {
|
||||
catdoc "$f"
|
||||
}
|
||||
|
||||
ods() {
|
||||
ssconvert --export-type=Gnumeric_stf:stf_assistant -O separator="$RS" "$f" "fd://1" | column -t -s "$RS"
|
||||
}
|
||||
|
||||
odt() {
|
||||
odt2txt "$f"
|
||||
}
|
||||
|
||||
pdf() {
|
||||
pdftoppm -jpeg -f 1 -singlefile "$f"
|
||||
}
|
||||
|
||||
sqlite() {
|
||||
printf ".headers on\n.mode list\nSELECT name FROM sqlite_master WHERE type='table';\n" |
|
||||
sqlite3 "$f" |
|
||||
grep -v name |
|
||||
while read -r table; do
|
||||
[ "$table" != "name" ] &&
|
||||
printf "SELECT * FROM %s;" "$table" |
|
||||
sqlite3 -header -separator "$RS" "$f"
|
||||
done |
|
||||
column -t -s "$RS"
|
||||
}
|
||||
|
||||
svg() {
|
||||
inkscape \
|
||||
--convert-dpi-method=none \
|
||||
--export-filename=- \
|
||||
--export-overwrite \
|
||||
--export-area-drawing \
|
||||
--export-png-color-mode=RGBA_16 "$f" |
|
||||
$CHAFA
|
||||
}
|
||||
|
||||
text() {
|
||||
$BAT "$f"
|
||||
}
|
||||
|
||||
torrent() {
|
||||
transmission-show "$f"
|
||||
}
|
||||
|
||||
video() {
|
||||
ffmpegthumbnailer -s 0 -i "$f" -o "/dev/stdout" | $CHAFA
|
||||
}
|
||||
|
||||
yaml() {
|
||||
$BAT --language=yaml "$f"
|
||||
}
|
||||
|
||||
# NOTE: other functions
|
||||
|
||||
run() {
|
||||
if [ "$1" = "-s" ]; then
|
||||
cache="$SIZED_CACHE"
|
||||
shift
|
||||
|
@ -34,105 +118,47 @@ cached_text() {
|
|||
cache="$CACHE"
|
||||
fi
|
||||
|
||||
if [ -f "$cache" ]; then
|
||||
# shellcheck disable=SC2086
|
||||
$BAT "$cache"
|
||||
if [ -r "$cache" ]; then
|
||||
cat "$cache"
|
||||
else
|
||||
# shellcheck disable=SC2086
|
||||
run "$@" | tee -p "$cache" | $BAT
|
||||
"$@" | tee -p "$cache"
|
||||
fi
|
||||
}
|
||||
|
||||
cached_image() {
|
||||
if [ -f "$CACHE" ]; then
|
||||
$CHAFA "$CACHE"
|
||||
else
|
||||
run "$@" | tee -p "$CACHE" | $CHAFA
|
||||
fi
|
||||
}
|
||||
preview() {
|
||||
mimetype=$1
|
||||
|
||||
mimetest() {
|
||||
case "$1" in
|
||||
# */pgp-encrypted) run gpg -d -- "$f" ;;
|
||||
*/vnd.openxmlformats-officedocument.spreadsheetml.sheet | */vnd.oasis.opendocument.spreadsheet)
|
||||
if [ -f "$CACHE" ]; then
|
||||
$BAT "$CACHE"
|
||||
else
|
||||
ssconvert --export-type=Gnumeric_stf:stf_assistant -O separator="$RS" "$f" "fd://1" |
|
||||
column -t -s "$RS" | tee -p "$CACHE"
|
||||
fi
|
||||
;;
|
||||
*/vnd.sqlite3)
|
||||
if [ -f "$CACHE" ]; then
|
||||
$BAT "$CACHE"
|
||||
else
|
||||
printf ".headers on\n.mode list\nSELECT name FROM sqlite_master WHERE type='table';\n" |
|
||||
sqlite3 "$f" |
|
||||
grep -v name |
|
||||
while read -r table; do
|
||||
[ "$table" != "name" ] &&
|
||||
printf "SELECT * FROM %s;" "$table" |
|
||||
sqlite3 -header -separator "$RS" "$f"
|
||||
done |
|
||||
column -t -s "$RS" | tee -p "$CACHE"
|
||||
fi
|
||||
;;
|
||||
*/x-7z-compressed | */vnd.rar | */x-tar | */zip | */x-java-archive | */x-xz | */gzip)
|
||||
if [ -f "$CACHE" ]; then
|
||||
$BAT "$CACHE"
|
||||
else
|
||||
run 7z l "$f" | sed 1,11d | tee -p "$CACHE" | $BAT
|
||||
fi
|
||||
;;
|
||||
*/epub+zip) cached_image gnome-epub-thumbnailer -s 1024 "$f" "$CACHE" ;;
|
||||
image/svg+xml)
|
||||
cached_image inkscape \
|
||||
--convert-dpi-method=none \
|
||||
--export-filename=- \
|
||||
--export-overwrite \
|
||||
--export-area-drawing \
|
||||
--export-png-color-mode=RGBA_16 "$f"
|
||||
;;
|
||||
image/*) $CHAFA "$f" ;;
|
||||
*/pdf) cached_image pdftoppm -jpeg -f 1 -singlefile "$f" ;;
|
||||
video/*) cached_image ffmpegthumbnailer -s 0 -i "$f" -o "/dev/stdout" ;;
|
||||
*/vnd.oasis.opendocument.text) cached_text odt2txt "$f" ;;
|
||||
*/vnd.openxmlformats-officedocument.wordprocessingml.document) cached_text docx2txt "$f" - ;;
|
||||
*/csv)
|
||||
export COLORTERM=truecolor
|
||||
export CLICOLOR_FORCE=1
|
||||
# TODO: caching
|
||||
mlr --icsv --omd cat "$f" | glow -w "$w" -s tokyo-night
|
||||
;;
|
||||
*/markdown)
|
||||
export COLORTERM=truecolor
|
||||
export CLICOLOR_FORCE=1
|
||||
cached_text -s glow -w "$w" -s tokyo-night "$f"
|
||||
;;
|
||||
*/html) cached_text lynx -dump "$f" ;;
|
||||
*/*json) cached_text jq -C . "$f" ;;
|
||||
*/msword) cached_text catdoc "$f" ;;
|
||||
*/x-bittorrent) cached_text transmission-show "$f" ;;
|
||||
*/x-cd-image) cached_text iso-info --no-header -l "$f" ;;
|
||||
*/x-bzip-compressed-tar | */x-compressed-tar | */x-xz-compressed-tar) cached_text als -- "$f" ;;
|
||||
*/*lua) $BAT --language=lua "$f" ;;
|
||||
*/*yaml) $BAT --language=yaml "$f" ;;
|
||||
*text*) $BAT "$f" ;;
|
||||
case "$mimetype" in
|
||||
*/vnd.openxmlformats-officedocument.spreadsheetml.sheet | */vnd.oasis.opendocument.spreadsheet) run ods ;;
|
||||
*/vnd.sqlite3) run sqlite ;;
|
||||
*/x-7z-compressed | */vnd.rar | */x-tar | */zip | */x-java-archive | */x-xz | */gzip) run archive1 ;;
|
||||
*/epub+zip) run -s epub ;;
|
||||
image/svg+xml) run -s svg ;;
|
||||
image/*) run -s image ;;
|
||||
*/pdf) run -s pdf ;;
|
||||
video/*) run -s video ;;
|
||||
*/vnd.oasis.opendocument.text) run odt ;;
|
||||
*/vnd.openxmlformats-officedocument.wordprocessingml.document) run docx ;;
|
||||
*/csv) run -s csv ;;
|
||||
*/markdown) run -s markdown ;;
|
||||
*/html) run html ;;
|
||||
*/*json) run json ;;
|
||||
*/msword) run word ;;
|
||||
*/x-bittorrent) run torrent ;;
|
||||
*/x-cd-image) run iso ;;
|
||||
*/x-bzip-compressed-tar | */x-compressed-tar | */x-xz-compressed-tar) run archive2 ;;
|
||||
*/*lua) run lua ;;
|
||||
*/*yaml) run yaml ;;
|
||||
*text*) run text ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Special cases
|
||||
[ "$(dirname "$(realpath "$f")" | rev | cut -d/ -f1 | rev)" = "factscache" ] &&
|
||||
[ -f "$(dirname "$(dirname "$(realpath "$f")")")/ansible.cfg" ] &&
|
||||
jq -C <"$f" && exit
|
||||
mimetype_gio="$(gio info "$f" 2>/dev/null | grep standard::content-type | cut -d' ' -f4)"
|
||||
preview "$mimetype_gio" && exit
|
||||
mimetype_file="$(file --brief --dereference --mime-type "$f")"
|
||||
preview "$mimetype_file" && exit
|
||||
|
||||
giorun="$(gio info "$f" 2>/dev/null | grep standard::content-type | cut -d' ' -f4)"
|
||||
mimetest "$giorun" && exit
|
||||
|
||||
filerun="$(file --brief --dereference --mime-type "$f")"
|
||||
mimetest "$filerun" && exit
|
||||
|
||||
printf "%b" "\$(file --mime-type)\t\t\t : $filerun\n"
|
||||
[ -n "$giorun" ] && printf "%b" "\$(gio info)\t\t\t\t : $giorun\n"
|
||||
[ -n "$mimetype_file" ] && printf "%b" "\$(file --mime-type)\t\t\t : ${mimetype_file}\n"
|
||||
[ -n "$mimetype_gio" ] && printf "%b" "\$(gio info)\t\t\t\t : ${mimetype_gio}\n"
|
||||
mediainfo "$1" | head -n -2 | grep -v -e "Complete name" -e "General"
|
||||
|
|
Loading…
Add table
Reference in a new issue