diff --git a/src/colormap.rs b/src/colormap.rs index 2a774aa..27bcbae 100644 --- a/src/colormap.rs +++ b/src/colormap.rs @@ -7,71 +7,82 @@ const COLOR_TEXT: color::Fg = color::Fg(color::Cyan); const COLOR_NUMBERS: color::Fg = color::Fg(color::LightBlue); pub fn map_u8_to_color(data: u8) -> String { - match data { - 32..=47 => format!("{}{:02x}{}", COLOR_SYMBOLS, data, color::Fg(color::Reset)), - 48..=57 => format!("{}{:02x}{}", COLOR_NUMBERS, data, color::Fg(color::Reset)), - 58..=64 => format!("{}{:02x}{}", COLOR_SYMBOLS, data, color::Fg(color::Reset)), - 65..=90 => format!("{}{:02x}{}", COLOR_TEXT, data, color::Fg(color::Reset)), - 91..=96 => format!("{}{:02x}{}", COLOR_SYMBOLS, data, color::Fg(color::Reset)), - 97..=122 => format!("{}{:02x}{}", COLOR_TEXT, data, color::Fg(color::Reset)), - 123..=126 => format!("{}{:02x}{}", COLOR_SYMBOLS, data, color::Fg(color::Reset)), - _ => format!( - "{}{:02x}{}", - color::Fg(color::LightBlack), - data, - color::Fg(color::Reset) - ), + if super::isatty() { + match data { + 32..=47 => format!("{}{:02x}{}", COLOR_SYMBOLS, data, color::Fg(color::Reset)), + 48..=57 => format!("{}{:02x}{}", COLOR_NUMBERS, data, color::Fg(color::Reset)), + 58..=64 => format!("{}{:02x}{}", COLOR_SYMBOLS, data, color::Fg(color::Reset)), + 65..=90 => format!("{}{:02x}{}", COLOR_TEXT, data, color::Fg(color::Reset)), + 91..=96 => format!("{}{:02x}{}", COLOR_SYMBOLS, data, color::Fg(color::Reset)), + 97..=122 => format!("{}{:02x}{}", COLOR_TEXT, data, color::Fg(color::Reset)), + 123..=126 => format!("{}{:02x}{}", COLOR_SYMBOLS, data, color::Fg(color::Reset)), + _ => format!( + "{}{:02x}{}", + color::Fg(color::LightBlack), + data, + color::Fg(color::Reset) + ), + } + } else { + format!("{:02x}", data) } } pub fn map_char_to_color(data: u8) -> String { - match data { - 32..=47 => format!( - "{}{}{}", - COLOR_SYMBOLS, - (data as char), - color::Fg(color::Reset) - ), - 48..=57 => format!( - "{}{}{}", - COLOR_NUMBERS, - (data as char), - color::Fg(color::Reset) - ), - 58..=64 => format!( - "{}{}{}", - COLOR_SYMBOLS, - (data as char), - color::Fg(color::Reset) - ), - 65..=90 => format!( - "{}{}{}", - COLOR_TEXT, - (data as char), - color::Fg(color::Reset) - ), - 91..=96 => format!( - "{}{}{}", - COLOR_SYMBOLS, - (data as char), - color::Fg(color::Reset) - ), - 97..=122 => format!( - "{}{}{}", - COLOR_TEXT, - (data as char), - color::Fg(color::Reset) - ), - 123..=126 => format!( - "{}{}{}", - COLOR_SYMBOLS, - (data as char), - color::Fg(color::Reset) - ), - _ => format!( - "{}.{}", - color::Fg(color::LightBlack), - color::Fg(color::Reset) - ), + if super::isatty() { + match data { + 32..=47 => format!( + "{}{}{}", + COLOR_SYMBOLS, + (data as char), + color::Fg(color::Reset) + ), + 48..=57 => format!( + "{}{}{}", + COLOR_NUMBERS, + (data as char), + color::Fg(color::Reset) + ), + 58..=64 => format!( + "{}{}{}", + COLOR_SYMBOLS, + (data as char), + color::Fg(color::Reset) + ), + 65..=90 => format!( + "{}{}{}", + COLOR_TEXT, + (data as char), + color::Fg(color::Reset) + ), + 91..=96 => format!( + "{}{}{}", + COLOR_SYMBOLS, + (data as char), + color::Fg(color::Reset) + ), + 97..=122 => format!( + "{}{}{}", + COLOR_TEXT, + (data as char), + color::Fg(color::Reset) + ), + 123..=126 => format!( + "{}{}{}", + COLOR_SYMBOLS, + (data as char), + color::Fg(color::Reset) + ), + _ => format!( + "{}.{}", + color::Fg(color::LightBlack), + color::Fg(color::Reset) + ), + } + } else { + match data { + 32..=126 => format!("{}", (data as char)), + _ => ".".to_string(), + } } } diff --git a/src/main.rs b/src/main.rs index 2ad8b44..e497bde 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,10 @@ struct Arguments { filename: Option, } +pub fn isatty() -> bool { + termion::is_tty(&File::create("/dev/stdout").expect("Could not open `stdout'")) +} + fn main() { let args = Arguments::parse(); diff --git a/src/tohex.rs b/src/tohex.rs index 978eee9..6cfb166 100644 --- a/src/tohex.rs +++ b/src/tohex.rs @@ -1,5 +1,7 @@ use std::io; +use crate::isatty; + use super::colormap::{map_char_to_color, map_u8_to_color}; const GLOBAL_BUFFER_LENGTH: usize = 16; @@ -16,7 +18,7 @@ fn dump_to_hex(bytes: &mut [u8]) -> String { }) .collect(); let padding_length = (8 - out_vec.len()) * 5; - format!("{:8}{}", out_vec.join(" "), " ".repeat(padding_length)) + format!("{}{}", out_vec.join(" "), " ".repeat(padding_length)) } fn dump_to_chr(bytes: &mut [u8]) -> String { @@ -43,9 +45,10 @@ pub fn hexdump(mut reader: Box, length: usize) { break; } else { println!( - "{:08x}: {:40} {:10}", + "{:08x}: {:40}{}{:10}", offset, dump_to_hex(&mut buf[0..to_read]), + if isatty() { " ".to_string() } else { " ".to_string() }, dump_to_chr(&mut buf[0..to_read]) ); offset += GLOBAL_BUFFER_LENGTH;