From 090cafe613f752b4af953baa652d25ea98bdaf0f Mon Sep 17 00:00:00 2001 From: Andre Ramnitz Date: Fri, 20 Sep 2024 08:33:25 +0200 Subject: [PATCH] lf: fix ueberzug preview import scripts from upstream --- config/dot-config/lf/clear_img.sh | 6 --- config/dot-config/lf/draw_img.sh | 8 --- config/dot-config/lf/lfrc | 5 +- config/dot-config/lf/previewer.sh | 64 ----------------------- config/dot-config/lf/scope-lf-wrapper.sh | 4 -- config/dot-config/lf/scripts/cleaner | 3 ++ config/dot-config/lf/scripts/lfub | 34 +++++++++++++ config/dot-config/lf/scripts/preview | 65 ++++++++++++++++++++++++ local/dot-local/bin/lfrun | 49 +++++++++++------- 9 files changed, 135 insertions(+), 103 deletions(-) delete mode 100755 config/dot-config/lf/clear_img.sh delete mode 100755 config/dot-config/lf/draw_img.sh delete mode 100755 config/dot-config/lf/previewer.sh delete mode 100755 config/dot-config/lf/scope-lf-wrapper.sh create mode 100755 config/dot-config/lf/scripts/cleaner create mode 100755 config/dot-config/lf/scripts/lfub create mode 100755 config/dot-config/lf/scripts/preview diff --git a/config/dot-config/lf/clear_img.sh b/config/dot-config/lf/clear_img.sh deleted file mode 100755 index 0402ed3a..00000000 --- a/config/dot-config/lf/clear_img.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/sh - -if [ -n "$FIFO_UEBERZUG" ]; then - printf '{"action": "remove", "identifier": "preview"}\n' >"$FIFO_UEBERZUG" -fi - diff --git a/config/dot-config/lf/draw_img.sh b/config/dot-config/lf/draw_img.sh deleted file mode 100755 index ff61491b..00000000 --- a/config/dot-config/lf/draw_img.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh - -if [ -n "$FIFO_UEBERZUG" ]; then - path="$(printf '%s' "$1" | sed 's/\\/\\\\/g;s/"/\\"/g')" - printf '{"action": "add", "identifier": "preview", "x": %d, "y": %d, "width": %d, "height": %d, "scaler": "contain", "scaling_position_x": 0.5, "scaling_position_y": 0.5, "path": "%s"}\n' \ - "$4" "$5" "$2" "$3" "$1" >"$FIFO_UEBERZUG" -fi - diff --git a/config/dot-config/lf/lfrc b/config/dot-config/lf/lfrc index 65e3e116..162ac790 100644 --- a/config/dot-config/lf/lfrc +++ b/config/dot-config/lf/lfrc @@ -8,8 +8,8 @@ set icons set info size set sortby name -set previewer ~/.config/lf/previewer.sh -set cleaner ~/.config/lf/clear_img.sh +set previewer ~/.config/lf/scripts/preview +set cleaner ~/.config/lf/scripts/cleaner # interpreter for shell commands set shell sh @@ -395,6 +395,7 @@ map gM cd /mnt map ge bottom map gs cd ~/Bilder/Screenshots map go :fzf_jump +map gw cd ~/Bilder/Wallpapers # o = open map oo push :open-with-cli diff --git a/config/dot-config/lf/previewer.sh b/config/dot-config/lf/previewer.sh deleted file mode 100755 index 46741ab2..00000000 --- a/config/dot-config/lf/previewer.sh +++ /dev/null @@ -1,64 +0,0 @@ -#!/bin/sh - -draw() { - ~/dotfiles/config/lf/draw_img.sh "$@" - exit 1 -} - -hash() { - printf '%s/.cache/lf/%s' "$HOME" \ - "$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | awk '{print $1}')" -} - -cache() { - if [ -f "$1" ]; then - draw "$@" - fi -} - -file="$1" -shift - -if [ -n "$FIFO_UEBERZUG" ]; then - case $(file -Lb --mime-type -- "$file") in - image/*) - orientation="$(identify -format '%[EXIF:Orientation]\n' -- "$file")" - if [ -n "$orientation" ] && [ "$orientation" != 1 ]; then - cache="$(hash "$file").jpg" - cache "$cache" "$@" - convert -- "$file" -auto-orient "$cache" - draw "$cache" "$@" - else - draw "$file" "$@" - fi - ;; - video/*) - cache="$(hash "$file").jpg" - cache "$cache" "$@" - ffmpegthumbnailer -i "$file" -o "$cache" -s 0 - draw "$cache" "$@" - ;; - application/pdf) - thumbnail="$LF_TEMPDIR/thumbnail.png" - gs -o "$thumbnail" -sDEVICE=pngalpha -dLastPage=1 "$file" >/dev/null - preview "$thumbnail" "$@" - ;; - application/tar*) - tar tf "$file" - ;; - application/zip) - unzip -l "$file" - ;; - application/7z) - 7z l "$file" - ;; - *) - bat -f --style plain "$file" - ;; - esac -fi - -# shellcheck disable=SC2154 -file -Lb -- "$1" | fold -s -w "$width" -exit 0 - diff --git a/config/dot-config/lf/scope-lf-wrapper.sh b/config/dot-config/lf/scope-lf-wrapper.sh deleted file mode 100755 index dbb7d794..00000000 --- a/config/dot-config/lf/scope-lf-wrapper.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh - -"$HOME/.config/ranger/scope.sh" "${1}" "${2}" "${3}" "" "" || true - diff --git a/config/dot-config/lf/scripts/cleaner b/config/dot-config/lf/scripts/cleaner new file mode 100755 index 00000000..0159d5af --- /dev/null +++ b/config/dot-config/lf/scripts/cleaner @@ -0,0 +1,3 @@ +#!/bin/sh + +ueberzugpp cmd -s $UB_SOCKET -a remove -i PREVIEW diff --git a/config/dot-config/lf/scripts/lfub b/config/dot-config/lf/scripts/lfub new file mode 100755 index 00000000..45864e52 --- /dev/null +++ b/config/dot-config/lf/scripts/lfub @@ -0,0 +1,34 @@ +#!/bin/sh + +# This is a wrapper script for lf that allows it to create image previews with +# ueberzug. This works in concert with the lf configuration file and the +# lf-cleaner script. + +set -e + +UB_PID=0 +UB_SOCKET="" + +case "$(uname -a)" in + *Darwin*) UEBERZUG_TMP_DIR="$TMPDIR" ;; + *) UEBERZUG_TMP_DIR="/tmp" ;; +esac + +cleanup() { + exec 3>&- + ueberzugpp cmd -s "$UB_SOCKET" -a exit +} + +if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then + lf "$@" +else + [ ! -d "$HOME/.cache/lf" ] && mkdir -p "$HOME/.cache/lf" + UB_PID_FILE="$UEBERZUG_TMP_DIR/.$(uuidgen)" + ueberzugpp layer --silent --no-stdin --use-escape-codes --pid-file "$UB_PID_FILE" + UB_PID=$(cat "$UB_PID_FILE") + rm "$UB_PID_FILE" + UB_SOCKET="$UEBERZUG_TMP_DIR/ueberzugpp-${UB_PID}.socket" + export UB_PID UB_SOCKET + trap cleanup HUP INT QUIT TERM EXIT + lf "$@" 3>&- +fi diff --git a/config/dot-config/lf/scripts/preview b/config/dot-config/lf/scripts/preview new file mode 100755 index 00000000..ab41c61a --- /dev/null +++ b/config/dot-config/lf/scripts/preview @@ -0,0 +1,65 @@ +#!/bin/sh + +image() { + FILE_PATH="$1" + X=$4 + Y=$5 + MW=$(($2 - 1)) + MH=$3 + ueberzugpp cmd -s "$UB_SOCKET" -a add -i PREVIEW -x "$X" -y "$Y" --max-width "$MW" --max-height "$MH" -f "$FILE_PATH" + exit 1 +} + +batorcat() { + file="$1" + shift + if command -v bat >/dev/null 2>&1; then + bat --color=always --style=plain --pager=never "$file" "$@" + else + cat "$file" + fi +} + +CACHE="$HOME/.cache/lf/thumbnail.$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | awk '{print $1}'))" + +case "$(printf "%s\n" "$(readlink -f "$1")" | tr '[:upper:]' '[:lower:]')" in + *.tgz | *.tar.gz) tar tzf "$1" ;; + *.tar.bz2 | *.tbz2) tar tjf "$1" ;; + *.tar.txz | *.txz) xz --list "$1" ;; + *.tar) tar tf "$1" ;; + *.zip | *.jar | *.war | *.ear | *.oxt) unzip -l "$1" ;; + *.rar) unrar l "$1" ;; + *.7z) 7z l "$1" ;; + *.[1-8]) man "$1" | col -b ;; + *.o) nm "$1" ;; + *.torrent) transmission-show "$1" ;; + *.iso) iso-info --no-header -l "$1" ;; + *.odt | *.ods | *.odp | *.sxw) odt2txt "$1" ;; + *.doc) catdoc "$1" ;; + *.docx) docx2txt "$1" - ;; + *.xls | *.xlsx) + ssconvert --export-type=Gnumeric_stf:stf_csv "$1" "fd://1" | batorcat --language=csv + ;; + *.wav | *.mp3 | *.flac | *.m4a | *.wma | *.ape | *.ac3 | *.og[agx] | *.spx | *.opus | *.as[fx] | *.mka) + exiftool "$1" + ;; + *.pdf) + [ ! -f "${CACHE}.jpg" ] && pdftoppm -jpeg -f 1 -singlefile "$1" "$CACHE" + image "${CACHE}.jpg" "$2" "$3" "$4" "$5" + ;; + *.avi | *.mp4 | *.wmv | *.dat | *.3gp | *.ogv | *.mkv | *.mpg | *.mpeg | *.vob | *.fl[icv] | *.m2v | *.mov | *.webm | *.ts | *.mts | *.m4v | *.r[am] | *.qt | *.divx) + [ ! -f "${CACHE}.jpg" ] && ffmpegthumbnailer -i "$1" -o "${CACHE}.jpg" -s 0 -q 5 + image "${CACHE}.jpg" "$2" "$3" "$4" "$5" + ;; + *.bmp | *.jpg | *.jpeg | *.png | *.xpm | *.webp | *.gif | *.jfif) + image "$1" "$2" "$3" "$4" "$5" + ;; + *.svg) + [ ! -f "${CACHE}.jpg" ] && convert "$1" "${CACHE}.jpg" + image "${CACHE}.jpg" "$2" "$3" "$4" "$5" + ;; + *) + batorcat "$1" + ;; +esac +exit 0 diff --git a/local/dot-local/bin/lfrun b/local/dot-local/bin/lfrun index 8952d5d0..114ad120 100755 --- a/local/dot-local/bin/lfrun +++ b/local/dot-local/bin/lfrun @@ -1,30 +1,41 @@ #!/bin/sh + +# This is a wrapper script for lf that allows it to create image previews with +# ueberzug. This works in concert with the lf configuration file and the +# lf-cleaner script. + set -e -BROWSER="$(printf "qutebrowser\n--untrusted-args\n%u")" -EDITOR="$(printf "kks\nedit")" -VISUAL="$(printf "kks\nedit")" -PAGER="$(printf "bat\n--style\nauto")" +BROWSER="$(printf "qutebrowser\n--untrusted-args\n")" +EDITOR="$(printf "kks\nedit\n")" +VISUAL="$(printf "kks\nedit\n")" +PAGER="$(printf "bat\n--style\nauto\n")" -if [ -n "$DISPLAY" ]; then - export FIFO_UEBERZUG="${TMPDIR:-/tmp}/lf-ueberzug-$$" - cleanup() { - exec 3>&- - rm "$FIFO_UEBERZUG" - } +UB_PID=0 +UB_SOCKET="" - mkfifo "$FIFO_UEBERZUG" - ueberzugpp layer -s <"$FIFO_UEBERZUG" -o sixel & - exec 3>"$FIFO_UEBERZUG" - trap cleanup EXIT +case "$(uname -a)" in + *Darwin*) UEBERZUG_TMP_DIR="$TMPDIR" ;; + *) UEBERZUG_TMP_DIR="/tmp" ;; +esac - if ! [ -d "$HOME/.cache/lf" ]; then - mkdir -p "$HOME/.cache/lf" - fi +cleanup() { + exec 3>&- + ueberzugpp cmd -s "$UB_SOCKET" -a exit +} - /usr/bin/lf "$@" 3>&- +if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then + lf "$@" else - exec /usr/bin/lf "$@" + [ ! -d "$HOME/.cache/lf" ] && mkdir -p "$HOME/.cache/lf" + UB_PID_FILE="$UEBERZUG_TMP_DIR/.$(uuidgen)" + ueberzugpp layer --silent --no-stdin --use-escape-codes --pid-file "$UB_PID_FILE" + UB_PID=$(cat "$UB_PID_FILE") + rm "$UB_PID_FILE" + UB_SOCKET="$UEBERZUG_TMP_DIR/ueberzugpp-${UB_PID}.socket" + export UB_PID UB_SOCKET + trap cleanup HUP INT QUIT TERM EXIT + lf "$@" 3>&- fi -- 2.51.2