aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/ndiff
blob: 3e9d8a462424d05a71cc910c9b85271430dc35ec (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
#!/usr/bin/env sh

# Description: Show diff of 2 directories or multiple files in vimdiff
#
# Note: vim may show the warning: 'Vim: Warning: Input is not from a terminal'
#       press 'Enter' to ignore and proceed.
#
# Shell: POSIX compliant
# Authors: Arun Prakash Jana, ath3

selection=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection

if [ -s $selection ]; then
    arr=$(tr '\0' '\n' < "$selection")
    if [ "$(echo "$arr" | wc -l)" -gt 1 ]; then
         f1="$(echo "$arr" | sed -n '1p')"
         f2="$(echo "$arr" | sed -n '2p')"
         if [ -d "$f1" ] && [ -d "$f2" ]; then
            dir1=$(mktemp "${TMPDIR:-/tmp}"/nnn-$(basename "$f1").XXXXXXXX)
            dir2=$(mktemp "${TMPDIR:-/tmp}"/nnn-$(basename "$f2").XXXXXXXX)
            ls -A1 "$f1" > "$dir1"
            ls -A1 "$f2" > "$dir2"
            vimdiff "$dir1" "$dir2"
            rm "$dir1" "$dir2"
        else
            cat $selection | xargs -0 vimdiff
        fi
    else
        echo "needs at least 2 files or directories selected for comparison"
    fi
fi