diff options
author | J Sitkevich <1553398+sitkevij@users.noreply.github.com> | 2021-08-28 13:07:56 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-28 13:07:56 -0700 |
commit | 8acc552292815ebac7bca012789a07efd383d343 (patch) | |
tree | ca511a1206f327cf5c6f52c9d450e27a373e2262 | |
parent | fa6944822c91954949c13271a0e12990daae0a9c (diff) | |
parent | aa5e279a0ef1e394f8e2ca1ca42577ce65e3fa92 (diff) |
Merge pull request #61 from sitkevij/developv0.4.2
#60 chore: merge develop for release
-rw-r--r-- | Cargo.lock | 2 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | src/lib.rs | 49 |
4 files changed, 38 insertions, 19 deletions
@@ -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", @@ -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 @@ -92,14 +92,14 @@ If `<USERDIR>/.cargo/bin` is part of the `PATH` environment variable, `hx` shoul ### 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 @@ -86,7 +86,7 @@ pub struct Line { /// hex body pub hex_body: Vec<u8>, /// ascii text - pub ascii: Vec<char>, + pub ascii: Vec<u8>, /// 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<u8>, 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<dyn Error>> { 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<dyn Error>> { )?; } + 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); } |