diff options
| author | Arpit Chakladar <arpitchakladar@proton.me> | 2026-04-22 23:10:09 +0530 |
|---|---|---|
| committer | Arpit Chakladar <arpitchakladar@proton.me> | 2026-04-22 23:10:09 +0530 |
| commit | c1b8da3f4618185345fb1b5bf4c6194906f6a2e2 (patch) | |
| tree | 8a8f951b0a63a7bd6af7792640564db561d0962f | |
| parent | 7e348c052967c536dad629b528a19214cd7ae119 (diff) | |
| download | home-manager-config-c1b8da3f4618185345fb1b5bf4c6194906f6a2e2.tar.gz home-manager-config-c1b8da3f4618185345fb1b5bf4c6194906f6a2e2.zip | |
successfully showing the image previews for lf
| -rw-r--r-- | modules/scripts/default.nix | 15 | ||||
| -rw-r--r-- | modules/scripts/file-preview-clean.sh | 1 | ||||
| -rw-r--r-- | modules/scripts/file-preview.sh | 131 | ||||
| -rw-r--r-- | modules/tools/lf/default.nix | 7 |
4 files changed, 149 insertions, 5 deletions
diff --git a/modules/scripts/default.nix b/modules/scripts/default.nix index a16cb8d..0ad8e0f 100644 --- a/modules/scripts/default.nix +++ b/modules/scripts/default.nix @@ -27,6 +27,16 @@ let scripts = { deep-clean = mkScript "deep-clean" ./deep-clean.sh { }; + file-preview = lib.mkIf config.tools.fzf.enable ( + mkScript "file-preview" ./file-preview.sh { + } + ); + + file-preview-clean = lib.mkIf config.tools.fzf.enable ( + mkScript "file-preview-clean" ./file-preview-clean.sh { + } + ); + fzf-launcher = lib.mkIf config.tools.fzf.enable ( mkScript "fzf-launcher" ./fzf-launcher.sh { FZF = lib.getExe pkgs.fzf; @@ -90,6 +100,9 @@ in config = { scripts = scripts; - home.packages = lib.filter (x: x != null) (lib.mapAttrsToList (_: v: lib.mkIf true v) scripts); + home.packages = [ + pkgs.file + ] + ++ (lib.filter (x: x != null) (lib.mapAttrsToList (_: v: lib.mkIf true v) scripts)); }; } diff --git a/modules/scripts/file-preview-clean.sh b/modules/scripts/file-preview-clean.sh new file mode 100644 index 0000000..243d937 --- /dev/null +++ b/modules/scripts/file-preview-clean.sh @@ -0,0 +1 @@ +exec kitten icat --clear --stdin no --transfer-mode memory </dev/null >/dev/tty diff --git a/modules/scripts/file-preview.sh b/modules/scripts/file-preview.sh new file mode 100644 index 0000000..c0996ba --- /dev/null +++ b/modules/scripts/file-preview.sh @@ -0,0 +1,131 @@ +# Timeout protection (prevents hanging) +TIMEOUT=5 +if [ -z "$LF_PREVIEW_RUNNING" ]; then + export LF_PREVIEW_RUNNING=1 + exec timeout "$TIMEOUT" "$0" "$@" +fi + +# Parameters from lf +FILE="$1" +WIDTH="$2" +HEIGHT="$3" +X="$4" +Y="$5" + +# Temp dir +TMP_DIR="/tmp/lf-preview-$(id -u)" +mkdir -p "$TMP_DIR" +TMP_IMG="$TMP_DIR/pv-$(basename "$FILE" | tr -c '[:alnum:]' '_').png" + +# Render image via kitty +render_image() { + kitten icat --stdin no --transfer-mode memory --place "${w}x${h}@${x}x${y}" "$1" </dev/null >/dev/tty + exit 1 +} + +file="$1" +w="$2" +h="$3" +x="$4" +y="$5" + +# Ensure file exists +[ ! -r "$FILE" ] && exit 0 + +MIMETYPE=$(file --dereference --brief --mime-type "$FILE") + +echo $FILE > /tmp/testing-my-script.txt +echo $MIMETYPE >> /tmp/testing-my-script.txt + +case "$MIMETYPE" in + # --- IMAGES --- + image/svg+xml) + rsvg-convert "$FILE" -o "$TMP_IMG" >/dev/null 2>&1 && render_image "$TMP_IMG" + ;; + image/*) + render_image "$FILE" + ;; + + # --- VIDEOS --- + video/*) + if command -v ffmpegthumbnailer >/dev/null; then + ffmpegthumbnailer -i "$FILE" -o "$TMP_IMG" -s 0 -q 5 >/dev/null 2>&1 + else + ffmpeg -y -i "$FILE" -ss 00:00:01 -vframes 1 "$TMP_IMG" >/dev/null 2>&1 + fi + [ -f "$TMP_IMG" ] && render_image "$TMP_IMG" + ;; + + # --- AUDIO --- + audio/*) + ffmpeg -y -i "$FILE" \ + -filter_complex "showwavespic=s=${WIDTH}x${HEIGHT}:colors=white" \ + -frames:v 1 "$TMP_IMG" >/dev/null 2>&1 + + ffprobe -hide_banner -v quiet -show_format -show_streams "$FILE" + + [ -f "$TMP_IMG" ] && render_image "$TMP_IMG" + exit 0 + ;; + + # --- PDF --- + application/pdf) + pdftoppm -f 1 -l 1 -png -singlefile "$FILE" "${TMP_IMG%.png}" >/dev/null 2>&1 + [ -f "$TMP_IMG" ] && render_image "$TMP_IMG" + ;; + + # --- EPUB / MOBI --- + application/epub+zip|application/x-mobipocket-ebook) + unzip -p "$FILE" "*/cover*.jpg" > "$TMP_IMG" 2>/dev/null + [ -s "$TMP_IMG" ] && render_image "$TMP_IMG" + ;; + + # --- OFFICE --- + application/vnd.openxmlformats-officedocument.*|application/msword|application/vnd.oasis.opendocument.*) + libreoffice --headless --convert-to pdf --outdir "$TMP_DIR" "$FILE" >/dev/null 2>&1 + PDF_VER="${TMP_DIR}/$(basename "${FILE%.*}").pdf" + if [ -f "$PDF_VER" ]; then + pdftoppm -f 1 -l 1 -png -singlefile "$PDF_VER" "${TMP_IMG%.png}" >/dev/null 2>&1 + render_image "$TMP_IMG" + fi + ;; + + # --- ARCHIVES --- + application/zip|application/x-7z-compressed|application/x-rar|application/x-tar|application/x-gzip) + 7z l "$FILE" 2>/dev/null || bsdtar -tf "$FILE" + exit 0 + ;; + + # --- TEXT / CODE --- + text/*|application/json|application/javascript|application/xml) + if command -v nvim >/dev/null; then + nvim --headless \ + +"set nomore" \ + +"set nowrap" \ + +"syntax on" \ + +"set number" \ + +"0,200print" \ + +q "$FILE" 2>/dev/null + else + cat "$FILE" + fi + exit 0 + ;; + + # --- FONTS --- + font/*|application/vnd.ms-fontobject|application/x-font-ttf) + fc-scan "$FILE" + exit 0 + ;; + + # --- FALLBACK --- + *) + file --dereference --brief "$FILE" + echo "------------------------------------------------" + hexdump -C "$FILE" | head -n 100 + exit 0 + ;; +esac + +# Always exit cleanly +exit 0 diff --git a/modules/tools/lf/default.nix b/modules/tools/lf/default.nix index 1b61fd8..be6fc2c 100644 --- a/modules/tools/lf/default.nix +++ b/modules/tools/lf/default.nix @@ -35,16 +35,15 @@ info = "size"; sortby = "natural"; dirfirst = true; + + cleaner = lib.getExe config.scripts.file-preview-clean; }; previewer = { - source = lib.getExe' pkgs.ctpv "ctpv"; + source = lib.getExe config.scripts.file-preview; }; extraConfig = '' - &${lib.getExe' pkgs.ctpv "ctpv"} -s $id - cmd on-quit %${lib.getExe' pkgs.ctpv "ctpv"} -e $id - set cleaner ${lib.getExe' pkgs.ctpv "ctpvclear"} ${builtins.readFile ./lfrc} ''; }; |
