aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/preview-tui
blob: 3e2249a03724ac5b727816ea00a5d9cd314c8789 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env sh

# Description: Text based file previewer
#
# Note: This plugin needs a "NNN_FIFO" to work.
#
# Dependencies: tmux (>=3.0) or xterm or $TERMINAL, less or $PAGER, file, tree
#
# Usage:
#   You need to set a NNN_FIFO path and set a key for the plugin,
#   then start `nnn`:
#
#     $ NNN_FIFO=/tmp/nnn.fifo nnn
#
#   Then in `nnn`, launch the `preview-tui` plugin.
#
#   If you provide the same NNN_FIFO to all nnn instances, there will be a
#   single common preview window. I you provide different FIFO path, they
#   will be independent.
#
#   Configure SPLIT to either "h" or "v" to set a 'h'orizontal split or a
#   'v'ertical split
#
# Shell: POSIX compliant
# Authors: Todd Yamakawa, Léo Villeveygoux, @Recidiviste

TERMINAL="${TERMINAL:-xterm}"
PAGER="${PAGER:-less}"
SPLIT=

lines=$(($(tput lines)-1))
cols=$(tput cols)

beginswith() {
    case $1 in "$2"*) true;; *) false;; esac;
}

preview_file () {
    kill "$(jobs -p)" 2>/dev/null
    clear

    encoding="$(file -b --mime-encoding "$1")"
    mimetype="$(file --dereference --brief --mime-type -- "$1")"

    ext="${1##*.}"
    if ! [ -z "$ext" ]; then
        ext="$(printf "%s" "${ext}" | tr '[:upper:]' '[:lower:]')"
    fi

    if [ -d "$1" ]; then
        # Print directory tree

        cd "$1" || return

        # we use a FIFO to access less PID
        tmpfifopath="${TMPDIR:-/tmp}/nnn-preview-tui-fifo.$$"
        mkfifo "$tmpfifopath" || return

        $PAGER < "$tmpfifopath" &

        (
            exec > "$tmpfifopath"
            tree&
        )

        rm "$tmpfifopath"
    elif beginswith "$mimetype" "image/" ; then
        viu "$1" | head -n "$lines"
    elif beginswith "$mimetype" "text/troff" ; then
        man -l "$1" &
    elif [ "$encoding" = "binary" ] ; then
        # Binary file: just print filetype info
        echo "-------- binary file --------"
        file -b "$1"
        echo "-------- stat --------"
        stat "$1"
    else
        # Text file:
        $PAGER "$1" &
    fi
}

if [ "$PREVIEW_MODE" ] ; then
    if [ ! -r "$NNN_FIFO" ] ; then
        echo "No FIFO available! (\$NNN_FIFO='$NNN_FIFO')" >&2
        read -r
        exit 1
    fi

    preview_file "$1"

    exec < "$NNN_FIFO"
    while read -r selection ; do
        preview_file "$selection"
    done
    exit 0
fi

if [ -e "${TMUX%%,*}" ] && [ "$(tmux -V | cut -c6)" -eq 3 ] ; then
    if [ -z "$SPLIT" ]; then
        if [ "$(( lines * 2 ))" -gt "$cols" ]; then
            SPLIT='v'
        else
            SPLIT='h'
        fi
    elif [ "$SPLIT" != "h" ] && [ "$SPLIT" != "v" ] ; then
        SPLIT='h'
    fi

    tmux split-window -e "NNN_FIFO=$NNN_FIFO" -e "PREVIEW_MODE=1" -d"$SPLIT" "$0" "$1"
else
    PREVIEW_MODE=1 $TERMINAL -e "$0" "$1" &
fi