2019-06-20 08:45:24 +00:00
|
|
|
#!/usr/bin/env sh
|
2019-03-20 16:20:56 +00:00
|
|
|
|
2019-05-26 02:06:24 +00:00
|
|
|
# Description: Show diff of 2 directories or multiple files in vimdiff
|
2019-03-20 16:20:56 +00:00
|
|
|
#
|
2020-02-23 09:02:23 +00:00
|
|
|
# Note: 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
|
2019-06-20 13:08:45 +00:00
|
|
|
#
|
2019-06-20 08:45:24 +00:00
|
|
|
# Shell: POSIX compliant
|
2019-06-21 17:06:34 +00:00
|
|
|
# Authors: Arun Prakash Jana, ath3
|
2019-03-20 16:20:56 +00:00
|
|
|
|
2020-04-24 04:42:24 +00:00
|
|
|
selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
|
2019-05-26 02:06:24 +00:00
|
|
|
|
2021-05-14 12:03:28 +00:00
|
|
|
if type nvim >/dev/null 2>&1; then
|
2020-04-20 16:49:35 +00:00
|
|
|
diffcmd="nvim -d"
|
|
|
|
else
|
|
|
|
diffcmd="vimdiff +0"
|
|
|
|
fi
|
|
|
|
|
2020-02-23 09:02:23 +00:00
|
|
|
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"
|
2020-04-20 16:49:35 +00:00
|
|
|
$diffcmd "$dir1" "$dir2"
|
2020-02-23 09:02:23 +00:00
|
|
|
rm "$dir1" "$dir2"
|
|
|
|
}
|
|
|
|
|
2019-11-21 20:44:25 +00:00
|
|
|
if [ -s "$selection" ]; then
|
2019-06-20 08:45:24 +00:00
|
|
|
arr=$(tr '\0' '\n' < "$selection")
|
|
|
|
if [ "$(echo "$arr" | wc -l)" -gt 1 ]; then
|
2020-02-23 09:02:23 +00:00
|
|
|
f1="$(echo "$arr" | sed -n '1p')"
|
|
|
|
f2="$(echo "$arr" | sed -n '2p')"
|
|
|
|
if [ -d "$f1" ] && [ -d "$f2" ]; then
|
|
|
|
dirdiff "$f1" "$f2"
|
2019-06-20 08:45:24 +00:00
|
|
|
else
|
2019-08-13 19:38:57 +00:00
|
|
|
# If xargs supports the -o option, use it to get rid of:
|
|
|
|
# Vim: Warning: Input is not from a terminal
|
2019-11-20 19:01:39 +00:00
|
|
|
# xargs -0 -o vimdiff < $selection
|
2019-08-13 19:38:57 +00:00
|
|
|
|
2020-04-20 16:49:35 +00:00
|
|
|
eval xargs -0 "$diffcmd" < "$selection"
|
2019-06-20 08:45:24 +00:00
|
|
|
fi
|
2020-11-22 14:39:14 +00:00
|
|
|
elif [ -n "$1" ]; then
|
2020-02-23 09:02:23 +00:00
|
|
|
f1="$(echo "$arr" | sed -n '1p')"
|
|
|
|
if [ -d "$f1" ] && [ -d "$1" ]; then
|
|
|
|
dirdiff "$f1" "$1"
|
|
|
|
elif [ -f "$f1" ] && [ -f "$1" ]; then
|
2020-04-20 16:49:35 +00:00
|
|
|
$diffcmd "$f1" "$1"
|
2020-02-23 09:02:23 +00:00
|
|
|
else
|
|
|
|
echo "cannot compare file with directory"
|
|
|
|
fi
|
2019-06-03 13:10:29 +00:00
|
|
|
else
|
2019-06-20 08:45:24 +00:00
|
|
|
echo "needs at least 2 files or directories selected for comparison"
|
2019-06-03 13:10:29 +00:00
|
|
|
fi
|
2019-05-26 02:06:24 +00:00
|
|
|
fi
|
2021-03-27 11:37:58 +00:00
|
|
|
|
|
|
|
# Clear selection
|
|
|
|
if [ -p "$NNN_PIPE" ]; then
|
|
|
|
printf "-" > "$NNN_PIPE"
|
|
|
|
fi
|