From 45fa72ec3b171e0034be07bc3c959df6b743e8c1 Mon Sep 17 00:00:00 2001 From: David Peter Date: Mon, 11 Dec 2023 23:22:36 +0100 Subject: Add ASCII character table closes #36 --- src/lib.rs | 9 ++++++++ src/main.rs | 28 +++++++++++++----------- tests/integration_tests.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c63485b..2edae33 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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], } } diff --git a/src/main.rs b/src/main.rs index 7cabed2..ddb4500 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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!(), }; diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index a44c77c..1115528 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -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, w┊orld!...│ +│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() -- cgit v1.2.3