Dump ignore colors when output is piped
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
This commit is contained in:
parent
43944cadfe
commit
b23f7a4de5
135
src/colormap.rs
135
src/colormap.rs
|
@ -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);
|
const COLOR_NUMBERS: color::Fg<color::LightBlue> = color::Fg(color::LightBlue);
|
||||||
|
|
||||||
pub fn map_u8_to_color(data: u8) -> String {
|
pub fn map_u8_to_color(data: u8) -> String {
|
||||||
match data {
|
if super::isatty() {
|
||||||
32..=47 => format!("{}{:02x}{}", COLOR_SYMBOLS, data, color::Fg(color::Reset)),
|
match data {
|
||||||
48..=57 => format!("{}{:02x}{}", COLOR_NUMBERS, data, color::Fg(color::Reset)),
|
32..=47 => format!("{}{:02x}{}", COLOR_SYMBOLS, data, color::Fg(color::Reset)),
|
||||||
58..=64 => format!("{}{:02x}{}", COLOR_SYMBOLS, data, color::Fg(color::Reset)),
|
48..=57 => format!("{}{:02x}{}", COLOR_NUMBERS, data, color::Fg(color::Reset)),
|
||||||
65..=90 => format!("{}{:02x}{}", COLOR_TEXT, data, color::Fg(color::Reset)),
|
58..=64 => format!("{}{:02x}{}", COLOR_SYMBOLS, data, color::Fg(color::Reset)),
|
||||||
91..=96 => format!("{}{:02x}{}", COLOR_SYMBOLS, data, color::Fg(color::Reset)),
|
65..=90 => format!("{}{:02x}{}", COLOR_TEXT, data, color::Fg(color::Reset)),
|
||||||
97..=122 => format!("{}{:02x}{}", COLOR_TEXT, data, color::Fg(color::Reset)),
|
91..=96 => format!("{}{:02x}{}", COLOR_SYMBOLS, data, color::Fg(color::Reset)),
|
||||||
123..=126 => format!("{}{:02x}{}", COLOR_SYMBOLS, data, color::Fg(color::Reset)),
|
97..=122 => format!("{}{:02x}{}", COLOR_TEXT, data, color::Fg(color::Reset)),
|
||||||
_ => format!(
|
123..=126 => format!("{}{:02x}{}", COLOR_SYMBOLS, data, color::Fg(color::Reset)),
|
||||||
"{}{:02x}{}",
|
_ => format!(
|
||||||
color::Fg(color::LightBlack),
|
"{}{:02x}{}",
|
||||||
data,
|
color::Fg(color::LightBlack),
|
||||||
color::Fg(color::Reset)
|
data,
|
||||||
),
|
color::Fg(color::Reset)
|
||||||
|
),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
format!("{:02x}", data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn map_char_to_color(data: u8) -> String {
|
pub fn map_char_to_color(data: u8) -> String {
|
||||||
match data {
|
if super::isatty() {
|
||||||
32..=47 => format!(
|
match data {
|
||||||
"{}{}{}",
|
32..=47 => format!(
|
||||||
COLOR_SYMBOLS,
|
"{}{}{}",
|
||||||
(data as char),
|
COLOR_SYMBOLS,
|
||||||
color::Fg(color::Reset)
|
(data as char),
|
||||||
),
|
color::Fg(color::Reset)
|
||||||
48..=57 => format!(
|
),
|
||||||
"{}{}{}",
|
48..=57 => format!(
|
||||||
COLOR_NUMBERS,
|
"{}{}{}",
|
||||||
(data as char),
|
COLOR_NUMBERS,
|
||||||
color::Fg(color::Reset)
|
(data as char),
|
||||||
),
|
color::Fg(color::Reset)
|
||||||
58..=64 => format!(
|
),
|
||||||
"{}{}{}",
|
58..=64 => format!(
|
||||||
COLOR_SYMBOLS,
|
"{}{}{}",
|
||||||
(data as char),
|
COLOR_SYMBOLS,
|
||||||
color::Fg(color::Reset)
|
(data as char),
|
||||||
),
|
color::Fg(color::Reset)
|
||||||
65..=90 => format!(
|
),
|
||||||
"{}{}{}",
|
65..=90 => format!(
|
||||||
COLOR_TEXT,
|
"{}{}{}",
|
||||||
(data as char),
|
COLOR_TEXT,
|
||||||
color::Fg(color::Reset)
|
(data as char),
|
||||||
),
|
color::Fg(color::Reset)
|
||||||
91..=96 => format!(
|
),
|
||||||
"{}{}{}",
|
91..=96 => format!(
|
||||||
COLOR_SYMBOLS,
|
"{}{}{}",
|
||||||
(data as char),
|
COLOR_SYMBOLS,
|
||||||
color::Fg(color::Reset)
|
(data as char),
|
||||||
),
|
color::Fg(color::Reset)
|
||||||
97..=122 => format!(
|
),
|
||||||
"{}{}{}",
|
97..=122 => format!(
|
||||||
COLOR_TEXT,
|
"{}{}{}",
|
||||||
(data as char),
|
COLOR_TEXT,
|
||||||
color::Fg(color::Reset)
|
(data as char),
|
||||||
),
|
color::Fg(color::Reset)
|
||||||
123..=126 => format!(
|
),
|
||||||
"{}{}{}",
|
123..=126 => format!(
|
||||||
COLOR_SYMBOLS,
|
"{}{}{}",
|
||||||
(data as char),
|
COLOR_SYMBOLS,
|
||||||
color::Fg(color::Reset)
|
(data as char),
|
||||||
),
|
color::Fg(color::Reset)
|
||||||
_ => format!(
|
),
|
||||||
"{}.{}",
|
_ => format!(
|
||||||
color::Fg(color::LightBlack),
|
"{}.{}",
|
||||||
color::Fg(color::Reset)
|
color::Fg(color::LightBlack),
|
||||||
),
|
color::Fg(color::Reset)
|
||||||
|
),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
match data {
|
||||||
|
32..=126 => format!("{}", (data as char)),
|
||||||
|
_ => ".".to_string(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,10 @@ struct Arguments {
|
||||||
filename: Option<String>,
|
filename: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn isatty() -> bool {
|
||||||
|
termion::is_tty(&File::create("/dev/stdout").expect("Could not open `stdout'"))
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args = Arguments::parse();
|
let args = Arguments::parse();
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
|
use crate::isatty;
|
||||||
|
|
||||||
use super::colormap::{map_char_to_color, map_u8_to_color};
|
use super::colormap::{map_char_to_color, map_u8_to_color};
|
||||||
|
|
||||||
const GLOBAL_BUFFER_LENGTH: usize = 16;
|
const GLOBAL_BUFFER_LENGTH: usize = 16;
|
||||||
|
@ -16,7 +18,7 @@ fn dump_to_hex(bytes: &mut [u8]) -> String {
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
let padding_length = (8 - out_vec.len()) * 5;
|
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 {
|
fn dump_to_chr(bytes: &mut [u8]) -> String {
|
||||||
|
@ -43,9 +45,10 @@ pub fn hexdump(mut reader: Box<dyn io::Read>, length: usize) {
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
println!(
|
println!(
|
||||||
"{:08x}: {:40} {:10}",
|
"{:08x}: {:40}{}{:10}",
|
||||||
offset,
|
offset,
|
||||||
dump_to_hex(&mut buf[0..to_read]),
|
dump_to_hex(&mut buf[0..to_read]),
|
||||||
|
if isatty() { " ".to_string() } else { " ".to_string() },
|
||||||
dump_to_chr(&mut buf[0..to_read])
|
dump_to_chr(&mut buf[0..to_read])
|
||||||
);
|
);
|
||||||
offset += GLOBAL_BUFFER_LENGTH;
|
offset += GLOBAL_BUFFER_LENGTH;
|
||||||
|
|
Loading…
Reference in New Issue