@@ -14,6 +14,7 @@ use strum::{Display, EnumIter, IntoEnumIterator};
1414use unicode_segmentation:: UnicodeSegmentation ;
1515
1616use crate :: components:: visibility_blocking;
17+ use crate :: queue:: Queue ;
1718use crate :: string_utils:: trim_length_left;
1819use crate :: ui:: style:: SharedTheme ;
1920use crate :: {
@@ -27,6 +28,7 @@ use crate::{
2728} ;
2829
2930#[ derive( EnumIter , Display , Clone ) ]
31+ #[ strum( serialize_all = "lowercase" ) ]
3032enum CommitType {
3133 Refactor ,
3234 #[ strum( to_string = "feat" ) ]
@@ -372,6 +374,8 @@ pub struct ConventionalCommitPopup {
372374 query_results : Vec < CommitType > ,
373375 input : TextInputComponent ,
374376 theme : SharedTheme ,
377+ seleted_commit_type : Option < CommitType > ,
378+ queue : Queue ,
375379}
376380
377381impl ConventionalCommitPopup {
@@ -392,7 +396,9 @@ impl ConventionalCommitPopup {
392396 query : None ,
393397 is_visible : false ,
394398 key_config : env. key_config . clone ( ) ,
399+ seleted_commit_type : None ,
395400 theme : env. theme . clone ( ) ,
401+ queue : env. queue . clone ( ) ,
396402 }
397403 }
398404
@@ -411,29 +417,61 @@ impl ConventionalCommitPopup {
411417 let list_height =
412418 height. saturating_sub ( HEIGHT_BLOCK_MARGIN ) ;
413419
420+ let a = self . query_results [ 0 ] . more_info ( ) [ 0 ] . strings ( ) ;
421+ assert ! ( a. 0 != "" ) ;
422+
414423 let scroll_skip =
415424 self . selected_index . saturating_sub ( list_height) ;
416425 let quick_shortcuts = self . quick_shortcuts ( ) ;
417426
418- let items = self
419- . query_results
420- . iter ( )
421- . enumerate ( )
422- . take ( height)
423- . map ( |( idx, commit_type) | {
424- let selected = self . selected_index == idx;
425- let commit_type_string = commit_type. to_string ( ) ;
426- let text = trim_length_left (
427- commit_type_string. as_str ( ) ,
428- width - 4 , // ` [k]`
429- ) ;
430- let text = format ! (
431- "{:w$} [{}]" ,
432- text,
433- quick_shortcuts[ idx] ,
434- w = width,
435- ) ;
436-
427+ let iter_over = if let Some ( commit_type) =
428+ & self . seleted_commit_type
429+ {
430+ commit_type
431+ . more_info ( )
432+ . iter ( )
433+ . enumerate ( )
434+ . take ( height)
435+ . map ( |( idx, more_info) | {
436+ let ( emoji, _, long_name) =
437+ more_info. strings ( ) ;
438+ let text_string =
439+ format ! ( "{emoji} {long_name}" ) ;
440+ let text = trim_length_left (
441+ & text_string,
442+ width - 4 , // ` [k]`
443+ ) ;
444+ ( self . selected_index == idx, text. to_owned ( ) )
445+ } )
446+ . collect_vec ( )
447+ } else {
448+ self . query_results
449+ . iter ( )
450+ . enumerate ( )
451+ . take ( height)
452+ . map ( |( idx, commit_type) | {
453+ let commit_type_string =
454+ commit_type. to_string ( ) ;
455+ let text = trim_length_left (
456+ commit_type_string. as_str ( ) ,
457+ width - 4 , // ` [k]`
458+ ) ;
459+ //FIXME: not working
460+ (
461+ self . selected_index == idx,
462+ format ! (
463+ "{:w$} [{}]" ,
464+ text,
465+ quick_shortcuts[ idx] ,
466+ w = width,
467+ ) ,
468+ )
469+ } )
470+ . collect_vec ( )
471+ } ;
472+
473+ let items =
474+ iter_over. into_iter ( ) . map ( |( selected, text) | {
437475 Line :: from (
438476 text. graphemes ( true )
439477 . enumerate ( )
@@ -497,18 +535,10 @@ impl ConventionalCommitPopup {
497535 _ => self . selected_index ,
498536 } ;
499537
500- // println!("{} {}", self.query, self.input);
501-
502- let new_selection = new_selection. clamp ( 0 , todo ! ( ) ) ;
503- // .clamp(0, self.filtered.len().saturating_sub(1));
504- // .clamp(0, self.filtered.len().saturating_sub(1));
538+ let new_selection = new_selection
539+ . clamp ( 0 , self . query_results . len ( ) . saturating_sub ( 1 ) ) ;
505540
506- // if new_selection != self.selection {
507541 self . selected_index = new_selection;
508- // return true;
509- // }
510- //
511- // false
512542 }
513543
514544 pub fn any_work_pending ( & self ) -> bool {
@@ -635,7 +665,27 @@ impl Component for ConventionalCommitPopup {
635665 if key_match ( key, self . key_config . keys . exit_popup )
636666 || key_match ( key, self . key_config . keys . enter )
637667 {
638- self . hide ( ) ;
668+ if let Some ( commit_type) =
669+ & self . seleted_commit_type
670+ {
671+ let ( emoji, short_msg, _) = commit_type. more_info ( ) [ self . selected_index ] . strings ( ) ;
672+ self . queue . push (
673+ crate :: queue:: InternalEvent :: OpenCommit ,
674+ ) ;
675+ self . queue . push (
676+ crate :: queue:: InternalEvent :: AddCommitMessage (
677+
678+ format ! ( "{emoji} {commit_type}: {short_msg}" ) ,
679+ ) ,
680+ ) ;
681+ self . hide ( ) ;
682+ } else {
683+ self . seleted_commit_type = self
684+ . query_results
685+ . get ( self . selected_index )
686+ . cloned ( ) ;
687+ self . selected_index = 0 ;
688+ }
639689 } else if key_match ( key, self . key_config . keys . insert )
640690 {
641691 self . is_insert = true ;
@@ -656,10 +706,6 @@ impl Component for ConventionalCommitPopup {
656706 }
657707 }
658708
659- // if self.find_text.event(event)?.is_consumed() {
660- // self.update_query();
661- // }
662-
663709 return Ok ( EventState :: Consumed ) ;
664710 }
665711
0 commit comments