@@ -49,6 +49,7 @@ enum CommitType {
49
49
CI ,
50
50
}
51
51
52
+ #[ derive( Clone ) ]
52
53
enum MoreInfoCommit {
53
54
// 🎨
54
55
CodeStyle ,
@@ -371,16 +372,15 @@ pub struct ConventionalCommitPopup {
371
372
query : Option < String > ,
372
373
selected_index : usize ,
373
374
options : Vec < CommitType > ,
374
- query_results : Vec < CommitType > ,
375
+ query_results_type : Vec < CommitType > ,
376
+ query_results_more_info : Vec < MoreInfoCommit > ,
375
377
input : TextInputComponent ,
376
378
theme : SharedTheme ,
377
379
seleted_commit_type : Option < CommitType > ,
378
380
queue : Queue ,
379
381
}
380
382
381
383
impl ConventionalCommitPopup {
382
- ///
383
- // pub fn new(env: &Environment) -> Self {
384
384
pub fn new ( env : & Environment ) -> Self {
385
385
let mut input =
386
386
TextInputComponent :: new ( env, "" , "Filter " , false )
@@ -391,7 +391,8 @@ impl ConventionalCommitPopup {
391
391
selected_index : 0 ,
392
392
input,
393
393
options : CommitType :: iter ( ) . collect_vec ( ) ,
394
- query_results : CommitType :: iter ( ) . collect_vec ( ) ,
394
+ query_results_type : CommitType :: iter ( ) . collect_vec ( ) ,
395
+ query_results_more_info : Vec :: new ( ) ,
395
396
is_insert : false ,
396
397
query : None ,
397
398
is_visible : false ,
@@ -404,103 +405,95 @@ impl ConventionalCommitPopup {
404
405
405
406
#[ inline]
406
407
fn draw_matches_list ( & self , f : & mut Frame , mut area : Rect ) {
407
- {
408
- // Block has two lines up and down which need to be considered
409
- const HEIGHT_BLOCK_MARGIN : usize = 2 ;
410
-
411
- let title =
412
- format ! ( "Results: {}" , self . query_results. len( ) ) ;
408
+ // Block has two lines up and down which need to be considered
409
+ const HEIGHT_BLOCK_MARGIN : usize = 2 ;
413
410
414
- let height = usize:: from ( area. height ) ;
415
- let width = usize:: from ( area. width ) ;
411
+ let height = usize:: from ( area. height ) ;
412
+ let width = usize:: from ( area. width ) ;
416
413
417
- let list_height =
418
- height. saturating_sub ( HEIGHT_BLOCK_MARGIN ) ;
414
+ let list_height = height. saturating_sub ( HEIGHT_BLOCK_MARGIN ) ;
419
415
420
- let scroll_skip =
421
- self . selected_index . saturating_sub ( list_height) ;
422
- let quick_shortcuts = self . quick_shortcuts ( ) ;
416
+ let scroll_skip =
417
+ self . selected_index . saturating_sub ( list_height) ;
418
+ let quick_shortcuts = self . quick_shortcuts ( ) ;
423
419
424
- let iter_over = if let Some ( commit_type) =
425
- & self . seleted_commit_type
426
- {
427
- commit_type
428
- . more_info ( )
429
- . iter ( )
430
- . enumerate ( )
431
- . take ( height)
432
- . map ( |( idx, more_info) | {
433
- let ( emoji, _, long_name) =
434
- more_info. strings ( ) ;
435
- let text_string =
436
- format ! ( "{emoji} {long_name}" ) ;
437
- let text = trim_length_left (
438
- & text_string,
439
- width - 4 , // ` [k]`
440
- ) ;
441
- ( self . selected_index == idx, text. to_owned ( ) )
442
- } )
443
- . collect_vec ( )
420
+ let title = format ! (
421
+ "Results: {}" ,
422
+ if self . seleted_commit_type. is_some( ) {
423
+ self . query_results_more_info. len( )
444
424
} else {
445
- let max_len = self
446
- . query_results
447
- . iter ( )
448
- . map ( |s| s. to_string ( ) . len ( ) )
449
- . max ( ) ;
450
-
451
- self . query_results
452
- . iter ( )
425
+ self . query_results_type. len( )
426
+ }
427
+ ) ;
428
+
429
+ let iter_over = if let Some ( commit_type) =
430
+ & self . seleted_commit_type
431
+ {
432
+ self . query_results_more_info
433
+ . iter ( )
434
+ . enumerate ( )
435
+ . take ( height)
436
+ . map ( |( idx, more_info) | {
437
+ let ( emoji, _, long_name) = more_info. strings ( ) ;
438
+ let text_string = format ! ( "{emoji} {long_name}" ) ;
439
+ let text =
440
+ trim_length_left ( & text_string, width - 4 ) ;
441
+ ( self . selected_index == idx, text. to_owned ( ) )
442
+ } )
443
+ . collect_vec ( )
444
+ } else {
445
+ let max_len = self
446
+ . query_results_type
447
+ . iter ( )
448
+ . map ( |s| s. to_string ( ) . len ( ) )
449
+ . max ( ) ;
450
+
451
+ self . query_results_type
452
+ . iter ( )
453
+ . enumerate ( )
454
+ . take ( height)
455
+ . map ( |( idx, commit_type) | {
456
+ let commit_type_string = commit_type. to_string ( ) ;
457
+ let text = trim_length_left (
458
+ commit_type_string. as_str ( ) ,
459
+ width - 4 , // ` [k]`
460
+ ) ;
461
+
462
+ (
463
+ self . selected_index == idx,
464
+ format ! (
465
+ "{:w$} [{}]" ,
466
+ text,
467
+ quick_shortcuts[ idx] ,
468
+ w = max_len. unwrap_or_default( ) ,
469
+ ) ,
470
+ )
471
+ } )
472
+ . collect_vec ( )
473
+ } ;
474
+
475
+ let items = iter_over. into_iter ( ) . map ( |( selected, text) | {
476
+ Line :: from (
477
+ text. graphemes ( true )
453
478
. enumerate ( )
454
- . take ( height)
455
- . map ( |( idx, commit_type) | {
456
- let commit_type_string =
457
- commit_type. to_string ( ) ;
458
- let text = trim_length_left (
459
- commit_type_string. as_str ( ) ,
460
- width - 4 , // ` [k]`
461
- ) ;
462
-
463
- (
464
- self . selected_index == idx,
465
- format ! (
466
- "{:w$} [{}]" ,
467
- text,
468
- quick_shortcuts[ idx] ,
469
- w = max_len. unwrap_or_default( ) ,
470
- ) ,
479
+ . map ( |( c_idx, c) | {
480
+ Span :: styled (
481
+ Cow :: from ( c. to_string ( ) ) ,
482
+ self . theme . text ( selected, selected) ,
471
483
)
472
484
} )
473
- . collect_vec ( )
474
- } ;
475
-
476
- let items =
477
- iter_over. into_iter ( ) . map ( |( selected, text) | {
478
- Line :: from (
479
- text. graphemes ( true )
480
- . enumerate ( )
481
- . map ( |( c_idx, c) | {
482
- Span :: styled (
483
- Cow :: from ( c. to_string ( ) ) ,
484
- self . theme
485
- . text ( selected, selected) ,
486
- )
487
- } )
488
- . collect :: < Vec < _ > > ( ) ,
489
- )
490
- } ) ;
485
+ . collect :: < Vec < _ > > ( ) ,
486
+ )
487
+ } ) ;
491
488
492
- ui:: draw_list_block (
493
- f,
494
- area,
495
- Block :: default ( )
496
- . title ( Span :: styled (
497
- title,
498
- self . theme . title ( true ) ,
499
- ) )
500
- . borders ( Borders :: TOP ) ,
501
- items,
502
- ) ;
503
- }
489
+ ui:: draw_list_block (
490
+ f,
491
+ area,
492
+ Block :: default ( )
493
+ . title ( Span :: styled ( title, self . theme . title ( true ) ) )
494
+ . borders ( Borders :: TOP ) ,
495
+ items,
496
+ ) ;
504
497
}
505
498
506
499
pub fn quick_shortcuts ( & self ) -> Vec < char > {
@@ -525,7 +518,7 @@ impl ConventionalCommitPopup {
525
518
}
526
519
} ) ;
527
520
528
- self . query_results
521
+ self . query_results_type
529
522
. iter ( )
530
523
. map ( |commit_type| commit_type. to_string ( ) )
531
524
. map ( |s| {
@@ -548,8 +541,10 @@ impl ConventionalCommitPopup {
548
541
_ => self . selected_index ,
549
542
} ;
550
543
551
- let new_selection = new_selection
552
- . clamp ( 0 , self . query_results . len ( ) . saturating_sub ( 1 ) ) ;
544
+ let new_selection = new_selection. clamp (
545
+ 0 ,
546
+ self . query_results_type . len ( ) . saturating_sub ( 1 ) ,
547
+ ) ;
553
548
554
549
self . selected_index = new_selection;
555
550
}
@@ -562,25 +557,45 @@ impl ConventionalCommitPopup {
562
557
if self
563
558
. query
564
559
. as_ref ( )
565
- . map_or ( true , |q| q != self . input . get_text ( ) )
560
+ . is_none_or ( |q| q != self . input . get_text ( ) )
566
561
{
567
562
self . set_query ( self . input . get_text ( ) . to_string ( ) ) ;
568
563
}
569
564
}
570
565
571
566
fn set_query ( & mut self , query : String ) {
567
+ let query = query. to_lowercase ( ) ;
572
568
self . query = Some ( query. clone ( ) ) ;
573
- self . query_results = self
574
- . options
575
- . iter ( )
576
- . filter ( |option| option. to_string ( ) == query)
577
- . cloned ( )
578
- . collect_vec ( ) ;
569
+
570
+ if let Some ( commit_type) = & self . seleted_commit_type {
571
+ self . query_results_more_info = commit_type
572
+ . more_info ( )
573
+ . iter ( )
574
+ . filter ( |more_info_commit| {
575
+ more_info_commit
576
+ . strings ( )
577
+ . 2
578
+ . to_lowercase ( )
579
+ . contains ( & query)
580
+ } )
581
+ . cloned ( )
582
+ . collect_vec ( ) ;
583
+ } else {
584
+ self . query_results_type = self
585
+ . options
586
+ . iter ( )
587
+ . filter ( |option| {
588
+ option. to_string ( ) . to_lowercase ( ) . contains ( & query)
589
+ } )
590
+ . cloned ( )
591
+ . collect_vec ( ) ;
592
+ }
579
593
}
580
594
581
595
fn validate_escape ( & mut self , commit_type : CommitType ) {
582
- let ( emoji, short_msg, _) =
583
- commit_type. more_info ( ) [ self . selected_index ] . strings ( ) ;
596
+ let ( emoji, short_msg, _) = self . query_results_more_info
597
+ [ self . selected_index ]
598
+ . strings ( ) ;
584
599
self . queue . push ( crate :: queue:: InternalEvent :: OpenCommit ) ;
585
600
self . queue . push (
586
601
crate :: queue:: InternalEvent :: AddCommitMessage ( format ! (
@@ -589,19 +604,11 @@ impl ConventionalCommitPopup {
589
604
) ) ,
590
605
) ;
591
606
self . hide ( ) ;
592
- self . selected_index = 0 ;
593
- self . seleted_commit_type = None ;
594
607
}
595
608
}
596
609
597
610
impl DrawableComponent for ConventionalCommitPopup {
598
611
fn draw ( & self , f : & mut Frame , area : Rect ) -> Result < ( ) > {
599
- // if self.is_visible() {
600
- // self.input.draw(f, rect)?;
601
- // self.draw_warnings(f);
602
- // }
603
- //
604
- // Ok(())
605
612
if self . is_visible {
606
613
const MAX_SIZE : ( u16 , u16 ) = ( 50 , 20 ) ;
607
614
@@ -691,22 +698,29 @@ impl Component for ConventionalCommitPopup {
691
698
if self . is_visible ( ) {
692
699
if let Event :: Key ( key) = event {
693
700
if key_match ( key, self . key_config . keys . exit_popup ) {
694
- self . hide ( ) ;
695
- self . selected_index = 0 ;
696
- self . seleted_commit_type = None ;
701
+ if self . is_insert {
702
+ self . is_insert = false ;
703
+ } else {
704
+ self . hide ( ) ;
705
+ }
697
706
} else if key_match ( key, self . key_config . keys . enter ) {
698
707
if let Some ( commit_type) =
699
708
self . seleted_commit_type . clone ( )
700
709
{
701
710
self . validate_escape ( commit_type) ;
702
711
} else {
703
712
let commit = self
704
- . query_results
713
+ . query_results_type
705
714
. get ( self . selected_index )
706
715
. cloned ( ) ;
707
716
708
717
self . seleted_commit_type = commit. clone ( ) ;
709
718
self . selected_index = 0 ;
719
+ self . is_insert = false ;
720
+ self . query = None ;
721
+ self . input . clear ( ) ;
722
+
723
+ self . update_query ( ) ;
710
724
711
725
if let Some ( more_infos) =
712
726
commit. as_ref ( ) . map ( |c| c. more_info ( ) )
@@ -728,7 +742,7 @@ impl Component for ConventionalCommitPopup {
728
742
self . move_selection ( ScrollType :: Up ) ;
729
743
} else {
730
744
if self . is_insert {
731
- if self . input . event ( event) ?. is_consumed ( ) {
745
+ if self . input . event ( & event) ?. is_consumed ( ) {
732
746
self . update_query ( ) ;
733
747
}
734
748
} else if key_match (
@@ -744,7 +758,8 @@ impl Component for ConventionalCommitPopup {
744
758
. position ( |ch| ch == c)
745
759
{
746
760
self . seleted_commit_type = Some (
747
- self . query_results [ idx] . clone ( ) ,
761
+ self . query_results_type [ idx]
762
+ . clone ( ) ,
748
763
) ;
749
764
}
750
765
}
@@ -764,10 +779,19 @@ impl Component for ConventionalCommitPopup {
764
779
765
780
fn hide ( & mut self ) {
766
781
self . is_visible = false ;
782
+ self . is_insert = false ;
783
+ self . selected_index = 0 ;
784
+ self . seleted_commit_type = None ;
785
+ self . query = None ;
786
+ self . query_results_type = CommitType :: iter ( ) . collect_vec ( ) ;
787
+ self . query_results_more_info = Vec :: new ( ) ;
788
+ self . input . clear ( ) ;
767
789
}
768
790
769
791
fn show ( & mut self ) -> Result < ( ) > {
770
792
self . is_visible = true ;
793
+ self . input . show ( ) ?;
794
+ self . input . set_text ( String :: new ( ) ) ;
771
795
Ok ( ( ) )
772
796
}
773
797
}
0 commit comments