Skip to content
This repository has been archived by the owner on Oct 30, 2021. It is now read-only.

Commit

Permalink
Add -c flag and change --stdout option as alias of it / out-file cond…
Browse files Browse the repository at this point in the history
…ition fix
  • Loading branch information
Jaeho Lee committed Mar 31, 2017
1 parent 7d2b708 commit eef1fcd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions data/help.txt
Expand Up @@ -13,6 +13,7 @@ EXAMPLE:
FLAGS:
-h, --help Prints help information
-V, --version Prints version information
-c, --stdout Prints result as standard output

OPTIONS:
Elements:
Expand Down
6 changes: 4 additions & 2 deletions src/cli.rs
Expand Up @@ -174,9 +174,12 @@ pub fn prepare_app<'a, 'b>() -> App<'a, 'b> {
.index(1)
.validator(is_svg))
.arg(Arg::with_name("out-file")
.required_if(KEYS[Key::Stdout], "false")
.required_unless(KEYS[Key::Stdout])
.index(2)
.validator(is_svg))
.arg(Arg::with_name(KEYS[Key::Stdout])
.short("c")
.long(KEYS[Key::Stdout]))

// elements
.arg(gen_flag!(Key::RemoveComments, "true"))
Expand Down Expand Up @@ -242,7 +245,6 @@ pub fn prepare_app<'a, 'b>() -> App<'a, 'b> {
.arg(gen_flag!(Key::Multipass, "false"))
.arg(gen_flag!(Key::CopyOnError, "false"))
.arg(gen_flag!(Key::Quiet, "false"))
.arg(gen_flag!(Key::Stdout, "false"))
}

fn is_svg(val: String) -> Result<(), String> {
Expand Down
17 changes: 9 additions & 8 deletions src/main.rs
Expand Up @@ -61,7 +61,8 @@ fn main() {
let cleaning_opt = cli::gen_cleaning_options(&args);

let in_file = args.value_of("in-file").unwrap();
let out_file = args.value_of("out-file").unwrap_or("");
let out_file = args.value_of("out-file");
let stdout_enabled = args.is_present("stdout");

if !Path::new(in_file).exists() {
writeln!(stderr(), "Error: Input file does not exist.").unwrap();
Expand All @@ -73,10 +74,10 @@ fn main() {

let on_err = || {
// copy original file to destination
if out_file.is_empty() && cli::get_flag(&args, Key::CopyOnError) {
if out_file.is_some() && cli::get_flag(&args, Key::CopyOnError) {
// copy a file only when paths are different
if in_file != out_file {
try_msg!(fs::copy(in_file, out_file));
if in_file != out_file.unwrap() {
try_msg!(fs::copy(in_file, out_file.unwrap()));
}
}

Expand Down Expand Up @@ -136,18 +137,18 @@ fn main() {
}

// save buffer
if cli::get_flag(&args, Key::Stdout) {
if stdout_enabled {
try_msg!(cleaner::write_stdout(&buf[..]));
}

if !out_file.is_empty() {
if let Some(out_file) = out_file {
try_msg!(cleaner::save_file(&buf[..], out_file));
}

// unwrap is safe, because 'save_file' will fail on write error,
// so file is totally exist
if !cli::get_flag(&args, Key::Stdout) && !out_file.is_empty() && !cli::get_flag(&args, Key::Quiet) {
let out_size = fs::File::open(out_file).unwrap().metadata().unwrap().len() as f64;
if !stdout_enabled && out_file.is_some() && !cli::get_flag(&args, Key::Quiet) {
let out_size = fs::File::open(out_file.unwrap()).unwrap().metadata().unwrap().len() as f64;
let ratio = 100.0 - out_size / (raw.len() as f64) * 100.0;
println!("Your image is {:.2}% smaller now.", ratio);
}
Expand Down

0 comments on commit eef1fcd

Please sign in to comment.