Skip to content

Instantly share code, notes, and snippets.

@unhammer

unhammer/vddiff

Last active Dec 14, 2019
Embed
What would you like to do?
little wrapper around `vd --diff` to get diff's -U option, since vd doesn't have a "go to next difference" feature ( https://github.com/saulpw/visidata/issues/303 )
#!/bin/bash
# Usage:
# $ vddiff -U4 before after -ftsv
# You can toggle between the before/after sheets with Ctrl-^
# If you keyboard layout makes that impossible to type, add an
# alternative binding to your ~/.visidatarc, e.g.
# bindkey("ø", "prev-sheet")
set -e -u
d=$(mktemp -td vddiff.XXXXXXXXXXX)
trap 'rm -rf "${d}"' EXIT
declare -i U=3
OPTIND=1; while getopts "U:" opt; do
case "$opt" in
U) U=$OPTARG;;
*)
echo "Unknown option" >&2
exit 1
;;
esac
done
shift $((OPTIND-1))
before="$1"
after="$2"
shift 2 # send anything after the files to vd
test -e "${before}"
test -e "${after}"
skipheader () {
tail -n+2 "$@"
}
{
printf " "
head -1 "${before}"
diff -U"$U" \
<(skipheader "${before}") \
<(skipheader "${after}") \
| grep -ve ^@@ -e ^--- -e ^+++
} > "$d/diffout"
undiff () {
local discard="$1"
grep -v ^"${discard}" "$d/diffout" \
| sed 's/^.//'
}
undiff + >"$d/before"
undiff - >"$d/after"
vd "$@" --diff "$d/before" "$d/after"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment