From 4262b1c45d39c2de47a93525a04bf4e3744de72e Mon Sep 17 00:00:00 2001 From: Ferran Basora Date: Wed, 8 Jan 2020 23:11:44 +0100 Subject: [PATCH] Fix scripts --- src/swapper.rs | 57 +++++++++++++++++++++++++++++++++++++++++------- tmux-thumbs.sh | 33 +++------------------------- tmux-thumbs.tmux | 2 +- 3 files changed, 53 insertions(+), 39 deletions(-) diff --git a/src/swapper.rs b/src/swapper.rs index d600fcf..5c0661c 100644 --- a/src/swapper.rs +++ b/src/swapper.rs @@ -4,6 +4,7 @@ use self::clap::{App, Arg}; use clap::crate_version; use regex::Regex; use std::process::Command; +use std::time::{SystemTime, UNIX_EPOCH}; trait Executor { fn execute(&mut self, args: Vec) -> String; @@ -29,7 +30,9 @@ impl Executor for RealShell { self.executed = Some(args); - String::from_utf8_lossy(&execution.stdout).into() + let output: String = String::from_utf8_lossy(&execution.stdout).into(); + + output.trim_end().to_string() } fn last_executed(&self) -> Option> { @@ -38,26 +41,39 @@ impl Executor for RealShell { } const TMP_FILE: &str = "/tmp/thumbs-last"; -const TMUX_SIGNAL: &str = "thumbs-finished"; pub struct Swapper<'a> { executor: Box<&'a mut dyn Executor>, + dir: String, command: String, upcase_command: String, active_pane_id: Option, thumbs_pane_id: Option, content: Option, + signal: String, } impl<'a> Swapper<'a> { - fn new(executor: Box<&'a mut dyn Executor>, command: String, upcase_command: String) -> Swapper { + fn new( + executor: Box<&'a mut dyn Executor>, + dir: String, + command: String, + upcase_command: String, + ) -> Swapper { + let since_the_epoch = SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("Time went backwards"); + let signal = format!("thumbs-finished-{}", since_the_epoch.as_secs()); + Swapper { executor: executor, + dir: dir, command: command, upcase_command: upcase_command, active_pane_id: None, thumbs_pane_id: None, content: None, + signal: signal, } } @@ -136,15 +152,21 @@ impl<'a> Swapper<'a> { }) .collect::>(); - let pane_command = format!("tmux capture-pane -t \"{}\" -p | ./target/release/thumbs -f '%U:%H' {} | tee {}; tmux wait-for -S {}", self.active_pane_id.clone().unwrap(), args.join(" "), TMP_FILE, TMUX_SIGNAL); + let active_pane_id = self.active_pane_id.as_mut().unwrap().clone(); + + // 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' {} | tee {}; tmux swap-pane -t {}; tmux wait-for -S {}", active_pane_id, self.dir, args.join(" "), TMP_FILE, active_pane_id, self.signal); + let thumbs_command = vec![ "tmux", "new-window", "-P", + "-d", "-n", "[thumbs]", pane_command.as_str(), ]; + let params: Vec = thumbs_command.iter().map(|arg| arg.to_string()).collect(); self.thumbs_pane_id = Some(self.executor.execute(params)); @@ -169,7 +191,7 @@ impl<'a> Swapper<'a> { } pub fn wait_thumbs(&mut self) { - let wait_command = vec!["tmux", "wait-for", TMUX_SIGNAL]; + let wait_command = vec!["tmux", "wait-for", self.signal.as_str()]; let params = wait_command.iter().map(|arg| arg.to_string()).collect(); self.executor.execute(params); @@ -242,7 +264,12 @@ mod tests { fn retrieve_active_pane() { let last_command_outputs = vec!["%97:active\n%106:nope\n%107:nope\n".to_string()]; let mut executor = TestShell::new(last_command_outputs); - let mut swapper = Swapper::new(Box::new(&mut executor), "".to_string(), "".to_string()); + let mut swapper = Swapper::new( + Box::new(&mut executor), + "".to_string(), + "".to_string(), + "".to_string(), + ); swapper.capture_active_pane(); @@ -258,7 +285,12 @@ mod tests { "%106:nope\n%98:active\n%107:nope\n".to_string(), ]; let mut executor = TestShell::new(last_command_outputs); - let mut swapper = Swapper::new(Box::new(&mut executor), "".to_string(), "".to_string()); + let mut swapper = Swapper::new( + Box::new(&mut executor), + "".to_string(), + "".to_string(), + "".to_string(), + ); swapper.capture_active_pane(); swapper.execute_thumbs(); @@ -273,6 +305,12 @@ fn app_args<'a>() -> clap::ArgMatches<'a> { return App::new("tmux-thumbs") .version(crate_version!()) .about("A lightning fast version of tmux-fingers, copy/pasting tmux like vimium/vimperator") + .arg( + Arg::with_name("dir") + .help("Directory where to execute thumbs") + .long("dir") + .default_value(""), + ) .arg( Arg::with_name("command") .help("Pick command") @@ -288,14 +326,16 @@ fn app_args<'a>() -> clap::ArgMatches<'a> { .get_matches(); } -fn main() { +fn main() -> std::io::Result<()> { let args = app_args(); + let dir = args.value_of("dir").unwrap(); let command = args.value_of("command").unwrap(); let upcase_command = args.value_of("upcase_command").unwrap(); let mut executor = RealShell::new(); let mut swapper = Swapper::new( Box::new(&mut executor), + dir.to_string(), command.to_string(), upcase_command.to_string(), ); @@ -307,4 +347,5 @@ fn main() { swapper.retrieve_content(); swapper.destroy_content(); swapper.execute_command(); + Ok(()) } diff --git a/tmux-thumbs.sh b/tmux-thumbs.sh index b0d76f7..9834961 100755 --- a/tmux-thumbs.sh +++ b/tmux-thumbs.sh @@ -4,14 +4,6 @@ PARAMS=() -function add-boolean-param { - VALUE=$(tmux show -vg @thumbs-$1 2> /dev/null) - - if [[ "${VALUE}" == "1" ]]; then - PARAMS+=("--$1") - fi -} - function add-option-param { VALUE=$(tmux show -vg @thumbs-$1 2> /dev/null) @@ -20,27 +12,8 @@ function add-option-param { fi } -function add-multi-param { - while read -r ITEM_KEY; do - VALUE=$(tmux show -vg $ITEM_KEY 2> /dev/null) - PARAMS+=("--$1=${VALUE}") - done < <(tmux show -g 2> /dev/null | grep thumbs-$1- | cut -d' ' -f1) -} - -add-boolean-param "reverse" -add-boolean-param "unique" -add-option-param "alphabet" -add-option-param "position" -add-option-param "fg-color" -add-option-param "bg-color" -add-option-param "hint-bg-color" -add-option-param "hint-fg-color" -add-option-param "select-fg-color" -add-option-param "select-bg-color" -add-option-param "command" -add-option-param "upcase-command" -add-multi-param "regexp" -add-boolean-param "contrast" +add-option-param "command" +add-option-param "upcase-command" # Remove empty arguments from PARAMS. # Otherwise, they would choke up tmux-thumbs when passed to it. @@ -50,4 +23,4 @@ done CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -${CURRENT_DIR}/target/release/tmux-thumbs +${CURRENT_DIR}/target/release/tmux-thumbs --dir "${CURRENT_DIR}" "${PARAMS[@]}" diff --git a/tmux-thumbs.tmux b/tmux-thumbs.tmux index 205572c..f02127e 100755 --- a/tmux-thumbs.tmux +++ b/tmux-thumbs.tmux @@ -6,4 +6,4 @@ DEFAULT_THUMBS_KEY="space" THUMBS_KEY=$(tmux show-option -gqv @thumbs-key) THUMBS_KEY=${THUMBS_KEY:-$DEFAULT_THUMBS_KEY} -tmux bind-key "$THUMBS_KEY" run-shell "${CURRENT_DIR}/tmux-thumbs.sh" +tmux bind-key "$THUMBS_KEY" run-shell -b "${CURRENT_DIR}/tmux-thumbs.sh"