diffpkg: allow to set column width for side-by-side view

The magic values `columns` and `auto` allow to set specific aspects,
with 'auto' as the default value:

- auto: Set width to the maximum line length of all input files
- columns: Set width to the shell defined $COLUMNS env var

Furthermore any number can be passed to set a static width.
This commit is contained in:
Levente Polyak 2022-08-22 00:42:56 +02:00
parent ba070f1ca9
commit 70a3041ff8
No known key found for this signature in database
GPG Key ID: FC1B547C8D8172C8
3 changed files with 57 additions and 11 deletions

View File

@ -51,6 +51,7 @@ _diffpkg_args=(
'(-u -U --unified)'{-u,-U,--unified}'[Output 3 lines of unified context]'
'(-y --side-by-side)'{-y,--side-by-side}'[Output in two columns]'
'--color=[Color output]:when:($_colors[*])'
'(-W --width=)'{-W,--width=}'[Output at most NUM print columns]:num:(auto columns)'
'(-v --verbose)'{-v,--verbose}'[Provide more detailed/unfiltered output]'
'(-h --help)'{-h,--help}'[Display usage]'
'*:packages:_devtools_completions_all_packages'

View File

@ -48,6 +48,10 @@ Output Options
*-y, --side-by-side*::
Output in two columns
*-W, --width*[='NUM']::
Output at most 'NUM' (default `'auto'`) print columns; 'NUM' can be `'auto'`, `'columns'` or a number.
`'auto'` will be resolved to the maximum line length of both files, guaranteeing the diff to be uncut.
Modes
-----

View File

@ -32,6 +32,8 @@ usage() {
Plain --color means --color='auto'
-u, -U, --unified Output 3 lines of unified context
-y, --side-by-side Output in two columns
-W, --width=NUM Output at most NUM (default 'auto') print columns
NUM can be 'auto', 'columns' or a number
MODES
-l, --list Activate content list diff mode (default)
@ -50,6 +52,7 @@ BUILDINFO=0
DIFFMODE=--side-by-side
DIFFCOLOR=--color
DIFFWIDTH=--width=auto
DIFFOPTIONS=(--expand-tabs)
# option checking
@ -100,6 +103,15 @@ while (( $# )); do
DIFFCOLOR="$1"
shift
;;
-W|--width)
(( $# <= 1 )) && die "missing argument for %s" "$1"
DIFFWIDTH="--width=$2"
shift 2
;;
--width=*)
DIFFWIDTH="$1"
shift
;;
--)
shift
break
@ -113,6 +125,7 @@ while (( $# )); do
esac
done
# Set options based on flags or magic values
if (( VERBOSE )); then
if [[ $DIFFMODE == --unified ]]; then
DIFFMODE="--unified=99999"
@ -120,6 +133,12 @@ if (( VERBOSE )); then
else
DIFFOPTIONS+=(--suppress-common-lines)
fi
if [[ $DIFFWIDTH == --width=columns ]]; then
DIFFWIDTH="--width=${COLUMNS:-130}"
fi
if [[ $DIFFWIDTH != --width=auto ]]; then
DIFFOPTIONS+=("${DIFFWIDTH}")
fi
DIFFOPTIONS+=("${DIFFMODE}" "${DIFFCOLOR}")
if ! (( DIFFOSCOPE || TARLIST || PKGINFO || BUILDINFO )); then
@ -156,6 +175,19 @@ tar_list() {
fi | sort
}
file_line_length() {
path="$1"
wc -L "${path}" | tail -n1 | sed -E 's/^ +//g' | cut -d' ' -f1
}
file_diff_columns() {
file1="$1"
file2="$2"
file1_length=$(file_line_length "$file1")
file2_length=$(file_line_length "$file2")
echo $(( file1_length + file2_length + 3 ))
}
diff_pkgs() {
local oldpkg newpkg
oldpkg=$(readlink -m "$1")
@ -167,24 +199,33 @@ diff_pkgs() {
DIFFOPTIONS+=(--label "${oldpkg}" --label "${newpkg}")
if (( TARLIST )); then
tar_list "$oldpkg" > "$TMPDIR/filelist-old"
tar_list "$newpkg" > "$TMPDIR/filelist"
diff "${DIFFOPTIONS[@]}" "$TMPDIR/filelist-old" "$TMPDIR/filelist"
tar_list "$oldpkg" > "$TMPDIR/old"
tar_list "$newpkg" > "$TMPDIR/new"
fi
if (( PKGINFO )); then
bsdtar xOqf "$oldpkg" .PKGINFO > "$TMPDIR/pkginfo-old"
bsdtar xOqf "$newpkg" .PKGINFO > "$TMPDIR/pkginfo"
diff "${DIFFOPTIONS[@]}" "$TMPDIR/pkginfo-old" "$TMPDIR/pkginfo"
bsdtar xOqf "$oldpkg" .PKGINFO > "$TMPDIR/old"
bsdtar xOqf "$newpkg" .PKGINFO > "$TMPDIR/new"
fi
if (( BUILDINFO )); then
bsdtar xOqf "$oldpkg" .BUILDINFO > "$TMPDIR/buildinfo-old"
bsdtar xOqf "$newpkg" .BUILDINFO > "$TMPDIR/buildinfo"
bsdtar xOqf "$oldpkg" .BUILDINFO > "$TMPDIR/old"
bsdtar xOqf "$newpkg" .BUILDINFO > "$TMPDIR/new"
fi
diff "${DIFFOPTIONS[@]}" "$TMPDIR/buildinfo-old" "$TMPDIR/buildinfo"
if (( TARLIST || PKGINFO || BUILDINFO )); then
# Resolve dynamic auto width one we know the content to diff
if [[ $DIFFWIDTH == --width=auto ]]; then
AUTOLENGTH=$(file_diff_columns "$TMPDIR/old" "$TMPDIR/new")
DIFFOPTIONS+=("--width=${AUTOLENGTH}")
fi
# Print a header for side-by-side view as it lacks labels
if [[ $DIFFMODE == --side-by-side ]]; then
printf -- "--- %s\n+++ %s\n" "${oldpkg}" "${newpkg}"
fi
diff "${DIFFOPTIONS[@]}" "$TMPDIR/old" "$TMPDIR/new"
fi
if (( DIFFOSCOPE )); then