use std::fs::File; use std::io; use std::io::BufReader; use std::usize::MAX; mod colormap; mod tohex; use tohex::hexdump; mod tobin; use tobin::revert_hexdump; use clap::Parser; #[derive(Parser)] #[command(version, about = "A simple hexdump tool")] struct Arguments { /// Stop after octets #[arg(short, long)] length: Option, /// Revert hexdump to binary #[arg(short, long)] revert: bool, /// Filename to read, if left out read from stdin filename: Option, } pub fn isatty() -> bool { termion::is_tty(&File::create("/dev/stdout").expect("Could not open `stdout'")) } fn main() { let args = Arguments::parse(); let length = if args.length.is_some() { args.length.unwrap() } else { MAX }; if !args.revert { let reader: Box = if args.filename.is_none() { Box::new(io::stdin()) } else { let filename = args.filename.unwrap(); Box::new(File::open(filename).expect("Could not open file.")) }; hexdump(reader, length); } else { let reader: Box = if args.filename.is_none() { Box::new(io::stdin().lock()) } else { let filename = args.filename.unwrap(); Box::new(BufReader::new(File::open(filename).expect("Could not open file."))) }; revert_hexdump(reader); } }