From 4c9117f6493e18beedd7f8955a9fb98fa2cfd906 Mon Sep 17 00:00:00 2001 From: Joeri van Ruth Date: Wed, 21 Feb 2024 15:27:31 +0100 Subject: [PATCH] In binary mode, colorize letters, digits and whitespace --- CHANGELOG.md | 3 +++ src/mapi/mod.rs | 14 ++++++++++++-- src/render.rs | 17 +++++++++++++---- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c202986..7993772 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ What changed in mapiproxy, per version - The use of colors can now be configured with --color=always|never|auto. +- Colorize text, digits and whitespace in binary output. This makes it easier + to match the hex codes on the left to the characters on the right. + - Raw IPv6 addresses are now allowed in LISTEN_ADDR and FORWARD_ADDR: `[::1]:50000`. - Clean up Unix sockets when Control-C is pressed. diff --git a/src/mapi/mod.rs b/src/mapi/mod.rs index 2e4d41b..265f5d7 100644 --- a/src/mapi/mod.rs +++ b/src/mapi/mod.rs @@ -379,7 +379,16 @@ impl Binary { } } - fn add(&mut self, byte: u8, style: Style, renderer: &mut Renderer) -> io::Result<()> { + fn add(&mut self, byte: u8, mut style: Style, renderer: &mut Renderer) -> io::Result<()> { + if style == Style::Normal { + style = match byte { + b'0'..=b'9' => Style::Digit, + b'a'..=b'z' | b'A'..=b'Z' => Style::Letter, + b' ' | b'\t' | b'\r' | b'\n' => Style::Whitespace, + _ => style, + } + } + self.row[self.col] = (byte, style); self.col += 1; @@ -472,7 +481,8 @@ impl Binary { fn readable(byte: &[u8; 1]) -> &[u8] { // note that the readable range does not include 0x7f (DEL) let s = match byte[0] { - b' '..=0x7e => return byte.as_ref(), + b' ' => "·", + 0x21..=0x7e => return byte.as_ref(), b'\n' => "↵", b'\t' => "→", 0 => "░", diff --git a/src/render.rs b/src/render.rs index b537fce..7b0f551 100644 --- a/src/render.rs +++ b/src/render.rs @@ -133,12 +133,18 @@ impl Renderer { } fn write_style(&mut self, style: Style) -> io::Result<()> { + // Black=30 Red=31 Green=32 Yellow=33 Blue=34 Magenta=35 Cyan=36 White=37 + let escape_sequence = match style { - Style::Normal => "\u{1b}[m", - Style::Header => "\u{1b}[1m", - Style::Frame => "\u{1b}[36m", - Style::Error => "\u{1b}[31m", + Style::Normal => "", + Style::Header => "\u{1b}[1m", // bold + Style::Frame => "\u{1b}[36m", // cyan + Style::Error => "\u{1b}[1m\u{1b}[31m", // bold red + Style::Whitespace => "\u{1b}[31m", // red + Style::Digit => "\u{1b}[32m", // green + Style::Letter => "\u{1b}[34m", // blue }; + self.out.write_all(b"\x1b[m")?; // NORMAL self.out.write_all(escape_sequence.as_bytes())?; Ok(()) } @@ -178,4 +184,7 @@ pub enum Style { Error, Frame, Header, + Whitespace, + Digit, + Letter, }