diff options
author | Levente Polyak <anthraxx@archlinux.org> | 2022-08-22 00:42:56 +0200 |
---|---|---|
committer | Levente Polyak <anthraxx@archlinux.org> | 2022-08-31 02:26:47 +0200 |
commit | 70a3041ff8b1cb85f14dc565d225b3f5ffd8b1d8 (patch) | |
tree | 4b9057d226c1ad05c99a127bbc12444d414db2e1 /src | |
parent | ba070f1ca9b0dd60567a61c0c3cdb287165246be (diff) | |
download | devtools-70a3041ff8b1cb85f14dc565d225b3f5ffd8b1d8.tar.xz |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/diffpkg.in | 63 |
1 files changed, 52 insertions, 11 deletions
diff --git a/src/diffpkg.in b/src/diffpkg.in index ce8b030..baeb546 100644 --- a/src/diffpkg.in +++ b/src/diffpkg.in @@ -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 + + 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/buildinfo-old" "$TMPDIR/buildinfo" + diff "${DIFFOPTIONS[@]}" "$TMPDIR/old" "$TMPDIR/new" fi if (( DIFFOSCOPE )); then |