Skip to content

Commit d5c3929

Browse files
committed
✨ feat: filtering on query working
1 parent 02d0962 commit d5c3929

File tree

1 file changed

+142
-118
lines changed

1 file changed

+142
-118
lines changed

src/popups/conventional_commit.rs

Lines changed: 142 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ enum CommitType {
4949
CI,
5050
}
5151

52+
#[derive(Clone)]
5253
enum MoreInfoCommit {
5354
// 🎨
5455
CodeStyle,
@@ -371,16 +372,15 @@ pub struct ConventionalCommitPopup {
371372
query: Option<String>,
372373
selected_index: usize,
373374
options: Vec<CommitType>,
374-
query_results: Vec<CommitType>,
375+
query_results_type: Vec<CommitType>,
376+
query_results_more_info: Vec<MoreInfoCommit>,
375377
input: TextInputComponent,
376378
theme: SharedTheme,
377379
seleted_commit_type: Option<CommitType>,
378380
queue: Queue,
379381
}
380382

381383
impl ConventionalCommitPopup {
382-
///
383-
// pub fn new(env: &Environment) -> Self {
384384
pub fn new(env: &Environment) -> Self {
385385
let mut input =
386386
TextInputComponent::new(env, "", "Filter ", false)
@@ -391,7 +391,8 @@ impl ConventionalCommitPopup {
391391
selected_index: 0,
392392
input,
393393
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(),
395396
is_insert: false,
396397
query: None,
397398
is_visible: false,
@@ -404,103 +405,95 @@ impl ConventionalCommitPopup {
404405

405406
#[inline]
406407
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;
413410

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);
416413

417-
let list_height =
418-
height.saturating_sub(HEIGHT_BLOCK_MARGIN);
414+
let list_height = height.saturating_sub(HEIGHT_BLOCK_MARGIN);
419415

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();
423419

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()
444424
} 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)
453478
.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),
471483
)
472484
})
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+
});
491488

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+
);
504497
}
505498

506499
pub fn quick_shortcuts(&self) -> Vec<char> {
@@ -525,7 +518,7 @@ impl ConventionalCommitPopup {
525518
}
526519
});
527520

528-
self.query_results
521+
self.query_results_type
529522
.iter()
530523
.map(|commit_type| commit_type.to_string())
531524
.map(|s| {
@@ -548,8 +541,10 @@ impl ConventionalCommitPopup {
548541
_ => self.selected_index,
549542
};
550543

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+
);
553548

554549
self.selected_index = new_selection;
555550
}
@@ -562,25 +557,45 @@ impl ConventionalCommitPopup {
562557
if self
563558
.query
564559
.as_ref()
565-
.map_or(true, |q| q != self.input.get_text())
560+
.is_none_or(|q| q != self.input.get_text())
566561
{
567562
self.set_query(self.input.get_text().to_string());
568563
}
569564
}
570565

571566
fn set_query(&mut self, query: String) {
567+
let query = query.to_lowercase();
572568
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+
}
579593
}
580594

581595
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();
584599
self.queue.push(crate::queue::InternalEvent::OpenCommit);
585600
self.queue.push(
586601
crate::queue::InternalEvent::AddCommitMessage(format!(
@@ -589,19 +604,11 @@ impl ConventionalCommitPopup {
589604
)),
590605
);
591606
self.hide();
592-
self.selected_index = 0;
593-
self.seleted_commit_type = None;
594607
}
595608
}
596609

597610
impl DrawableComponent for ConventionalCommitPopup {
598611
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(())
605612
if self.is_visible {
606613
const MAX_SIZE: (u16, u16) = (50, 20);
607614

@@ -691,22 +698,29 @@ impl Component for ConventionalCommitPopup {
691698
if self.is_visible() {
692699
if let Event::Key(key) = event {
693700
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+
}
697706
} else if key_match(key, self.key_config.keys.enter) {
698707
if let Some(commit_type) =
699708
self.seleted_commit_type.clone()
700709
{
701710
self.validate_escape(commit_type);
702711
} else {
703712
let commit = self
704-
.query_results
713+
.query_results_type
705714
.get(self.selected_index)
706715
.cloned();
707716

708717
self.seleted_commit_type = commit.clone();
709718
self.selected_index = 0;
719+
self.is_insert = false;
720+
self.query = None;
721+
self.input.clear();
722+
723+
self.update_query();
710724

711725
if let Some(more_infos) =
712726
commit.as_ref().map(|c| c.more_info())
@@ -728,7 +742,7 @@ impl Component for ConventionalCommitPopup {
728742
self.move_selection(ScrollType::Up);
729743
} else {
730744
if self.is_insert {
731-
if self.input.event(event)?.is_consumed() {
745+
if self.input.event(&event)?.is_consumed() {
732746
self.update_query();
733747
}
734748
} else if key_match(
@@ -744,7 +758,8 @@ impl Component for ConventionalCommitPopup {
744758
.position(|ch| ch == c)
745759
{
746760
self.seleted_commit_type = Some(
747-
self.query_results[idx].clone(),
761+
self.query_results_type[idx]
762+
.clone(),
748763
);
749764
}
750765
}
@@ -764,10 +779,19 @@ impl Component for ConventionalCommitPopup {
764779

765780
fn hide(&mut self) {
766781
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();
767789
}
768790

769791
fn show(&mut self) -> Result<()> {
770792
self.is_visible = true;
793+
self.input.show()?;
794+
self.input.set_text(String::new());
771795
Ok(())
772796
}
773797
}

0 commit comments

Comments
 (0)