]> Freerunner's - dotfiles.git/commitdiff
lf: fix ueberzug preview
authorAndre Ramnitz <tux.rising@gmail.com>
Fri, 20 Sep 2024 06:33:25 +0000 (08:33 +0200)
committerAndre Ramnitz <tux.rising@gmail.com>
Fri, 20 Sep 2024 21:35:14 +0000 (23:35 +0200)
import scripts from upstream

config/dot-config/lf/clear_img.sh [deleted file]
config/dot-config/lf/draw_img.sh [deleted file]
config/dot-config/lf/lfrc
config/dot-config/lf/previewer.sh [deleted file]
config/dot-config/lf/scope-lf-wrapper.sh [deleted file]
config/dot-config/lf/scripts/cleaner [new file with mode: 0755]
config/dot-config/lf/scripts/lfub [new file with mode: 0755]
config/dot-config/lf/scripts/preview [new file with mode: 0755]
local/dot-local/bin/lfrun

diff --git a/config/dot-config/lf/clear_img.sh b/config/dot-config/lf/clear_img.sh
deleted file mode 100755 (executable)
index 0402ed3..0000000
+++ /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 (executable)
index ff61491..0000000
+++ /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
-
index 65e3e1167518224400035c034dd7c9b5c89ad7fd..162ac7906349744266141d099aad6ee0d8b58ce6 100644 (file)
@@ -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<space>
diff --git a/config/dot-config/lf/previewer.sh b/config/dot-config/lf/previewer.sh
deleted file mode 100755 (executable)
index 46741ab..0000000
+++ /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 (executable)
index dbb7d79..0000000
+++ /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 (executable)
index 0000000..0159d5a
--- /dev/null
@@ -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 (executable)
index 0000000..45864e5
--- /dev/null
@@ -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 (executable)
index 0000000..ab41c61
--- /dev/null
@@ -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
index 8952d5d0cb1d67021a467ffada4ef86e20ce9988..114ad12091a6b824c2e00cde5cb910b3193a19f3 100755 (executable)
@@ -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