rhd/src/main.rs

60 lines
1.4 KiB
Rust
Raw Normal View History

2021-04-18 16:40:31 +02:00
use std::fs::File;
use std::io;
2023-01-06 18:24:02 +01:00
use std::io::BufReader;
2022-12-15 12:54:11 +01:00
use std::usize::MAX;
2022-12-12 16:54:20 +01:00
mod colormap;
2021-04-18 16:40:31 +02:00
mod tohex;
use tohex::hexdump;
2023-01-06 18:24:02 +01:00
mod tobin;
use tobin::revert_hexdump;
2021-04-18 16:40:31 +02:00
use clap::Parser;
2021-04-18 16:40:31 +02:00
#[derive(Parser)]
2022-12-14 17:43:10 +01:00
#[command(version, about = "A simple hexdump tool")]
struct Arguments {
2022-12-15 11:10:05 +01:00
/// Stop after <len> octets
#[arg(short, long)]
length: Option<usize>,
2023-01-06 18:24:02 +01:00
/// Revert hexdump to binary
#[arg(short, long)]
revert: bool,
/// Filename to read, if left out read from stdin
filename: Option<String>,
2021-04-18 16:40:31 +02:00
}
pub fn isatty() -> bool {
termion::is_tty(&File::create("/dev/stdout").expect("Could not open `stdout'"))
}
2021-04-18 16:40:31 +02:00
fn main() {
let args = Arguments::parse();
let length = if args.length.is_some() {
args.length.unwrap()
} else {
MAX
};
2023-01-06 18:24:02 +01:00
if !args.revert {
let reader: Box<dyn io::Read> = 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<dyn io::BufRead> = 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);
}
2021-04-18 16:40:31 +02:00
}