Refine the support of byte grouping

This commit is contained in:
RinHizakura authored and David Peter committed 2022-11-26 13:07:06 +01:00
1 parent 55fed5e3c0
commit c0f94ad6aa
2 files changed
+30 -17

No files matched your search

+6 -4
View File
@@ -169,12 +169,12 @@ fn run() -> Result<(), AnyhowError> {
.arg( .arg(
Arg::new("group_bytes") Arg::new("group_bytes")
.short('g') .short('g')
.long("group-bytes")
.num_args(1) .num_args(1)
.value_name("N") .value_name("N")
.help( .help(
"Sets the number of octets per group to be displayed. \ "Sets the number of octets per group to be displayed. \
The possible option will be 1, 2, 4, 8, otherwise it will be set \ The possible options are 1, 2, 4, 8. The default is 1",
to 1 by default.",
), ),
) )
.arg( .arg(
@@ -352,7 +352,7 @@ fn run() -> Result<(), AnyhowError> {
.get_one::<String>("group_bytes") .get_one::<String>("group_bytes")
.map(|s| { .map(|s| {
s.parse::<NonZeroU8>().map(u8::from).context(anyhow!( s.parse::<NonZeroU8>().map(u8::from).context(anyhow!(
"failed to parse `-g` arg {:?} as unsigned nonzero integer", "failed to parse `--group-bytes` arg {:?} as unsigned nonzero integer",
s s
)) ))
}) })
@@ -361,7 +361,9 @@ fn run() -> Result<(), AnyhowError> {
if (group_bytes <= 8) && ((group_bytes & (group_bytes - 1)) == 0) { if (group_bytes <= 8) && ((group_bytes & (group_bytes - 1)) == 0) {
group_bytes group_bytes
} else { } else {
1 return Err(anyhow!(
"the possible options of group_bytes could only be 1, 2, 4 or 8. "
));
} }
} else { } else {
1 1
+24 -13
View File
@@ -280,13 +280,13 @@ mod group {
hexyl() hexyl()
.arg("ascii") .arg("ascii")
.arg("--color=never") .arg("--color=never")
.arg("-g=2") .arg("--group-bytes=2")
.assert() .assert()
.success() .success()
.stdout( .stdout(
"┌────────┬─────────────────────┬─────────────────────┬────────┬────────┐\n\ "┌────────┬─────────────────────┬─────────────────────┬────────┬────────┐\n\
│00000000│ 3031 3233 3435 3637 ┊ 3839 6162 6364 650a │01234567┊89abcde_│\n\ │00000000│ 3031 3233 3435 3637 ┊ 3839 6162 6364 650a │01234567┊89abcde_│\n\
└────────┴─────────────────────┴─────────────────────┴────────┴────────┘\n", └────────┴─────────────────────┴─────────────────────┴────────┴────────┘\n",
); );
} }
@@ -295,13 +295,13 @@ mod group {
hexyl() hexyl()
.arg("ascii") .arg("ascii")
.arg("--color=never") .arg("--color=never")
.arg("-g=4") .arg("--group-bytes=4")
.assert() .assert()
.success() .success()
.stdout( .stdout(
"┌────────┬───────────────────┬───────────────────┬────────┬────────┐\n\ "┌────────┬───────────────────┬───────────────────┬────────┬────────┐\n\
│00000000│ 30313233 34353637 ┊ 38396162 6364650a │01234567┊89abcde_│\n\ │00000000│ 30313233 34353637 ┊ 38396162 6364650a │01234567┊89abcde_│\n\
└────────┴───────────────────┴───────────────────┴────────┴────────┘\n", └────────┴───────────────────┴───────────────────┴────────┴────────┘\n",
); );
} }
@@ -310,13 +310,13 @@ mod group {
hexyl() hexyl()
.arg("ascii") .arg("ascii")
.arg("--color=never") .arg("--color=never")
.arg("-g=8") .arg("--group-bytes=8")
.assert() .assert()
.success() .success()
.stdout( .stdout(
"┌────────┬──────────────────┬──────────────────┬────────┬────────┐\n\ "┌────────┬──────────────────┬──────────────────┬────────┬────────┐\n\
│00000000│ 3031323334353637 ┊ 383961626364650a │01234567┊89abcde_│\n\ │00000000│ 3031323334353637 ┊ 383961626364650a │01234567┊89abcde_│\n\
└────────┴──────────────────┴──────────────────┴────────┴────────┘\n", └────────┴──────────────────┴──────────────────┴────────┴────────┘\n",
); );
} }
@@ -326,7 +326,7 @@ mod group {
.arg("ascii") .arg("ascii")
.arg("--color=never") .arg("--color=never")
.arg("--plain") .arg("--plain")
.arg("-g=2") .arg("--group-bytes=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");
@@ -336,14 +336,25 @@ mod group {
fn group_bytes_fill_space() { fn group_bytes_fill_space() {
hexyl() hexyl()
.arg("--color=never") .arg("--color=never")
.arg("-g=2") .arg("--group-bytes=2")
.write_stdin("abc") .write_stdin("abc")
.assert() .assert()
.success() .success()
.stdout( .stdout(
"┌────────┬─────────────────────┬─────────────────────┬────────┬────────┐\n\ "┌────────┬─────────────────────┬─────────────────────┬────────┬────────┐\n\
│00000000│ 6162 63 ┊ │abc ┊ │\n\ │00000000│ 6162 63 ┊ │abc ┊ │\n\
└────────┴─────────────────────┴─────────────────────┴────────┴────────┘\n", └────────┴─────────────────────┴─────────────────────┴────────┴────────┘\n",
); );
} }
#[test]
fn group_bytes_invalid() {
hexyl()
.arg("ascii")
.arg("--color=never")
.arg("--plain")
.arg("--group-bytes=3")
.assert()
.failure();
}
} }