From 770f9a04a20524d208bc4fa88934095753e76a00 Mon Sep 17 00:00:00 2001 From: jpk Date: Mon, 12 Dec 2022 16:54:20 +0100 Subject: [PATCH] Colourful dumping --- Cargo.lock | 55 ++++++++++++++++++++++++++++- Cargo.toml | 3 +- src/colormap.rs | 92 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 41 +++++++++++++--------- 4 files changed, 172 insertions(+), 19 deletions(-) create mode 100644 src/colormap.rs diff --git a/Cargo.lock b/Cargo.lock index cf548c5..f868716 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,58 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] -name = "hd" +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "libc" +version = "0.2.138" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db6d7e329c562c5dfab7a46a2afabc8b987ab9a4834c9d1ca04dc54c1546cef8" + +[[package]] +name = "numtoa" version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef" + +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags", +] + +[[package]] +name = "redox_termios" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8440d8acb4fd3d277125b4bd01a6f38aee8d814b3b5fc09b3f2b825d37d3fe8f" +dependencies = [ + "redox_syscall", +] + +[[package]] +name = "rhd" +version = "0.1.0" +dependencies = [ + "termion", +] + +[[package]] +name = "termion" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "659c1f379f3408c7e5e84c7d0da6d93404e3800b6b9d063ba24436419302ec90" +dependencies = [ + "libc", + "numtoa", + "redox_syscall", + "redox_termios", +] diff --git a/Cargo.toml b/Cargo.toml index 252519a..29b77c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "hd" +name = "rhd" version = "0.1.0" authors = ["JayPiKay "] edition = "2018" @@ -7,3 +7,4 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +termion = "2.0.1" diff --git a/src/colormap.rs b/src/colormap.rs new file mode 100644 index 0000000..6ca850f --- /dev/null +++ b/src/colormap.rs @@ -0,0 +1,92 @@ +extern crate termion; + +use termion::color; + +pub fn map_u8_to_color(data: u8) -> String { + match data { + 32..=47 => format!( + "{}{:02x}{}", + color::Fg(color::Yellow), + data, + color::Fg(color::Reset) + ), + 48..=64 => format!( + "{}{:02x}{}", + color::Fg(color::Green), + data, + color::Fg(color::Reset) + ), + 65..=90 => format!( + "{}{:02x}{}", + color::Fg(color::Blue), + data, + color::Fg(color::Reset) + ), + 91..=96 => format!( + "{}{:02x}{}", + color::Fg(color::Yellow), + data, + color::Fg(color::Reset) + ), + 97..=122 => format!( + "{}{:02x}{}", + color::Fg(color::Blue), + data, + color::Fg(color::Reset) + ), + 123..=126 => format!( + "{}{:02x}{}", + color::Fg(color::Yellow), + data, + color::Fg(color::Reset) + ), + _ => format!( + "{}{:02x}{}", + color::Fg(color::LightRed), + data, + color::Fg(color::Reset) + ), + } +} + +pub fn map_char_to_color(data: u8) -> String { + match data { + 32..=47 => format!( + "{}{}{}", + color::Fg(color::Yellow), + (data as char), + color::Fg(color::Reset) + ), + 48..=64 => format!( + "{}{}{}", + color::Fg(color::Green), + (data as char), + color::Fg(color::Reset) + ), + 65..=90 => format!( + "{}{}{}", + color::Fg(color::Blue), + (data as char), + color::Fg(color::Reset) + ), + 91..=96 => format!( + "{}{}{}", + color::Fg(color::Yellow), + (data as char), + color::Fg(color::Reset) + ), + 97..=122 => format!( + "{}{}{}", + color::Fg(color::Blue), + (data as char), + color::Fg(color::Reset) + ), + 123..=126 => format!( + "{}{}{}", + color::Fg(color::Yellow), + (data as char), + color::Fg(color::Reset) + ), + _ => format!("{}.{}", color::Fg(color::LightRed), color::Fg(color::Reset)), + } +} diff --git a/src/main.rs b/src/main.rs index a0434f9..078e5df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,33 +1,38 @@ +mod colormap; + use std::env; -use std::io::prelude::*; use std::fs::File; +use std::io::prelude::*; + +use colormap::{map_char_to_color, map_u8_to_color}; const GLOBAL_BUFFER_LENGTH: usize = 16; fn get_file(filename: String) -> File { match File::open(filename) { - Ok(f) => File::from(f), + Ok(f) => f, Err(e) => { - panic!(e); + panic!("{:?}", e); } } } fn dump_to_hex(bytes: &mut [u8]) -> String { - let out_vec: Vec = bytes.chunks(2) + let out_vec: Vec = bytes + .chunks(2) .map(|c| { - if c.len() == 2 { format!("{:02x}{:02x}", c[0], c[1]) } - else { format!("{:02x}", c[0]) } - }).collect(); + if c.len() == 2 { + format!("{}{}", map_u8_to_color(c[0]), map_u8_to_color(c[1])) + } else { + map_u8_to_color(c[0]) + } + }) + .collect(); out_vec.join(" ") } fn dump_to_chr(bytes: &mut [u8]) -> String { - let out_vec: Vec = bytes.iter().map(|ord| { - if *ord >= 32 && *ord <= 126 { (*ord as char).to_string() } - else { '.'.to_string() } - }).collect(); - + let out_vec: Vec = bytes.iter().map(|ord| map_char_to_color(*ord)).collect(); out_vec.join("") } @@ -50,13 +55,15 @@ fn main() { if num == 0 { break; } else { - println!("{:08x}: {:40} {:10}", - offset, - dump_to_hex(&mut buf[0..num]), - dump_to_chr(&mut buf[0..num])); + println!( + "{:08x}: {:40} {:10}", + offset, + dump_to_hex(&mut buf[0..num]), + dump_to_chr(&mut buf[0..num]) + ); offset += GLOBAL_BUFFER_LENGTH; } - }, + } Err(e) => { eprintln!("hd: {}", e); break;