From 70fa092df122b97b1de0cb26e29514445a9390ed Mon Sep 17 00:00:00 2001 From: David Peter Date: Wed, 7 Dec 2022 21:30:10 +0100 Subject: [PATCH 1/3] Do not fail if output is not a TTY for `--panels=auto` closes #184 --- CHANGELOG.md | 11 +++++++++++ src/bin/hexyl.rs | 11 +++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5a1944..c7c0453 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,14 @@ +# unreleased + +## Features + +- Only show one panel by default if the terminal width is not wide enough for two panels, see #182 (@sharkdp) + +## Bugfixes + +- Do not fail with an error if `--panels=auto` is used and the output is piped, see #184 (@sharkdp) + + # v0.11.0 ## Features diff --git a/src/bin/hexyl.rs b/src/bin/hexyl.rs index 3ecf1b0..42d4768 100644 --- a/src/bin/hexyl.rs +++ b/src/bin/hexyl.rs @@ -163,7 +163,8 @@ fn run() -> Result<()> { .help( "Sets the number of hex data panels to be displayed. \ `--panels=auto` will display the maximum number of hex data panels \ - based on the current terminal width", + based on the current terminal width. By default, hexyl will show \ + two panels, unless the terminal is not wide enough for that.", ), ) .arg( @@ -389,12 +390,10 @@ fn run() -> Result<()> { 1 }; + let terminal_width = terminal_size().map(|s| s.0 .0 as u64).unwrap_or(80); + let panels = if matches.get_one::("panels").map(String::as_ref) == Some("auto") { - max_panels_fn( - terminal_size().ok_or_else(|| anyhow!("not a TTY"))?.0 .0 as u64, - base_digits, - group_bytes.into(), - ) + max_panels_fn(terminal_width, base_digits, group_bytes.into()) } else if let Some(panels) = matches .get_one::("panels") .map(|s| { From 437a15f3d680d78c2c2889bddb7a7270a7a12d06 Mon Sep 17 00:00:00 2001 From: David Peter Date: Wed, 7 Dec 2022 21:35:34 +0100 Subject: [PATCH 2/3] One panel by default is terminal is not wide enough closes #182 --- src/bin/hexyl.rs | 5 ++++- tests/integration_tests.rs | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/bin/hexyl.rs b/src/bin/hexyl.rs index 42d4768..5855b3c 100644 --- a/src/bin/hexyl.rs +++ b/src/bin/hexyl.rs @@ -417,7 +417,10 @@ fn run() -> Result<()> { { max_panels_fn(terminal_width, base_digits, group_bytes.into()) } else { - 2 + std::cmp::min( + 2, + max_panels_fn(terminal_width, base_digits, group_bytes.into()), + ) }; let stdout = io::stdout(); diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index d07831e..028f67b 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -565,7 +565,8 @@ mod base { .assert() .success() .pretty_stdout( - " 00110000 00110001 00110010 00110011 00110100 00110101 00110110 00110111 00111000 00111001 01100001 01100010 01100011 01100100 01100101 00001010 \n" + " 00110000 00110001 00110010 00110011 00110100 00110101 00110110 00110111 \n \ + 00111000 00111001 01100001 01100010 01100011 01100100 01100101 00001010 \n", ); } } From 1df35eeb6312ff642922ca2185b53fd2f06d3ef7 Mon Sep 17 00:00:00 2001 From: David Peter Date: Wed, 7 Dec 2022 21:50:12 +0100 Subject: [PATCH 3/3] Renme --group-bytes to --group-size For `xxd`-compatibility reasons see #121 --- CHANGELOG.md | 8 ++++++++ examples/simple.rs | 2 +- src/bin/hexyl.rs | 36 +++++++++++++++++++----------------- src/lib.rs | 24 ++++++++++++------------ tests/integration_tests.rs | 18 +++++++++--------- 5 files changed, 49 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c7c0453..74d2f09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,14 @@ - Do not fail with an error if `--panels=auto` is used and the output is piped, see #184 (@sharkdp) +## Changes + +- Breaking: For `xxd`-compatibility reasons, `--group_bytes` has been renamed to `--group-size` (with an `--groupsize` alias), see #121 (@sharkdp) + +## `hexyl` as a library + +- Breaking: `num_group_bytes` has been renamed to `group_size`. + # v0.11.0 diff --git a/examples/simple.rs b/examples/simple.rs index 4f4b671..f90a620 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -18,7 +18,7 @@ fn main() { .with_border_style(BorderStyle::Unicode) .enable_squeezing(false) .num_panels(2) - .num_group_bytes(1) + .group_size(1) .build(); printer.print_all(&input[..]).unwrap(); } diff --git a/src/bin/hexyl.rs b/src/bin/hexyl.rs index 5855b3c..c91d3a4 100644 --- a/src/bin/hexyl.rs +++ b/src/bin/hexyl.rs @@ -168,14 +168,16 @@ fn run() -> Result<()> { ), ) .arg( - Arg::new("group_bytes") + Arg::new("group_size") .short('g') - .long("group-bytes") + .long("group-size") + .alias("groupsize") .num_args(1) .value_name("N") .help( - "Sets the number of octets per group to be displayed. \ - The possible options are 1, 2, 4, 8. The default is 1", + "Number of bytes/octets that should be grouped together. \ + Possible group sizes are 1, 2, 4, 8. The default is 1. \ + '--groupsize can be used as an alias (xxd-compatibility).", ), ) .arg( @@ -323,12 +325,12 @@ fn run() -> Result<()> { .transpose()? .unwrap_or(0); - let max_panels_fn = |terminal_width: u64, base_digits: u64, group_bytes: u64| { + let max_panels_fn = |terminal_width: u64, base_digits: u64, group_size: u64| { let offset = if show_position_panel { 10 } else { 1 }; let col_width = if show_char_panel { - ((8 / group_bytes) * (base_digits * group_bytes + 1)) + 2 + 8 + ((8 / group_size) * (base_digits * group_size + 1)) + 2 + 8 } else { - ((8 / group_bytes) * (base_digits * group_bytes + 1)) + 2 + ((8 / group_size) * (base_digits * group_size + 1)) + 2 }; if (terminal_width - offset) / col_width < 1 { 1 @@ -369,21 +371,21 @@ fn run() -> Result<()> { Base::Hexadecimal => 2, }; - let group_bytes = if let Some(group_bytes) = matches - .get_one::("group_bytes") + let group_size = if let Some(group_size) = matches + .get_one::("group_size") .map(|s| { s.parse::().map(u8::from).context(anyhow!( - "failed to parse `--group-bytes`/`-g` arg {:?} as unsigned nonzero integer", + "Failed to parse `--group-size`/`-g` argument {:?} as unsigned nonzero integer", s )) }) .transpose()? { - if (group_bytes <= 8) && ((group_bytes & (group_bytes - 1)) == 0) { - group_bytes + if (group_size <= 8) && ((group_size & (group_size - 1)) == 0) { + group_size } else { return Err(anyhow!( - "Possible options for the `--group-bytes`/`-g` option are 1, 2, 4 or 8. " + "Possible sizes for the `--group-size`/`-g` option are 1, 2, 4 or 8. " )); } } else { @@ -393,7 +395,7 @@ fn run() -> Result<()> { let terminal_width = terminal_size().map(|s| s.0 .0 as u64).unwrap_or(80); let panels = if matches.get_one::("panels").map(String::as_ref) == Some("auto") { - max_panels_fn(terminal_width, base_digits, group_bytes.into()) + max_panels_fn(terminal_width, base_digits, group_size.into()) } else if let Some(panels) = matches .get_one::("panels") .map(|s| { @@ -415,11 +417,11 @@ fn run() -> Result<()> { }) .transpose()? { - max_panels_fn(terminal_width, base_digits, group_bytes.into()) + max_panels_fn(terminal_width, base_digits, group_size.into()) } else { std::cmp::min( 2, - max_panels_fn(terminal_width, base_digits, group_bytes.into()), + max_panels_fn(terminal_width, base_digits, group_size.into()), ) }; @@ -433,7 +435,7 @@ fn run() -> Result<()> { .with_border_style(border_style) .enable_squeezing(squeeze) .num_panels(panels) - .num_group_bytes(group_bytes) + .group_size(group_size) .with_base(base) .build(); printer.display_offset(skip_offset + display_offset); diff --git a/src/lib.rs b/src/lib.rs index 46f8503..a9a4c5a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -168,7 +168,7 @@ pub struct PrinterBuilder<'a, Writer: Write> { border_style: BorderStyle, use_squeeze: bool, panels: u64, - group_bytes: u8, + group_size: u8, base: Base, } @@ -182,7 +182,7 @@ impl<'a, Writer: Write> PrinterBuilder<'a, Writer> { border_style: BorderStyle::Unicode, use_squeeze: true, panels: 2, - group_bytes: 1, + group_size: 1, base: Base::Hexadecimal, } } @@ -217,8 +217,8 @@ impl<'a, Writer: Write> PrinterBuilder<'a, Writer> { self } - pub fn num_group_bytes(mut self, num: u8) -> Self { - self.group_bytes = num; + pub fn group_size(mut self, num: u8) -> Self { + self.group_size = num; self } @@ -236,7 +236,7 @@ impl<'a, Writer: Write> PrinterBuilder<'a, Writer> { self.border_style, self.use_squeeze, self.panels, - self.group_bytes, + self.group_size, self.base, ) } @@ -264,7 +264,7 @@ pub struct Printer<'a, Writer: Write> { panels: u64, squeeze_byte: usize, /// The number of octets per group. - group_bytes: u8, + group_size: u8, /// The number of digits used to write the base. base_digits: u8, } @@ -278,7 +278,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> { border_style: BorderStyle, use_squeeze: bool, panels: u64, - group_bytes: u8, + group_size: u8, base: Base, ) -> Printer<'a, Writer> { Printer { @@ -322,7 +322,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> { display_offset: 0, panels, squeeze_byte: 0x00, - group_bytes, + group_size, base_digits: match base { Base::Binary => 8, Base::Octal => 3, @@ -339,8 +339,8 @@ impl<'a, Writer: Write> Printer<'a, Writer> { fn panel_sz(&self) -> usize { // add one to include the trailing space of a group - let group_sz = self.base_digits as usize * self.group_bytes as usize + 1; - let group_per_panel = 8 / self.group_bytes as usize; + let group_sz = self.base_digits as usize * self.group_size as usize + 1; + let group_per_panel = 8 / self.group_size as usize; // add one to include the leading space 1 + group_sz * group_per_panel } @@ -510,7 +510,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> { .write_all(self.colors[ByteColor::Reset as usize].as_bytes())?; } } else { - if i % (self.group_bytes as usize) == 0 { + if i % (self.group_size as usize) == 0 { self.writer.write_all(b" ")?; } } @@ -520,7 +520,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> { } Squeezer::Delete => self.writer.write_all(b" ")?, Squeezer::Ignore | Squeezer::Disabled => { - if i % (self.group_bytes as usize) == 0 { + if i % (self.group_size as usize) == 0 { self.writer.write_all(b" ")?; } if self.show_color && self.curr_color != Some(Byte(b).color()) { diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 028f67b..fda9daf 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -301,7 +301,7 @@ mod group { hexyl() .arg("ascii") .arg("--color=never") - .arg("--group-bytes=2") + .arg("--group-size=2") .assert() .success() .stdout( @@ -316,7 +316,7 @@ mod group { hexyl() .arg("ascii") .arg("--color=never") - .arg("--group-bytes=4") + .arg("--group-size=4") .assert() .success() .stdout( @@ -331,7 +331,7 @@ mod group { hexyl() .arg("ascii") .arg("--color=never") - .arg("--group-bytes=8") + .arg("--group-size=8") .assert() .success() .stdout( @@ -342,22 +342,22 @@ mod group { } #[test] - fn group_bytes_plain() { + fn group_size_plain() { hexyl() .arg("ascii") .arg("--color=never") .arg("--plain") - .arg("--group-bytes=2") + .arg("--group-size=2") .assert() .success() .stdout(" 3031 3233 3435 3637 3839 6162 6364 650a \n"); } #[test] - fn group_bytes_fill_space() { + fn group_size_fill_space() { hexyl() .arg("--color=never") - .arg("--group-bytes=2") + .arg("--group-size=2") .write_stdin("abc") .assert() .success() @@ -369,12 +369,12 @@ mod group { } #[test] - fn group_bytes_invalid() { + fn group_size_invalid() { hexyl() .arg("ascii") .arg("--color=never") .arg("--plain") - .arg("--group-bytes=3") + .arg("--group-size=3") .assert() .failure(); }