clean up preview script
This commit is contained in:
parent
7bb116da0f
commit
1449dde205
1 changed files with 56 additions and 19 deletions
|
@ -1,14 +1,31 @@
|
|||
#!/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
|
||||
rs="$(printf "\037")"
|
||||
|
||||
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"
|
||||
|
||||
# NOTE: preview functions
|
||||
CACHE_DIR="${XDG_CACHE_HOME:-"${HOME}/.cache"}/lf"
|
||||
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
|
||||
"
|
||||
|
||||
archive1() {
|
||||
7z l "$f" | sed 1,11d
|
||||
|
@ -19,7 +36,9 @@ archive2() {
|
|||
}
|
||||
|
||||
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() {
|
||||
|
@ -51,7 +70,8 @@ lua() {
|
|||
}
|
||||
|
||||
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() {
|
||||
|
@ -59,7 +79,11 @@ word() {
|
|||
}
|
||||
|
||||
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() {
|
||||
|
@ -77,9 +101,9 @@ sqlite() {
|
|||
while read -r table; do
|
||||
[ "$table" != "name" ] &&
|
||||
printf "SELECT * FROM %s;" "$table" |
|
||||
sqlite3 -header -separator "$RS" "$f"
|
||||
sqlite3 -header -separator "$rs" "$f"
|
||||
done |
|
||||
column -t -s "$RS"
|
||||
column -t -s "$rs"
|
||||
}
|
||||
|
||||
svg() {
|
||||
|
@ -108,14 +132,18 @@ yaml() {
|
|||
$BAT --language=yaml "$f"
|
||||
}
|
||||
|
||||
# NOTE: other functions
|
||||
|
||||
run() {
|
||||
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
|
||||
else
|
||||
cache="$CACHE"
|
||||
cache="${CACHE_DIR}/$(
|
||||
stat --printf "%n %i %F %s %W %Y" -- "$(readlink -f "$f")" |
|
||||
sha256sum | awk '{print $1}'
|
||||
)"
|
||||
fi
|
||||
|
||||
if [ -r "$cache" ]; then
|
||||
|
@ -127,7 +155,6 @@ run() {
|
|||
|
||||
preview() {
|
||||
mimetype=$1
|
||||
|
||||
case "$mimetype" in
|
||||
*/vnd.openxmlformats-officedocument.spreadsheetml.sheet | */vnd.oasis.opendocument.spreadsheet) run ods ;;
|
||||
*/vnd.sqlite3) run sqlite ;;
|
||||
|
@ -154,11 +181,21 @@ preview() {
|
|||
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)"
|
||||
preview "$mimetype_gio" && exit
|
||||
|
||||
# If gio detection fails, we try again with 'file --mime-type'
|
||||
mimetype_file="$(file --brief --dereference --mime-type "$f")"
|
||||
preview "$mimetype_file" && exit
|
||||
|
||||
[ -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"
|
||||
# If all detection fails, simply print the results of file and gio
|
||||
[ -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"
|
||||
|
|
Loading…
Add table
Reference in a new issue