Skip to content

Commit

Permalink
Merge pull request #9 from scottlamb/pr-max-rows
Browse files Browse the repository at this point in the history
Support limiting printed bytes
  • Loading branch information
wolandr authored Apr 16, 2022
2 parents 8f73395 + d0031c5 commit 9ef5b54
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pretty-hex"
version = "0.2.1"
version = "0.3.0"
authors = ["Andrei Volnin <[email protected]>"]
license = "MIT"
description = "Pretty hex dump of bytes slice in the common style."
Expand Down
19 changes: 15 additions & 4 deletions src/pretty_hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ pub struct HexConfig {
pub group: usize,
/// Source bytes per chunk (word). 0 for single word.
pub chunk: usize,
/// Maximum bytes to print.
pub max_bytes: usize,
}

/// Default configuration with `title`, `ascii`, 16 source bytes `width` grouped to 4 separate
Expand All @@ -72,6 +74,7 @@ impl Default for HexConfig {
width: 16,
group: 4,
chunk: 1,
max_bytes: usize::MAX,
}
}
}
Expand Down Expand Up @@ -112,18 +115,23 @@ where
T: AsRef<[u8]> + ?Sized,
W: fmt::Write,
{
let mut source = source.as_ref();
if cfg.title {
writeln!(writer, "Length: {0} (0x{0:x}) bytes", source.as_ref().len())?;
writeln!(writer, "Length: {0} (0x{0:x}) bytes", source.len())?;
}

if source.as_ref().is_empty() {
if source.is_empty() {
return Ok(());
}

let lines = source.as_ref().chunks(if cfg.width > 0 {
let omitted = source.len().checked_sub(cfg.max_bytes);
if omitted.is_some() {
source = &source[..cfg.max_bytes];
}
let lines = source.chunks(if cfg.width > 0 {
cfg.width
} else {
source.as_ref().len()
source.len()
});
let lines_len = lines.len();
for (i, row) in lines.enumerate() {
Expand All @@ -150,6 +158,9 @@ where
writeln!(writer)?;
}
}
if let Some(o) = omitted {
write!(writer, "\n...{0} (0x{0:x}) bytes not shown...", o)?;
}
Ok(())
}

Expand Down
28 changes: 26 additions & 2 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ fn test_config() {
width: 0,
group: 0,
chunk: 0,
max_bytes: usize::MAX,
};
assert!(config_hex(&vec![], cfg).is_empty());
assert_eq!("2425262728", config_hex(&"$%&'(", cfg));
Expand Down Expand Up @@ -108,7 +109,8 @@ fn test_config() {
ascii: true,
width: 11,
group: 2,
chunk: 3
chunk: 3,
max_bytes: usize::MAX,
}),
"0000: 000102 030405 060708 090a ...........\n\
000b: 0b0c .."
Expand All @@ -122,7 +124,8 @@ fn test_config() {
ascii: true,
width: 16,
group: 3,
chunk: 3
chunk: 3,
max_bytes: usize::MAX,
}
),
"0000: 000102 030405 060708 090a0b 0c0d0e 0f ................\n\
Expand All @@ -143,6 +146,27 @@ fn test_config() {
format!("{}", v.hex_conf(cfg)),
"00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12"
);

let cfg = HexConfig {
max_bytes: 16,
..HexConfig::default()
};
assert_eq!(
format!("{:?}", v.hex_conf(cfg)),
"Length: 19 (0x13) bytes\n\
0000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ................\n\
...3 (0x3) bytes not shown..."
);
let cfg = HexConfig {
max_bytes: 4,
..HexConfig::default()
};
assert_eq!(
format!("{:?}", v.hex_conf(cfg)),
"Length: 19 (0x13) bytes\n\
0000: 00 01 02 03 ....\n\
...15 (0xf) bytes not shown..."
);
}

#[cfg(feature = "alloc")]
Expand Down

0 comments on commit 9ef5b54

Please sign in to comment.