1
0
Fork 0

lf: streamline previews, fix new_lf_term function

This commit is contained in:
Luca Bilke 2024-10-13 19:49:29 +02:00
parent 66d3356dd2
commit ab95fa9dc4
No known key found for this signature in database
GPG Key ID: C9E851809C1A5BDE
4 changed files with 2 additions and 164 deletions

View File

@ -193,9 +193,8 @@ cmd extract ${{
done
}}
# HACK: This is a dirty hack to have an lf terminal that I can "exit" out of into a shell
cmd new_lf_term &{{
mapcmd='map q $sh -c "tmux; kill $id"'
mapcmd='map q $sh -c "$SHELL; kill $id"'
$TERMINAL -e lfX -command "$mapcmd"
}}
@ -219,8 +218,7 @@ set period 1
set cursorpreviewfmt "\033[7;90m"
set promptfmt # this is set in a function later
set sixel
set previewer ~/.config/lf/preview/preview
set cleaner ~/.config/lf/preview/clean
set previewer ~/.config/lf/preview
# Bindings
clearmaps

View File

@ -1,5 +0,0 @@
#!/bin/sh
if [ -n "$FIFO_UEBERZUG" ]; then
printf '{"action": "remove", "identifier": "PREVIEW"}\n' > "$FIFO_UEBERZUG"
fi

View File

@ -1,119 +0,0 @@
#!/bin/sh
RED="\033[31m"
RESET="\033[0m"
set -C -f
f="$(realpath "$1")" w=$2 h=$3 # x=$4 y=$5
image() {
file=${1:-$f}
# shellcheck disable=2086
chafa -f sixel -s "${w}x${h}" --polite on --animate false "$file"
exit
}
video() {
thumb="$("$HOME/.config/lf/preview/vidthumb" "$f")" || {
printf "%b" "${RED}vidthumb failed to execute${RESET}"
return 1
}
image "$thumb"
}
text() {
for cmd in bat batcat; do
command -v $cmd >/dev/null 2>&1 && break
done
$cmd \
--style=full \
--color=always \
--italic-text=always \
--pager=never \
--tabs=4 \
--theme=base16 \
--line-range=":$h" \
--terminal-width="$((w - 2))" \
"$@"
}
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
exit 1
}
mimetest() {
case "$1" in
*/x-bzip-compressed-tar | */x-compressed-tar | */x-xz-compressed-tar) run als -- "$f" ;;
*/x-7z-compressed | */vnd.rar | */x-tar | */zip | */x-java-archive | */x-xz | */gzip) run 7z l "$f" | sed 1,11d ;;
*/x-cd-image) run iso-info --no-header -l "$f" ;;
*/x-bittorrent) run transmission-show "$f" ;;
*/vnd.sun.xml.writer) run odt2txt "$f" ;;
*/msword) run catdoc "$f" ;;
*/vnd.openxmlformats-officedocument.wordprocessingml.document) run docx2txt <"$f" ;;
*/vnd.ms-excel) run ssconvert --export-type=Gnumeric_stf:stf_csv "$f" "fd://1" | text --language=csv "$f" ;;
*/vnd.openxmlformats-officedocument.spreadsheetml.sheet) run ssconvert --export-type=Gnumeric_stf:stf_csv "$f" "fd://1" | text --language=csv "$f" ;;
# */pgp-encrypted) run gpg -d -- "$f" ;;
*/pkix-cert) run openssl x509 -text -noout -in "$f" ;;
*opendocument*) run odt2txt "$f" ;;
*/markdown)
[ ! -f "${CACHE}.txt" ] && run mdless "$f" >"${CACHE}.txt"
text "${CACHE}.txt"
;;
*/html) run lynx -dump "$f" ;;
*/*json)
[ ! -f "${CACHE}.txt" ] && run jq -C <"$f" >"${CACHE}.txt"
text "${CACHE}.txt"
;;
*/*yaml)
# [ ! -f "${CACHE}.txt" ] && run yq -C <"$f" >"${CACHE}.txt"
# text "${CACHE}.txt"
text --language=yaml "$f"
;;
*/*lua) text "$f" ;;
*/epub+zip)
[ ! -f "$CACHE" ] && run epub-thumbnailer "$f" "$CACHE" 1024
image "$CACHE"
;;
image/svg+xml)
[ ! -f "${CACHE}.png" ] && run inkscape --convert-dpi-method=none -o "${CACHE}.png" --export-overwrite -D --export-png-color-mode=RGBA_16 "$f"
image "${CACHE}.png"
;;
*/pdf)
[ ! -f "${CACHE}.jpg" ] && run pdftoppm -jpeg -f 1 -singlefile "$f" "$CACHE"
image "${CACHE}.jpg"
;;
video/*) video ;;
image/*) image ;;
*text*) text "$f" ;;
*) return 1 ;;
esac
}
CACHE="${XDG_CACHE_HOME}/lf/$(stat --printf "%n\0%i\0%F\0%s\0%W\0%Y" -- "$(readlink -f "$1")" | sha256sum | awk '{print $1}')"
# Special cases
[ "$(dirname "$(realpath "$f")" | rev | cut -d/ -f1 | rev)" = "factscache" ] &&
[ -f "$(dirname "$(dirname "$(realpath "$f")")")/ansible.cfg" ] &&
jq -C <"$f" && 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"
mediainfo "$1" | head -n -2 | grep -v -e "Complete name" -e "General"

View File

@ -1,36 +0,0 @@
#!/bin/sh
if ! [ -f "$1" ]; then
exit 1
fi
cache="$XDG_CACHE_HOME/lf"
index="$cache/index.json"
file="$(realpath "$1")"
mkdir -p "$cache"
if [ -f "$index" ]; then
thumbnail="$(jq -r ". \"$file\"" <"$index")"
if [ "$thumbnail" != "null" ]; then
if [ ! -f "$cache/$thumbnail" ]; then
exit 1
fi
echo "$cache/$thumbnail"
exit 0
fi
fi
thumbnail="$(uuidgen).jpg"
if ! ffmpegthumbnailer -i "$file" -o "$cache/$thumbnail" -s 0 2>/dev/null; then
exit 1
fi
if [ ! -f "$index" ]; then
echo "{\"$file\": \"$thumbnail\"}" >"$index"
fi
json="$(jq -r --arg "$file" "$thumbnail" ". + {\"$file\": \"$thumbnail\"}" <"$index")"
echo "$json" >"$index"
echo "$cache/$thumbnail"