Dump ignore colors when output is piped
continuous-integration/drone/push Build is failing Details

This commit is contained in:
jpk 2022-12-16 13:54:21 +01:00
parent 43944cadfe
commit b23f7a4de5
3 changed files with 82 additions and 64 deletions

View File

@ -7,71 +7,82 @@ const COLOR_TEXT: color::Fg<color::Cyan> = color::Fg(color::Cyan);
const COLOR_NUMBERS: color::Fg<color::LightBlue> = 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(),
}
}
}

View File

@ -20,6 +20,10 @@ struct Arguments {
filename: Option<String>,
}
pub fn isatty() -> bool {
termion::is_tty(&File::create("/dev/stdout").expect("Could not open `stdout'"))
}
fn main() {
let args = Arguments::parse();

View File

@ -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<dyn io::Read>, 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;