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,6 +1,4 @@
use clap::Clap; use clap::Clap;
use std::fs::OpenOptions;
use std::io::prelude::*;
use std::path; use std::path;
pub mod alphabets; pub mod alphabets;
@ -14,12 +12,12 @@ pub mod view;
/// # Note /// # Note
/// ///
/// Maybe the decision to move ownership is a bit bold. /// Maybe the decision to move ownership is a bit bold.
pub fn run(buffer: String, opt: Opt) { pub fn run(buffer: String, opt: &Opt) -> String {
let lines: Vec<&str> = buffer.split('\n').collect(); let lines: Vec<&str> = buffer.split('\n').collect();
let mut state = state::State::new(&lines, &opt.alphabet, &opt.custom_regex); let mut state = state::State::new(&lines, &opt.alphabet, &opt.custom_regex);
let hint_style = match opt.hint_style { let hint_style = match &opt.hint_style {
None => None, None => None,
Some(style) => match style { Some(style) => match style {
HintStyleCli::Underline => Some(view::HintStyle::Underline), HintStyleCli::Underline => Some(view::HintStyle::Underline),
@ -37,7 +35,7 @@ pub fn run(buffer: String, opt: Opt) {
opt.multi_selection, opt.multi_selection,
opt.reverse, opt.reverse,
opt.unique, opt.unique,
opt.hint_alignment, &opt.hint_alignment,
&opt.colors, &opt.colors,
hint_style, hint_style,
); );
@ -64,19 +62,7 @@ pub fn run(buffer: String, opt: Opt) {
.join("\n") .join("\n")
}; };
match opt.target_path { output
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();
}
}
} }
/// Main configuration, parsed from command line. /// Main configuration, parsed from command line.
@ -108,7 +94,7 @@ pub struct Opt {
unique: bool, unique: bool,
/// Align hint with its match. /// Align hint with its match.
#[clap(short = "a", long, arg_enum, default_value = "Leading")] #[clap(short = "a", long, arg_enum, default_value = "leading")]
hint_alignment: view::HintAlignment, hint_alignment: view::HintAlignment,
/// Additional regex patterns. /// Additional regex patterns.
@ -129,11 +115,11 @@ pub struct Opt {
/// Target path where to store the selected matches. /// Target path where to store the selected matches.
#[clap(short = "o", long = "output", parse(from_os_str))] #[clap(short = "o", long = "output", parse(from_os_str))]
target_path: Option<path::PathBuf>, pub target_path: Option<path::PathBuf>,
/// Describes if the uppercased marker should be added to the output, /// Describes if the uppercased marker should be added to the output,
/// indicating if hint key was uppercased. This is only used by /// indicating if hint key was uppercased. This is only used by
/// tmux-copyrat, so it is skipped from clap configuration. /// tmux-copyrat, so it is hidden (skipped) from the CLI.
#[clap(skip)] #[clap(skip)]
uppercased_marker: bool, uppercased_marker: bool,
} }

View file

@ -1,4 +1,6 @@
use clap::Clap; use clap::Clap;
use std::fs::OpenOptions;
use std::io::prelude::*;
use std::io::{self, Read}; use std::io::{self, Read};
use copyrat::{run, Opt}; use copyrat::{run, Opt};
@ -13,5 +15,22 @@ fn main() {
let mut buffer = String::new(); let mut buffer = String::new();
handle.read_to_string(&mut buffer).unwrap(); 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();
}
}
} }

View file

@ -14,7 +14,7 @@ pub struct View<'a> {
matches: Vec<state::Match<'a>>, matches: Vec<state::Match<'a>>,
focus_index: usize, focus_index: usize,
multi: bool, multi: bool,
hint_alignment: HintAlignment, hint_alignment: &'a HintAlignment,
rendering_colors: &'a ViewColors, rendering_colors: &'a ViewColors,
hint_style: Option<HintStyle>, hint_style: Option<HintStyle>,
} }
@ -91,7 +91,7 @@ impl<'a> View<'a> {
multi: bool, multi: bool,
reversed: bool, reversed: bool,
unique: bool, unique: bool,
hint_alignment: HintAlignment, hint_alignment: &'a HintAlignment,
rendering_colors: &'a ViewColors, rendering_colors: &'a ViewColors,
hint_style: Option<HintStyle>, hint_style: Option<HintStyle>,
) -> View<'a> { ) -> View<'a> {
@ -703,7 +703,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
matches: vec![], // no matches matches: vec![], // no matches
focus_index: 0, focus_index: 0,
multi: false, multi: false,
hint_alignment, hint_alignment: &hint_alignment,
rendering_colors: &rendering_colors, rendering_colors: &rendering_colors,
hint_style: None, hint_style: None,
}; };
@ -764,7 +764,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
multi, multi,
reversed, reversed,
unique, unique,
hint_alignment, &hint_alignment,
&rendering_colors, &rendering_colors,
hint_style, hint_style,
); );