My build of nnn with minor changes
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

73 lignes
2.2 KiB

  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. clear
  29. lines=$(($(tput lines)-1))
  30. cols=$(tput cols)
  31. mime="$(file -b --mime-type "$1")"
  32. encoding="$(file -b --mime-encoding "$1")"
  33. if [ -d "$1" ]; then
  34. # Print directory tree
  35. # shellcheck disable=SC2015
  36. cd "$1" && \
  37. COLUMNS=$cols exa -G --colour=always 2>/dev/null || ls --color=alway
  38. elif [ "${mime%%/*}" = "image" ] ; then
  39. kitty +kitten icat --silent --transfer-mode=stream --stdin=no "$1"
  40. elif [ "$encoding" = "binary" ] ; then
  41. # Binary file: show file info
  42. printf -- "-------- \033[1;31mBinary file\033[0m --------\n"
  43. mediainfo "$1" 2>/dev/null || file -b "$1"
  44. else
  45. # Text file: print colored file content
  46. bat --terminal-width="$cols" --paging=never --decorations=always \
  47. --color=always "$1" 2>/dev/null || cat
  48. fi | head -n $lines
  49. }
  50. if [ "$PREVIEW_MODE" ] ; then
  51. if [ ! -r "$NNN_FIFO" ] ; then
  52. echo "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')" >&2
  53. read -r
  54. exit 1
  55. fi
  56. preview_file "$1"
  57. exec < "$NNN_FIFO"
  58. while read -r selection ; do
  59. preview_file "$selection"
  60. done
  61. exit 0
  62. fi
  63. kitty @ launch --no-response --title "nnn preview" --keep-focus \
  64. --cwd="$PWD" --env "NNN_FIFO=$NNN_FIFO" --env "PREVIEW_MODE=1" \
  65. "$0" "$1" > /dev/null