File: diffs

package info (click to toggle)
nnn 5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,184 kB
  • sloc: ansic: 11,902; sh: 3,585; makefile: 512; cpp: 80; python: 31; csh: 2
file content (62 lines) | stat: -rwxr-xr-x 1,829 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env sh

# Description: Show diff of 2 directories or multiple files in vimdiff
#
# Notes:
#   1. vim may show the warning: 'Vim: Warning: Input is not from a terminal'
#      press 'Enter' to ignore and proceed.
#   2. if only one file is in selection, the hovered file is considered as the
#      second file to diff with
#
# Shell: POSIX compliant
# Authors: Arun Prakash Jana, ath3

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

if type nvim >/dev/null 2>&1; then
    diffcmd="nvim -d"
else
    diffcmd="vimdiff +0"
fi

dirdiff() {
    dir1=$(mktemp "${TMPDIR:-/tmp}"/nnn-"$(basename "$1")".XXXXXXXX)
    dir2=$(mktemp "${TMPDIR:-/tmp}"/nnn-"$(basename "$2")".XXXXXXXX)
    ls -A1 "$1" > "$dir1"
    ls -A1 "$2" > "$dir2"
    $diffcmd "$dir1" "$dir2"
    rm -- "$dir1" "$dir2"
}

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
            dirdiff "$f1" "$f2"
        else
            # If xargs supports the -o option, use it to get rid of:
            #     Vim: Warning: Input is not from a terminal
            # xargs -0 -o vimdiff < $selection

            eval xargs -0 "$diffcmd" < "$selection"
        fi
    elif [ -n "$1" ]; then
        f1="$(echo "$arr" | sed -n '1p')"
        if [ -d "$f1" ] && [ -d "$1" ]; then
            dirdiff "$f1" "$1"
        elif [ -f "$f1" ] && [ -f "$1" ]; then
            $diffcmd "$f1" "$1"
        else
            echo "cannot compare file with directory"
        fi
    else
        echo "needs at least 2 files or directories selected for comparison"
    fi
fi

# Clear selection
if [ -p "$NNN_PIPE" ]; then
    printf "-" > "$NNN_PIPE"
fi