refactor: refactor

This commit is contained in:
graelo 2020-05-26 08:11:45 +02:00
parent b3099b42c9
commit 623c66cbba
4 changed files with 39 additions and 38 deletions

View file

@ -34,7 +34,7 @@ pub fn run(buffer: String, opt: &Opt) -> String {
&mut state, &mut state,
opt.multi_selection, opt.multi_selection,
opt.reverse, opt.reverse,
opt.unique, opt.unique_hint,
&opt.hint_alignment, &opt.hint_alignment,
&opt.colors, &opt.colors,
hint_style, hint_style,
@ -91,7 +91,7 @@ pub struct Opt {
/// Keep the same hint for identical matches. /// Keep the same hint for identical matches.
#[clap(short, long)] #[clap(short, long)]
unique: bool, unique_hint: 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")]
@ -113,7 +113,7 @@ pub struct Opt {
parse(try_from_str = parse_chars))] parse(try_from_str = parse_chars))]
hint_surroundings: (char, char), hint_surroundings: (char, char),
/// Target path where to store the selected matches. /// Optional 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))]
pub target_path: Option<path::PathBuf>, pub target_path: Option<path::PathBuf>,

View file

@ -198,14 +198,13 @@ impl<'a> Swapper<'a> {
// NOTE: For debugging add echo $PWD && sleep 5 after tee // NOTE: For debugging add echo $PWD && sleep 5 after tee
let pane_command = format!( let pane_command = format!(
"tmux capture-pane -t {} -p{} | {}/target/release/thumbs -f '%U:%H' -t {} {}; tmux swap-pane -t {}; tmux wait-for -S {}", "tmux capture-pane -t {active_id} -p{scroll_params} | {dir}/target/release/thumbs -f '%U:%H' -t {tmpfile} {args}; tmux swap-pane -t {active_id}; tmux wait-for -S {signal}",
active_pane_id, active_id = active_pane_id,
scroll_params, scroll_params = scroll_params,
self.directory.to_str().unwrap(), dir = self.directory.to_str().unwrap(),
TMP_FILE, tmpfile = TMP_FILE,
args.join(" "), args = args.join(" "),
active_pane_id, signal = self.signal
self.signal
); );
let thumbs_command = vec![ let thumbs_command = vec![
@ -358,7 +357,7 @@ struct Opt {
#[clap(short, long, default_value = "'tmux set-buffer {}'")] #[clap(short, long, default_value = "'tmux set-buffer {}'")]
command: String, command: String,
/// Command to execute on uppercased selection. /// Command to execute on alt selection.
#[clap( #[clap(
short, short,
long, long,
@ -369,11 +368,15 @@ struct Opt {
/// Retrieve options from tmux. /// Retrieve options from tmux.
/// ///
/// If active, options formatted like `copyrat-*` are read from tmux. /// If active, options formatted like `copyrat-*` are read from tmux.
/// You should prefer reading them from the config file (the default /// You should consider reading them from the config file (the default
/// option) as this saves both a command call (about 10ms) and a Regex /// option) as this saves both a command call (about 10ms) and a Regex
/// compilation. /// compilation.
#[clap(long)] #[clap(long)]
options_from_tmux: bool, options_from_tmux: bool,
/// Optionally capture entire pane history.
#[clap(long, arg_enum, default_value = "entire-history")]
capture: tmux::CaptureRegion,
} }
fn main() -> Result<(), error::ParseError> { fn main() -> Result<(), error::ParseError> {
@ -392,6 +395,8 @@ fn main() -> Result<(), error::ParseError> {
.find(|p| p.is_active) .find(|p| p.is_active)
.expect("One tmux pane should be active"); .expect("One tmux pane should be active");
let content = tmux::capture_pane(&active_pane, &opt.capture)?;
let mut executor = RealShell::new(); let mut executor = RealShell::new();
let mut swapper = Swapper::new( let mut swapper = Swapper::new(

View file

@ -1,3 +1,4 @@
use clap::Clap;
use copyrat::error::ParseError; use copyrat::error::ParseError;
use regex::Regex; use regex::Regex;
use std::collections::HashMap; use std::collections::HashMap;
@ -5,7 +6,7 @@ use std::process::Command;
/// Execute an arbitrary Unix command and return the stdout as a `String` if /// Execute an arbitrary Unix command and return the stdout as a `String` if
/// successful. /// successful.
pub fn execute(command: &str, args: &Vec<&str>) -> Result<String, ParseError> { fn execute(command: &str, args: &Vec<&str>) -> Result<String, ParseError> {
let output = Command::new(command).args(args).output()?; let output = Command::new(command).args(args).output()?;
if !output.status.success() { if !output.status.success() {
@ -177,25 +178,24 @@ pub fn get_options(prefix: &str) -> Result<HashMap<String, String>, ParseError>
// } // }
// };} // };}
#[derive(Debug)] #[derive(Clap, Debug)]
pub enum CaptureRegion { pub enum CaptureRegion {
/// The entire history. /// The entire history.
/// ///
/// This will end up sending `-S - -E -` to `tmux capture-pane`. /// This will end up sending `-S - -E -` to `tmux capture-pane`.
EntireHistory, EntireHistory,
/// Region from start line to end line /// The visible area.
/// VisibleArea,
/// This works as defined in tmux's docs (order does not matter). ///// Region from start line to end line
Region(i32, i32), /////
///// This works as defined in tmux's docs (order does not matter).
//Region(i32, i32),
} }
/// Returns the Pane's content as a `String`. The `CaptureRegion` if `None` /// Returns the entire Pane content as a `String`.
/// will result in the Pane's visible area to be capture (mimics the default ///
/// behavior of tmux `capture-pane`). If a `CaptureRegion` is provided, /// `CaptureRegion` specifies if the visible area is captured, or the entire
/// depending on its value, either the entire history will be captured, or a /// history.
/// user-provided region. For the user-provided region, the order of `start`
/// and `end` does not matter. They have the same meaning as described in Tmux
/// documentation.
/// ///
/// # Note /// # Note
/// ///
@ -204,11 +204,11 @@ pub enum CaptureRegion {
/// pane is in copy mode, we need to take into account the current scroll /// pane is in copy mode, we need to take into account the current scroll
/// position. To support both cases, the implementation always provides those /// position. To support both cases, the implementation always provides those
/// parameters to tmux. /// parameters to tmux.
pub fn capture_pane(pane: &Pane, region: &Option<CaptureRegion>) -> Result<String, ParseError> { pub fn capture_pane(pane: &Pane, region: &CaptureRegion) -> Result<String, ParseError> {
let mut args = format!("capture-pane -t {id} -p", id = pane.id); let mut args = format!("capture-pane -t {id} -p", id = pane.id);
let region_str = match region { let region_str = match region {
None => { CaptureRegion::VisibleArea => {
// Will capture the visible area. // Will capture the visible area.
// Providing start/end helps support both copy and normal modes. // Providing start/end helps support both copy and normal modes.
format!( format!(
@ -217,12 +217,7 @@ pub fn capture_pane(pane: &Pane, region: &Option<CaptureRegion>) -> Result<Strin
end = pane.height - pane.scroll_position - 1 end = pane.height - pane.scroll_position - 1
) )
} }
Some(region) => match region { CaptureRegion::EntireHistory => String::from(" -S - -E -"),
CaptureRegion::EntireHistory => String::from(" -S - -E -"),
CaptureRegion::Region(start, end) => {
format!(" -S {start} -E {end}", start = start, end = end)
}
},
}; };
args.push_str(&region_str); args.push_str(&region_str);

View file

@ -1,4 +1,5 @@
use super::{colors, state}; use super::{colors, state};
use clap::Clap; use clap::Clap;
use std::char; use std::char;
use std::io::{stdout, Read, Write}; use std::io::{stdout, Read, Write};
@ -90,12 +91,12 @@ impl<'a> View<'a> {
state: &'a mut state::State<'a>, state: &'a mut state::State<'a>,
multi: bool, multi: bool,
reversed: bool, reversed: bool,
unique: bool, unique_hint: bool,
hint_alignment: &'a 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> {
let matches = state.matches(reversed, unique); let matches = state.matches(reversed, unique_hint);
let focus_index = if reversed { matches.len() - 1 } else { 0 }; let focus_index = if reversed { matches.len() - 1 } else { 0 };
View { View {
@ -746,7 +747,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
let mut state = state::State::new(&lines, &alphabet, &custom_regexes); let mut state = state::State::new(&lines, &alphabet, &custom_regexes);
let multi = false; let multi = false;
let reversed = true; let reversed = true;
let unique = false; let unique_hint = false;
let rendering_colors = ViewColors { let rendering_colors = ViewColors {
focused_fg: Box::new(color::Red), focused_fg: Box::new(color::Red),
@ -763,7 +764,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
&mut state, &mut state,
multi, multi,
reversed, reversed,
unique, unique_hint,
&hint_alignment, &hint_alignment,
&rendering_colors, &rendering_colors,
hint_style, hint_style,