From fb7e6da20a5cfa09676a73842258afe2bd35dd5d Mon Sep 17 00:00:00 2001 From: Ferran Basora Date: Tue, 3 Dec 2019 20:54:04 +0000 Subject: [PATCH] Fix command and paste command arguments Improve flexibility in `command` and `upcase-command` replacing `{}` with the selected hint. --- README.md | 8 ++++---- src/main.rs | 31 +++++++++++++++++++------------ 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 7a05356..3750294 100644 --- a/README.md +++ b/README.md @@ -156,24 +156,24 @@ set @thumbs-regexp-2 '[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2 `default: 'tmux set-buffer {}'` -Choose which command execute when you press a hint. +Choose which command execute when you press a hint. `tmux-thumbs` will replace `{}` with the picked hint. For example: ``` -set -g @thumbs-command 'pbcopy' +set -g @thumbs-command 'echo {} | pbcopy' ``` ### @thumbs-upcase-command `default: 'tmux paste-buffer'` -Choose which command execute when you press a upcase hint +Choose which command execute when you press a upcase hint. `tmux-thumbs` will replace `{}` with the picked hint. For example: ``` -set -g @thumbs-upcase-command 'pbcopy' +set -g @thumbs-upcase-command 'echo {} | pbcopy' ``` ### @thumbs-bg-color diff --git a/src/main.rs b/src/main.rs index 1b182d5..6439b4b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,9 +10,7 @@ use self::clap::{App, Arg}; use clap::crate_version; use std::process::Command; -fn exec_command(command: String) -> std::process::Output { - let args: Vec<_> = command.split(" ").collect(); - +fn exec_command(args: Vec<&str>) -> std::process::Output { return Command::new(args[0]) .args(&args[1..]) .output() @@ -136,13 +134,14 @@ fn main() { let command = args.value_of("command").unwrap(); let upcase_command = args.value_of("upcase_command").unwrap(); - let tmux_subcommand = if let Some(pane) = args.value_of("tmux_pane") { - format!(" -t {}", pane) - } else { - "".to_string() - }; - let execution = exec_command(format!("tmux capture-pane -e -J -p{}", tmux_subcommand)); + let mut capture_command = vec!["tmux", "capture-pane", "-e", "-J", "-p"]; + + if let Some(pane) = args.value_of("tmux_pane") { + capture_command.extend(vec!["-t", pane].iter().cloned()); + } + + let execution = exec_command(capture_command); let output = String::from_utf8_lossy(&execution.stdout); let lines = output.split("\n").collect::>(); @@ -166,14 +165,22 @@ fn main() { }; if let Some(pane) = args.value_of("tmux_pane") { - exec_command(format!("tmux swap-pane -t {}", pane)); + exec_command(vec!["tmux", "swap-pane", "-t", pane]); }; if let Some((text, paste)) = selected { - exec_command(str::replace(command, "{}", text.as_str())); + exec_command(vec![ + "bash", + "-c", + str::replace(command, "{}", text.as_str()).as_str(), + ]); if paste { - exec_command(upcase_command.to_string()); + exec_command(vec![ + "bash", + "-c", + str::replace(upcase_command, "{}", text.as_str()).as_str(), + ]); } } }