aboutsummaryrefslogtreecommitdiff
path: root/src/app.rs
diff options
context:
space:
mode:
authorpablu <me@pablu.de>2026-05-07 14:34:40 +0200
committerpablu <me@pablu.de>2026-05-07 14:34:40 +0200
commit673853a8857c06a1b3e38122818e4eb0a3a77bb6 (patch)
treef0ba92453891d55af50faff2552fc6e8b9e39436 /src/app.rs
parentcc8e924ce828d237d7cdd245b794c532a4ee80d8 (diff)
searching list instead of only one
Diffstat (limited to 'src/app.rs')
-rw-r--r--src/app.rs77
1 files changed, 70 insertions, 7 deletions
diff --git a/src/app.rs b/src/app.rs
index 2e9856a..f2b4363 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -11,6 +11,9 @@ pub struct App<'a> {
pub showing_search_window: bool,
pub textarea: TextArea<'a>,
pub search_found: Option<bool>,
+ search_indexes: Option<Vec<usize>>,
+ pub search_indexes_len: Option<usize>,
+ pub current_search_index: Option<usize>,
}
#[allow(dead_code)]
@@ -22,6 +25,9 @@ impl<'a> App<'a> {
showing_search_window: false,
textarea: TextArea::default(),
search_found: None,
+ search_indexes: None,
+ current_search_index: None,
+ search_indexes_len: None,
}
}
@@ -36,17 +42,74 @@ impl<'a> App<'a> {
}
pub fn search(&mut self) -> bool {
- let index = self.portage.installed_packages.iter().position(|s| {
- s.name
- .to_lowercase()
- .contains(&self.textarea.lines()[0].to_lowercase())
- });
+ let indexes: Vec<usize> = self
+ .portage
+ .installed_packages
+ .iter()
+ .enumerate()
+ .filter(|(_, s)| {
+ s.name
+ .to_lowercase()
+ .contains(&self.textarea.lines()[0].to_lowercase())
+ })
+ .map(|(i, _)| i)
+ .collect();
- if let Some(index) = index {
- self.list_state.select(Some(index));
+ if let Some(index) = indexes.first() {
+ self.list_state.select(Some(index.clone()));
+ self.search_indexes_len = Some(indexes.len());
+ self.search_indexes = Some(indexes);
+ self.current_search_index = Some(0);
return true;
}
+ self.search_indexes = None;
return false;
}
+
+ pub fn next_search(&mut self) {
+ let mut current_index = match self.current_search_index {
+ Some(i) => i,
+ None => return,
+ };
+
+ let search_indexes = match &self.search_indexes {
+ Some(i) => i,
+ None => return,
+ };
+
+ if current_index + 1 >= search_indexes.len() {
+ current_index = 0;
+ } else {
+ current_index += 1;
+ }
+
+ let list_index = search_indexes[current_index];
+
+ self.list_state.select(Some(list_index));
+ self.current_search_index = Some(current_index)
+ }
+
+ pub fn prev_search(&mut self) {
+ let mut current_index = match self.current_search_index {
+ Some(i) => i,
+ None => return,
+ };
+
+ let search_indexes = match &self.search_indexes {
+ Some(i) => i,
+ None => return,
+ };
+
+ if current_index == 0 {
+ current_index = search_indexes.len() - 1;
+ } else {
+ current_index -= 1;
+ }
+
+ let list_index = search_indexes[current_index];
+
+ self.list_state.select(Some(list_index));
+ self.current_search_index = Some(current_index)
+ }
}