scope 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/bin/sh
  2. # File preview handler for lf.
  3. set -C -f
  4. IFS="$(printf '%b_' '\n')"
  5. IFS="${IFS%_}"
  6. # Define the draw function using Kitty's native protocol
  7. draw() {
  8. # $1=path, $2=width, $3=height, $4=x, $5=y
  9. # Mapping: width(2) x height(3) @ x(4) x y(5)
  10. kitten icat --stdin no --transfer-mode memory --place "${2}x${3}@${4}x${5}" "$1" </dev/null >/dev/tty
  11. exit 1 # this prevents lf from caching the image (which Kitty doesn't need)
  12. }
  13. case "$(file --dereference --brief --mime-type -- "$1")" in
  14. image/*) draw "$1" "$2" "$3" "$4" "$5" ;;
  15. text/html) lynx -width="$4" -display_charset=utf-8 -dump "$1" ;;
  16. text/troff) man ./ "$1" | col -b ;;
  17. text/* | */xml | application/json) bat --terminal-width "$4" -f "$1" ;;
  18. application/zip) zipinfo -2htz "$1" ;;
  19. application/vnd*) pptx2text -v "$1" ;;
  20. #application/zip) atool --list -- "$1" ;;
  21. audio/* | application/octet-stream) mediainfo "$1" || exit 1 ;;
  22. video/*)
  23. CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/lf/thumb.$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | awk '{print $1}')"
  24. [ ! -f "$CACHE" ] && ffmpegthumbnailer -i "$1" -o "$CACHE" -s 0
  25. draw "$CACHE" "$2" "$3" "$4" "$5"
  26. ;;
  27. */pdf)
  28. CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/lf/thumb.$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | awk '{print $1}')"
  29. [ ! -f "$CACHE.jpg" ] && pdftoppm -jpeg -f 1 -singlefile "$1" "$CACHE"
  30. draw "$CACHE.jpg" "$2" "$3" "$4" "$5"
  31. ;;
  32. *opendocument*) odt2txt "$1" ;;
  33. application/pgp-encrypted) gpg -d -- "$1" ;;
  34. *)
  35. # Default text preview for other files
  36. bat --color=always --style=plain "$1" || cat "$1"
  37. ;;
  38. esac
  39. exit 0