]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.2.0580: xxd: binary output is not colored with -R v9.2.0580
authorHirohito Higashi <h.east.727@gmail.com>
Sun, 31 May 2026 21:11:55 +0000 (21:11 +0000)
committerChristian Brabandt <cb@256bit.org>
Sun, 31 May 2026 21:11:55 +0000 (21:11 +0000)
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>
runtime/doc/xxd.1
runtime/doc/xxd.man
src/testdir/test_xxd.vim
src/version.c
src/xxd/xxd.c

index 48166e03cc219ba745789babcbf9f7596b4dfb71..d80c5e9c08a73bebf32e665322bcbc3030aa1d5b 100644 (file)
@@ -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).
index 8beba9c88a5a6f104df1219f3756c13a97866af8..17a9dd14ba23f2317832142c23a3eb2bf494c614 100644 (file)
@@ -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 <offset> added to file positions
index 430a07ccfbb595daf6f5f6a50119152eefba20d9..990683b1724f479c13c1394ab1db51389938fd64 100644 (file)
@@ -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
index 95107bb286f0501387cf4f764d8464c3fa3c99f4..e39f67aad73c8cf52ca4ff5208d91244a3a0cb76 100644 (file)
@@ -729,6 +729,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    580,
 /**/
     579,
 /**/
index fb45f6c2846d91fc6ca4e7167e49fcc4cb12181b..323d5a3982efe7bde9b44ca3a554c4bd21ba312e 100644 (file)
@@ -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++;