Merge branch 'master' into owo

This commit is contained in:
sharif authored and GitHub committed 2022-12-07 16:51:30 -05:00
commit fd4aee2b85
5 files changed
+62 -38

No files matched your search

+19
View File
@@ -1,3 +1,22 @@
# 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)
## 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
## Features ## Features
+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();
} }
+26 -22
View File
@@ -161,18 +161,21 @@ fn run() -> Result<()> {
.help( .help(
"Sets the number of hex data panels to be displayed. \ "Sets the number of hex data panels to be displayed. \
`--panels=auto` will display the maximum number of hex data panels \ `--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( .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(
@@ -322,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
@@ -368,33 +371,31 @@ 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 {
1 1
}; };
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( max_panels_fn(terminal_width, base_digits, group_size.into())
terminal_size().ok_or_else(|| anyhow!("not a TTY"))?.0 .0 as u64,
base_digits,
group_bytes.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| {
@@ -416,9 +417,12 @@ 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 {
2 std::cmp::min(
2,
max_panels_fn(terminal_width, base_digits, group_size.into()),
)
}; };
let stdout = io::stdout(); let stdout = io::stdout();
@@ -431,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);
+5 -5
View File
@@ -157,7 +157,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,
} }
@@ -171,7 +171,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,
} }
} }
@@ -206,8 +206,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
} }
@@ -225,7 +225,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,
) )
} }
+11 -10
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();
} }
@@ -565,7 +565,8 @@ mod base {
.assert() .assert()
.success() .success()
.pretty_stdout( .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",
); );
} }
} }