From b40d31bb274ff3b6845bb4daffadea4862a94d72 Mon Sep 17 00:00:00 2001 From: jgardona Date: Mon, 15 Jan 2024 10:12:52 -0300 Subject: [PATCH 1/3] Fix tests and add ansistream dependency Removed owo_colors in favor of ansistream by its buffered nature. --- Cargo.toml | 4 +- src/cli/view.rs | 97 ++++++++++++++++++++++++++++++------------------- 2 files changed, 62 insertions(+), 39 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 262c0b6..1e7145c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mhv" -version = "0.3.3" +version = "0.3.4" edition = "2021" description = "A minimalist hex viewer" authors = ["Julio C. B. Gardona "] @@ -13,7 +13,7 @@ categories = ["command-line-utilities"] [dependencies] clap = { version = "4.4.8", features = ["derive"]} -owo-colors = "3.5.0" +ansistream = "0.1.6" [dev-dependencies] assert_cmd = "2.0.12" diff --git a/src/cli/view.rs b/src/cli/view.rs index 40100f2..9a3263a 100644 --- a/src/cli/view.rs +++ b/src/cli/view.rs @@ -1,5 +1,5 @@ -use owo_colors::*; -use std::io::{Cursor, Write}; +use ansistream::*; +use std::io::Write; const OFFSET: usize = 16; static EMPTY_LINE: &str = "*"; @@ -11,7 +11,7 @@ pub fn display_data( writer: &mut W, ) -> std::io::Result<()> { let mut position = skip; - let mut output = Cursor::new(Vec::::new()); + let mut output = AnsiEscapeStream::default(); let mut old_buffer = [0u8; 16]; let mut is_printing = false; let mut first_line = true; @@ -24,19 +24,33 @@ pub fn display_data( &mut is_printing, &mut first_line, ) { - write!(output, "{:08x} ", position.bright_black()).unwrap(); + output + .write_text_fc_fmt(FC_DARK_GRAY, format_args!("{:08x} ", position)) + .unwrap(); for b in d { match *b { // null byte - 0x00 => write!(output, "{} ", "00".bright_black()).unwrap(), - 0xff => write!(output, "{} ", "ff".bright_red()).unwrap(), + 0x00 => output + .write_text_fc_fmt(FC_DARK_GRAY, format_args!("00 ")) + .unwrap(), + 0xff => output + .write_text_fc_fmt(FC_LIGHT_RED, format_args!("FF ")) + .unwrap(), // ascii printable characters - 0x21..=0x7e => write!(output, "{:02x} ", b.blue()).unwrap(), + 0x21..=0x7e => output + .write_text_fc_fmt(FC_BLUE, format_args!("{:02x} ", b)) + .unwrap(), // ascii white space characters and controls - 0x01..=0x08 | 0x0e..=0x1f => write!(output, "{:02x} ", b.green()).unwrap(), - 0x09..=0x0d | 0x20 | 0x7f => write!(output, "{:02x} ", b.green()).unwrap(), + 0x01..=0x08 | 0x0e..=0x1f => output + .write_text_fc_fmt(FC_GREEN, format_args!("{:02x} ", b)) + .unwrap(), + 0x09..=0x0d | 0x20 | 0x7f => output + .write_text_fc_fmt(FC_GREEN, format_args!("{:02x} ", b)) + .unwrap(), // ascii extended codes - 0x80..=0xfe => write!(output, "{:02x} ", b.bright_red()).unwrap(), + 0x80..=0xfe => output + .write_text_fc_fmt(FC_LIGHT_RED, format_args!("{:02x} ", b)) + .unwrap(), } } @@ -46,12 +60,24 @@ pub fn display_data( for b in d { match *b { - 0x00 => write!(output, "{}", "◦".bright_black()).unwrap(), - 0xff => write!(output, "{}", "×".bright_red()).unwrap(), - 0x21..=0x7e => write!(output, "{}", (*b as char).blue()).unwrap(), - 0x09..=0x0d | 0x20 | 0x7f => write!(output, "{}", "_".green()).unwrap(), - 0x01..=0x08 | 0x0e..=0x1f => write!(output, "{}", "•".green()).unwrap(), - 0x80..=0xfe => write!(output, "{}", "×".bright_red()).unwrap(), + 0x00 => output + .write_text_fc_fmt(FC_DARK_GRAY, format_args!("◦")) + .unwrap(), + 0xff => output + .write_text_fc_fmt(FC_LIGHT_RED, format_args!("×")) + .unwrap(), + 0x21..=0x7e => output + .write_text_fc_fmt(FC_BLUE, format_args!("{}", *b as char)) + .unwrap(), + 0x09..=0x0d | 0x20 | 0x7f => output + .write_text_fc_fmt(FC_GREEN, format_args!("_")) + .unwrap(), + 0x01..=0x08 | 0x0e..=0x1f => output + .write_text_fc_fmt(FC_GREEN, format_args!("•")) + .unwrap(), + 0x80..=0xfe => output + .write_text_fc_fmt(FC_LIGHT_RED, format_args!("×")) + .unwrap(), } } writeln!(output).unwrap(); @@ -60,15 +86,15 @@ pub fn display_data( }); output.set_position(0); - std::io::copy(&mut output, writer)?; + std::io::copy(&mut *output, writer).unwrap(); Ok(()) } -fn squeeze<'a, W: Write>( +fn squeeze<'a>( new_buffer: &'a [u8], old_buffer: &'a mut [u8], - writer: &mut W, + writer: &mut AnsiEscapeStream, squeezing: bool, printed: &mut bool, first_line: &mut bool, @@ -82,7 +108,10 @@ fn squeeze<'a, W: Write>( return false; } if !*printed { - writeln!(writer, "{}", EMPTY_LINE.bright_black()).expect("cant write to writer"); + //writeln!(writer).expect("cant write to writer"); + writer + .write_text_fc_fmt(FC_DARK_GRAY, format_args!("{EMPTY_LINE}\n")) + .expect("cant write to writer"); *printed = true; } @@ -102,15 +131,16 @@ mod test_view { use std::{io::Cursor, ops::RangeInclusive}; use super::{display_data, squeeze}; + use ansistream::AnsiEscapeStream; use anyhow::{Ok, Result}; - const OFFSET: RangeInclusive = RangeInclusive::new(0, 17); - const FIRST_CHAR: RangeInclusive = RangeInclusive::new(19, 30); + const OFFSET: RangeInclusive = RangeInclusive::new(0, 18); + const FIRST_CHAR: RangeInclusive = RangeInclusive::new(19, 31); #[test] fn test_squeeze() { let new_buffer = vec![0u8; 32]; - let mut cursor = Cursor::new(Vec::::new()); + let mut cursor = AnsiEscapeStream::default(); let mut old_buffer = [0u8; 16]; let mut is_print = false; let mut first_line = true; @@ -128,14 +158,14 @@ mod test_view { assert_eq!(d, old_buffer); } }); - let striped_buffer = strip_ansi_escapes::strip(cursor.into_inner()); + let striped_buffer = strip_ansi_escapes::strip(cursor.get_ref().to_vec()); assert_eq!("*\n", String::from_utf8_lossy(&striped_buffer)); } #[test] fn test_squeeze_5_bytes() { let new_buffer = vec![1, 1, 1, 1, 4, 5]; - let mut cursor = Cursor::new(Vec::::new()); + let mut cursor = AnsiEscapeStream::default(); let mut old_buffer = [0u8; 16]; let mut is_print = false; let mut first_line = true; @@ -152,7 +182,7 @@ mod test_view { assert_eq!(d, old_buffer); } }); - assert_eq!("", String::from_utf8_lossy(&cursor.into_inner())); + assert_eq!("", String::from_utf8_lossy(&cursor.get_ref().to_vec())); } #[test] @@ -182,8 +212,7 @@ mod test_view { let result = output.get_ref(); let offset_data = &result[OFFSET]; let expect = [ - 0x1b, 0x5b, 0x39, 0x30, 0x6d, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x1b, - 0x5b, 0x33, 0x39, 0x6d, + 27, 91, 57, 48, 109, 48, 48, 48, 48, 48, 48, 48, 48, 32, 27, 91, 51, 57, 109, ]; // prove output offset section data is eight zeros with ansi code bright black color assert_eq!(expect, offset_data); @@ -198,9 +227,7 @@ mod test_view { let result = output.get_ref(); let first_char = &result[FIRST_CHAR]; // ansi code red 0x05 ascii control - let expected = [ - 0x1b, 0x5b, 0x33, 0x32, 0x6d, 0x30, 0x35, 0x1b, 0x5b, 0x33, 0x39, 0x6d, - ]; + let expected = [27, 91, 51, 50, 109, 48, 53, 32, 27, 91, 51, 57, 109]; assert_eq!(expected, first_char); Ok(()) } @@ -213,9 +240,7 @@ mod test_view { let result = output.get_ref(); let first_char = &result[FIRST_CHAR]; // ansi code green 0x05 ascii control - let expected = [ - 0x1b, 0x5b, 0x33, 0x32, 0x6d, 0x32, 0x30, 0x1b, 0x5b, 0x33, 0x39, 0x6d, - ]; + let expected = [27, 91, 51, 50, 109, 50, 48, 32, 27, 91, 51, 57, 109]; assert_eq!(expected, first_char); Ok(()) } @@ -228,9 +253,7 @@ mod test_view { let result = output.get_ref(); let first_char = &result[FIRST_CHAR]; // ansi code red 0x05 ascii extended - let expected = [ - 0x1b, 0x5b, 0x39, 0x31, 0x6d, 0x38, 0x30, 0x1b, 0x5b, 0x33, 0x39, 0x6d, - ]; + let expected = [27, 91, 57, 49, 109, 56, 48, 32, 27, 91, 51, 57, 109]; assert_eq!(expected, first_char); Ok(()) } From 9125dc4a94e3a2fcba3bca2d2db19007c03cc72f Mon Sep 17 00:00:00 2001 From: jgardona Date: Mon, 15 Jan 2024 10:14:15 -0300 Subject: [PATCH 2/3] Revert "Fix tests and add ansistream dependency" This reverts commit b40d31bb274ff3b6845bb4daffadea4862a94d72. --- Cargo.toml | 4 +- src/cli/view.rs | 97 +++++++++++++++++++------------------------------ 2 files changed, 39 insertions(+), 62 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1e7145c..262c0b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mhv" -version = "0.3.4" +version = "0.3.3" edition = "2021" description = "A minimalist hex viewer" authors = ["Julio C. B. Gardona "] @@ -13,7 +13,7 @@ categories = ["command-line-utilities"] [dependencies] clap = { version = "4.4.8", features = ["derive"]} -ansistream = "0.1.6" +owo-colors = "3.5.0" [dev-dependencies] assert_cmd = "2.0.12" diff --git a/src/cli/view.rs b/src/cli/view.rs index 9a3263a..40100f2 100644 --- a/src/cli/view.rs +++ b/src/cli/view.rs @@ -1,5 +1,5 @@ -use ansistream::*; -use std::io::Write; +use owo_colors::*; +use std::io::{Cursor, Write}; const OFFSET: usize = 16; static EMPTY_LINE: &str = "*"; @@ -11,7 +11,7 @@ pub fn display_data( writer: &mut W, ) -> std::io::Result<()> { let mut position = skip; - let mut output = AnsiEscapeStream::default(); + let mut output = Cursor::new(Vec::::new()); let mut old_buffer = [0u8; 16]; let mut is_printing = false; let mut first_line = true; @@ -24,33 +24,19 @@ pub fn display_data( &mut is_printing, &mut first_line, ) { - output - .write_text_fc_fmt(FC_DARK_GRAY, format_args!("{:08x} ", position)) - .unwrap(); + write!(output, "{:08x} ", position.bright_black()).unwrap(); for b in d { match *b { // null byte - 0x00 => output - .write_text_fc_fmt(FC_DARK_GRAY, format_args!("00 ")) - .unwrap(), - 0xff => output - .write_text_fc_fmt(FC_LIGHT_RED, format_args!("FF ")) - .unwrap(), + 0x00 => write!(output, "{} ", "00".bright_black()).unwrap(), + 0xff => write!(output, "{} ", "ff".bright_red()).unwrap(), // ascii printable characters - 0x21..=0x7e => output - .write_text_fc_fmt(FC_BLUE, format_args!("{:02x} ", b)) - .unwrap(), + 0x21..=0x7e => write!(output, "{:02x} ", b.blue()).unwrap(), // ascii white space characters and controls - 0x01..=0x08 | 0x0e..=0x1f => output - .write_text_fc_fmt(FC_GREEN, format_args!("{:02x} ", b)) - .unwrap(), - 0x09..=0x0d | 0x20 | 0x7f => output - .write_text_fc_fmt(FC_GREEN, format_args!("{:02x} ", b)) - .unwrap(), + 0x01..=0x08 | 0x0e..=0x1f => write!(output, "{:02x} ", b.green()).unwrap(), + 0x09..=0x0d | 0x20 | 0x7f => write!(output, "{:02x} ", b.green()).unwrap(), // ascii extended codes - 0x80..=0xfe => output - .write_text_fc_fmt(FC_LIGHT_RED, format_args!("{:02x} ", b)) - .unwrap(), + 0x80..=0xfe => write!(output, "{:02x} ", b.bright_red()).unwrap(), } } @@ -60,24 +46,12 @@ pub fn display_data( for b in d { match *b { - 0x00 => output - .write_text_fc_fmt(FC_DARK_GRAY, format_args!("◦")) - .unwrap(), - 0xff => output - .write_text_fc_fmt(FC_LIGHT_RED, format_args!("×")) - .unwrap(), - 0x21..=0x7e => output - .write_text_fc_fmt(FC_BLUE, format_args!("{}", *b as char)) - .unwrap(), - 0x09..=0x0d | 0x20 | 0x7f => output - .write_text_fc_fmt(FC_GREEN, format_args!("_")) - .unwrap(), - 0x01..=0x08 | 0x0e..=0x1f => output - .write_text_fc_fmt(FC_GREEN, format_args!("•")) - .unwrap(), - 0x80..=0xfe => output - .write_text_fc_fmt(FC_LIGHT_RED, format_args!("×")) - .unwrap(), + 0x00 => write!(output, "{}", "◦".bright_black()).unwrap(), + 0xff => write!(output, "{}", "×".bright_red()).unwrap(), + 0x21..=0x7e => write!(output, "{}", (*b as char).blue()).unwrap(), + 0x09..=0x0d | 0x20 | 0x7f => write!(output, "{}", "_".green()).unwrap(), + 0x01..=0x08 | 0x0e..=0x1f => write!(output, "{}", "•".green()).unwrap(), + 0x80..=0xfe => write!(output, "{}", "×".bright_red()).unwrap(), } } writeln!(output).unwrap(); @@ -86,15 +60,15 @@ pub fn display_data( }); output.set_position(0); - std::io::copy(&mut *output, writer).unwrap(); + std::io::copy(&mut output, writer)?; Ok(()) } -fn squeeze<'a>( +fn squeeze<'a, W: Write>( new_buffer: &'a [u8], old_buffer: &'a mut [u8], - writer: &mut AnsiEscapeStream, + writer: &mut W, squeezing: bool, printed: &mut bool, first_line: &mut bool, @@ -108,10 +82,7 @@ fn squeeze<'a>( return false; } if !*printed { - //writeln!(writer).expect("cant write to writer"); - writer - .write_text_fc_fmt(FC_DARK_GRAY, format_args!("{EMPTY_LINE}\n")) - .expect("cant write to writer"); + writeln!(writer, "{}", EMPTY_LINE.bright_black()).expect("cant write to writer"); *printed = true; } @@ -131,16 +102,15 @@ mod test_view { use std::{io::Cursor, ops::RangeInclusive}; use super::{display_data, squeeze}; - use ansistream::AnsiEscapeStream; use anyhow::{Ok, Result}; - const OFFSET: RangeInclusive = RangeInclusive::new(0, 18); - const FIRST_CHAR: RangeInclusive = RangeInclusive::new(19, 31); + const OFFSET: RangeInclusive = RangeInclusive::new(0, 17); + const FIRST_CHAR: RangeInclusive = RangeInclusive::new(19, 30); #[test] fn test_squeeze() { let new_buffer = vec![0u8; 32]; - let mut cursor = AnsiEscapeStream::default(); + let mut cursor = Cursor::new(Vec::::new()); let mut old_buffer = [0u8; 16]; let mut is_print = false; let mut first_line = true; @@ -158,14 +128,14 @@ mod test_view { assert_eq!(d, old_buffer); } }); - let striped_buffer = strip_ansi_escapes::strip(cursor.get_ref().to_vec()); + let striped_buffer = strip_ansi_escapes::strip(cursor.into_inner()); assert_eq!("*\n", String::from_utf8_lossy(&striped_buffer)); } #[test] fn test_squeeze_5_bytes() { let new_buffer = vec![1, 1, 1, 1, 4, 5]; - let mut cursor = AnsiEscapeStream::default(); + let mut cursor = Cursor::new(Vec::::new()); let mut old_buffer = [0u8; 16]; let mut is_print = false; let mut first_line = true; @@ -182,7 +152,7 @@ mod test_view { assert_eq!(d, old_buffer); } }); - assert_eq!("", String::from_utf8_lossy(&cursor.get_ref().to_vec())); + assert_eq!("", String::from_utf8_lossy(&cursor.into_inner())); } #[test] @@ -212,7 +182,8 @@ mod test_view { let result = output.get_ref(); let offset_data = &result[OFFSET]; let expect = [ - 27, 91, 57, 48, 109, 48, 48, 48, 48, 48, 48, 48, 48, 32, 27, 91, 51, 57, 109, + 0x1b, 0x5b, 0x39, 0x30, 0x6d, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x1b, + 0x5b, 0x33, 0x39, 0x6d, ]; // prove output offset section data is eight zeros with ansi code bright black color assert_eq!(expect, offset_data); @@ -227,7 +198,9 @@ mod test_view { let result = output.get_ref(); let first_char = &result[FIRST_CHAR]; // ansi code red 0x05 ascii control - let expected = [27, 91, 51, 50, 109, 48, 53, 32, 27, 91, 51, 57, 109]; + let expected = [ + 0x1b, 0x5b, 0x33, 0x32, 0x6d, 0x30, 0x35, 0x1b, 0x5b, 0x33, 0x39, 0x6d, + ]; assert_eq!(expected, first_char); Ok(()) } @@ -240,7 +213,9 @@ mod test_view { let result = output.get_ref(); let first_char = &result[FIRST_CHAR]; // ansi code green 0x05 ascii control - let expected = [27, 91, 51, 50, 109, 50, 48, 32, 27, 91, 51, 57, 109]; + let expected = [ + 0x1b, 0x5b, 0x33, 0x32, 0x6d, 0x32, 0x30, 0x1b, 0x5b, 0x33, 0x39, 0x6d, + ]; assert_eq!(expected, first_char); Ok(()) } @@ -253,7 +228,9 @@ mod test_view { let result = output.get_ref(); let first_char = &result[FIRST_CHAR]; // ansi code red 0x05 ascii extended - let expected = [27, 91, 57, 49, 109, 56, 48, 32, 27, 91, 51, 57, 109]; + let expected = [ + 0x1b, 0x5b, 0x39, 0x31, 0x6d, 0x38, 0x30, 0x1b, 0x5b, 0x33, 0x39, 0x6d, + ]; assert_eq!(expected, first_char); Ok(()) } From 03ae4099137a5907b474aefc5d1e5ae6136265b3 Mon Sep 17 00:00:00 2001 From: jgardona Date: Mon, 15 Jan 2024 18:15:42 -0300 Subject: [PATCH 3/3] Change owo version owo-colors version updated. --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 262c0b6..5bac74f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mhv" -version = "0.3.3" +version = "0.3.4" edition = "2021" description = "A minimalist hex viewer" authors = ["Julio C. B. Gardona "] @@ -13,7 +13,7 @@ categories = ["command-line-utilities"] [dependencies] clap = { version = "4.4.8", features = ["derive"]} -owo-colors = "3.5.0" +owo-colors = "4.0.0" [dev-dependencies] assert_cmd = "2.0.12"