nnn

mirror of https://github.com/jarun/nnn
Log | Files | Refs | README | LICENSE

.npreview (4512B)


      1 #!/usr/bin/env sh
      2 
      3 # Description: Preview plugin for nnn's built-in preview pane
      4 # Auto-detected from plugins dir
      5 # Usage: .npreview <filepath> <width> <height>
      6 # All dependencies optional — graceful fallback to head/file/strings
      7 
      8 set -euf
      9 
     10 F="$1"
     11 W="${2:-80}"
     12 H="${3:-25}"
     13 ext="${F##*.}"
     14 ext="$(printf "%s" "$ext" | tr '[:upper:]' '[:lower:]')"
     15 
     16 has() { type "$1" >/dev/null 2>&1; }
     17 hd() { head -n "$H"; }
     18 
     19 # --- Handlers ---
     20 
     21 show_dir() {
     22     has tree && { tree -L 1 -n --dirsfirst --noreport "$F" | hd; return; }
     23     find "$F" -maxdepth 1 -print 2>/dev/null | hd
     24 }
     25 
     26 show_text() {
     27     has bat && { bat --style=plain --color=never --paging=never --line-range=":${H}" -- "$F" 2>/dev/null; return; }
     28     has batcat && { batcat --style=plain --color=never --paging=never --line-range=":${H}" -- "$F" 2>/dev/null; return; }
     29     head -n "$H" "$F"
     30 }
     31 
     32 show_pdf() {
     33     has pdftotext && { pdftotext -l 1 -nopgbrk -q -- "$F" - 2>/dev/null | hd; return; }
     34     has mutool && { mutool draw -F txt -i -- "$F" 1 2>/dev/null | hd; return; }
     35     show_meta
     36 }
     37 
     38 show_archive() {
     39     case "$ext" in
     40         rar) has unrar && { unrar lt -p- -- "$F" 2>/dev/null | hd; return; } ;;
     41         7z)  has 7z && { 7z l -p -- "$F" 2>/dev/null | hd; return; } ;;
     42     esac
     43     has atool && { atool --list -- "$F" 2>/dev/null | hd; return; }
     44     has bsdtar && { bsdtar --list --file "$F" 2>/dev/null | hd; return; }
     45     has tar && { tar tf "$F" 2>/dev/null | hd; return; }
     46     has unzip && [ "$ext" = "zip" ] && { unzip -l "$F" 2>/dev/null | hd; return; }
     47     printf "[archive %s]\n" "$(du -h "$F" 2>/dev/null | cut -f1)"
     48 }
     49 
     50 show_image() {
     51     has img2txt && { img2txt --gamma=0.6 --width="$W" -- "$F" 2>/dev/null | hd; return; }
     52     show_meta
     53 }
     54 
     55 show_meta() {
     56     has exiftool && { exiftool "$F" 2>/dev/null | hd; return; }
     57     has mediainfo && { mediainfo "$F" 2>/dev/null | hd; return; }
     58     has file && { file -b "$F"; return; }
     59     printf "[no metadata tool]\n"
     60 }
     61 
     62 show_json() {
     63     has jq && { head -c 65536 "$F" | jq --monochrome-output . 2>/dev/null | hd; return; }
     64     has python3 && { head -c 65536 "$F" | python3 -m json.tool 2>/dev/null | hd; return; }
     65     show_text
     66 }
     67 
     68 show_md() {
     69     has glow && { glow -s notty "$F" 2>/dev/null | hd; return; }
     70     has lowdown && { lowdown -Tterm "$F" 2>/dev/null | hd; return; }
     71     show_text
     72 }
     73 
     74 show_html() {
     75     has w3m && { w3m -dump "$F" 2>/dev/null | hd; return; }
     76     has lynx && { lynx -dump -- "$F" 2>/dev/null | hd; return; }
     77     show_text
     78 }
     79 
     80 show_elf() {
     81     has readelf && { readelf -h "$F" 2>/dev/null | hd; return; }
     82     has objdump && { objdump -f "$F" 2>/dev/null | hd; return; }
     83     has file && file -b "$F"
     84 }
     85 
     86 show_doc() {
     87     has odt2txt && { odt2txt "$F" 2>/dev/null | hd; return; }
     88     show_meta
     89 }
     90 
     91 show_bin() {
     92     has strings && { printf "[binary]\n"; strings "$F" 2>/dev/null | hd; return; }
     93     printf "[binary %s]\n" "$(du -h "$F" 2>/dev/null | cut -f1)"
     94 }
     95 
     96 # --- Directory ---
     97 [ -d "$F" ] && { show_dir; exit 0; }
     98 
     99 # --- Dispatch by extension ---
    100 case "$ext" in
    101     a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\
    102     rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tz|tzo|war|xpi|xz|z|zip|rar|7z|zst)
    103         show_archive; exit 0 ;;
    104     pdf) show_pdf; exit 0 ;;
    105     md|mkd|markdown) show_md; exit 0 ;;
    106     htm|html|xhtml) show_html; exit 0 ;;
    107     json) show_json; exit 0 ;;
    108     odt|ods|odp|sxw) show_doc; exit 0 ;;
    109     torrent) has transmission-show && { transmission-show -- "$F" 2>/dev/null | hd; exit 0; } ;;
    110     sqlite|sqlite3|db) has sqlite3 && { printf ".tables\n" | sqlite3 "$F" 2>/dev/null | hd; exit 0; } ;;
    111     o|so|dylib) show_elf; exit 0 ;;
    112     c|cc|cpp|h|hpp|rs|go|py|rb|js|ts|java|sh|bash|zsh|pl|lua|vim|el|\
    113     css|scss|yaml|yml|toml|ini|conf|xml|sql|diff|patch|awk|sed|make)
    114         show_text; exit 0 ;;
    115 esac
    116 
    117 # --- Dispatch by mime type ---
    118 has file || { show_text; exit 0; }
    119 mime="$(file -bL --mime-type -- "$F" 2>/dev/null)"
    120 
    121 case "$mime" in
    122     text/*|*/xml) show_text ;;
    123     application/json) show_json ;;
    124     application/pdf) show_pdf ;;
    125     application/zip|application/x-tar|application/x-rar*|\
    126     application/x-7z*|application/x-bzip*|application/x-xz|\
    127     application/x-compress*|application/gzip|application/zstd)
    128         show_archive ;;
    129     image/*) show_image ;;
    130     audio/*|video/*) show_meta ;;
    131     application/x-executable|application/x-sharedlib|application/x-pie-executable|\
    132     application/x-object) show_elf ;;
    133     application/vnd.openxmlformats*|application/vnd.oasis.opendocument*) show_doc ;;
    134     *) show_bin ;;
    135 esac