1
0
Fork 0

clean up preview script

This commit is contained in:
Luca Bilke 2025-03-07 14:01:01 +01:00
commit 1449dde205
Signed by: luca
GPG key ID: F6E11C9BAA7C82F5

View file

@ -1,14 +1,31 @@
#!/bin/sh #!/bin/sh
# shellcheck disable=SC2016
# Preview functions for each filetype must print their results to stdout.
# The preview functions are called by the "run" wrapper function, which handles caching.
# This wrapper function can take "-s" as an argument to indicate that the
# preview should be regenerated if the terminal is resized.
f="$(realpath "$1")" w=$2 h=$3 _x=$4 _y=$5 f="$(realpath "$1")" w=$2 h=$3 _x=$4 _y=$5
rs="$(printf "\037")"
RS="$(printf "\037")" CACHE_DIR="${XDG_CACHE_HOME:-"${HOME}/.cache"}/lf"
CACHE="${XDG_CACHE_HOME}/lf/$(stat --printf "%n %i %F %s %W %Y" -- "$(readlink -f "$1")" | sha256sum | awk '{print $1}')" BAT="bat \
SIZED_CACHE="${XDG_CACHE_HOME}/lf/$(stat --printf "%n %i %F %s %W %Y ${w} ${h}" -- "$(readlink -f "$1")" | sha256sum | awk '{print $1}')" --style=plain \
BAT="bat --style=plain --color=always --italic-text=always --pager=never --tabs=4 --theme=base16 --line-range=:$h --terminal-width=$((w - 2))" --color=always \
CHAFA="chafa -f sixel -s ${w}x${h} --polite on --animate false" --italic-text=always \
--pager=never \
# NOTE: preview functions --tabs=4 \
--theme=base16 \
--line-range=:$h \
--terminal-width=$((w - 2))
"
CHAFA="chafa \
-f sixel \
-s ${w}x${h} \
--polite on \
--animate false
"
archive1() { archive1() {
7z l "$f" | sed 1,11d 7z l "$f" | sed 1,11d
@ -19,7 +36,9 @@ archive2() {
} }
csv() { csv() {
mlr --icsv --omd cat "$f" | CLICOLOR_FORCE=1 COLORTERM=truecolor glow -w "$w" -s tokyo-night mlr --icsv --omd cat "$f" |
CLICOLOR_FORCE=1 COLORTERM=truecolor \
glow -w "$w" -s tokyo-night
} }
docx() { docx() {
@ -51,7 +70,8 @@ lua() {
} }
markdown() { markdown() {
CLICOLOR_FORCE=1 COLORTERM=truecolor glow -w "$w" -s tokyo-night "$f" CLICOLOR_FORCE=1 COLORTERM=truecolor \
glow -w "$w" -s tokyo-night "$f"
} }
word() { word() {
@ -59,7 +79,11 @@ word() {
} }
ods() { ods() {
ssconvert --export-type=Gnumeric_stf:stf_assistant -O separator="$RS" "$f" "fd://1" | column -t -s "$RS" ssconvert \
--export-type=Gnumeric_stf:stf_assistant \
-O separator="$rs" \
"$f" "fd://1" |
column -t -s "$rs"
} }
odt() { odt() {
@ -77,9 +101,9 @@ sqlite() {
while read -r table; do while read -r table; do
[ "$table" != "name" ] && [ "$table" != "name" ] &&
printf "SELECT * FROM %s;" "$table" | printf "SELECT * FROM %s;" "$table" |
sqlite3 -header -separator "$RS" "$f" sqlite3 -header -separator "$rs" "$f"
done | done |
column -t -s "$RS" column -t -s "$rs"
} }
svg() { svg() {
@ -108,14 +132,18 @@ yaml() {
$BAT --language=yaml "$f" $BAT --language=yaml "$f"
} }
# NOTE: other functions
run() { run() {
if [ "$1" = "-s" ]; then if [ "$1" = "-s" ]; then
cache="$SIZED_CACHE" cache="${CACHE_DIR}/$(
stat --printf "%n %i %F %s %W %Y ${w} ${h}" -- "$(readlink -f "$f")" |
sha256sum | awk '{print $1}'
)"
shift shift
else else
cache="$CACHE" cache="${CACHE_DIR}/$(
stat --printf "%n %i %F %s %W %Y" -- "$(readlink -f "$f")" |
sha256sum | awk '{print $1}'
)"
fi fi
if [ -r "$cache" ]; then if [ -r "$cache" ]; then
@ -127,7 +155,6 @@ run() {
preview() { preview() {
mimetype=$1 mimetype=$1
case "$mimetype" in case "$mimetype" in
*/vnd.openxmlformats-officedocument.spreadsheetml.sheet | */vnd.oasis.opendocument.spreadsheet) run ods ;; */vnd.openxmlformats-officedocument.spreadsheetml.sheet | */vnd.oasis.opendocument.spreadsheet) run ods ;;
*/vnd.sqlite3) run sqlite ;; */vnd.sqlite3) run sqlite ;;
@ -154,11 +181,21 @@ preview() {
esac esac
} }
# We don't want to reprocess files in the cache, as this will generate a bunch of duplicate cache entries
[ "$(readlink -f "$f" | xargs dirname)" = "${CACHE_DIR}" ] && {
cat "$f"
exit
}
# First mimetype detection is made with 'gio info'
mimetype_gio="$(gio info "$f" 2>/dev/null | grep standard::content-type | cut -d' ' -f4)" mimetype_gio="$(gio info "$f" 2>/dev/null | grep standard::content-type | cut -d' ' -f4)"
preview "$mimetype_gio" && exit preview "$mimetype_gio" && exit
# If gio detection fails, we try again with 'file --mime-type'
mimetype_file="$(file --brief --dereference --mime-type "$f")" mimetype_file="$(file --brief --dereference --mime-type "$f")"
preview "$mimetype_file" && exit preview "$mimetype_file" && exit
[ -n "$mimetype_file" ] && printf "%b" "\$(file --mime-type)\t\t\t : ${mimetype_file}\n" # If all detection fails, simply print the results of file and gio
[ -n "$mimetype_gio" ] && printf "%b" "\$(gio info)\t\t\t\t : ${mimetype_gio}\n" [ -n "$mimetype_gio" ] && printf '$(gio info)\t\t\t\t : %s\n' "${mimetype_gio}"
[ -n "$mimetype_file" ] && printf '$(file --mime-type)\t\t\t : %s\n' "${mimetype_file}"
mediainfo "$1" | head -n -2 | grep -v -e "Complete name" -e "General" mediainfo "$1" | head -n -2 | grep -v -e "Complete name" -e "General"