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
## Features
+1 -1
View File
@@ -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();
}
+26 -22
View File
@@ -161,18 +161,21 @@ 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(
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(
@@ -322,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
@@ -368,33 +371,31 @@ fn run() -> Result<()> {
Base::Hexadecimal => 2,
};
let group_bytes = if let Some(group_bytes) = matches
.get_one::<String>("group_bytes")
let group_size = if let Some(group_size) = matches
.get_one::<String>("group_size")
.map(|s| {
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
))
})
.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 {
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") {
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_size.into())
} else if let Some(panels) = matches
.get_one::<String>("panels")
.map(|s| {
@@ -416,9 +417,12 @@ 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 {
2
std::cmp::min(
2,
max_panels_fn(terminal_width, base_digits, group_size.into()),
)
};
let stdout = io::stdout();
@@ -431,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);
+5 -5
View File
@@ -157,7 +157,7 @@ pub struct PrinterBuilder<'a, Writer: Write> {
border_style: BorderStyle,
use_squeeze: bool,
panels: u64,
group_bytes: u8,
group_size: u8,
base: Base,
}
@@ -171,7 +171,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,
}
}
@@ -206,8 +206,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
}
@@ -225,7 +225,7 @@ impl<'a, Writer: Write> PrinterBuilder<'a, Writer> {
self.border_style,
self.use_squeeze,
self.panels,
self.group_bytes,
self.group_size,
self.base,
)
}
+11 -10
View File
@@ -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();
}
@@ -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",
);
}
}