Fix issue with multi selection

This commit is contained in:
Ferran Basora 2020-05-03 15:31:18 +00:00
parent 6ef320d676
commit 122c26b8b0

View File

@ -8,6 +8,7 @@ mod view;
use self::clap::{App, Arg}; use self::clap::{App, Arg};
use clap::crate_version; use clap::crate_version;
use std::fs::OpenOptions;
use std::io::prelude::*; use std::io::prelude::*;
use std::io::{self, Read}; use std::io::{self, Read};
@ -167,25 +168,32 @@ fn main() {
}; };
if !selected.is_empty() { if !selected.is_empty() {
for (text, upcase) in selected.iter() { let output = selected
let mut output = format.to_string(); .iter()
let break_line = if multi { "\n" } else { "" }; .map(|(text, upcase)| {
let upcase_value = if *upcase { "true" } else { "false" }; let upcase_value = if *upcase { "true" } else { "false" };
let mut output = format.to_string();
output = str::replace(&output, "%U", upcase_value); output = str::replace(&output, "%U", upcase_value);
output = str::replace(&output, "%H", text.as_str()); output = str::replace(&output, "%H", text.as_str());
output
let output = format!("{}{}", output, break_line); })
.collect::<Vec<_>>()
.join("\n");
if target.is_empty() { if target.is_empty() {
print!("{}", output); print!("{}", output);
} else { } else {
let mut file = std::fs::File::create(target).expect("Unable to open the target file"); let mut file = OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(target)
.expect("Unable to open the target file");
file.write(output.as_bytes()).unwrap(); file.write(output.as_bytes()).unwrap();
} }
}
} else { } else {
::std::process::exit(1); ::std::process::exit(1);
} }