Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::{io, fs, env};
- use ansi_term::Colour::RGB;
- #[derive(Debug)]
- struct Hex {
- content: Vec<u8>,
- }
- impl Hex {
- fn new(file: &str) -> io::Result<Self> { //check documentation for reference
- let content = fs::read(file)?;
- Ok(Self {
- content,
- })
- }
- fn show(&self) {
- println!("╔══════════╦═════════════════════════════════════════════════╦══════════════════╗");
- println!("║ Offset ║ 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ║ Content ║");
- println!("╠══════════╬═════════════════════════════════════════════════╬══════════════════╣");
- let mut offset = 0;
- let mut hexdump = self.content.iter();
- let match_color = |byte, src: String| {
- match byte {
- 0 => print!("{}", RGB(200, 200, 200).paint(src)),
- 1..=31 => print!("{}", RGB(255, 150, 0).paint(src)),
- 32..=126 => print!("{}", RGB(0, 200, 100).paint(src)),
- 127..=160 => print!("{}", RGB(100, 0, 100).paint(src)),
- _ => print!("{}", RGB(50, 100, 100).paint(src)),
- }
- };
- self.content.chunks(16).for_each(|chunk| {
- print!("║ {:08X} ║ ", offset);
- for _ in 0..16 {
- match hexdump.next() {
- Some(b) => {
- let byte = format!("{:02X}", b);
- match_color(*b, byte);
- },
- _ => print!(" "),
- }
- print!(" ");
- }
- print!("║ ");
- chunk.iter().for_each(|b| {
- let src = match b {
- 0..=31 | 127..=160 => ".".to_string(),
- _ => (*b as char).to_string(),
- };
- match_color(*b, src);
- });
- println!(" ║");
- offset += 0x10;
- });
- print!("╚══════════╩═══════════════════════════════════════════════════════╩═══════════╝")
- }
- }
- /* fn help() {
- println!("A CLI hex viewer.\n");
- println!("USAGE:");
- println!(" hexview.exe [COMMAND]\n");
- println!("COMMANDS:");
- println!(" view\t View the raw bytes.");
- println!(" help\t Show this help output.")
- } */
- fn main() {
- let args: Vec<String> = env::args().collect();
- match args.get(1) {
- Some(p) => {
- let hex = Hex::new(p);
- if let Ok(c) = hex {
- c.show();
- }
- },
- _ => println!("Help command is WIP, enter a file as argument"),
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement