Custom commands

This commit is contained in:
Ferran Basora 2019-03-02 18:49:00 +00:00
parent 50b5c328d7
commit 139808e7ef
2 changed files with 37 additions and 17 deletions

View File

@ -65,11 +65,14 @@ FLAGS:
OPTIONS: OPTIONS:
-a, --alphabet <alphabet> Sets the alphabet [default: qwerty] -a, --alphabet <alphabet> Sets the alphabet [default: qwerty]
--bg-color <background_color> Sets the background color for matches [default: black] --bg-color <background_color> Sets the background color for matches [default: black]
--command <command> Pick command [default: tmux set-buffer {}]
--fg-color <foreground_color> Sets the foregroud color for matches [default: green] --fg-color <foreground_color> Sets the foregroud color for matches [default: green]
--hint-bg-color <hint_background_color> Sets the background color for hints [default: black] --hint-bg-color <hint_background_color> Sets the background color for hints [default: black]
--hint-fg-color <hint_foreground_color> Sets the foregroud color for hints [default: yellow] --hint-fg-color <hint_foreground_color> Sets the foregroud color for hints [default: yellow]
-p, --position <position> Hint position [default: left] -p, --position <position> Hint position [default: left]
--select-fg-color <select_foreground_color> Sets the foregroud color for selection [default: blue] --select-fg-color <select_foreground_color> Sets the foregroud color for selection [default: blue]
--upcase-command <upcase_command> Upcase command [default: tmux paste-buffer]
``` ```
### Arguments ### Arguments
@ -78,6 +81,8 @@ OPTIONS:
- **reverse:** Choose in which direction you want to assign hints. Useful to get shorter hints closer. - **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. - **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] - **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] - **bg-color:** Sets the background color for matches [default: black]
- **fg-color:** Sets the foregroud color for matches [default: green] - **fg-color:** Sets the foregroud color for matches [default: green]

View File

@ -64,6 +64,14 @@ fn app_args<'a> () -> clap::ArgMatches<'a> {
.long("position") .long("position")
.default_value("left") .default_value("left")
.short("p")) .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(); .get_matches();
} }
@ -80,13 +88,21 @@ fn main() {
let hint_background_color = colors::get_color(args.value_of("hint_background_color").unwrap()); 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 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 execution = exec_command(format!("tmux capture-pane -e -J -p"));
let output = String::from_utf8_lossy(&execution.stdout); let output = String::from_utf8_lossy(&execution.stdout);
let lines = output.split("\n").collect::<Vec<&str>>(); let lines = output.split("\n").collect::<Vec<&str>>();
let mut state = state::State::new(&lines, alphabet); let mut state = state::State::new(&lines, alphabet);
let mut selected;
let mut paste = false; 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()) { let mut rustbox = match RustBox::init(Default::default()) {
Result::Ok(v) => v, 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 { loop {
let mut selected = matches.last(); selected = matches.last();
match matches.iter().enumerate().find(|&h| h.0 == state.skip) { match matches.iter().enumerate().find(|&h| h.0 == state.skip) {
Some(hm) => { Some(hm) => {
@ -147,17 +159,15 @@ fn main() {
match key { match key {
Key::Esc => { break; } Key::Esc => { break; }
Key::Enter => { Key::Enter => {
let mut choosen = matches.first().unwrap(); selected = Some(matches.first().unwrap());
match matches.iter().enumerate().find(|&h| h.0 == state.skip) { match matches.iter().enumerate().find(|&h| h.0 == state.skip) {
Some(hm) => { Some(hm) => {
choosen = hm.1; selected = Some(hm.1);
} }
_ => {} _ => {}
} }
exec_command(format!("tmux set-buffer {}", choosen.text));
break; break;
} }
Key::Up => { state.prev(); } Key::Up => { state.prev(); }
@ -167,19 +177,19 @@ fn main() {
Key::Char(ch) => { Key::Char(ch) => {
let key = ch.to_string(); let key = ch.to_string();
let lower_key = key.to_lowercase(); let lower_key = key.to_lowercase();
typed_hint.push_str(lower_key.as_str()); typed_hint.push_str(lower_key.as_str());
match matches.iter().find(|mat| mat.hint == Some(typed_hint.clone())) { match matches.iter().find(|mat| mat.hint == Some(typed_hint.clone())) {
Some(mat) => { Some(mat) => {
exec_command(format!("tmux set-buffer {}", mat.text)); selected = Some(mat);
paste = key != lower_key;
if key != lower_key {
paste = true;
}
break; break;
}, },
None => { None => {
if typed_hint.len() > longest_hint { if typed_hint.len() >= longest_hint {
selected = None;
break; break;
} }
} }
@ -194,7 +204,12 @@ fn main() {
} }
} }
if paste { if let Some(mat) = selected {
exec_command(format!("tmux paste-buffer")); exec_command(str::replace(command, "{}", mat.text));
if paste {
exec_command(upcase_command.to_string());
}
} }
} }