From: Hirohito Higashi Date: Sun, 31 May 2026 21:11:55 +0000 (+0000) Subject: patch 9.2.0580: xxd: binary output is not colored with -R X-Git-Tag: v9.2.0580^0 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f0cae9d5abba1ad97f8c78c8d3cbb0d1c46d5aff;p=thirdparty%2Fvim.git patch 9.2.0580: xxd: binary output is not colored with -R Problem: With xxd the -R option colors the hex output but leaves the binary output produced by -b uncolored (Boris Verkhovskiy) Solution: Color the binary (bits) output per byte with the same colors as the hex output, update the documentation and add a test (Hirohito Higashi). fixes: #20385 closes: #20401 Signed-off-by: Hirohito Higashi Signed-off-by: Christian Brabandt --- diff --git a/runtime/doc/xxd.1 b/runtime/doc/xxd.1 index 48166e03cc..d80c5e9c08 100644 --- a/runtime/doc/xxd.1 +++ b/runtime/doc/xxd.1 @@ -142,9 +142,9 @@ anywhere. Use the combination to read a bits dump instead of a hex dump. .TP .IR \-R " " when -In the output the hex-value and the value are both colored with the same color -depending on the hex-value. Mostly helping to differentiate printable and -non-printable characters. +In the output both the data column (hex, or bits with \-b) and the character +column are colored with the same color depending on the byte value. Mostly +helping to differentiate printable and non-printable characters. .I \fIwhen\fP is .BR never ", " always ", or " auto " (default: auto). diff --git a/runtime/doc/xxd.man b/runtime/doc/xxd.man index 8beba9c88a..17a9dd14ba 100644 --- a/runtime/doc/xxd.man +++ b/runtime/doc/xxd.man @@ -101,11 +101,12 @@ OPTIONS instead of a hex dump. -R when - In the output the hex-value and the value are both colored with - the same color depending on the hex-value. Mostly helping to - differentiate printable and non-printable characters. when is - never, always, or auto (default: auto). When the $NO_COLOR en‐ - vironment variable is set, colorization will be disabled. + In the output both the data column (hex, or bits with -b) and + the character column are colored with the same color depending + on the byte value. Mostly helping to differentiate printable and + non-printable characters. when is never, always, or auto (de‐ + fault: auto). When the $NO_COLOR environment variable is set, + colorization will be disabled. -seek offset When used after -r: revert with added to file positions diff --git a/src/testdir/test_xxd.vim b/src/testdir/test_xxd.vim index 430a07ccfb..990683b172 100644 --- a/src/testdir/test_xxd.vim +++ b/src/testdir/test_xxd.vim @@ -663,6 +663,22 @@ call writefile(data,'Xinput') endfunc +func Test_xxd_color_bits() + " Binary output (-b) should be colored per byte like the hex output, + " see issue #20385. Bytes cover the white/yellow/green/blue color groups. + let s:test = 1 + call writefile(0z000941FF, 'Xxxdbits') + + %d + exe '0r! ' . s:xxd_cmd . ' -b -R always -c 4 Xxxdbits' + $d + let expected = [ + \ "00000000: \e[1;37m00000000\e[0m \e[1;33m00001001\e[0m \e[1;32m01000001\e[0m \e[1;34m11111111\e[0m \e[1;37m.\e[0m\e[1;33m.\e[0m\e[1;32mA\e[0m\e[1;34m.\e[0m"] + call assert_equal(expected, getline(1, '$'), s:Mess(s:test)) + + call delete('Xxxdbits') +endfunc + func Test_xxd_color2() CheckScreendump CheckUnix diff --git a/src/version.c b/src/version.c index 95107bb286..e39f67aad7 100644 --- a/src/version.c +++ b/src/version.c @@ -729,6 +729,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ +/**/ + 580, /**/ 579, /**/ diff --git a/src/xxd/xxd.c b/src/xxd/xxd.c index fb45f6c284..323d5a3982 100644 --- a/src/xxd/xxd.c +++ b/src/xxd/xxd.c @@ -75,6 +75,7 @@ * 19.03.2026 Add -t option to end output with terminating null * 25.03.2026 Fix color output issues * 26.04.2026 Use unsigned long for printing offsets + * 31.05.2026 Colorize binary output * * (c) 1990-1998 by Juergen Weigert (jnweiger@gmail.com) * @@ -155,7 +156,7 @@ extern void perror __P((char *)); # endif #endif -char version[] = "xxd 2026-04-26 by Juergen Weigert et al."; +char version[] = "xxd 2026-05-31 by Juergen Weigert et al."; #ifdef WIN32 char osver[] = " (Win32)"; #else @@ -1194,8 +1195,15 @@ main(int argc, char *argv[]) } else /* hextype == HEX_BITS */ { + if (color) + cur_color = get_color_char(e, ebcdic); + for (i = 7; i >= 0; i--) - l[c++] = (e & (1 << i)) ? '1' : '0'; + { + if (color) + colors[c] = cur_color; + l[c++] = (e & (1 << i)) ? '1' : '0'; + } } if (e) nonzero++;