From 139808e7effb6d6cda04b0fb616fc74b7a5aea3f Mon Sep 17 00:00:00 2001 From: Ferran Basora Date: Sat, 2 Mar 2019 18:49:00 +0000 Subject: [PATCH] Custom commands --- README.md | 5 +++++ src/main.rs | 49 ++++++++++++++++++++++++++++++++----------------- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index fe60d5a..542a785 100644 --- a/README.md +++ b/README.md @@ -65,11 +65,14 @@ FLAGS: OPTIONS: -a, --alphabet Sets the alphabet [default: qwerty] --bg-color Sets the background color for matches [default: black] + --command Pick command [default: tmux set-buffer {}] --fg-color Sets the foregroud color for matches [default: green] --hint-bg-color Sets the background color for hints [default: black] --hint-fg-color Sets the foregroud color for hints [default: yellow] -p, --position Hint position [default: left] --select-fg-color Sets the foregroud color for selection [default: blue] + --upcase-command Upcase command [default: tmux paste-buffer] + ``` ### Arguments @@ -78,6 +81,8 @@ OPTIONS: - **reverse:** Choose in which direction you want to assign hints. Useful to get shorter hints closer. - **unique:** Choose if you want to assign the same hint for the same matched strings. - **position:** Choose where do you want to show the hint in the matched string. Options (left, right). Default [left] +- **command:** Choose whish command execute when you press a hint +- **upcase-command:** Choose which command execute when you press a upcase hint - **bg-color:** Sets the background color for matches [default: black] - **fg-color:** Sets the foregroud color for matches [default: green] diff --git a/src/main.rs b/src/main.rs index ba50d11..6044a06 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,6 +64,14 @@ fn app_args<'a> () -> clap::ArgMatches<'a> { .long("position") .default_value("left") .short("p")) + .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 paste-buffer")) .get_matches(); } @@ -80,13 +88,21 @@ fn main() { let hint_background_color = colors::get_color(args.value_of("hint_background_color").unwrap()); let select_foreground_color = colors::get_color(args.value_of("select_foreground_color").unwrap()); + let command = args.value_of("command").unwrap(); + let upcase_command = args.value_of("upcase_command").unwrap(); + let execution = exec_command(format!("tmux capture-pane -e -J -p")); let output = String::from_utf8_lossy(&execution.stdout); let lines = output.split("\n").collect::>(); let mut state = state::State::new(&lines, alphabet); + let mut selected; let mut paste = false; + let mut typed_hint: String = "".to_owned(); + let matches = state.matches(reverse, unique); + let longest_hint = matches.iter().filter(|&m| m.hint.clone().is_some()).last().unwrap().hint.clone().expect("Unknown hint").len(); + { let mut rustbox = match RustBox::init(Default::default()) { Result::Ok(v) => v, @@ -104,12 +120,8 @@ fn main() { } } - let mut typed_hint: String = "".to_owned(); - let matches = state.matches(reverse, unique); - let longest_hint = matches.iter().filter(|&m| m.hint.clone().is_some()).last().unwrap().hint.clone().expect("Unknown hint").len(); - loop { - let mut selected = matches.last(); + selected = matches.last(); match matches.iter().enumerate().find(|&h| h.0 == state.skip) { Some(hm) => { @@ -147,17 +159,15 @@ fn main() { match key { Key::Esc => { break; } Key::Enter => { - let mut choosen = matches.first().unwrap(); + selected = Some(matches.first().unwrap()); match matches.iter().enumerate().find(|&h| h.0 == state.skip) { Some(hm) => { - choosen = hm.1; + selected = Some(hm.1); } _ => {} } - exec_command(format!("tmux set-buffer {}", choosen.text)); - break; } Key::Up => { state.prev(); } @@ -167,19 +177,19 @@ fn main() { Key::Char(ch) => { let key = ch.to_string(); let lower_key = key.to_lowercase(); + typed_hint.push_str(lower_key.as_str()); + match matches.iter().find(|mat| mat.hint == Some(typed_hint.clone())) { Some(mat) => { - exec_command(format!("tmux set-buffer {}", mat.text)); - - if key != lower_key { - paste = true; - } + selected = Some(mat); + paste = key != lower_key; break; }, None => { - if typed_hint.len() > longest_hint { + if typed_hint.len() >= longest_hint { + selected = None; break; } } @@ -194,7 +204,12 @@ fn main() { } } - if paste { - exec_command(format!("tmux paste-buffer")); + if let Some(mat) = selected { + exec_command(str::replace(command, "{}", mat.text)); + + if paste { + exec_command(upcase_command.to_string()); + } } + }