refactor: refactor

This commit is contained in:
graelo 2020-05-25 23:32:37 +02:00
parent 905bd2862c
commit b3099b42c9
3 changed files with 31 additions and 26 deletions

View file

@ -1,4 +1,6 @@
use clap::Clap;
use std::fs::OpenOptions;
use std::io::prelude::*;
use std::io::{self, Read};
use copyrat::{run, Opt};
@ -13,5 +15,22 @@ fn main() {
let mut buffer = String::new();
handle.read_to_string(&mut buffer).unwrap();
run(buffer, opt);
// Execute copyrat over the buffer (will take control over stdout).
// This returns the selected matches.
let output: String = run(buffer, &opt);
// Write output to a target_path if provided, else print to original stdout.
match opt.target_path {
None => println!("{}", output),
Some(target) => {
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();
}
}
}