mirror of
https://github.com/FliegendeWurst/tmux-thumbs.git
synced 2024-11-09 16:00:35 +00:00
Fix scripts
This commit is contained in:
parent
19e2a2380a
commit
4262b1c45d
@ -4,6 +4,7 @@ use self::clap::{App, Arg};
|
|||||||
use clap::crate_version;
|
use clap::crate_version;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
trait Executor {
|
trait Executor {
|
||||||
fn execute(&mut self, args: Vec<String>) -> String;
|
fn execute(&mut self, args: Vec<String>) -> String;
|
||||||
@ -29,7 +30,9 @@ impl Executor for RealShell {
|
|||||||
|
|
||||||
self.executed = Some(args);
|
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<Vec<String>> {
|
fn last_executed(&self) -> Option<Vec<String>> {
|
||||||
@ -38,26 +41,39 @@ impl Executor for RealShell {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const TMP_FILE: &str = "/tmp/thumbs-last";
|
const TMP_FILE: &str = "/tmp/thumbs-last";
|
||||||
const TMUX_SIGNAL: &str = "thumbs-finished";
|
|
||||||
|
|
||||||
pub struct Swapper<'a> {
|
pub struct Swapper<'a> {
|
||||||
executor: Box<&'a mut dyn Executor>,
|
executor: Box<&'a mut dyn Executor>,
|
||||||
|
dir: String,
|
||||||
command: String,
|
command: String,
|
||||||
upcase_command: String,
|
upcase_command: String,
|
||||||
active_pane_id: Option<String>,
|
active_pane_id: Option<String>,
|
||||||
thumbs_pane_id: Option<String>,
|
thumbs_pane_id: Option<String>,
|
||||||
content: Option<String>,
|
content: Option<String>,
|
||||||
|
signal: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Swapper<'a> {
|
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 {
|
Swapper {
|
||||||
executor: executor,
|
executor: executor,
|
||||||
|
dir: dir,
|
||||||
command: command,
|
command: command,
|
||||||
upcase_command: upcase_command,
|
upcase_command: upcase_command,
|
||||||
active_pane_id: None,
|
active_pane_id: None,
|
||||||
thumbs_pane_id: None,
|
thumbs_pane_id: None,
|
||||||
content: None,
|
content: None,
|
||||||
|
signal: signal,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,15 +152,21 @@ impl<'a> Swapper<'a> {
|
|||||||
})
|
})
|
||||||
.collect::<Vec<String>>();
|
.collect::<Vec<String>>();
|
||||||
|
|
||||||
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![
|
let thumbs_command = vec![
|
||||||
"tmux",
|
"tmux",
|
||||||
"new-window",
|
"new-window",
|
||||||
"-P",
|
"-P",
|
||||||
|
"-d",
|
||||||
"-n",
|
"-n",
|
||||||
"[thumbs]",
|
"[thumbs]",
|
||||||
pane_command.as_str(),
|
pane_command.as_str(),
|
||||||
];
|
];
|
||||||
|
|
||||||
let params: Vec<String> = thumbs_command.iter().map(|arg| arg.to_string()).collect();
|
let params: Vec<String> = thumbs_command.iter().map(|arg| arg.to_string()).collect();
|
||||||
|
|
||||||
self.thumbs_pane_id = Some(self.executor.execute(params));
|
self.thumbs_pane_id = Some(self.executor.execute(params));
|
||||||
@ -169,7 +191,7 @@ impl<'a> Swapper<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn wait_thumbs(&mut self) {
|
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();
|
let params = wait_command.iter().map(|arg| arg.to_string()).collect();
|
||||||
|
|
||||||
self.executor.execute(params);
|
self.executor.execute(params);
|
||||||
@ -242,7 +264,12 @@ mod tests {
|
|||||||
fn retrieve_active_pane() {
|
fn retrieve_active_pane() {
|
||||||
let last_command_outputs = vec!["%97:active\n%106:nope\n%107:nope\n".to_string()];
|
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 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.capture_active_pane();
|
||||||
|
|
||||||
@ -258,7 +285,12 @@ mod tests {
|
|||||||
"%106:nope\n%98:active\n%107:nope\n".to_string(),
|
"%106:nope\n%98:active\n%107:nope\n".to_string(),
|
||||||
];
|
];
|
||||||
let mut executor = TestShell::new(last_command_outputs);
|
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.capture_active_pane();
|
||||||
swapper.execute_thumbs();
|
swapper.execute_thumbs();
|
||||||
@ -273,6 +305,12 @@ fn app_args<'a>() -> clap::ArgMatches<'a> {
|
|||||||
return App::new("tmux-thumbs")
|
return App::new("tmux-thumbs")
|
||||||
.version(crate_version!())
|
.version(crate_version!())
|
||||||
.about("A lightning fast version of tmux-fingers, copy/pasting tmux like vimium/vimperator")
|
.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(
|
||||||
Arg::with_name("command")
|
Arg::with_name("command")
|
||||||
.help("Pick command")
|
.help("Pick command")
|
||||||
@ -288,14 +326,16 @@ fn app_args<'a>() -> clap::ArgMatches<'a> {
|
|||||||
.get_matches();
|
.get_matches();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() -> std::io::Result<()> {
|
||||||
let args = app_args();
|
let args = app_args();
|
||||||
|
let dir = args.value_of("dir").unwrap();
|
||||||
let command = args.value_of("command").unwrap();
|
let command = args.value_of("command").unwrap();
|
||||||
let upcase_command = args.value_of("upcase_command").unwrap();
|
let upcase_command = args.value_of("upcase_command").unwrap();
|
||||||
|
|
||||||
let mut executor = RealShell::new();
|
let mut executor = RealShell::new();
|
||||||
let mut swapper = Swapper::new(
|
let mut swapper = Swapper::new(
|
||||||
Box::new(&mut executor),
|
Box::new(&mut executor),
|
||||||
|
dir.to_string(),
|
||||||
command.to_string(),
|
command.to_string(),
|
||||||
upcase_command.to_string(),
|
upcase_command.to_string(),
|
||||||
);
|
);
|
||||||
@ -307,4 +347,5 @@ fn main() {
|
|||||||
swapper.retrieve_content();
|
swapper.retrieve_content();
|
||||||
swapper.destroy_content();
|
swapper.destroy_content();
|
||||||
swapper.execute_command();
|
swapper.execute_command();
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -4,14 +4,6 @@
|
|||||||
|
|
||||||
PARAMS=()
|
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 {
|
function add-option-param {
|
||||||
VALUE=$(tmux show -vg @thumbs-$1 2> /dev/null)
|
VALUE=$(tmux show -vg @thumbs-$1 2> /dev/null)
|
||||||
|
|
||||||
@ -20,27 +12,8 @@ function add-option-param {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function add-multi-param {
|
add-option-param "command"
|
||||||
while read -r ITEM_KEY; do
|
add-option-param "upcase-command"
|
||||||
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"
|
|
||||||
|
|
||||||
# Remove empty arguments from PARAMS.
|
# Remove empty arguments from PARAMS.
|
||||||
# Otherwise, they would choke up tmux-thumbs when passed to it.
|
# 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="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
|
|
||||||
${CURRENT_DIR}/target/release/tmux-thumbs
|
${CURRENT_DIR}/target/release/tmux-thumbs --dir "${CURRENT_DIR}" "${PARAMS[@]}"
|
||||||
|
@ -6,4 +6,4 @@ DEFAULT_THUMBS_KEY="space"
|
|||||||
THUMBS_KEY=$(tmux show-option -gqv @thumbs-key)
|
THUMBS_KEY=$(tmux show-option -gqv @thumbs-key)
|
||||||
THUMBS_KEY=${THUMBS_KEY:-$DEFAULT_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"
|
||||||
|
Loading…
Reference in New Issue
Block a user