]> Sergey Matveev's repositories - nnn.git/blob - plugins/diffs
Clear less'es screen
[nnn.git] / plugins / diffs
1 #!/usr/bin/env sh
2
3 # Description: Show diff of 2 directories or multiple files in vimdiff
4 #
5 # Notes:
6 #   1. vim may show the warning: 'Vim: Warning: Input is not from a terminal'
7 #      press 'Enter' to ignore and proceed.
8 #   2. if only one file is in selection, the hovered file is considered as the
9 #      second file to diff with
10 #
11 # Shell: POSIX compliant
12 # Authors: Arun Prakash Jana, ath3
13
14 selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
15
16 if type nvim >/dev/null 2>&1; then
17     diffcmd="nvim -d"
18 else
19     diffcmd="vimdiff +0"
20 fi
21
22 dirdiff() {
23     dir1=$(mktemp "${TMPDIR:-/tmp}"/nnn-"$(basename "$1")".XXXXXXXX)
24     dir2=$(mktemp "${TMPDIR:-/tmp}"/nnn-"$(basename "$2")".XXXXXXXX)
25     ls -A1 "$1" > "$dir1"
26     ls -A1 "$2" > "$dir2"
27     $diffcmd "$dir1" "$dir2"
28     rm "$dir1" "$dir2"
29 }
30
31 if [ -s "$selection" ]; then
32     arr=$(tr '\0' '\n' < "$selection")
33     if [ "$(echo "$arr" | wc -l)" -gt 1 ]; then
34         f1="$(echo "$arr" | sed -n '1p')"
35         f2="$(echo "$arr" | sed -n '2p')"
36         if [ -d "$f1" ] && [ -d "$f2" ]; then
37             dirdiff "$f1" "$f2"
38         else
39             # If xargs supports the -o option, use it to get rid of:
40             #     Vim: Warning: Input is not from a terminal
41             # xargs -0 -o vimdiff < $selection
42
43             eval xargs -0 "$diffcmd" < "$selection"
44         fi
45     elif [ -n "$1" ]; then
46         f1="$(echo "$arr" | sed -n '1p')"
47         if [ -d "$f1" ] && [ -d "$1" ]; then
48             dirdiff "$f1" "$1"
49         elif [ -f "$f1" ] && [ -f "$1" ]; then
50             $diffcmd "$f1" "$1"
51         else
52             echo "cannot compare file with directory"
53         fi
54     else
55         echo "needs at least 2 files or directories selected for comparison"
56     fi
57 fi
58
59 # Clear selection
60 if [ -p "$NNN_PIPE" ]; then
61     printf "-" > "$NNN_PIPE"
62 fi