Skip to content

Commit

Permalink
In binary mode, colorize letters, digits and whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
joerivanruth committed Feb 21, 2024
1 parent 1971e54 commit 4c9117f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 12 additions & 2 deletions src/mapi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 => "░",
Expand Down
17 changes: 13 additions & 4 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down Expand Up @@ -178,4 +184,7 @@ pub enum Style {
Error,
Frame,
Header,
Whitespace,
Digit,
Letter,
}

0 comments on commit 4c9117f

Please sign in to comment.