Skip to content

Commit f0cae9d

Browse files
h-eastchrisbra
authored andcommitted
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 <h.east.727@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent d69cf0d commit f0cae9d

5 files changed

Lines changed: 37 additions & 10 deletions

File tree

runtime/doc/xxd.1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ anywhere. Use the combination
142142
to read a bits dump instead of a hex dump.
143143
.TP
144144
.IR \-R " " when
145-
In the output the hex-value and the value are both colored with the same color
146-
depending on the hex-value. Mostly helping to differentiate printable and
147-
non-printable characters.
145+
In the output both the data column (hex, or bits with \-b) and the character
146+
column are colored with the same color depending on the byte value. Mostly
147+
helping to differentiate printable and non-printable characters.
148148
.I \fIwhen\fP
149149
is
150150
.BR never ", " always ", or " auto " (default: auto).

runtime/doc/xxd.man

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,12 @@ OPTIONS
101101
instead of a hex dump.
102102

103103
-R when
104-
In the output the hex-value and the value are both colored with
105-
the same color depending on the hex-value. Mostly helping to
106-
differentiate printable and non-printable characters. when is
107-
never, always, or auto (default: auto). When the $NO_COLOR en‐
108-
vironment variable is set, colorization will be disabled.
104+
In the output both the data column (hex, or bits with -b) and
105+
the character column are colored with the same color depending
106+
on the byte value. Mostly helping to differentiate printable and
107+
non-printable characters. when is never, always, or auto (de‐
108+
fault: auto). When the $NO_COLOR environment variable is set,
109+
colorization will be disabled.
109110

110111
-seek offset
111112
When used after -r: revert with <offset> added to file positions

src/testdir/test_xxd.vim

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,22 @@ call writefile(data,'Xinput')
663663

664664
endfunc
665665

666+
func Test_xxd_color_bits()
667+
" Binary output (-b) should be colored per byte like the hex output,
668+
" see issue #20385. Bytes cover the white/yellow/green/blue color groups.
669+
let s:test = 1
670+
call writefile(0z000941FF, 'Xxxdbits')
671+
672+
%d
673+
exe '0r! ' . s:xxd_cmd . ' -b -R always -c 4 Xxxdbits'
674+
$d
675+
let expected = [
676+
\ "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"]
677+
call assert_equal(expected, getline(1, '$'), s:Mess(s:test))
678+
679+
call delete('Xxxdbits')
680+
endfunc
681+
666682
func Test_xxd_color2()
667683
CheckScreendump
668684
CheckUnix

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,8 @@ static char *(features[]) =
729729

730730
static int included_patches[] =
731731
{ /* Add new patch number below this line */
732+
/**/
733+
580,
732734
/**/
733735
579,
734736
/**/

src/xxd/xxd.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
* 19.03.2026 Add -t option to end output with terminating null
7676
* 25.03.2026 Fix color output issues
7777
* 26.04.2026 Use unsigned long for printing offsets
78+
* 31.05.2026 Colorize binary output
7879
*
7980
* (c) 1990-1998 by Juergen Weigert (jnweiger@gmail.com)
8081
*
@@ -155,7 +156,7 @@ extern void perror __P((char *));
155156
# endif
156157
#endif
157158

158-
char version[] = "xxd 2026-04-26 by Juergen Weigert et al.";
159+
char version[] = "xxd 2026-05-31 by Juergen Weigert et al.";
159160
#ifdef WIN32
160161
char osver[] = " (Win32)";
161162
#else
@@ -1194,8 +1195,15 @@ main(int argc, char *argv[])
11941195
}
11951196
else /* hextype == HEX_BITS */
11961197
{
1198+
if (color)
1199+
cur_color = get_color_char(e, ebcdic);
1200+
11971201
for (i = 7; i >= 0; i--)
1198-
l[c++] = (e & (1 << i)) ? '1' : '0';
1202+
{
1203+
if (color)
1204+
colors[c] = cur_color;
1205+
l[c++] = (e & (1 << i)) ? '1' : '0';
1206+
}
11991207
}
12001208
if (e)
12011209
nonzero++;

0 commit comments

Comments
 (0)