use std::fs;
use std::io::Result;
use std::path::{Path, PathBuf};
use structopt::StructOpt;
use imgtiger::*;
#[derive(StructOpt)]
struct Opt {
#[structopt(short = "h", long = "height")]
height: Option<Dimension>,
#[structopt(short = "w", long = "width")]
width: Option<Dimension>,
#[structopt(short = "A", long = "no-preserve-aspect")]
no_preserve_aspect: bool,
#[structopt(short = "d", long = "download")]
is_download: bool,
#[structopt(short = "n", long = "no-newline")]
no_newline: bool,
#[structopt(name = "file", required = true)]
files: Vec<PathBuf>,
}
fn main() -> Result<()> {
let opt = Opt::from_args();
let action = if opt.is_download {
TransferAction::Download
} else {
TransferAction::Display {
height: opt.height,
width: opt.width,
preserve_aspect_ratio: Some(opt.no_preserve_aspect),
}
};
for file in opt.files {
if file == Path::new("-") {
FileTransfer::new(&action, Box::new(std::io::stdin()), None)
} else {
let file_name_option = file
.file_name()
.map(|o| o.to_str())
.unwrap_or_else(|| file.to_str())
.map(|s| s.to_owned());
FileTransfer::new(&action, Box::new(fs::File::open(file)?), file_name_option)
}.transfer(&mut std::io::stdout())?;
if !opt.is_download && !opt.no_newline {
println!();
}
}
Ok(())
}