mirror of
https://github.com/TECHNOFAB11/tmux-copyrat.git
synced 2025-12-12 16:10:07 +01:00
refactor: refactor
This commit is contained in:
parent
b3099b42c9
commit
623c66cbba
4 changed files with 39 additions and 38 deletions
|
|
@ -34,7 +34,7 @@ pub fn run(buffer: String, opt: &Opt) -> String {
|
|||
&mut state,
|
||||
opt.multi_selection,
|
||||
opt.reverse,
|
||||
opt.unique,
|
||||
opt.unique_hint,
|
||||
&opt.hint_alignment,
|
||||
&opt.colors,
|
||||
hint_style,
|
||||
|
|
@ -91,7 +91,7 @@ pub struct Opt {
|
|||
|
||||
/// Keep the same hint for identical matches.
|
||||
#[clap(short, long)]
|
||||
unique: bool,
|
||||
unique_hint: bool,
|
||||
|
||||
/// Align hint with its match.
|
||||
#[clap(short = "a", long, arg_enum, default_value = "leading")]
|
||||
|
|
@ -113,7 +113,7 @@ pub struct Opt {
|
|||
parse(try_from_str = parse_chars))]
|
||||
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))]
|
||||
pub target_path: Option<path::PathBuf>,
|
||||
|
||||
|
|
|
|||
|
|
@ -198,14 +198,13 @@ impl<'a> Swapper<'a> {
|
|||
|
||||
// NOTE: For debugging add echo $PWD && sleep 5 after tee
|
||||
let pane_command = format!(
|
||||
"tmux capture-pane -t {} -p{} | {}/target/release/thumbs -f '%U:%H' -t {} {}; tmux swap-pane -t {}; tmux wait-for -S {}",
|
||||
active_pane_id,
|
||||
scroll_params,
|
||||
self.directory.to_str().unwrap(),
|
||||
TMP_FILE,
|
||||
args.join(" "),
|
||||
active_pane_id,
|
||||
self.signal
|
||||
"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_id = active_pane_id,
|
||||
scroll_params = scroll_params,
|
||||
dir = self.directory.to_str().unwrap(),
|
||||
tmpfile = TMP_FILE,
|
||||
args = args.join(" "),
|
||||
signal = self.signal
|
||||
);
|
||||
|
||||
let thumbs_command = vec![
|
||||
|
|
@ -358,7 +357,7 @@ struct Opt {
|
|||
#[clap(short, long, default_value = "'tmux set-buffer {}'")]
|
||||
command: String,
|
||||
|
||||
/// Command to execute on uppercased selection.
|
||||
/// Command to execute on alt selection.
|
||||
#[clap(
|
||||
short,
|
||||
long,
|
||||
|
|
@ -369,11 +368,15 @@ struct Opt {
|
|||
/// Retrieve options 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
|
||||
/// compilation.
|
||||
#[clap(long)]
|
||||
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> {
|
||||
|
|
@ -392,6 +395,8 @@ fn main() -> Result<(), error::ParseError> {
|
|||
.find(|p| p.is_active)
|
||||
.expect("One tmux pane should be active");
|
||||
|
||||
let content = tmux::capture_pane(&active_pane, &opt.capture)?;
|
||||
|
||||
let mut executor = RealShell::new();
|
||||
|
||||
let mut swapper = Swapper::new(
|
||||
|
|
|
|||
37
src/tmux.rs
37
src/tmux.rs
|
|
@ -1,3 +1,4 @@
|
|||
use clap::Clap;
|
||||
use copyrat::error::ParseError;
|
||||
use regex::Regex;
|
||||
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
|
||||
/// 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()?;
|
||||
|
||||
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 {
|
||||
/// The entire history.
|
||||
///
|
||||
/// This will end up sending `-S - -E -` to `tmux capture-pane`.
|
||||
EntireHistory,
|
||||
/// Region from start line to end line
|
||||
///
|
||||
/// This works as defined in tmux's docs (order does not matter).
|
||||
Region(i32, i32),
|
||||
/// The visible area.
|
||||
VisibleArea,
|
||||
///// Region from start line to end line
|
||||
/////
|
||||
///// 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`
|
||||
/// will result in the Pane's visible area to be capture (mimics the default
|
||||
/// behavior of tmux `capture-pane`). If a `CaptureRegion` is provided,
|
||||
/// depending on its value, either the entire history will be captured, or a
|
||||
/// 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.
|
||||
/// Returns the entire Pane content as a `String`.
|
||||
///
|
||||
/// `CaptureRegion` specifies if the visible area is captured, or the entire
|
||||
/// history.
|
||||
///
|
||||
/// # Note
|
||||
///
|
||||
|
|
@ -204,11 +204,11 @@ pub enum CaptureRegion {
|
|||
/// pane is in copy mode, we need to take into account the current scroll
|
||||
/// position. To support both cases, the implementation always provides those
|
||||
/// 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 region_str = match region {
|
||||
None => {
|
||||
CaptureRegion::VisibleArea => {
|
||||
// Will capture the visible area.
|
||||
// Providing start/end helps support both copy and normal modes.
|
||||
format!(
|
||||
|
|
@ -217,12 +217,7 @@ pub fn capture_pane(pane: &Pane, region: &Option<CaptureRegion>) -> Result<Strin
|
|||
end = pane.height - pane.scroll_position - 1
|
||||
)
|
||||
}
|
||||
Some(region) => match region {
|
||||
CaptureRegion::EntireHistory => String::from(" -S - -E -"),
|
||||
CaptureRegion::Region(start, end) => {
|
||||
format!(" -S {start} -E {end}", start = start, end = end)
|
||||
}
|
||||
},
|
||||
CaptureRegion::EntireHistory => String::from(" -S - -E -"),
|
||||
};
|
||||
|
||||
args.push_str(®ion_str);
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
use super::{colors, state};
|
||||
|
||||
use clap::Clap;
|
||||
use std::char;
|
||||
use std::io::{stdout, Read, Write};
|
||||
|
|
@ -90,12 +91,12 @@ impl<'a> View<'a> {
|
|||
state: &'a mut state::State<'a>,
|
||||
multi: bool,
|
||||
reversed: bool,
|
||||
unique: bool,
|
||||
unique_hint: bool,
|
||||
hint_alignment: &'a HintAlignment,
|
||||
rendering_colors: &'a ViewColors,
|
||||
hint_style: Option<HintStyle>,
|
||||
) -> 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 };
|
||||
|
||||
View {
|
||||
|
|
@ -746,7 +747,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
|
|||
let mut state = state::State::new(&lines, &alphabet, &custom_regexes);
|
||||
let multi = false;
|
||||
let reversed = true;
|
||||
let unique = false;
|
||||
let unique_hint = false;
|
||||
|
||||
let rendering_colors = ViewColors {
|
||||
focused_fg: Box::new(color::Red),
|
||||
|
|
@ -763,7 +764,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
|
|||
&mut state,
|
||||
multi,
|
||||
reversed,
|
||||
unique,
|
||||
unique_hint,
|
||||
&hint_alignment,
|
||||
&rendering_colors,
|
||||
hint_style,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue