Merge branch 'master' into anstyle

This commit is contained in:
Sharif Haason committed 2022-11-27 10:03:53 -05:00
commit 252b4e84c3
7 files changed
+404 -12

No files matched your search

+2
View File
@@ -9,6 +9,8 @@
- Added variable panels through the `--panels` and `--terminal-width` flags, see [#13](https://github.com/sharkdp/hexyl/issues/13) and [#164](https://github.com/sharkdp/hexyl/pull/164) (@sharifhsn)
- Added new `--group-bytes`/`-g` option, see #104 and #170 (@RinHizakura)
## Bugfixes
Generated
+44
View File
@@ -122,6 +122,22 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "ctor"
version = "0.1.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d2301688392eb071b0bf1a37be05c469d3cc4dbbd95df672fe28ab021e6a096"
dependencies = [
"quote",
"syn",
]
[[package]]
name = "diff"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8"
[[package]]
name = "difflib"
version = "0.4.0"
@@ -200,6 +216,7 @@ dependencies = [
"const_format",
"libc",
"predicates",
"pretty_assertions",
"terminal_size",
"thiserror",
]
@@ -292,6 +309,15 @@ version = "6.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee"
[[package]]
name = "output_vt100"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "628223faebab4e3e40667ee0b2336d34a5b960ff60ea743ddfdbcf7770bcfb66"
dependencies = [
"winapi",
]
[[package]]
name = "predicates"
version = "2.1.3"
@@ -322,6 +348,18 @@ dependencies = [
"termtree",
]
[[package]]
name = "pretty_assertions"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a25e9bcb20aa780fd0bb16b72403a9064d6b3f22f026946029acb941a50af755"
dependencies = [
"ctor",
"diff",
"output_vt100",
"yansi",
]
[[package]]
name = "proc-macro2"
version = "1.0.47"
@@ -567,3 +605,9 @@ name = "windows_x86_64_msvc"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5"
[[package]]
name = "yansi"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec"
+1
View File
@@ -27,6 +27,7 @@ features = ["std", "suggestions", "color", "wrap_help", "cargo", "help", "usage"
[dev-dependencies]
assert_cmd = "2.0"
predicates = "2.0"
pretty_assertions = "1.3.0"
[profile.release]
lto = true
+1
View File
@@ -18,6 +18,7 @@ fn main() {
.with_border_style(BorderStyle::Unicode)
.enable_squeezing(false)
.num_panels(2)
.num_group_bytes(1)
.build();
printer.print_all(&input[..]).unwrap();
}
+34 -1
View File
@@ -4,7 +4,7 @@ extern crate clap;
use std::convert::TryFrom;
use std::fs::File;
use std::io::{self, prelude::*, BufWriter, SeekFrom};
use std::num::{NonZeroI64, NonZeroU64};
use std::num::{NonZeroI64, NonZeroU64, NonZeroU8};
use clap::builder::ArgPredicate;
use clap::{crate_name, crate_version, Arg, ArgAction, ColorChoice, Command};
@@ -166,6 +166,17 @@ fn run() -> Result<(), AnyhowError> {
based on the current terminal width",
),
)
.arg(
Arg::new("group_bytes")
.short('g')
.long("group-bytes")
.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",
),
)
.arg(
Arg::new("terminal_width")
.long("terminal-width")
@@ -337,6 +348,27 @@ fn run() -> Result<(), AnyhowError> {
2
};
let group_bytes = if let Some(group_bytes) = matches
.get_one::<String>("group_bytes")
.map(|s| {
s.parse::<NonZeroU8>().map(u8::from).context(anyhow!(
"failed to parse `--group-bytes`/`-g` arg {:?} as unsigned nonzero integer",
s
))
})
.transpose()?
{
if (group_bytes <= 8) && ((group_bytes & (group_bytes - 1)) == 0) {
group_bytes
} else {
return Err(anyhow!(
"Possible options for the `--group-bytes`/`-g` option are 1, 2, 4 or 8. "
));
}
} else {
1
};
let stdout = io::stdout();
let mut stdout_lock = BufWriter::new(stdout.lock());
@@ -347,6 +379,7 @@ fn run() -> Result<(), AnyhowError> {
.with_border_style(border_style)
.enable_squeezing(squeeze)
.num_panels(panels)
.num_group_bytes(group_bytes)
.build();
printer.display_offset(skip_offset + display_offset);
printer.print_all(&mut reader).map_err(|e| anyhow!(e))?;
+40 -9
View File
@@ -161,6 +161,7 @@ pub struct PrinterBuilder<'a, Writer: Write> {
border_style: BorderStyle,
use_squeeze: bool,
panels: u64,
group_bytes: u8,
}
impl<'a, Writer: Write> PrinterBuilder<'a, Writer> {
@@ -173,6 +174,7 @@ impl<'a, Writer: Write> PrinterBuilder<'a, Writer> {
border_style: BorderStyle::Unicode,
use_squeeze: true,
panels: 2,
group_bytes: 1,
}
}
@@ -206,6 +208,11 @@ impl<'a, Writer: Write> PrinterBuilder<'a, Writer> {
self
}
pub fn num_group_bytes(mut self, num: u8) -> Self {
self.group_bytes = num;
self
}
pub fn build(self) -> Printer<'a, Writer> {
Printer::new(
self.writer,
@@ -215,6 +222,7 @@ impl<'a, Writer: Write> PrinterBuilder<'a, Writer> {
self.border_style,
self.use_squeeze,
self.panels,
self.group_bytes,
)
}
}
@@ -240,6 +248,8 @@ pub struct Printer<'a, Writer: Write> {
/// The number of panels to draw.
panels: u64,
squeeze_byte: usize,
/// The number of octets per group.
group_bytes: u8,
}
impl<'a, Writer: Write> Printer<'a, Writer> {
@@ -251,6 +261,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
border_style: BorderStyle,
use_squeeze: bool,
panels: u64,
group_bytes: u8,
) -> Printer<'a, Writer> {
Printer {
idx: 0,
@@ -286,6 +297,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
display_offset: 0,
panels,
squeeze_byte: 0x00,
group_bytes,
}
}
@@ -294,13 +306,21 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
self
}
fn panel_sz(&self) -> usize {
// add one to include the trailing space of a group
let group_sz = 2 * self.group_bytes as usize + 1;
let group_per_panel = 8 / self.group_bytes as usize;
// add one to include the leading space
1 + group_sz * group_per_panel
}
fn write_border(&mut self, border_elements: BorderElements) -> io::Result<()> {
let h = border_elements.horizontal_line;
let c = border_elements.column_separator;
let l = border_elements.left_corner;
let r = border_elements.right_corner;
let h8 = h.to_string().repeat(8);
let h25 = h.to_string().repeat(25);
let h_repeat = h.to_string().repeat(self.panel_sz());
if self.show_position_panel {
write!(self.writer, "{l}{h8}{c}", l = l, c = c, h8 = h8).ok();
@@ -309,12 +329,12 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
}
for _ in 0..self.panels - 1 {
write!(self.writer, "{h25}{c}", h25 = h25, c = c).ok();
write!(self.writer, "{h_repeat}{c}", h_repeat = h_repeat, c = c).ok();
}
if self.show_char_panel {
write!(self.writer, "{h25}{c}", h25 = h25, c = c).ok();
write!(self.writer, "{h_repeat}{c}", h_repeat = h_repeat, c = c).ok();
} else {
write!(self.writer, "{h25}", h25 = h25).ok();
write!(self.writer, "{h_repeat}", h_repeat = h_repeat).ok();
}
if self.show_char_panel {
@@ -459,13 +479,17 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
.write_all(self.colors[ByteColor::Reset as usize].as_bytes())?;
}
} else {
self.writer.write_all(b" ")?;
if i % (self.group_bytes as usize) == 0 {
self.writer.write_all(b" ")?;
}
}
self.writer.write_all(b" ")?;
}
Squeezer::Delete => self.writer.write_all(b" ")?,
Squeezer::Ignore | Squeezer::Disabled => {
self.writer.write_all(b" ")?;
if i % (self.group_bytes as usize) == 0 {
self.writer.write_all(b" ")?;
}
if self.show_color && self.curr_color != Some(Byte(b).color()) {
self.writer
.write_all(self.colors[Byte(b).color() as usize].as_bytes())?;
@@ -598,8 +622,11 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
}
write!(
self.writer,
"{0:2}{1:24}{0}{0:>26}",
"│", "No content to print"
"{0:2}{1:2$}{0}{0:>3$}",
"│",
"No content",
self.panel_sz() - 1,
self.panel_sz() + 1,
)?;
if self.show_char_panel {
write!(self.writer, "{0:>9}{0:>9}", "│")?;
@@ -650,6 +677,7 @@ mod tests {
BorderStyle::Unicode,
true,
2,
1,
);
printer.print_all(input).unwrap();
@@ -663,7 +691,7 @@ mod tests {
let input = io::empty();
let expected_string = "\
┌────────┬─────────────────────────┬─────────────────────────┬────────┬────────┐
│ │ No content to print │ │ │ │
│ │ No content │ │ │ │
└────────┴─────────────────────────┴─────────────────────────┴────────┴────────┘
"
.to_owned();
@@ -702,6 +730,7 @@ mod tests {
BorderStyle::Unicode,
true,
2,
1,
);
printer.display_offset(0xdeadbeef);
@@ -733,6 +762,7 @@ mod tests {
BorderStyle::Unicode,
true,
4,
1,
);
printer.print_all(input).unwrap();
@@ -790,6 +820,7 @@ mod tests {
BorderStyle::Unicode,
true,
3,
1,
);
printer.print_all(input).unwrap();
+282 -2
View File
@@ -5,6 +5,26 @@ fn hexyl() -> Command {
cmd.current_dir("tests/examples");
cmd
}
trait PrettyAssert<S>
where
S: AsRef<str>,
{
fn pretty_stdout(self, other: S);
}
// https://github.com/assert-rs/assert_cmd/issues/121#issuecomment-849937376
//
impl<S> PrettyAssert<S> for assert_cmd::assert::Assert
where
S: AsRef<str>,
{
fn pretty_stdout(self, other: S) {
println!("{}", other.as_ref().len());
let self_str = String::from_utf8(self.get_output().stdout.clone()).unwrap();
println!("{}", self_str.len());
pretty_assertions::assert_eq!(self_str, other.as_ref());
}
}
mod basic {
use super::hexyl;
@@ -51,7 +71,7 @@ mod basic {
.success()
.stdout(
"┌────────┬─────────────────────────┬─────────────────────────┬────────┬────────┐\n\
│ │ No content to print │ │ │ │\n\
│ │ No content │ │ │ │\n\
└────────┴─────────────────────────┴─────────────────────────┴────────┴────────┘\n",
);
}
@@ -140,7 +160,7 @@ mod skip {
.success()
.stdout(
"┌────────┬─────────────────────────┬─────────────────────────┬────────┬────────┐\n\
│ │ No content to print │ │ │ │\n\
│ │ No content │ │ │ │\n\
└────────┴─────────────────────────┴─────────────────────────┴────────┴────────┘\n",
);
}
@@ -271,3 +291,263 @@ mod display_settings {
);
}
}
mod group {
use super::hexyl;
use super::PrettyAssert;
#[test]
fn group_2_bytes() {
hexyl()
.arg("ascii")
.arg("--color=never")
.arg("--group-bytes=2")
.assert()
.success()
.stdout(
"┌────────┬─────────────────────┬─────────────────────┬────────┬────────┐\n\
│00000000│ 3031 3233 3435 3637 ┊ 3839 6162 6364 650a │01234567┊89abcde_│\n\
└────────┴─────────────────────┴─────────────────────┴────────┴────────┘\n",
);
}
#[test]
fn group_4_bytes() {
hexyl()
.arg("ascii")
.arg("--color=never")
.arg("--group-bytes=4")
.assert()
.success()
.stdout(
"┌────────┬───────────────────┬───────────────────┬────────┬────────┐\n\
│00000000│ 30313233 34353637 ┊ 38396162 6364650a │01234567┊89abcde_│\n\
└────────┴───────────────────┴───────────────────┴────────┴────────┘\n",
);
}
#[test]
fn group_8_bytes() {
hexyl()
.arg("ascii")
.arg("--color=never")
.arg("--group-bytes=8")
.assert()
.success()
.stdout(
"┌────────┬──────────────────┬──────────────────┬────────┬────────┐\n\
│00000000│ 3031323334353637 ┊ 383961626364650a │01234567┊89abcde_│\n\
└────────┴──────────────────┴──────────────────┴────────┴────────┘\n",
);
}
#[test]
fn group_bytes_plain() {
hexyl()
.arg("ascii")
.arg("--color=never")
.arg("--plain")
.arg("--group-bytes=2")
.assert()
.success()
.stdout(" 3031 3233 3435 3637 3839 6162 6364 650a \n");
}
#[test]
fn group_bytes_fill_space() {
hexyl()
.arg("--color=never")
.arg("--group-bytes=2")
.write_stdin("abc")
.assert()
.success()
.stdout(
"┌────────┬─────────────────────┬─────────────────────┬────────┬────────┐\n\
│00000000│ 6162 63 ┊ │abc ┊ │\n\
└────────┴─────────────────────┴─────────────────────┴────────┴────────┘\n",
);
}
#[test]
fn group_bytes_invalid() {
hexyl()
.arg("ascii")
.arg("--color=never")
.arg("--plain")
.arg("--group-bytes=3")
.assert()
.failure();
}
#[test]
fn squeeze_no_chars() {
hexyl()
.arg("hello_world_elf64")
.arg("--color=never")
.arg("--skip=1024")
.arg("--length=4096")
.arg("--no-characters")
.assert()
.success()
.pretty_stdout(
"\
┌────────┬─────────────────────────┬─────────────────────────┐
│00000400│ 00 00 00 00 00 00 00 00 ┊ 00 00 00 00 00 00 00 00 │
│* │ ┊ │
│00001000│ ba 0e 00 00 00 b9 00 20 ┊ 40 00 bb 01 00 00 00 b8 │
│00001010│ 04 00 00 00 cd 80 b8 01 ┊ 00 00 00 cd 80 00 00 00 │
│00001020│ 00 00 00 00 00 00 00 00 ┊ 00 00 00 00 00 00 00 00 │
│* │ ┊ │
│00001400│ ┊ │
└────────┴─────────────────────────┴─────────────────────────┘
",
);
}
#[test]
fn squeeze_no_chars_one_panel() {
hexyl()
.arg("hello_world_elf64")
.arg("--color=never")
.arg("--skip=1024")
.arg("--length=4096")
.arg("--no-characters")
.arg("--panels=1")
.assert()
.success()
.pretty_stdout(
"\
┌────────┬─────────────────────────┐
│00000400│ 00 00 00 00 00 00 00 00 │
│* │ │
│00001000│ ba 0e 00 00 00 b9 00 20 │
│00001008│ 40 00 bb 01 00 00 00 b8 │
│00001010│ 04 00 00 00 cd 80 b8 01 │
│00001018│ 00 00 00 cd 80 00 00 00 │
│00001020│ 00 00 00 00 00 00 00 00 │
│* │ │
│00001400│ │
└────────┴─────────────────────────┘
",
);
}
#[test]
fn squeeze_no_position() {
hexyl()
.arg("hello_world_elf64")
.arg("--color=never")
.arg("--skip=1024")
.arg("--length=4096")
.arg("--no-position")
.assert()
.success()
.pretty_stdout(
"\
┌─────────────────────────┬─────────────────────────┬────────┬────────┐
│ 00 00 00 00 00 00 00 00 ┊ 00 00 00 00 00 00 00 00 │00000000┊00000000│
│* ┊ │ ┊ │
│ ba 0e 00 00 00 b9 00 20 ┊ 40 00 bb 01 00 00 00 b8 │ו000×0 ┊@0ו000×│
│ 04 00 00 00 cd 80 b8 01 ┊ 00 00 00 cd 80 00 00 00 │•000××ו┊000××000│
│ 00 00 00 00 00 00 00 00 ┊ 00 00 00 00 00 00 00 00 │00000000┊00000000│
│* ┊ │ ┊ │
│* ┊ │ ┊ │
└─────────────────────────┴─────────────────────────┴────────┴────────┘
",
);
}
#[test]
fn squeeze_no_position_one_panel() {
hexyl()
.arg("hello_world_elf64")
.arg("--color=never")
.arg("--skip=1024")
.arg("--length=4096")
.arg("--no-position")
.arg("--panels=1")
.assert()
.success()
.pretty_stdout(
"\
┌─────────────────────────┬────────┐
│ 00 00 00 00 00 00 00 00 │00000000│
│* │ │
│ ba 0e 00 00 00 b9 00 20 │ו000×0 │
│ 40 00 bb 01 00 00 00 b8 │@0ו000×│
│ 04 00 00 00 cd 80 b8 01 │•000××ו│
│ 00 00 00 cd 80 00 00 00 │000××000│
│ 00 00 00 00 00 00 00 00 │00000000│
│* │ │
│* │ │
└─────────────────────────┴────────┘
",
);
}
#[test]
fn squeeze_odd_panels_remainder_bytes() {
hexyl()
.arg("hello_world_elf64")
.arg("--color=never")
.arg("--skip=1024")
.arg("--length=4092") // 4 byte remainder
.arg("--panels=3")
.assert()
.success()
.pretty_stdout(
"\
┌────────┬─────────────────────────┬─────────────────────────┬─────────────────────────┬────────┬────────┬────────┐
│00000400│ 00 00 00 00 00 00 00 00 ┊ 00 00 00 00 00 00 00 00 ┊ 00 00 00 00 00 00 00 00 │00000000┊00000000┊00000000│
│* │ ┊ ┊ │ ┊ ┊ │
│00001000│ ba 0e 00 00 00 b9 00 20 ┊ 40 00 bb 01 00 00 00 b8 ┊ 04 00 00 00 cd 80 b8 01 │ו000×0 ┊@0ו000×┊•000××ו│
│00001018│ 00 00 00 cd 80 00 00 00 ┊ 00 00 00 00 00 00 00 00 ┊ 00 00 00 00 00 00 00 00 │000××000┊00000000┊00000000│
│00001030│ 00 00 00 00 00 00 00 00 ┊ 00 00 00 00 00 00 00 00 ┊ 00 00 00 00 00 00 00 00 │00000000┊00000000┊00000000│
│* │ ┊ ┊ │ ┊ ┊ │
│000013f0│ 00 00 00 00 00 00 00 00 ┊ 00 00 00 00 ┊ │00000000┊0000 ┊ │
└────────┴─────────────────────────┴─────────────────────────┴─────────────────────────┴────────┴────────┴────────┘
",
);
}
#[test]
fn squeeze_plain() {
hexyl()
.arg("hello_world_elf64")
.arg("--color=never")
.arg("--skip=1024")
.arg("--length=4096")
.arg("--plain")
.assert()
.success()
.pretty_stdout(
" \
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
ba 0e 00 00 00 b9 00 20 40 00 bb 01 00 00 00 b8
04 00 00 00 cd 80 b8 01 00 00 00 cd 80 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
*
",
);
}
#[test]
fn squeeze_plain_remainder() {
hexyl()
.arg("hello_world_elf64")
.arg("--color=never")
.arg("--skip=1024")
.arg("--length=4092") // 4 byte remainder
.arg("--plain")
.assert()
.success()
.pretty_stdout(
" \
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
ba 0e 00 00 00 b9 00 20 40 00 bb 01 00 00 00 b8
04 00 00 00 cd 80 b8 01 00 00 00 cd 80 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
00 00 00 00 00 00 00 00 00 00 00 00
",
);
}
}