My build of nnn with minor changes
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

preview-kitty 2.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env sh
  2. # Description: File previewer for kitty term using NNN_FIFO
  3. #
  4. # Dependencies:
  5. # - kitty (https://sw.kovidgoyal.net/kitty/) with allow_remote_control on
  6. # - file
  7. # - exa (https://github.com/ogham/exa) (fallback: ls)
  8. # - bat (https://github.com/sharkdp/bat) (fallback: cat)
  9. # - mediainfo (fallback: file)
  10. #
  11. # Usage:
  12. # This plugin only works in kitty (https://sw.kovidgoyal.net/kitty/),
  13. # and with kitty's 'allow_remote_control' option turned on.
  14. # You need to set a NNN_FIFO path and set a key for the plugin,
  15. # then start `nnn`:
  16. #
  17. # $ NNN_FIFO=/tmp/nnn.fifo nnn
  18. #
  19. # Then in `nnn`, launch the `preview-kitty` plugin.
  20. #
  21. # If you provide the same NNN_FIFO to all nnn instances, there will be a
  22. # single common preview window. I you provide different FIFO path, they
  23. # will be independent.
  24. #
  25. # Shell: POSIX compliant
  26. # Authors: Léo Villeveygoux
  27. preview_file () {
  28. kill %% 2>/dev/null
  29. clear
  30. lines=$(($(tput lines)-1))
  31. cols=$(tput cols)
  32. mime="$(file -b --mime-type "$1")"
  33. encoding="$(file -b --mime-encoding "$1")"
  34. if [ -d "$1" ]; then
  35. # Print directory tree
  36. # shellcheck disable=SC2015
  37. cd "$1" && \
  38. (COLUMNS=$cols exa -G --colour=always 2>/dev/null ||\
  39. ls --color=alway) | head -n $lines &
  40. # remove second clause to preview SVG files (but this is slow)
  41. elif [ "${mime%%/*}" = "image" ] && [ "$encoding" = "binary" ] ; then
  42. kitty +kitten icat --silent --transfer-mode=stream --stdin=no "$1" &
  43. elif [ "$encoding" = "binary" ] ; then
  44. # Binary file: show file info
  45. printf -- "-------- \033[1;31mBinary file\033[0m --------\n"
  46. (mediainfo "$1" 2>/dev/null || file -b "$1") | head -n $((lines - 1)) &
  47. else
  48. # Text file: print colored file content
  49. (bat --terminal-width="$cols" --paging=never --decorations=always \
  50. --color=always "$1" 2>/dev/null || cat) | head -n $lines &
  51. fi
  52. }
  53. if [ "$PREVIEW_MODE" ] ; then
  54. if [ ! -r "$NNN_FIFO" ] ; then
  55. echo "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')" >&2
  56. read -r
  57. exit 1
  58. fi
  59. preview_file "$1"
  60. exec < "$NNN_FIFO"
  61. while read -r selection ; do
  62. preview_file "$selection"
  63. done
  64. exit 0
  65. fi
  66. kitty @ launch --no-response --title "nnn preview" --keep-focus \
  67. --cwd="$PWD" --env "NNN_FIFO=$NNN_FIFO" --env "PREVIEW_MODE=1" \
  68. "$0" "$1" > /dev/null