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::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 option will be 1, 2, 4, 8, otherwise it will be set \
to 1 by default.",
The possible options are 1, 2, 4, 8. The default is 1",
),
)
.arg(
@@ -352,7 +352,7 @@ fn run() -> Result<(), AnyhowError> {
.get_one::<String>("group_bytes")
.map(|s| {
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
))
})
@@ -361,7 +361,9 @@ fn run() -> Result<(), AnyhowError> {
if (group_bytes <= 8) && ((group_bytes & (group_bytes - 1)) == 0) {
group_bytes
} else {
1
return Err(anyhow!(
"the possible options of group_bytes could only be 1, 2, 4 or 8. "
));
}
} else {
1
+24 -13
View File
@@ -280,13 +280,13 @@ mod group {
hexyl()
.arg("ascii")
.arg("--color=never")
.arg("-g=2")
.arg("--group-bytes=2")
.assert()
.success()
.stdout(
"┌────────┬─────────────────────┬─────────────────────┬────────┬────────┐\n\
│00000000│ 3031 3233 3435 3637 ┊ 3839 6162 6364 650a │01234567┊89abcde_│\n\
└────────┴─────────────────────┴─────────────────────┴────────┴────────┘\n",
│00000000│ 3031 3233 3435 3637 ┊ 3839 6162 6364 650a │01234567┊89abcde_│\n\
└────────┴─────────────────────┴─────────────────────┴────────┴────────┘\n",
);
}
@@ -295,13 +295,13 @@ mod group {
hexyl()
.arg("ascii")
.arg("--color=never")
.arg("-g=4")
.arg("--group-bytes=4")
.assert()
.success()
.stdout(
"┌────────┬───────────────────┬───────────────────┬────────┬────────┐\n\
│00000000│ 30313233 34353637 ┊ 38396162 6364650a │01234567┊89abcde_│\n\
└────────┴───────────────────┴───────────────────┴────────┴────────┘\n",
│00000000│ 30313233 34353637 ┊ 38396162 6364650a │01234567┊89abcde_│\n\
└────────┴───────────────────┴───────────────────┴────────┴────────┘\n",
);
}
@@ -310,13 +310,13 @@ mod group {
hexyl()
.arg("ascii")
.arg("--color=never")
.arg("-g=8")
.arg("--group-bytes=8")
.assert()
.success()
.stdout(
"┌────────┬──────────────────┬──────────────────┬────────┬────────┐\n\
│00000000│ 3031323334353637 ┊ 383961626364650a │01234567┊89abcde_│\n\
└────────┴──────────────────┴──────────────────┴────────┴────────┘\n",
│00000000│ 3031323334353637 ┊ 383961626364650a │01234567┊89abcde_│\n\
└────────┴──────────────────┴──────────────────┴────────┴────────┘\n",
);
}
@@ -326,7 +326,7 @@ mod group {
.arg("ascii")
.arg("--color=never")
.arg("--plain")
.arg("-g=2")
.arg("--group-bytes=2")
.assert()
.success()
.stdout(" 3031 3233 3435 3637 3839 6162 6364 650a \n");
@@ -336,14 +336,25 @@ mod group {
fn group_bytes_fill_space() {
hexyl()
.arg("--color=never")
.arg("-g=2")
.arg("--group-bytes=2")
.write_stdin("abc")
.assert()
.success()
.stdout(
"┌────────┬─────────────────────┬─────────────────────┬────────┬────────┐\n\
│00000000│ 6162 63 ┊ │abc ┊ │\n\
└────────┴─────────────────────┴─────────────────────┴────────┴────────┘\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();
}
}