1
1
extern crate clap;
2
2
3
+ mod view;
4
+
3
5
use self :: clap:: { App , Arg } ;
4
6
use clap:: crate_version;
5
7
use regex:: Regex ;
@@ -58,9 +60,11 @@ fn dbg(msg: &str) {
58
60
pub struct Swapper < ' a > {
59
61
executor : Box < & ' a mut dyn Executor > ,
60
62
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 ,
64
68
osc52 : bool ,
65
69
active_pane_id : Option < String > ,
66
70
active_pane_height : Option < i32 > ,
@@ -75,9 +79,11 @@ impl<'a> Swapper<'a> {
75
79
fn new (
76
80
executor : Box < & ' a mut dyn Executor > ,
77
81
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 ,
81
87
osc52 : bool ,
82
88
) -> Swapper {
83
89
let since_the_epoch = SystemTime :: now ( )
@@ -88,9 +94,11 @@ impl<'a> Swapper<'a> {
88
94
Swapper {
89
95
executor,
90
96
dir,
91
- command,
92
- upcase_command,
93
- multi_command,
97
+ main_action,
98
+ shift_action,
99
+ ctrl_action,
100
+ alt_action,
101
+ multi_action,
94
102
osc52,
95
103
active_pane_id : None ,
96
104
active_pane_height : None ,
@@ -215,7 +223,7 @@ impl<'a> Swapper<'a> {
215
223
} ;
216
224
217
225
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}" ,
219
227
active_pane_id = active_pane_id,
220
228
scroll_params = scroll_params,
221
229
height = self . active_pane_height. unwrap_or( i32 :: MAX ) ,
@@ -320,7 +328,8 @@ impl<'a> Swapper<'a> {
320
328
. collect :: < Vec < & str > > ( )
321
329
. join ( " " ) ;
322
330
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 ( ) ) ;
324
333
325
334
return ;
326
335
}
@@ -330,7 +339,7 @@ impl<'a> Swapper<'a> {
330
339
331
340
let mut splitter = item. splitn ( 2 , ':' ) ;
332
341
333
- if let Some ( upcase ) = splitter. next ( ) {
342
+ if let Some ( modkey ) = splitter. next ( ) {
334
343
if let Some ( text) = splitter. next ( ) {
335
344
if self . osc52 {
336
345
let base64_text = base64:: encode ( text. as_bytes ( ) ) ;
@@ -360,10 +369,17 @@ impl<'a> Swapper<'a> {
360
369
std:: io:: stdout ( ) . flush ( ) . unwrap ( ) ;
361
370
}
362
371
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 ( ) ,
367
383
} ;
368
384
369
385
// The command we run has two arguments:
@@ -389,19 +405,20 @@ impl<'a> Swapper<'a> {
389
405
// Ideally user commands would just use "${THUMB}" to begin with rather than having any
390
406
// sort of ad-hoc string splicing here at all, and then they could specify the quoting they
391
407
// 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) ;
393
409
}
394
410
}
395
411
}
396
412
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 ) {
398
414
let final_command = str:: replace ( execute_command, "{}" , "${THUMB}" ) ;
399
415
let retrieve_command = vec ! [
400
416
"bash" ,
401
417
"-c" ,
402
- "THUMB=\" $1\" ; eval \" $2 \" " ,
418
+ "THUMB=\" $1\" ; MODIFIER= \" $2 \" ; eval \" $3 \" " ,
403
419
"--" ,
404
420
text,
421
+ modkey,
405
422
final_command. as_str( ) ,
406
423
] ;
407
424
@@ -450,6 +467,8 @@ mod tests {
450
467
"" . to_string ( ) ,
451
468
"" . to_string ( ) ,
452
469
"" . to_string ( ) ,
470
+ "" . to_string ( ) ,
471
+ "" . to_string ( ) ,
453
472
false ,
454
473
) ;
455
474
@@ -473,6 +492,8 @@ mod tests {
473
492
"" . to_string ( ) ,
474
493
"" . to_string ( ) ,
475
494
"" . to_string ( ) ,
495
+ "" . to_string ( ) ,
496
+ "" . to_string ( ) ,
476
497
false ,
477
498
) ;
478
499
@@ -490,15 +511,19 @@ mod tests {
490
511
let last_command_outputs = vec ! [ "Blah blah blah, the ignored user script output" . to_string( ) ] ;
491
512
let mut executor = TestShell :: new ( last_command_outputs) ;
492
513
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 ( ) ;
496
519
let mut swapper = Swapper :: new (
497
520
Box :: new ( & mut executor) ,
498
521
"" . 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,
502
527
false ,
503
528
) ;
504
529
@@ -539,21 +564,33 @@ fn app_args<'a>() -> clap::ArgMatches<'a> {
539
564
. default_value ( "" ) ,
540
565
)
541
566
. arg (
542
- Arg :: with_name ( "command " )
567
+ Arg :: with_name ( "main-action " )
543
568
. help ( "Command to execute after choose a hint" )
544
- . long ( "command " )
569
+ . long ( "main-action " )
545
570
. default_value ( "tmux set-buffer -- \" {}\" && tmux display-message \" Copied {}\" " ) ,
546
571
)
547
572
. 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 " )
551
576
. default_value ( "tmux set-buffer -- \" {}\" && tmux paste-buffer && tmux display-message \" Copied {}\" " ) ,
552
577
)
553
578
. 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" )
555
592
. help ( "Command to execute after choose multiple hints" )
556
- . long ( "multi-command " )
593
+ . long ( "multi-action " )
557
594
. default_value ( "tmux set-buffer -- \" {}\" && tmux paste-buffer && tmux display-message \" Multi copied {}\" " ) ,
558
595
)
559
596
. arg (
@@ -568,9 +605,11 @@ fn app_args<'a>() -> clap::ArgMatches<'a> {
568
605
fn main ( ) -> std:: io:: Result < ( ) > {
569
606
let args = app_args ( ) ;
570
607
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 ( ) ;
574
613
let osc52 = args. is_present ( "osc52" ) ;
575
614
576
615
if dir. is_empty ( ) {
@@ -581,9 +620,11 @@ fn main() -> std::io::Result<()> {
581
620
let mut swapper = Swapper :: new (
582
621
Box :: new ( & mut executor) ,
583
622
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 ( ) ,
587
628
osc52,
588
629
) ;
589
630
0 commit comments