Renme --group-bytes to --group-size

For `xxd`-compatibility reasons

see #121
This commit is contained in:
David Peter committed 2022-12-07 21:50:12 +01:00
1 parent 437a15f3d6
commit 1df35eeb63
5 files changed
+49 -39

No files matched your search

+8
View File
@@ -8,6 +8,14 @@
- Do not fail with an error if `--panels=auto` is used and the output is piped, see #184 (@sharkdp) - 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 # v0.11.0
+1 -1
View File
@@ -18,7 +18,7 @@ fn main() {
.with_border_style(BorderStyle::Unicode) .with_border_style(BorderStyle::Unicode)
.enable_squeezing(false) .enable_squeezing(false)
.num_panels(2) .num_panels(2)
.num_group_bytes(1) .group_size(1)
.build(); .build();
printer.print_all(&input[..]).unwrap(); printer.print_all(&input[..]).unwrap();
} }
+19 -17
View File
@@ -168,14 +168,16 @@ fn run() -> Result<()> {
), ),
) )
.arg( .arg(
Arg::new("group_bytes") Arg::new("group_size")
.short('g') .short('g')
.long("group-bytes") .long("group-size")
.alias("groupsize")
.num_args(1) .num_args(1)
.value_name("N") .value_name("N")
.help( .help(
"Sets the number of octets per group to be displayed. \ "Number of bytes/octets that should be grouped together. \
The possible options are 1, 2, 4, 8. The default is 1", Possible group sizes are 1, 2, 4, 8. The default is 1. \
'--groupsize can be used as an alias (xxd-compatibility).",
), ),
) )
.arg( .arg(
@@ -323,12 +325,12 @@ fn run() -> Result<()> {
.transpose()? .transpose()?
.unwrap_or(0); .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 offset = if show_position_panel { 10 } else { 1 };
let col_width = if show_char_panel { 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 { } 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 { if (terminal_width - offset) / col_width < 1 {
1 1
@@ -369,21 +371,21 @@ fn run() -> Result<()> {
Base::Hexadecimal => 2, Base::Hexadecimal => 2,
}; };
let group_bytes = if let Some(group_bytes) = matches let group_size = if let Some(group_size) = matches
.get_one::<String>("group_bytes") .get_one::<String>("group_size")
.map(|s| { .map(|s| {
s.parse::<NonZeroU8>().map(u8::from).context(anyhow!( s.parse::<NonZeroU8>().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 s
)) ))
}) })
.transpose()? .transpose()?
{ {
if (group_bytes <= 8) && ((group_bytes & (group_bytes - 1)) == 0) { if (group_size <= 8) && ((group_size & (group_size - 1)) == 0) {
group_bytes group_size
} else { } else {
return Err(anyhow!( 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 { } else {
@@ -393,7 +395,7 @@ fn run() -> Result<()> {
let terminal_width = terminal_size().map(|s| s.0 .0 as u64).unwrap_or(80); let terminal_width = terminal_size().map(|s| s.0 .0 as u64).unwrap_or(80);
let panels = if matches.get_one::<String>("panels").map(String::as_ref) == Some("auto") { let panels = if matches.get_one::<String>("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 } else if let Some(panels) = matches
.get_one::<String>("panels") .get_one::<String>("panels")
.map(|s| { .map(|s| {
@@ -415,11 +417,11 @@ fn run() -> Result<()> {
}) })
.transpose()? .transpose()?
{ {
max_panels_fn(terminal_width, base_digits, group_bytes.into()) max_panels_fn(terminal_width, base_digits, group_size.into())
} else { } else {
std::cmp::min( std::cmp::min(
2, 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) .with_border_style(border_style)
.enable_squeezing(squeeze) .enable_squeezing(squeeze)
.num_panels(panels) .num_panels(panels)
.num_group_bytes(group_bytes) .group_size(group_size)
.with_base(base) .with_base(base)
.build(); .build();
printer.display_offset(skip_offset + display_offset); printer.display_offset(skip_offset + display_offset);
+12 -12
View File
@@ -168,7 +168,7 @@ pub struct PrinterBuilder<'a, Writer: Write> {
border_style: BorderStyle, border_style: BorderStyle,
use_squeeze: bool, use_squeeze: bool,
panels: u64, panels: u64,
group_bytes: u8, group_size: u8,
base: Base, base: Base,
} }
@@ -182,7 +182,7 @@ impl<'a, Writer: Write> PrinterBuilder<'a, Writer> {
border_style: BorderStyle::Unicode, border_style: BorderStyle::Unicode,
use_squeeze: true, use_squeeze: true,
panels: 2, panels: 2,
group_bytes: 1, group_size: 1,
base: Base::Hexadecimal, base: Base::Hexadecimal,
} }
} }
@@ -217,8 +217,8 @@ impl<'a, Writer: Write> PrinterBuilder<'a, Writer> {
self self
} }
pub fn num_group_bytes(mut self, num: u8) -> Self { pub fn group_size(mut self, num: u8) -> Self {
self.group_bytes = num; self.group_size = num;
self self
} }
@@ -236,7 +236,7 @@ impl<'a, Writer: Write> PrinterBuilder<'a, Writer> {
self.border_style, self.border_style,
self.use_squeeze, self.use_squeeze,
self.panels, self.panels,
self.group_bytes, self.group_size,
self.base, self.base,
) )
} }
@@ -264,7 +264,7 @@ pub struct Printer<'a, Writer: Write> {
panels: u64, panels: u64,
squeeze_byte: usize, squeeze_byte: usize,
/// The number of octets per group. /// The number of octets per group.
group_bytes: u8, group_size: u8,
/// The number of digits used to write the base. /// The number of digits used to write the base.
base_digits: u8, base_digits: u8,
} }
@@ -278,7 +278,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
border_style: BorderStyle, border_style: BorderStyle,
use_squeeze: bool, use_squeeze: bool,
panels: u64, panels: u64,
group_bytes: u8, group_size: u8,
base: Base, base: Base,
) -> Printer<'a, Writer> { ) -> Printer<'a, Writer> {
Printer { Printer {
@@ -322,7 +322,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
display_offset: 0, display_offset: 0,
panels, panels,
squeeze_byte: 0x00, squeeze_byte: 0x00,
group_bytes, group_size,
base_digits: match base { base_digits: match base {
Base::Binary => 8, Base::Binary => 8,
Base::Octal => 3, Base::Octal => 3,
@@ -339,8 +339,8 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
fn panel_sz(&self) -> usize { fn panel_sz(&self) -> usize {
// add one to include the trailing space of a group // 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_sz = self.base_digits as usize * self.group_size as usize + 1;
let group_per_panel = 8 / self.group_bytes as usize; let group_per_panel = 8 / self.group_size as usize;
// add one to include the leading space // add one to include the leading space
1 + group_sz * group_per_panel 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())?; .write_all(self.colors[ByteColor::Reset as usize].as_bytes())?;
} }
} else { } else {
if i % (self.group_bytes as usize) == 0 { if i % (self.group_size as usize) == 0 {
self.writer.write_all(b" ")?; self.writer.write_all(b" ")?;
} }
} }
@@ -520,7 +520,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
} }
Squeezer::Delete => self.writer.write_all(b" ")?, Squeezer::Delete => self.writer.write_all(b" ")?,
Squeezer::Ignore | Squeezer::Disabled => { 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" ")?; self.writer.write_all(b" ")?;
} }
if self.show_color && self.curr_color != Some(Byte(b).color()) { if self.show_color && self.curr_color != Some(Byte(b).color()) {
+9 -9
View File
@@ -301,7 +301,7 @@ mod group {
hexyl() hexyl()
.arg("ascii") .arg("ascii")
.arg("--color=never") .arg("--color=never")
.arg("--group-bytes=2") .arg("--group-size=2")
.assert() .assert()
.success() .success()
.stdout( .stdout(
@@ -316,7 +316,7 @@ mod group {
hexyl() hexyl()
.arg("ascii") .arg("ascii")
.arg("--color=never") .arg("--color=never")
.arg("--group-bytes=4") .arg("--group-size=4")
.assert() .assert()
.success() .success()
.stdout( .stdout(
@@ -331,7 +331,7 @@ mod group {
hexyl() hexyl()
.arg("ascii") .arg("ascii")
.arg("--color=never") .arg("--color=never")
.arg("--group-bytes=8") .arg("--group-size=8")
.assert() .assert()
.success() .success()
.stdout( .stdout(
@@ -342,22 +342,22 @@ mod group {
} }
#[test] #[test]
fn group_bytes_plain() { fn group_size_plain() {
hexyl() hexyl()
.arg("ascii") .arg("ascii")
.arg("--color=never") .arg("--color=never")
.arg("--plain") .arg("--plain")
.arg("--group-bytes=2") .arg("--group-size=2")
.assert() .assert()
.success() .success()
.stdout(" 3031 3233 3435 3637 3839 6162 6364 650a \n"); .stdout(" 3031 3233 3435 3637 3839 6162 6364 650a \n");
} }
#[test] #[test]
fn group_bytes_fill_space() { fn group_size_fill_space() {
hexyl() hexyl()
.arg("--color=never") .arg("--color=never")
.arg("--group-bytes=2") .arg("--group-size=2")
.write_stdin("abc") .write_stdin("abc")
.assert() .assert()
.success() .success()
@@ -369,12 +369,12 @@ mod group {
} }
#[test] #[test]
fn group_bytes_invalid() { fn group_size_invalid() {
hexyl() hexyl()
.arg("ascii") .arg("ascii")
.arg("--color=never") .arg("--color=never")
.arg("--plain") .arg("--plain")
.arg("--group-bytes=3") .arg("--group-size=3")
.assert() .assert()
.failure(); .failure();
} }