Skip to content

Commit e29b07f

Browse files
committed
New set of available keys (Ctrl, Alt)
A part of the common `tmux-thumbs` hints + the shift ones, we want to introduce a new set of combinations with `ctrl` and `alt` keys. This is a breaking change because the configuration options has been renamed.
1 parent 50b8cc8 commit e29b07f

File tree

5 files changed

+151
-60
lines changed

5 files changed

+151
-60
lines changed

README.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ These are the list of matched patterns that will be highlighted by default. If
3333
you want to highlight a pattern that is not in this list you can add one or
3434
more with `--regexp` parameter.
3535

36+
37+
## Key shortcuts
38+
39+
While in **[thumbs]** mode, you can use the following shortcuts:
40+
41+
* <kbd>a</kbd>-<kbd>z</kbd>: copies selected match to the clipboard
42+
* <kbd>CTRL</kbd> + <kbd>a</kbd>-<kbd>z</kbd>: copies selected match to the clipboard and triggers [@thumbs-ctrl-action](#thumbs-ctrl-action).
43+
* <kbd>SHIFT</kbd> + <kbd>a</kbd>-<kbd>z</kbd>: copies selected match to the clipboard and triggers [@thumbs-shift-action](#thumbs-shift-action).
44+
* <kbd>ALT</kbd> + <kbd>a</kbd>-<kbd>z</kbd>: copies selected match to the clipboard and triggers [@thumbs-alt-action](#thumbs-alt-action).
45+
* <kbd>CTRL</kbd> + <kbd>c</kbd>: exit **[thumbs]** mode
46+
* <kbd>ESC</kbd>: exit help or **[thumbs]** mode
47+
* <kbd>?</kbd>: show help.
48+
3649
## Demo
3750

3851
[![demo](https://asciinema.org/a/232775.png?ts=1)](https://asciinema.org/a/232775?autoplay=1)
@@ -93,9 +106,13 @@ NOTE: for changes to take effect, you'll need to source again your `.tmux.conf`
93106
* [@thumbs-unique](#thumbs-unique)
94107
* [@thumbs-position](#thumbs-position)
95108
* [@thumbs-regexp-N](#thumbs-regexp-N)
96-
* [@thumbs-command](#thumbs-command)
97-
* [@thumbs-upcase-command](#thumbs-upcase-command)
98-
* [@thumbs-multi-command](#thumbs-multi-command)
109+
110+
* [@thumbs-main-action](#thumbs-main-action)
111+
* [@thumbs-shift-action](#thumbs-shift-action)
112+
* [@thumbs-ctrl-action](#thumbs-ctrl-action)
113+
* [@thumbs-alt-action](#thumbs-alt-action)
114+
* [@thumbs-multi-action](#thumbs-multi-action)
115+
99116
* [@thumbs-bg-color](#thumbs-bg-color)
100117
* [@thumbs-fg-color](#thumbs-fg-color)
101118
* [@thumbs-hint-bg-color](#thumbs-hint-bg-color)
@@ -106,6 +123,9 @@ NOTE: for changes to take effect, you'll need to source again your `.tmux.conf`
106123
* [@thumbs-multi-bg-color](#thumbs-multi-bg-color)
107124
* [@thumbs-contrast](#thumbs-contrast)
108125
* [@thumbs-osc52](#thumbs-osc52)
126+
* deprecated: [@thumbs-command](#thumbs-command)
127+
* deprecated: [@thumbs-upcase-command](#thumbs-upcase-command)
128+
* deprecated: [@thumbs-multi-command](#thumbs-multi-command)
109129

110130
### @thumbs-key
111131

src/main.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn app_args<'a>() -> clap::ArgMatches<'a> {
3737
)
3838
.arg(
3939
Arg::with_name("format")
40-
.help("Specifies the out format for the picked hint. (%U: Upcase, %H: Hint)")
40+
.help("Specifies the out format for the picked hint. (%K: Key, %H: Hint)")
4141
.long("format")
4242
.short("f")
4343
.default_value("%H"),
@@ -198,12 +198,10 @@ fn main() {
198198
if !selected.is_empty() {
199199
let output = selected
200200
.iter()
201-
.map(|(text, upcase)| {
202-
let upcase_value = if *upcase { "true" } else { "false" };
203-
201+
.map(|(text, modkey)| {
204202
let mut output = format.to_string();
205203

206-
output = str::replace(&output, "%U", upcase_value);
204+
output = str::replace(&output, "%K", modkey);
207205
output = str::replace(&output, "%H", text.as_str());
208206
output
209207
})

src/swapper.rs

Lines changed: 79 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
extern crate clap;
22

3+
mod view;
4+
35
use self::clap::{App, Arg};
46
use clap::crate_version;
57
use regex::Regex;
@@ -58,9 +60,11 @@ fn dbg(msg: &str) {
5860
pub struct Swapper<'a> {
5961
executor: Box<&'a mut dyn Executor>,
6062
dir: String,
61-
command: String,
62-
upcase_command: String,
63-
multi_command: String,
63+
main_action: String,
64+
shift_action: String,
65+
ctrl_action: String,
66+
alt_action: String,
67+
multi_action: String,
6468
osc52: bool,
6569
active_pane_id: Option<String>,
6670
active_pane_height: Option<i32>,
@@ -75,9 +79,11 @@ impl<'a> Swapper<'a> {
7579
fn new(
7680
executor: Box<&'a mut dyn Executor>,
7781
dir: String,
78-
command: String,
79-
upcase_command: String,
80-
multi_command: String,
82+
main_action: String,
83+
shift_action: String,
84+
ctrl_action: String,
85+
alt_action: String,
86+
multi_action: String,
8187
osc52: bool,
8288
) -> Swapper {
8389
let since_the_epoch = SystemTime::now()
@@ -88,9 +94,11 @@ impl<'a> Swapper<'a> {
8894
Swapper {
8995
executor,
9096
dir,
91-
command,
92-
upcase_command,
93-
multi_command,
97+
main_action,
98+
shift_action,
99+
ctrl_action,
100+
alt_action,
101+
multi_action,
94102
osc52,
95103
active_pane_id: None,
96104
active_pane_height: None,
@@ -215,7 +223,7 @@ impl<'a> Swapper<'a> {
215223
};
216224

217225
let pane_command = format!(
218-
"tmux capture-pane -t {active_pane_id} -p{scroll_params} | tail -n {height} | {dir}/target/release/thumbs -f '%U:%H' -t {tmp} {args}; tmux swap-pane -t {active_pane_id}; {zoom_command} tmux wait-for -S {signal}",
226+
"tmux capture-pane -t {active_pane_id} -p{scroll_params} | tail -n {height} | {dir}/target/release/thumbs -f '%K:%H' -t {tmp} {args}; tmux swap-pane -t {active_pane_id}; {zoom_command} tmux wait-for -S {signal}",
219227
active_pane_id = active_pane_id,
220228
scroll_params = scroll_params,
221229
height = self.active_pane_height.unwrap_or(i32::MAX),
@@ -320,7 +328,8 @@ impl<'a> Swapper<'a> {
320328
.collect::<Vec<&str>>()
321329
.join(" ");
322330

323-
self.execute_final_command(&text, &self.multi_command.clone());
331+
// REVIEW: Not sure about the ModifierKeys here
332+
self.execute_final_command(&text, &view::ModifierKeys::Main.to_string(), &self.multi_action.clone());
324333

325334
return;
326335
}
@@ -330,7 +339,7 @@ impl<'a> Swapper<'a> {
330339

331340
let mut splitter = item.splitn(2, ':');
332341

333-
if let Some(upcase) = splitter.next() {
342+
if let Some(modkey) = splitter.next() {
334343
if let Some(text) = splitter.next() {
335344
if self.osc52 {
336345
let base64_text = base64::encode(text.as_bytes());
@@ -360,10 +369,17 @@ impl<'a> Swapper<'a> {
360369
std::io::stdout().flush().unwrap();
361370
}
362371

363-
let execute_command = if upcase.trim_end() == "true" {
364-
self.upcase_command.clone()
365-
} else {
366-
self.command.clone()
372+
let modkey = modkey.trim_end();
373+
374+
let shift = view::ModifierKeys::Shift.to_string();
375+
let ctrl = view::ModifierKeys::Ctrl.to_string();
376+
let alt = view::ModifierKeys::Alt.to_string();
377+
378+
let execute_command = match modkey {
379+
shift => self.shift_action.clone(),
380+
ctrl => self.ctrl_action.clone(),
381+
alt => self.alt_action.clone(),
382+
_ => self.main_action.clone(),
367383
};
368384

369385
// The command we run has two arguments:
@@ -389,19 +405,20 @@ impl<'a> Swapper<'a> {
389405
// Ideally user commands would just use "${THUMB}" to begin with rather than having any
390406
// sort of ad-hoc string splicing here at all, and then they could specify the quoting they
391407
// want, but that would break backwards compatibility.
392-
self.execute_final_command(text.trim_end(), &execute_command);
408+
self.execute_final_command(text.trim_end(), modkey, &execute_command);
393409
}
394410
}
395411
}
396412

397-
pub fn execute_final_command(&mut self, text: &str, execute_command: &str) {
413+
pub fn execute_final_command(&mut self, text: &str, modkey: &str, execute_command: &str) {
398414
let final_command = str::replace(execute_command, "{}", "${THUMB}");
399415
let retrieve_command = vec![
400416
"bash",
401417
"-c",
402-
"THUMB=\"$1\"; eval \"$2\"",
418+
"THUMB=\"$1\"; MODIFIER=\"$2\"; eval \"$3\"",
403419
"--",
404420
text,
421+
modkey,
405422
final_command.as_str(),
406423
];
407424

@@ -450,6 +467,8 @@ mod tests {
450467
"".to_string(),
451468
"".to_string(),
452469
"".to_string(),
470+
"".to_string(),
471+
"".to_string(),
453472
false,
454473
);
455474

@@ -473,6 +492,8 @@ mod tests {
473492
"".to_string(),
474493
"".to_string(),
475494
"".to_string(),
495+
"".to_string(),
496+
"".to_string(),
476497
false,
477498
);
478499

@@ -490,15 +511,19 @@ mod tests {
490511
let last_command_outputs = vec!["Blah blah blah, the ignored user script output".to_string()];
491512
let mut executor = TestShell::new(last_command_outputs);
492513

493-
let user_command = "echo \"{}\"".to_string();
494-
let upcase_command = "open \"{}\"".to_string();
495-
let multi_command = "open \"{}\"".to_string();
514+
let main_action = "echo \"{}\"".to_string();
515+
let shift_action = "open \"{}\"".to_string();
516+
let ctrl_action = "echo \"{}\"".to_string();
517+
let alt_action = "echo \"{}\"".to_string();
518+
let multi_action = "open \"{}\"".to_string();
496519
let mut swapper = Swapper::new(
497520
Box::new(&mut executor),
498521
"".to_string(),
499-
user_command,
500-
upcase_command,
501-
multi_command,
522+
main_action,
523+
shift_action,
524+
ctrl_action,
525+
alt_action,
526+
multi_action,
502527
false,
503528
);
504529

@@ -539,21 +564,33 @@ fn app_args<'a>() -> clap::ArgMatches<'a> {
539564
.default_value(""),
540565
)
541566
.arg(
542-
Arg::with_name("command")
567+
Arg::with_name("main-action")
543568
.help("Command to execute after choose a hint")
544-
.long("command")
569+
.long("main-action")
545570
.default_value("tmux set-buffer -- \"{}\" && tmux display-message \"Copied {}\""),
546571
)
547572
.arg(
548-
Arg::with_name("upcase_command")
549-
.help("Command to execute after choose a hint, in upcase")
550-
.long("upcase-command")
573+
Arg::with_name("shift-action")
574+
.help("Command to execute after choose a hint with SHIFT key pressed (Upcase)")
575+
.long("shift-action")
551576
.default_value("tmux set-buffer -- \"{}\" && tmux paste-buffer && tmux display-message \"Copied {}\""),
552577
)
553578
.arg(
554-
Arg::with_name("multi_command")
579+
Arg::with_name("ctrl-action")
580+
.help("Command to execute after choose a hint with CTRL key pressed")
581+
.long("ctrl-action")
582+
.default_value("tmux display-message \"No CTRL action configured! Hint: {}\""),
583+
)
584+
.arg(
585+
Arg::with_name("alt-action")
586+
.help("Command to execute after choose a hint with ALT key pressed")
587+
.long("alt-action")
588+
.default_value("tmux display-message \"No ALT action configured! Hint: {}\" && echo \"FOO $MODIFIER ~ $HINT BAR\" > /tmp/tx.txt"),
589+
)
590+
.arg(
591+
Arg::with_name("multi-action")
555592
.help("Command to execute after choose multiple hints")
556-
.long("multi-command")
593+
.long("multi-action")
557594
.default_value("tmux set-buffer -- \"{}\" && tmux paste-buffer && tmux display-message \"Multi copied {}\""),
558595
)
559596
.arg(
@@ -568,9 +605,11 @@ fn app_args<'a>() -> clap::ArgMatches<'a> {
568605
fn main() -> std::io::Result<()> {
569606
let args = app_args();
570607
let dir = args.value_of("dir").unwrap();
571-
let command = args.value_of("command").unwrap();
572-
let upcase_command = args.value_of("upcase_command").unwrap();
573-
let multi_command = args.value_of("multi_command").unwrap();
608+
let main_action = args.value_of("main-action").unwrap();
609+
let shift_action = args.value_of("shift-action").unwrap();
610+
let ctrl_action = args.value_of("ctrl-action").unwrap();
611+
let alt_action = args.value_of("alt-action").unwrap();
612+
let multi_action = args.value_of("multi-action").unwrap();
574613
let osc52 = args.is_present("osc52");
575614

576615
if dir.is_empty() {
@@ -581,9 +620,11 @@ fn main() -> std::io::Result<()> {
581620
let mut swapper = Swapper::new(
582621
Box::new(&mut executor),
583622
dir.to_string(),
584-
command.to_string(),
585-
upcase_command.to_string(),
586-
multi_command.to_string(),
623+
main_action.to_string(),
624+
shift_action.to_string(),
625+
ctrl_action.to_string(),
626+
alt_action.to_string(),
627+
multi_action.to_string(),
587628
osc52,
588629
);
589630

0 commit comments

Comments
 (0)