nushell/crates/nu-pretty-hex
Maxim Zhiburt cc8f2b6419
nu-explore/ Use hex-dump for binary data (#12184)
Hi there

So as 2 minute thing we could show `hex-dump` as it is as a string
(no-coloring).

But I'd do some more things around,.
Probably will take a few days (WIP).

```
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

─────────────────────────────────────────────────────────────────────────────────────┬─────────────────────────────────────────────────────────────
  00000000:   6d 6f 64 20  63 6f 6d 6d  61 6e 64 3b  0a 6d 6f 64   mod command;_mod  │
  00000010:   20 63 6f 6e  66 69 67 5f  66 69 6c 65  73 3b 0a 6d    config_files;_m  │
  00000020:   6f 64 20 69  64 65 3b 0a  6d 6f 64 20  6c 6f 67 67   od ide;_mod logg  │
  00000030:   65 72 3b 0a  6d 6f 64 20  72 75 6e 3b  0a 6d 6f 64   er;_mod run;_mod  │
  00000040:   20 73 69 67  6e 61 6c 73  3b 0a 23 5b  63 66 67 28    signals;_#[cfg(  │
  00000050:   75 6e 69 78  29 5d 0a 6d  6f 64 20 74  65 72 6d 69   unix)]_mod termi  │
  00000060:   6e 61 6c 3b  0a 6d 6f 64  20 74 65 73  74 5f 62 69   nal;_mod test_bi  │
  00000070:   6e 73 3b 0a  23 5b 63 66  67 28 74 65  73 74 29 5d   ns;_#[cfg(test)]  │
  00000080:   0a 6d 6f 64  20 74 65 73  74 73 3b 0a  0a 23 5b 63   _mod tests;__#[c  │
  00000090:   66 67 28 66  65 61 74 75  72 65 20 3d  20 22 6d 69   fg(feature = "mi  │
  000000a0:   6d 61 6c 6c  6f 63 22 29  5d 0a 23 5b  67 6c 6f 62   malloc")]_#[glob  │
  000000b0:   61 6c 5f 61  6c 6c 6f 63  61 74 6f 72  5d 0a 73 74   al_allocator]_st  │
  000000c0:   61 74 69 63  20 47 4c 4f  42 41 4c 3a  20 6d 69 6d   atic GLOBAL: mim  │
  000000d0:   61 6c 6c 6f  63 3a 3a 4d  69 4d 61 6c  6c 6f 63 20   alloc::MiMalloc   │
  000000e0:   3d 20 6d 69  6d 61 6c 6c  6f 63 3a 3a  4d 69 4d 61   = mimalloc::MiMa  │
  000000f0:   6c 6c 6f 63  3b 0a 0a 75  73 65 20 63  72 61 74 65   lloc;__use crate  │
  00000100:   3a 3a 7b 0a  20 20 20 20  63 6f 6d 6d  61 6e 64 3a   ::{_    command:  │
  00000110:   3a 70 61 72  73 65 5f 63  6f 6d 6d 61  6e 64 6c 69   :parse_commandli  │
  00000120:   6e 65 5f 61  72 67 73 2c  0a 20 20 20  20 63 6f 6e   ne_args,_    con  │
  00000130:   66 69 67 5f  66 69 6c 65  73 3a 3a 73  65 74 5f 63   fig_files::set_c  │
  00000140:   6f 6e 66 69  67 5f 70 61  74 68 2c 0a  20 20 20 20   onfig_path,_      │
  00000150:   6c 6f 67 67  65 72 3a 3a  7b 63 6f 6e  66 69 67 75   logger::{configu  │
  00000160:   72 65 2c 20  6c 6f 67 67  65 72 7d 2c  0a 7d 3b 0a   re, logger},_};_  │
  00000170:   75 73 65 20  63 6f 6d 6d  61 6e 64 3a  3a 67 61 74   use command::gat  │
  00000180:   68 65 72 5f  63 6f 6d 6d  61 6e 64 6c  69 6e 65 5f   her_commandline_  │
  00000190:   61 72 67 73  3b 0a 75 73  65 20 6c 6f  67 3a 3a 4c   args;_use log::L  │
  000001a0:   65 76 65 6c  3b 0a 75 73  65 20 6d 69  65 74 74 65   evel;_use miette  │
  000001b0:   3a 3a 52 65  73 75 6c 74  3b 0a 75 73  65 20 6e 75   ::Result;_use nu  │
  000001c0:   5f 63 6c 69  3a 3a 67 61  74 68 65 72  5f 70 61 72   _cli::gather_par  │

```

ref: #12157
cc: @fdncred @lrdickson
2024-03-21 19:02:03 -05:00
..
examples Make the example names unique across workspace (#7046) 2022-11-07 09:00:21 +01:00
src nu-explore/ Use hex-dump for binary data (#12184) 2024-03-21 19:02:03 -05:00
tests Fix typos and use more idiomatic assertions (#7755) 2023-01-15 15:03:32 +13:00
Cargo.toml Introduce workspace dependencies (#12043) 2024-03-07 14:40:31 -08:00
LICENSE Fix rest of license year ranges (#8727) 2023-04-04 09:03:29 +12:00
README.md Remove old nushell/merge engine-q 2022-02-07 14:54:06 -05:00

nu-pretty-hex

An update of prett-hex to make it prettier

crates.io docs.rs

A Rust library providing pretty hex dump.

A simple_hex() way renders one-line hex dump, a pretty_hex() way renders columned multi-line hex dump with addressing and ASCII representation. A config_hex() way renders hex dump in specified format.

Inspiration

Hexed
Hexyl
Pretty-hex

Example of simple_hex()

use pretty_hex::*;

let v = vec![222, 173, 190, 239, 202, 254, 32, 24];
assert_eq!(simple_hex(&v), format!("{}", v.hex_dump()));

println!("{}", v.hex_dump());

Output:

de ad be ef  ca fe 20 18

Example of pretty_hex()

use pretty_hex::*;

let v: &[u8] = &random::<[u8;30]>();
assert_eq!(pretty_hex(&v), format!("{:?}", v.hex_dump()));

println!("{:?}", v.hex_dump());

Output:

Length: 30 (0x1e) bytes
0000:   6b 4e 1a c3  af 03 d2 1e  7e 73 ba c8  bd 84 0f 83   kN......~s......
0010:   89 d5 cf 90  23 67 4b 48  db b1 bc 35  bf ee         ....#gKH...5..

Example of config_hex()

use pretty_hex::*;

let cfg = HexConfig {title: false, width: 8, group: 0, ..HexConfig::default() };

let v = &include_bytes!("data");
assert_eq!(config_hex(&v, cfg), format!("{:?}", v.hex_conf(cfg)));

println!("{:?}", v.hex_conf(cfg));

Output:

0000:   6b 4e 1a c3 af 03 d2 1e   kN......
0008:   7e 73 ba c8 bd 84 0f 83   ~s......
0010:   89 d5 cf 90 23 67 4b 48   ....#gKH
0018:   db b1 bc 35 bf ee         ...5..

Inspired by haskell's pretty-hex.