From 947d24ac7377d2c9327d617e38a9f1566c6f0053 Mon Sep 17 00:00:00 2001
From: Luca Bilke <bilke@tralios.de>
Date: Mon, 12 Jun 2023 19:48:29 +0200
Subject: [PATCH] new lf preview to reduce dependency on kitty

---
 .config/lf/clean             |  3 +-
 .config/lf/preview           |  9 ++---
 .config/zsh/configs/bindings |  2 +-
 .config/zsh/configs/widgets  | 69 ++++++++++++++++++++----------------
 TODO.md                      |  1 +
 5 files changed, 48 insertions(+), 36 deletions(-)

diff --git a/.config/lf/clean b/.config/lf/clean
index 3dfe95d2..ca25ea9e 100755
--- a/.config/lf/clean
+++ b/.config/lf/clean
@@ -1,2 +1,3 @@
 #!/bin/sh
-kitty +kitten icat --clear --silent --stdin no --transfer-mode memory >/dev/tty
+# kitty +kitten icat --clear --silent --stdin no --transfer-mode memory >/dev/tty
+ueberzugpp cmd -s "$UB_SOCKET" -a remove -i PREVIEW
diff --git a/.config/lf/preview b/.config/lf/preview
index 3cbd546f..31682a2b 100755
--- a/.config/lf/preview
+++ b/.config/lf/preview
@@ -5,10 +5,11 @@ image() {
     h=$3
     x=$4
     y=$5
-    kitty +kitten icat --clear --stdin no --silent --transfer-mode memory --place "${w}x${h}@${x}x${y}" "$f" >/dev/tty && exit 1
-    chafa "$f" -f symbols -s "$((w-2))x$h" && exit 1
-    printf "%b" "\033[31mImage previewer failed\033[0m"
-    return 1
+    # kitty +kitten icat --clear --stdin no --silent --transfer-mode memory --place "${w}x${h}@${x}x${y}" "$f" >/dev/tty && exit 1
+    # chafa "$f" -f symbols -s "$((w-2))x$h" && exit 1
+    ueberzugpp cmd -s "$UB_SOCKET" -a add -i PREVIEW -x "$x" -y "$y" --max-width $(($2-1)) --max-height "$h" -f "$f" && exit 1
+    printf "%b" "\033[31mImage previewer failed\033[0m\n"
+    exit 1
 }
 
 video() {
diff --git a/.config/zsh/configs/bindings b/.config/zsh/configs/bindings
index 96a44863..b5e47580 100644
--- a/.config/zsh/configs/bindings
+++ b/.config/zsh/configs/bindings
@@ -9,7 +9,7 @@ bindkey -M menuselect 'l' vi-forward-char
 bindkey -M menuselect 'j' vi-down-line-or-history
 
 # Widgets
-bindkey '^e' _lfcd
+bindkey '^e' _lf
 bindkey '^g' lg
 bindkey -M vicmd v edit-command-line
 
diff --git a/.config/zsh/configs/widgets b/.config/zsh/configs/widgets
index cbc149ad..4c0ddb9b 100644
--- a/.config/zsh/configs/widgets
+++ b/.config/zsh/configs/widgets
@@ -1,35 +1,45 @@
 # vim:set ft=zsh
-# LF Change Dir
-function lfcd () {
-    tmp="$(mktemp)"
-    fid="$(mktemp)"
-    lf -command '$printf $id > '"$fid"'' -last-dir-path="$tmp" "$@"
-    id="$(cat "$fid")"
-    archivemount_dir="/tmp/__lf_archivemount_$id"
-    if [ -f "$archivemount_dir" ]; then
-        cat "$archivemount_dir" | \
-            while read -r line; do
-                sudo umount "$line"
-                rmdir "$line"
-            done
-        command rm -f "$archivemount_dir"
-    fi
-    if [ -f "$tmp" ]; then
-        dir="$(cat "$tmp")"
-        command rm -f "$tmp"
-        if [ -d "$dir" ]; then
-            if [ "$dir" != "$(pwd)" ]; then
-                cd "$dir"
-            fi
+
+function lfclean () {
+    exec 3>&- 
+    ueberzugpp cmd -s $UB_SOCKET -a exit
+    dir="$(cat "$lftmp")"
+    rm "$lftmp"
+    if [ -d "$dir" ]; then
+        if [ "$dir" != "$(pwd)" ]; then
+            cd "$dir"
         fi
     fi
+}
+
+# LF wrapper to allow for ueberzug previews
+function lfwrap () {
+    UB_PID=0
+    UB_SOCKET=""
+    lftmp="/tmp/lfdir.$(uuidgen)"
+    if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
+        command lf -last-dir-path="$lftmp" "$@"
+    else
+        [ ! -d "$XDG_CACHE_HOME/lf" ] && mkdir --parents "$XDG_CACHE_HOME/lf"
+        ubtmp="/tmp/ubpid.$(uuidgen)"
+        {
+            ueberzugpp layer --silent --no-stdin --use-escape-codes --pid-file "$ubtmp"
+            UB_PID=$(cat "$ubtmp")
+            rm "$ubtmp"
+            UB_SOCKET="/tmp/ueberzugpp-${UB_PID}.socket"
+            export UB_PID UB_SOCKET
+        }
+        trap lfclean HUP INT QUIT TERM PWR EXIT
+        command lf -last-dir-path="$lftmp" "$@" 3>&-
+    fi
     tput cuu1;tput el
 }
-function _lfcd () {
-  BUFFER="lfcd"
-  zle accept-line
+
+function _lf () {
+    BUFFER="lfwrap"
+    zle accept-line
 }
-zle -N _lfcd
+zle -N _lf
 
 # Cursor Shape
 function zle-keymap-select () {
@@ -50,9 +60,8 @@ zle -N edit-command-line
 
 # Lazygit
 function lg () {
-  [ ! -d "$(pwd)/.git" ] && [[ $(read -ek "?Not in a git repository. Create a new git repository? (y/n): ") =~ ^[Yy]$ ]] && git init
-  [ -d "$(pwd)/.git" ] && lazygit -p $(pwd)
-  zle reset-prompt
+    [ ! -d "$(pwd)/.git" ] && [[ $(read -erk "?Not in a git repository. Create a new git repository? (y/n): ") =~ ^[Yy]$ ]] && git init
+    [ -d "$(pwd)/.git" ] && lazygit -p "$(pwd)"
+    zle reset-prompt
 }
 zle -N lg{,}
-
diff --git a/TODO.md b/TODO.md
index b253c74b..db3e7d1e 100644
--- a/TODO.md
+++ b/TODO.md
@@ -3,6 +3,7 @@
 - [ ] Remove username from ssh completion
 - [ ] add a messaging function to dotsync to warn for breaking changes
 - [ ] check font names for void, adjust fontconfig
+- [ ] async ueberzugpp startup
 
 # FIX
 - [ ] sb-playerctl trailing dash when no artist