From b3099b42c9a351b6ada6245f472d74df11d115e5 Mon Sep 17 00:00:00 2001 From: graelo Date: Mon, 25 May 2020 23:32:37 +0200 Subject: [PATCH] refactor: refactor --- src/lib.rs | 28 +++++++--------------------- src/main.rs | 21 ++++++++++++++++++++- src/view.rs | 8 ++++---- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c3f9900..37e52c1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,4 @@ use clap::Clap; -use std::fs::OpenOptions; -use std::io::prelude::*; use std::path; pub mod alphabets; @@ -14,12 +12,12 @@ pub mod view; /// # Note /// /// 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 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, Some(style) => match style { HintStyleCli::Underline => Some(view::HintStyle::Underline), @@ -37,7 +35,7 @@ pub fn run(buffer: String, opt: Opt) { opt.multi_selection, opt.reverse, opt.unique, - opt.hint_alignment, + &opt.hint_alignment, &opt.colors, hint_style, ); @@ -64,19 +62,7 @@ pub fn run(buffer: String, opt: Opt) { .join("\n") }; - 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(); - } - } + output } /// Main configuration, parsed from command line. @@ -108,7 +94,7 @@ pub struct Opt { unique: bool, /// 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, /// Additional regex patterns. @@ -129,11 +115,11 @@ pub struct Opt { /// Target path where to store the selected matches. #[clap(short = "o", long = "output", parse(from_os_str))] - target_path: Option, + pub target_path: Option, /// Describes if the uppercased marker should be added to the output, /// 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)] uppercased_marker: bool, } diff --git a/src/main.rs b/src/main.rs index 244a514..cec8f83 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(); + } + } } diff --git a/src/view.rs b/src/view.rs index 429e3be..383fb34 100644 --- a/src/view.rs +++ b/src/view.rs @@ -14,7 +14,7 @@ pub struct View<'a> { matches: Vec>, focus_index: usize, multi: bool, - hint_alignment: HintAlignment, + hint_alignment: &'a HintAlignment, rendering_colors: &'a ViewColors, hint_style: Option, } @@ -91,7 +91,7 @@ impl<'a> View<'a> { multi: bool, reversed: bool, unique: bool, - hint_alignment: HintAlignment, + hint_alignment: &'a HintAlignment, rendering_colors: &'a ViewColors, hint_style: Option, ) -> View<'a> { @@ -703,7 +703,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - "; matches: vec![], // no matches focus_index: 0, multi: false, - hint_alignment, + hint_alignment: &hint_alignment, rendering_colors: &rendering_colors, hint_style: None, }; @@ -764,7 +764,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - "; multi, reversed, unique, - hint_alignment, + &hint_alignment, &rendering_colors, hint_style, );