Add --no-color flag to disable ANSI sequences

This commit is contained in:
Bennett Hardwick authored and David Peter committed 2019-01-14 09:14:47 +01:00
1 parent 78d8cc516f
commit 74861353fa
1 file changed
+33 -14
+33 -14
View File
@@ -81,26 +81,37 @@ struct Printer<'a> {
/// The buffered line built with each byte, ready to print to stdout. /// The buffered line built with each byte, ready to print to stdout.
buffer_line: Vec<u8>, buffer_line: Vec<u8>,
stdout: StdoutLock<'a>, stdout: StdoutLock<'a>,
show_color: bool,
byte_hex_table: Vec<String>, byte_hex_table: Vec<String>,
byte_char_table: Vec<String>, byte_char_table: Vec<String>,
} }
impl<'a> Printer<'a> { impl<'a> Printer<'a> {
fn new(stdout: StdoutLock) -> Printer { fn new(stdout: StdoutLock, show_color: bool) -> Printer {
Printer { Printer {
idx: 1, idx: 1,
raw_line: vec![], raw_line: vec![],
buffer_line: vec![], buffer_line: vec![],
stdout, stdout,
show_color,
byte_hex_table: (0u8..=u8::max_value()) byte_hex_table: (0u8..=u8::max_value())
.map(|i| format!("{} ", Byte(i).color().paint(format!("{:02x}", i)))) .map(|i| {
let byte_hex = format!("{:02x} ", i);
if show_color {
Byte(i).color().paint(byte_hex).to_string()
} else {
byte_hex
}
})
.collect(), .collect(),
byte_char_table: (0u8..=u8::max_value()) byte_char_table: (0u8..=u8::max_value())
.map(|i| { .map(|i| {
format!( let byte_char = format!("{}", Byte(i).as_char());
"{}", if show_color {
Byte(i).color().paint(format!("{}", Byte(i).as_char())) Byte(i).color().paint(byte_char).to_string()
) } else {
byte_char
}
}) })
.collect(), .collect(),
} }
@@ -127,13 +138,13 @@ impl<'a> Printer<'a> {
fn print_byte(&mut self, b: u8) -> io::Result<()> { fn print_byte(&mut self, b: u8) -> io::Result<()> {
if self.idx % 16 == 1 { if self.idx % 16 == 1 {
let style = COLOR_OFFSET.normal(); let style = COLOR_OFFSET.normal();
let _ = write!( let byte_index = format!("{:08x}", self.idx -1);
&mut self.buffer_line, let formatted_string = if self.show_color {
"│{}{:08x}{}│ ", format!("{}", style.paint(byte_index))
style.prefix(), } else {
self.idx - 1, byte_index
style.suffix() };
); let _ = write!(&mut self.buffer_line, "│{}│ ", formatted_string);
} }
write!(&mut self.buffer_line, "{}", self.byte_hex_table[b as usize])?; write!(&mut self.buffer_line, "{}", self.byte_hex_table[b as usize])?;
@@ -216,6 +227,12 @@ fn run() -> Result<(), Box<::std::error::Error>> {
.takes_value(true) .takes_value(true)
.value_name("N") .value_name("N")
.help("read only N bytes from the input"), .help("read only N bytes from the input"),
)
.arg(
Arg::with_name("no-color")
.long("no-color")
.takes_value(false)
.help("Don't output ANSI color characters")
); );
let matches = app.get_matches_safe()?; let matches = app.get_matches_safe()?;
@@ -234,8 +251,10 @@ fn run() -> Result<(), Box<::std::error::Error>> {
reader = Box::new(reader.take(length)); reader = Box::new(reader.take(length));
} }
let show_color = !matches.is_present("no-color");
let stdout = io::stdout(); let stdout = io::stdout();
let mut printer = Printer::new(stdout.lock()); let mut printer = Printer::new(stdout.lock(), show_color);
printer.header(); printer.header();
let mut buffer = [0; BUFFER_SIZE]; let mut buffer = [0; BUFFER_SIZE];