2019-12-08 23:21:25 +00:00
|
|
|
extern crate clap;
|
|
|
|
|
|
|
|
use self::clap::{App, Arg};
|
|
|
|
use clap::crate_version;
|
|
|
|
use regex::Regex;
|
|
|
|
use std::process::Command;
|
2020-01-08 22:11:44 +00:00
|
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
2019-12-08 23:21:25 +00:00
|
|
|
|
|
|
|
trait Executor {
|
|
|
|
fn execute(&mut self, args: Vec<String>) -> String;
|
|
|
|
fn last_executed(&self) -> Option<Vec<String>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct RealShell {
|
|
|
|
executed: Option<Vec<String>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RealShell {
|
|
|
|
fn new() -> RealShell {
|
|
|
|
RealShell { executed: None }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Executor for RealShell {
|
|
|
|
fn execute(&mut self, args: Vec<String>) -> String {
|
|
|
|
let execution = Command::new(args[0].as_str())
|
|
|
|
.args(&args[1..])
|
|
|
|
.output()
|
|
|
|
.expect("Couldn't run it");
|
|
|
|
|
|
|
|
self.executed = Some(args);
|
|
|
|
|
2020-01-08 22:11:44 +00:00
|
|
|
let output: String = String::from_utf8_lossy(&execution.stdout).into();
|
|
|
|
|
|
|
|
output.trim_end().to_string()
|
2019-12-08 23:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn last_executed(&self) -> Option<Vec<String>> {
|
|
|
|
self.executed.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const TMP_FILE: &str = "/tmp/thumbs-last";
|
|
|
|
|
|
|
|
pub struct Swapper<'a> {
|
|
|
|
executor: Box<&'a mut dyn Executor>,
|
2020-01-08 22:11:44 +00:00
|
|
|
dir: String,
|
2019-12-08 23:21:25 +00:00
|
|
|
command: String,
|
|
|
|
upcase_command: String,
|
|
|
|
active_pane_id: Option<String>,
|
2020-01-19 23:10:21 +00:00
|
|
|
active_pane_height: Option<i32>,
|
|
|
|
active_pane_scroll_position: Option<i32>,
|
|
|
|
active_pane_in_copy_mode: Option<String>,
|
2019-12-08 23:21:25 +00:00
|
|
|
thumbs_pane_id: Option<String>,
|
|
|
|
content: Option<String>,
|
2020-01-08 22:11:44 +00:00
|
|
|
signal: String,
|
2019-12-08 23:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Swapper<'a> {
|
2020-04-30 16:10:07 +00:00
|
|
|
fn new(executor: Box<&'a mut dyn Executor>, dir: String, command: String, upcase_command: String) -> Swapper {
|
2020-01-08 22:11:44 +00:00
|
|
|
let since_the_epoch = SystemTime::now()
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
.expect("Time went backwards");
|
|
|
|
let signal = format!("thumbs-finished-{}", since_the_epoch.as_secs());
|
|
|
|
|
2019-12-08 23:21:25 +00:00
|
|
|
Swapper {
|
2020-04-23 19:56:45 +00:00
|
|
|
executor,
|
|
|
|
dir,
|
|
|
|
command,
|
|
|
|
upcase_command,
|
2019-12-08 23:21:25 +00:00
|
|
|
active_pane_id: None,
|
2020-01-19 23:10:21 +00:00
|
|
|
active_pane_height: None,
|
|
|
|
active_pane_scroll_position: None,
|
|
|
|
active_pane_in_copy_mode: None,
|
2019-12-08 23:21:25 +00:00
|
|
|
thumbs_pane_id: None,
|
|
|
|
content: None,
|
2020-04-23 19:56:45 +00:00
|
|
|
signal,
|
2019-12-08 23:21:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn capture_active_pane(&mut self) {
|
|
|
|
let active_command = vec![
|
|
|
|
"tmux",
|
|
|
|
"list-panes",
|
|
|
|
"-F",
|
2020-01-19 23:10:21 +00:00
|
|
|
"#{pane_id}:#{?pane_in_mode,1,0}:#{pane_height}:#{scroll_position}:#{?pane_active,active,nope}",
|
2019-12-08 23:21:25 +00:00
|
|
|
];
|
2020-01-19 23:10:21 +00:00
|
|
|
|
2019-12-08 23:21:25 +00:00
|
|
|
let output = self
|
|
|
|
.executor
|
|
|
|
.execute(active_command.iter().map(|arg| arg.to_string()).collect());
|
2020-01-19 23:10:21 +00:00
|
|
|
|
2020-04-23 19:56:45 +00:00
|
|
|
let lines: Vec<&str> = output.split('\n').collect();
|
2020-04-30 16:10:07 +00:00
|
|
|
let chunks: Vec<Vec<&str>> = lines.into_iter().map(|line| line.split(':').collect()).collect();
|
2019-12-08 23:21:25 +00:00
|
|
|
|
2020-01-19 23:10:21 +00:00
|
|
|
let active_pane = chunks
|
2019-12-08 23:21:25 +00:00
|
|
|
.iter()
|
2020-04-23 19:56:45 +00:00
|
|
|
.find(|&chunks| *chunks.get(4).unwrap() == "active")
|
2019-12-08 23:21:25 +00:00
|
|
|
.expect("Unable to find active pane");
|
|
|
|
|
2020-04-23 19:56:45 +00:00
|
|
|
let pane_id = active_pane.get(0).unwrap();
|
|
|
|
let pane_in_copy_mode = active_pane.get(1).unwrap().to_string();
|
2020-01-19 23:10:21 +00:00
|
|
|
|
|
|
|
self.active_pane_id = Some(pane_id.to_string());
|
|
|
|
self.active_pane_in_copy_mode = Some(pane_in_copy_mode);
|
|
|
|
|
|
|
|
if self.active_pane_in_copy_mode.clone().unwrap() == "1" {
|
|
|
|
let pane_height = active_pane
|
2020-04-23 19:56:45 +00:00
|
|
|
.get(2)
|
2020-01-19 23:10:21 +00:00
|
|
|
.unwrap()
|
|
|
|
.parse()
|
|
|
|
.expect("Unable to retrieve pane height");
|
|
|
|
let pane_scroll_position = active_pane
|
2020-04-23 19:56:45 +00:00
|
|
|
.get(3)
|
2020-01-19 23:10:21 +00:00
|
|
|
.unwrap()
|
|
|
|
.parse()
|
|
|
|
.expect("Unable to retrieve pane scroll");
|
|
|
|
|
|
|
|
self.active_pane_height = Some(pane_height);
|
|
|
|
self.active_pane_scroll_position = Some(pane_scroll_position);
|
|
|
|
}
|
2019-12-08 23:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn execute_thumbs(&mut self) {
|
|
|
|
let options_command = vec!["tmux", "show", "-g"];
|
|
|
|
let params: Vec<String> = options_command.iter().map(|arg| arg.to_string()).collect();
|
|
|
|
let options = self.executor.execute(params);
|
2020-04-23 19:56:45 +00:00
|
|
|
let lines: Vec<&str> = options.split('\n').collect();
|
2019-12-08 23:21:25 +00:00
|
|
|
|
|
|
|
let pattern = Regex::new(r#"@thumbs-([\w\-0-9]+) "(.*)""#).unwrap();
|
|
|
|
|
|
|
|
let args = lines
|
|
|
|
.iter()
|
|
|
|
.flat_map(|line| {
|
|
|
|
if let Some(captures) = pattern.captures(line) {
|
|
|
|
let name = captures.get(1).unwrap().as_str();
|
|
|
|
let value = captures.get(2).unwrap().as_str();
|
|
|
|
|
|
|
|
let boolean_params = vec!["reverse", "unique", "contrast"];
|
|
|
|
|
|
|
|
if boolean_params.iter().any(|&x| x == name) {
|
2020-04-23 19:56:45 +00:00
|
|
|
return vec![format!("--{}", name)];
|
2019-12-08 23:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let string_params = vec![
|
|
|
|
"position",
|
|
|
|
"fg-color",
|
|
|
|
"bg-color",
|
|
|
|
"hint-bg-color",
|
|
|
|
"hint-fg-color",
|
|
|
|
"select-fg-color",
|
|
|
|
"select-bg-color",
|
|
|
|
];
|
|
|
|
|
|
|
|
if string_params.iter().any(|&x| x == name) {
|
2020-04-30 16:10:07 +00:00
|
|
|
return vec![format!("--{}", name), format!("'{}'", value)];
|
2019-12-08 23:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if name.starts_with("regexp") {
|
2020-04-23 19:56:45 +00:00
|
|
|
return vec!["--regexp".to_string(), format!("'{}'", value)];
|
2019-12-08 23:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vec![]
|
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<String>>();
|
|
|
|
|
2020-01-08 22:11:44 +00:00
|
|
|
let active_pane_id = self.active_pane_id.as_mut().unwrap().clone();
|
|
|
|
|
2020-01-19 23:10:21 +00:00
|
|
|
let scroll_params = if self.active_pane_in_copy_mode.is_some() {
|
2020-04-30 16:10:07 +00:00
|
|
|
if let (Some(pane_height), Some(scroll_position)) =
|
|
|
|
(self.active_pane_scroll_position, self.active_pane_scroll_position)
|
|
|
|
{
|
|
|
|
format!(" -S {} -E {}", -scroll_position, pane_height - scroll_position - 1)
|
2020-01-19 23:10:21 +00:00
|
|
|
} else {
|
|
|
|
"".to_string()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
"".to_string()
|
|
|
|
};
|
|
|
|
|
2020-01-08 22:11:44 +00:00
|
|
|
// NOTE: For debugging add echo $PWD && sleep 5 after tee
|
2020-01-19 23:10:21 +00:00
|
|
|
let pane_command = format!(
|
2020-04-30 16:10:07 +00:00
|
|
|
"tmux capture-pane -t {} -p{} | {}/target/release/thumbs -f '%U:%H' -t {} {}; tmux swap-pane -t {}; tmux wait-for -S {}",
|
2020-01-19 23:10:21 +00:00
|
|
|
active_pane_id,
|
|
|
|
scroll_params,
|
|
|
|
self.dir,
|
|
|
|
TMP_FILE,
|
2020-04-30 16:10:07 +00:00
|
|
|
args.join(" "),
|
2020-01-19 23:10:21 +00:00
|
|
|
active_pane_id,
|
|
|
|
self.signal
|
|
|
|
);
|
2020-01-08 22:11:44 +00:00
|
|
|
|
2019-12-08 23:21:25 +00:00
|
|
|
let thumbs_command = vec![
|
|
|
|
"tmux",
|
|
|
|
"new-window",
|
|
|
|
"-P",
|
2020-01-08 22:11:44 +00:00
|
|
|
"-d",
|
2019-12-08 23:21:25 +00:00
|
|
|
"-n",
|
|
|
|
"[thumbs]",
|
|
|
|
pane_command.as_str(),
|
|
|
|
];
|
2020-01-08 22:11:44 +00:00
|
|
|
|
2019-12-08 23:21:25 +00:00
|
|
|
let params: Vec<String> = thumbs_command.iter().map(|arg| arg.to_string()).collect();
|
|
|
|
|
|
|
|
self.thumbs_pane_id = Some(self.executor.execute(params));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn swap_panes(&mut self) {
|
|
|
|
let active_pane_id = self.active_pane_id.as_mut().unwrap().clone();
|
|
|
|
let thumbs_pane_id = self.thumbs_pane_id.as_mut().unwrap().clone();
|
|
|
|
|
|
|
|
let swap_command = vec![
|
|
|
|
"tmux",
|
|
|
|
"swap-pane",
|
|
|
|
"-d",
|
|
|
|
"-s",
|
|
|
|
active_pane_id.as_str(),
|
|
|
|
"-t",
|
|
|
|
thumbs_pane_id.as_str(),
|
|
|
|
];
|
|
|
|
let params = swap_command.iter().map(|arg| arg.to_string()).collect();
|
|
|
|
|
|
|
|
self.executor.execute(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn wait_thumbs(&mut self) {
|
2020-01-08 22:11:44 +00:00
|
|
|
let wait_command = vec!["tmux", "wait-for", self.signal.as_str()];
|
2019-12-08 23:21:25 +00:00
|
|
|
let params = wait_command.iter().map(|arg| arg.to_string()).collect();
|
|
|
|
|
|
|
|
self.executor.execute(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn retrieve_content(&mut self) {
|
|
|
|
let retrieve_command = vec!["cat", TMP_FILE];
|
|
|
|
let params = retrieve_command.iter().map(|arg| arg.to_string()).collect();
|
|
|
|
|
|
|
|
self.content = Some(self.executor.execute(params));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn destroy_content(&mut self) {
|
2020-04-30 16:10:07 +00:00
|
|
|
let retrieve_command = vec!["rm", TMP_FILE];
|
2019-12-08 23:21:25 +00:00
|
|
|
let params = retrieve_command.iter().map(|arg| arg.to_string()).collect();
|
|
|
|
|
|
|
|
self.executor.execute(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn execute_command(&mut self) {
|
|
|
|
let content = self.content.clone().unwrap();
|
|
|
|
let mut splitter = content.splitn(2, ':');
|
|
|
|
|
2020-05-02 15:29:32 +00:00
|
|
|
if let Some(upcase) = splitter.next() {
|
|
|
|
if let Some(text) = splitter.next() {
|
|
|
|
let execute_command = if upcase.trim_end() == "true" {
|
|
|
|
self.upcase_command.clone()
|
|
|
|
} else {
|
|
|
|
self.command.clone()
|
|
|
|
};
|
2019-12-08 23:21:25 +00:00
|
|
|
|
2020-05-02 15:29:32 +00:00
|
|
|
let final_command = str::replace(execute_command.as_str(), "{}", text.trim_end());
|
|
|
|
let retrieve_command = vec!["bash", "-c", final_command.as_str()];
|
|
|
|
let params = retrieve_command.iter().map(|arg| arg.to_string()).collect();
|
2019-12-08 23:21:25 +00:00
|
|
|
|
2020-05-02 15:29:32 +00:00
|
|
|
self.executor.execute(params);
|
|
|
|
}
|
|
|
|
}
|
2019-12-08 23:21:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
struct TestShell {
|
|
|
|
outputs: Vec<String>,
|
|
|
|
executed: Option<Vec<String>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestShell {
|
|
|
|
fn new(outputs: Vec<String>) -> TestShell {
|
|
|
|
TestShell {
|
|
|
|
executed: None,
|
2020-04-23 19:56:45 +00:00
|
|
|
outputs,
|
2019-12-08 23:21:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Executor for TestShell {
|
|
|
|
fn execute(&mut self, args: Vec<String>) -> String {
|
|
|
|
self.executed = Some(args);
|
|
|
|
self.outputs.pop().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn last_executed(&self) -> Option<Vec<String>> {
|
|
|
|
self.executed.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn retrieve_active_pane() {
|
2020-04-30 16:10:07 +00:00
|
|
|
let last_command_outputs = vec!["%97:100:24:1:active\n%106:100:24:1:nope\n%107:100:24:1:nope\n".to_string()];
|
2019-12-08 23:21:25 +00:00
|
|
|
let mut executor = TestShell::new(last_command_outputs);
|
2020-04-30 16:10:07 +00:00
|
|
|
let mut swapper = Swapper::new(Box::new(&mut executor), "".to_string(), "".to_string(), "".to_string());
|
2019-12-08 23:21:25 +00:00
|
|
|
|
|
|
|
swapper.capture_active_pane();
|
|
|
|
|
|
|
|
assert_eq!(swapper.active_pane_id.unwrap(), "%97");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn swap_panes() {
|
|
|
|
let last_command_outputs = vec![
|
|
|
|
"".to_string(),
|
|
|
|
"%100".to_string(),
|
|
|
|
"".to_string(),
|
2020-01-19 23:10:21 +00:00
|
|
|
"%106:100:24:1:nope\n%98:100:24:1:active\n%107:100:24:1:nope\n".to_string(),
|
2019-12-08 23:21:25 +00:00
|
|
|
];
|
|
|
|
let mut executor = TestShell::new(last_command_outputs);
|
2020-04-30 16:10:07 +00:00
|
|
|
let mut swapper = Swapper::new(Box::new(&mut executor), "".to_string(), "".to_string(), "".to_string());
|
2019-12-08 23:21:25 +00:00
|
|
|
|
|
|
|
swapper.capture_active_pane();
|
|
|
|
swapper.execute_thumbs();
|
|
|
|
swapper.swap_panes();
|
|
|
|
|
|
|
|
let expectation = vec!["tmux", "swap-pane", "-d", "-s", "%98", "-t", "%100"];
|
|
|
|
|
|
|
|
assert_eq!(executor.last_executed().unwrap(), expectation);
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 19:56:45 +00:00
|
|
|
|
2019-12-08 23:21:25 +00:00
|
|
|
fn app_args<'a>() -> clap::ArgMatches<'a> {
|
2020-04-23 19:56:45 +00:00
|
|
|
App::new("tmux-thumbs")
|
2019-12-08 23:21:25 +00:00
|
|
|
.version(crate_version!())
|
|
|
|
.about("A lightning fast version of tmux-fingers, copy/pasting tmux like vimium/vimperator")
|
2020-01-08 22:11:44 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("dir")
|
|
|
|
.help("Directory where to execute thumbs")
|
|
|
|
.long("dir")
|
|
|
|
.default_value(""),
|
|
|
|
)
|
2019-12-08 23:21:25 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("command")
|
|
|
|
.help("Pick command")
|
|
|
|
.long("command")
|
|
|
|
.default_value("tmux set-buffer {}"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("upcase_command")
|
|
|
|
.help("Upcase command")
|
|
|
|
.long("upcase-command")
|
|
|
|
.default_value("tmux set-buffer {} && tmux paste-buffer"),
|
|
|
|
)
|
2020-04-23 19:56:45 +00:00
|
|
|
.get_matches()
|
2019-12-08 23:21:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 22:11:44 +00:00
|
|
|
fn main() -> std::io::Result<()> {
|
2019-12-08 23:21:25 +00:00
|
|
|
let args = app_args();
|
2020-01-08 22:11:44 +00:00
|
|
|
let dir = args.value_of("dir").unwrap();
|
2019-12-08 23:21:25 +00:00
|
|
|
let command = args.value_of("command").unwrap();
|
|
|
|
let upcase_command = args.value_of("upcase_command").unwrap();
|
|
|
|
|
2020-05-02 15:29:32 +00:00
|
|
|
if dir.is_empty() {
|
|
|
|
panic!("Invalid tmux-thumbs execution. Are you trying to execute tmux-thumbs directly?")
|
|
|
|
}
|
|
|
|
|
2019-12-08 23:21:25 +00:00
|
|
|
let mut executor = RealShell::new();
|
|
|
|
let mut swapper = Swapper::new(
|
|
|
|
Box::new(&mut executor),
|
2020-01-08 22:11:44 +00:00
|
|
|
dir.to_string(),
|
2019-12-08 23:21:25 +00:00
|
|
|
command.to_string(),
|
|
|
|
upcase_command.to_string(),
|
|
|
|
);
|
|
|
|
|
|
|
|
swapper.capture_active_pane();
|
|
|
|
swapper.execute_thumbs();
|
|
|
|
swapper.swap_panes();
|
|
|
|
swapper.wait_thumbs();
|
|
|
|
swapper.retrieve_content();
|
|
|
|
swapper.destroy_content();
|
|
|
|
swapper.execute_command();
|
2020-01-08 22:11:44 +00:00
|
|
|
Ok(())
|
2019-12-08 23:21:25 +00:00
|
|
|
}
|