From 70a00fb6956a975abf0c9b0ed17d41149b1fccf3 Mon Sep 17 00:00:00 2001 From: Levente Polyak Date: Sat, 7 Aug 2021 19:27:59 +0200 Subject: feat: colorize ascii for easier visual mapping of bytes to chars Signed-off-by: Levente Polyak --- src/lib.rs | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 37634e3..8c2cda0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -86,7 +86,7 @@ pub struct Line { /// hex body pub hex_body: Vec, /// ascii text - pub ascii: Vec, + pub ascii: Vec, /// total bytes in Line pub bytes: u64, } @@ -162,12 +162,9 @@ pub fn hex_binary(b: u8) -> String { /// print byte to std out pub fn print_byte(w: &mut impl Write, b: u8, format: Format, colorize: bool) -> io::Result<()> { - let mut color: u8 = b; - if color < 1 { - color = 0x16; - } if colorize { // note, for color testing: for (( i = 0; i < 256; i++ )); do echo "$(tput setaf $i)This is ($i) $(tput sgr0)"; done + let color = byte_to_color(b); match format { Format::Octal => write!( w, @@ -210,6 +207,32 @@ pub fn print_byte(w: &mut impl Write, b: u8, format: Format, colorize: bool) -> } } +/// get the color for a specific byte +pub fn byte_to_color(b: u8) -> u8 { + let mut color: u8 = b; + if color < 1 { + color = 0x16; + } + color +} + +/// append char representation of a byte to a buffer +pub fn append_ascii(target: &mut Vec, b: u8, colorize: bool) { + let char = match b > 31 && b < 127 { + true => b as char, + false => '.', + }; + + if colorize { + let string = ansi_term::Style::new() + .fg(ansi_term::Color::Fixed(byte_to_color(b))) + .paint(char.to_string()); + target.extend(format!("{}", string).as_bytes()); + } else { + target.extend(format!("{}", char).as_bytes()); + } +} + /// In most hex editor applications, the data of the computer file is /// represented as hexadecimal values grouped in 4 groups of 4 bytes (or /// two groups of 8 bytes), followed by one group of 16 printable ASCII @@ -316,12 +339,7 @@ pub fn run(matches: ArgMatches) -> Result<(), Box> { offset_counter += 1; byte_column += 1; print_byte(&mut locked, *hex, format_out, colorize)?; - - if *hex > 31 && *hex < 127 { - ascii_line.ascii.push(*hex as char); - } else { - ascii_line.ascii.push('.'); - } + append_ascii(&mut ascii_line.ascii, *hex, colorize); } if byte_column < column_width { @@ -333,10 +351,11 @@ pub fn run(matches: ArgMatches) -> Result<(), Box> { )?; } + locked.write_all(ascii_line.ascii.as_slice())?; + writeln!(locked)?; + byte_column = 0x0; - let ascii_string: String = ascii_line.ascii.iter().cloned().collect(); ascii_line = Line::new(); - writeln!(locked, "{}", ascii_string)?; // print ascii string } if true { writeln!(locked, " bytes: {}", page.bytes)?; @@ -534,8 +553,8 @@ mod tests { #[test] fn test_line_struct() { let mut ascii_line: Line = Line::new(); - ascii_line.ascii.push('.'); - assert_eq!(ascii_line.ascii[0], '.'); + ascii_line.ascii.push(b'.'); + assert_eq!(ascii_line.ascii[0], b'.'); assert_eq!(ascii_line.offset, 0x0); } -- cgit v1.2.3 From 09a2ee7e239b2b49a631ebf6307e8af2bd57b89d Mon Sep 17 00:00:00 2001 From: sitkevij <1553398+sitkevij@users.noreply.github.com> Date: Wed, 18 Aug 2021 22:03:24 -0700 Subject: chore: update develop with latest --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b5e0007..485074e 100644 --- a/README.md +++ b/README.md @@ -90,10 +90,16 @@ Which will compile the release version, run tests and install release binary to If `/.cargo/bin` is part of the `PATH` environment variable, `hx` should be able to be executed anywhere in the shell. +### arch linux install + +```sh +pacman -S hex +``` + ### debian install ```sh -curl -sLO https://github.com/sitkevij/hex/releases/download/v0.4.1/hx_0.4.1_amd64.deb && dpkg -i hx_0.4.1_amd64.deb +curl -sLO https://github.com/sitkevij/hex/releases/download/v0.4.2/hx_0.4.2_amd64.deb && dpkg -i hx_0.4.2_amd64.deb ``` ### docker run -- cgit v1.2.3 From aa5e279a0ef1e394f8e2ca1ca42577ce65e3fa92 Mon Sep 17 00:00:00 2001 From: sitkevij <1553398+sitkevij@users.noreply.github.com> Date: Sat, 28 Aug 2021 12:52:53 -0700 Subject: #60 chore: increment release version --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4407e28..7343a34 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -77,7 +77,7 @@ checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" [[package]] name = "hx" -version = "0.4.1" +version = "0.4.2" dependencies = [ "ansi_term 0.12.1", "assert_cmd", diff --git a/Cargo.toml b/Cargo.toml index 78069bf..88caf11 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ include = ["src/**/*","Cargo.toml","Cargo.lock", "README.md"] license = "MIT" name = "hx" readme ="README.md" -version = "0.4.1" +version = "0.4.2" edition = "2018" # see https://doc.rust-lang.org/cargo/reference/manifest.html -- cgit v1.2.3