From 7e9d33700595cd6c4ddd157c013e5690a4ac1bab Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Wed, 17 Jan 2024 23:48:41 +0100 Subject: [PATCH] #60 #61 improvements and bugfixes --- members/libpt-bintols/src/hedu/mod.rs | 68 ++++++++++++++++++++------- src/hedu/mod.rs | 14 +++++- 2 files changed, 64 insertions(+), 18 deletions(-) diff --git a/members/libpt-bintols/src/hedu/mod.rs b/members/libpt-bintols/src/hedu/mod.rs index 0b99706..6ffa67a 100644 --- a/members/libpt-bintols/src/hedu/mod.rs +++ b/members/libpt-bintols/src/hedu/mod.rs @@ -27,7 +27,7 @@ pub struct HeduConfig { rd_counter: usize, buf: [[u8; BYTES_PER_LINE]; 2], alt_buf: usize, - display_buf: String, + pub display_buf: String, } impl HeduConfig { @@ -38,7 +38,7 @@ impl HeduConfig { show_identical, limit, stop: false, - len: usize::MIN, + len: BYTES_PER_LINE, data_idx: usize::MIN, rd_counter: usize::MIN, buf: [[0; BYTES_PER_LINE]; 2], @@ -47,10 +47,24 @@ impl HeduConfig { } } #[inline] - fn display(&mut self) { + pub fn display(&mut self) { println!("{}", self.display_buf); self.display_buf = String::new(); } + #[inline] + pub fn sep(&mut self) { + if self.chars { + self.display_buf += &format!("{LINE_SEP_HORIZ}").repeat(80); + } else { + self.display_buf += &format!("{LINE_SEP_HORIZ}").repeat(59); + } + self.display(); + } + #[inline] + pub fn newline(&mut self) { + self.display_buf += "\n"; + self.display(); + } } pub trait DataSource: Read { @@ -82,20 +96,15 @@ pub fn dump(data: &mut dyn DataSource, config: &mut HeduConfig) -> Result<()> { // print the head config.display_buf += &format!("DATA IDX {LINE_SEP_VERT} DATA AS HEX"); if config.chars { - config.display_buf += &format!("{:width$} {LINE_SEP_VERT}", "", width = 37); - } - config.display(); - if config.chars { - config.display_buf += &format!("{LINE_SEP_HORIZ}").repeat(80); - } else { - config.display_buf += &format!("{LINE_SEP_HORIZ}").repeat(59); + config.display_buf += &format!("{:width$} {LINE_SEP_VERT} DATA AS CHAR", "", width = 37); } config.display(); + config.sep(); // data dump loop - rd_data(data, config)?; while config.len > 0 { config.display_buf += &format!("{:08X} {LINE_SEP_VERT} ", config.data_idx); + rd_data(data, config)?; for i in 0..config.len { if i as usize % BYTES_PER_LINE == BYTES_PER_LINE / 2 { config.display_buf += " "; @@ -127,7 +136,6 @@ pub fn dump(data: &mut dyn DataSource, config: &mut HeduConfig) -> Result<()> { } // after line logic - rd_data(data, config)?; config.alt_buf ^= 1; // toggle the alt buf if config.buf[0] == config.buf[1] && config.len == BYTES_PER_LINE && !config.show_identical { @@ -139,11 +147,14 @@ pub fn dump(data: &mut dyn DataSource, config: &mut HeduConfig) -> Result<()> { while config.buf[0] == config.buf[1] && config.len == BYTES_PER_LINE { rd_data(data, config)?; } - config.alt_buf ^= 1; // toggle the alt buf (now that we have a not same line) config.display_buf += &format!( - "^^^^^^^^ {LINE_SEP_VERT} (repeats {} lines)", - (config.data_idx - start_line) / (BYTES_PER_LINE * 2) + "^^^^^^^^ {LINE_SEP_VERT} (repeats {} lines){:32}", + (config.data_idx - start_line) / (BYTES_PER_LINE * 2), + "" ); + if config.chars { + config.display_buf += &format!("{LINE_SEP_VERT}"); + } trace!( buf = format!("{:X?}", config.buf), "dumping buf after line skip" @@ -154,6 +165,31 @@ pub fn dump(data: &mut dyn DataSource, config: &mut HeduConfig) -> Result<()> { // We detect duplicate lines with this config.alt_buf ^= 1; // toggle the alt buf } + config.data_idx += config.len; + + config.sep(); + config.display_buf += &format!( + "{:08X} {LINE_SEP_VERT} dumped total:\t{:<8} {:<16}{:3}", + config.rd_counter, + humanbytes(config.rd_counter), + format!("({} B)",config.rd_counter), + "" + ); + if config.chars { + config.display_buf += &format!("{LINE_SEP_VERT}"); + } + config.display(); + config.display_buf += &format!( + "{:08X} {LINE_SEP_VERT} read total:\t\t{:<8} {:<16}{:3}", + config.data_idx, + humanbytes(config.data_idx), + format!("({} B)",config.data_idx), + "" + ); + if config.chars { + config.display_buf += &format!("{LINE_SEP_VERT}"); + } + config.display(); Ok(()) } @@ -173,7 +209,7 @@ fn mask_chars(c: char) -> char { #[inline] fn adjust_data_idx(config: &mut HeduConfig) { - config.data_idx += BYTES_PER_LINE - config.data_idx % BYTES_PER_LINE; + config.data_idx -= config.data_idx % BYTES_PER_LINE; } fn rd_data(data: &mut dyn DataSource, config: &mut HeduConfig) -> Result<()> { diff --git a/src/hedu/mod.rs b/src/hedu/mod.rs index f8f8165..52547c0 100644 --- a/src/hedu/mod.rs +++ b/src/hedu/mod.rs @@ -104,7 +104,7 @@ fn main() { if !data_source.is_file() { debug!("Not a regular file'{:?}'", data_source); // std::process::exit(1); - continue + continue; } trace!("Trying to open '{:?}'", data_source); match File::open(&data_source) { @@ -128,8 +128,15 @@ fn main() { sources.push(Box::new(stdin)); } for (i, source) in sources.iter_mut().enumerate() { - println!("{:=^59}", format!(" {} ", cli.data_source[i])); let mut config = HeduConfig::new(cli.chars, cli.skip, cli.show_identical, cli.limit); + match config.chars { + false => { + println!("{:─^59}", format!(" {} ", cli.data_source[i])); + } + true => { + println!("{:─^80}", format!(" {} ", cli.data_source[i])); + } + } match dump(&mut **source, &mut config) { Ok(_) => (), Err(err) => { @@ -137,6 +144,9 @@ fn main() { std::process::exit(3); } } + if i < cli.data_source.len() - 1 { + config.newline(); + } } } ////////////////////////////////////////////////////////////////////////////////////////////////////