From 70a3041ff8b1cb85f14dc565d225b3f5ffd8b1d8 Mon Sep 17 00:00:00 2001 From: Levente Polyak Date: Mon, 22 Aug 2022 00:42:56 +0200 Subject: 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. --- contrib/completion/zsh/_devtools.in | 1 + doc/man/diffpkg.1.asciidoc | 4 +++ src/diffpkg.in | 63 ++++++++++++++++++++++++++++++------- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/contrib/completion/zsh/_devtools.in b/contrib/completion/zsh/_devtools.in index 46e04fb..b93e510 100644 --- a/contrib/completion/zsh/_devtools.in +++ b/contrib/completion/zsh/_devtools.in @@ -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' diff --git a/doc/man/diffpkg.1.asciidoc b/doc/man/diffpkg.1.asciidoc index ec89f89..69e0c27 100644 --- a/doc/man/diffpkg.1.asciidoc +++ b/doc/man/diffpkg.1.asciidoc @@ -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 ----- 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 -- cgit v1.2.3-54-g00ecf