use clap::{Parser, Subcommand};
use putpng::crc::*;
use putpng::crop::*;
use putpng::grab::*;
#[derive(Parser)]
#[command(version)]
struct Args {
#[command(subcommand)]
commands: Commands,
#[arg(global = true)]
paths: Vec<String>,
#[arg(short, long, global=true, num_args = 1 ..)]
ignore: Vec<String>,
}
#[derive(Subcommand)]
enum Commands {
Grab { x: String, y: String },
Crop,
Show,
}
fn ignoring(ignore: Vec<String>) -> impl Fn(&String) -> bool {
move |p| !ignore.iter().any(|i| p.contains(i))
}
fn main() {
let args = Args::parse_from(wild::args());
let (commands, paths) = (
args.commands,
args.paths.into_iter().filter(ignoring(args.ignore)),
);
match commands {
Commands::Grab { x, y } => {
let crc = Crc32::new();
let _ = grab_all(paths, &crc, x, y, false).inspect_err(|e| eprintln!("{e}"));
}
Commands::Crop => {
let crc = Crc32::new();
let _ = crop_all(paths, &crc).inspect_err(|e| eprintln!("{e}"));
}
Commands::Show => {
for path in paths {
match read_grab(&path) {
Ok(Some(offset)) => println!("'{path}': {offset:?}"),
Err(e) => eprintln!("{e}"),
_ => println!("'{path}' does not have an offset"),
}
}
}
}
}