Support byte grouping

This commit is contained in:
RinHizakura authored and David Peter committed 2022-11-26 13:07:06 +01:00
1 parent 9106b899b2
commit 179c8f40a2
4 files changed
+75 -12

No files matched your search

+32 -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')
.takes_value(true)
.value_name("N")
.help(
"Sets the number of octets per group to be displayed. \
The possible option will be 1, 2, 4, 8, otherwise it will be set \
to 1 by default.",
),
)
.arg(
Arg::new("terminal_width")
.long("terminal-width")
@@ -337,6 +348,25 @@ fn run() -> Result<(), AnyhowError> {
2
};
let group_bytes = if let Some(group_bytes) = matches
.value_of("group_bytes")
.map(|s| {
s.parse::<NonZeroU8>().map(u8::from).context(anyhow!(
"failed to parse `-g` arg {:?} as unsigned nonzero integer",
s
))
})
.transpose()?
{
if (group_bytes <= 8) && ((group_bytes & (group_bytes - 1)) == 0) {
group_bytes
} else {
1
}
} else {
1
};
let stdout = io::stdout();
let mut stdout_lock = BufWriter::new(stdout.lock());
@@ -347,6 +377,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))?;