Add ASCII character table

closes #36
This commit is contained in:
David Peter 2023-12-11 23:22:36 +01:00 committed by David Peter
parent 711627bfe8
commit 45fa72ec3b
3 changed files with 79 additions and 12 deletions

View file

@ -26,6 +26,7 @@ pub enum ByteCategory {
#[non_exhaustive]
pub enum CharacterTable {
Default,
Ascii,
CP437,
}
@ -83,6 +84,14 @@ impl Byte {
AsciiOther => '•',
NonAscii => '×',
},
CharacterTable::Ascii => match self.category() {
Null => '.',
AsciiPrintable => self.0 as char,
AsciiWhitespace if self.0 == 0x20 => ' ',
AsciiWhitespace => '.',
AsciiOther => '.',
NonAscii => '.',
},
CharacterTable::CP437 => CP437[self.0 as usize],
}
}

View file

@ -144,6 +144,21 @@ fn run() -> Result<()> {
.action(ArgAction::SetTrue)
.help("Show the character panel on the right. This is the default, unless --no-characters has been specified."),
)
.arg(
Arg::new("character-table")
.long("character-table")
.value_name("FORMAT")
.value_parser(["default", "ascii", "codepage-437"])
.default_value("default")
.help(
"Defines how bytes are mapped to characters:\n \
\"default\": show printable ASCII characters as-is, '⋄' for NULL bytes, \
' ' for space, '_' for other ASCII whitespace, \
'•' for other ASCII characters, and '×' for non-ASCII bytes.\n \
\"ascii\": show printable ASCII as-is, ' ' for space, '.' for everything else.\n \
\"codepage-437\": uses code page 437 (for non-ASCII bytes).\n"
),
)
.arg(
Arg::new("no_position")
.short('P')
@ -213,18 +228,6 @@ fn run() -> Result<()> {
.hide(true)
.help("An alias for '--endianness=little'."),
)
.arg(
Arg::new("character-table")
.long("character-table")
.value_name("FORMAT")
.value_parser(["default", "codepage-437"])
.default_value("default")
.help(
"The character table that should be used. 'default' \
will show dots for non-ASCII characters, 'codepage-437' \
will use code page 437 for those characters."
),
)
.arg(
Arg::new("base")
.short('b')
@ -497,6 +500,7 @@ fn run() -> Result<()> {
.as_ref()
{
"default" => CharacterTable::Default,
"ascii" => CharacterTable::Ascii,
"codepage-437" => CharacterTable::CP437,
_ => unreachable!(),
};

View file

@ -623,6 +623,60 @@ mod character_table {
use super::hexyl;
use super::PrettyAssert;
#[test]
fn ascii() {
hexyl()
.arg("hello_world_elf64")
.arg("--color=never")
.arg("--character-table=ascii")
.assert()
.success()
.pretty_stdout(
"┌────────┬─────────────────────────┬─────────────────────────┬────────┬────────┐
00000000 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 .ELF............
00000010 02 00 3e 00 01 00 00 00 00 10 40 00 00 00 00 00 ..>.......@.....
00000020 40 00 00 00 00 00 00 00 28 20 00 00 00 00 00 00 @.......( ......
00000030 00 00 00 00 40 00 38 00 03 00 40 00 04 00 03 00 ....@.8...@.....
00000040 01 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 ................
00000050 00 00 40 00 00 00 00 00 00 00 40 00 00 00 00 00 ..@.......@.....
00000060 e8 00 00 00 00 00 00 00 e8 00 00 00 00 00 00 00 ................
00000070 00 10 00 00 00 00 00 00 01 00 00 00 05 00 00 00 ................
00000080 00 10 00 00 00 00 00 00 00 10 40 00 00 00 00 00 ..........@.....
00000090 00 10 40 00 00 00 00 00 1d 00 00 00 00 00 00 00 ..@.............
000000a0 1d 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00 ................
000000b0 01 00 00 00 06 00 00 00 00 20 00 00 00 00 00 00 ......... ......
000000c0 00 20 40 00 00 00 00 00 00 20 40 00 00 00 00 00 . @...... @.....
000000d0 0e 00 00 00 00 00 00 00 0e 00 00 00 00 00 00 00 ................
000000e0 00 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000000f0 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 ................
*
00002000 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 0a 00 2e Hello, world!...
00002010 73 68 73 74 72 74 61 62 00 2e 74 65 78 74 00 2e shstrtab..text..
00002020 64 61 74 61 00 00 00 00 00 00 00 00 00 00 00 00 data............
00002030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
*
00002060 00 00 00 00 00 00 00 00 0b 00 00 00 01 00 00 00 ................
00002070 06 00 00 00 00 00 00 00 00 10 40 00 00 00 00 00 ..........@.....
00002080 00 10 00 00 00 00 00 00 1d 00 00 00 00 00 00 00 ................
00002090 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 ................
000020a0 00 00 00 00 00 00 00 00 11 00 00 00 01 00 00 00 ................
000020b0 03 00 00 00 00 00 00 00 00 20 40 00 00 00 00 00 ......... @.....
000020c0 00 20 00 00 00 00 00 00 0e 00 00 00 00 00 00 00 . ..............
000020d0 00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 ................
000020e0 00 00 00 00 00 00 00 00 01 00 00 00 03 00 00 00 ................
000020f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00002100 0e 20 00 00 00 00 00 00 17 00 00 00 00 00 00 00 . ..............
00002110 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 ................
00002120 00 00 00 00 00 00 00 00 ........
",
);
}
#[test]
fn codepage_437() {
hexyl()