mirror of
https://github.com/TECHNOFAB11/tmux-copyrat.git
synced 2025-12-13 08:30:07 +01:00
refactor: refactor
This commit is contained in:
parent
905bd2862c
commit
b3099b42c9
3 changed files with 31 additions and 26 deletions
28
src/lib.rs
28
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<path::PathBuf>,
|
||||
pub target_path: Option<path::PathBuf>,
|
||||
|
||||
/// 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,
|
||||
}
|
||||
|
|
|
|||
21
src/main.rs
21
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ pub struct View<'a> {
|
|||
matches: Vec<state::Match<'a>>,
|
||||
focus_index: usize,
|
||||
multi: bool,
|
||||
hint_alignment: HintAlignment,
|
||||
hint_alignment: &'a HintAlignment,
|
||||
rendering_colors: &'a ViewColors,
|
||||
hint_style: Option<HintStyle>,
|
||||
}
|
||||
|
|
@ -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<HintStyle>,
|
||||
) -> 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,
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue