added search functionality

This commit is contained in:
2026-03-17 09:24:58 +01:00
parent 7b2217886c
commit 0c217c5129
6 changed files with 114 additions and 2 deletions

View File

@@ -7,6 +7,8 @@ pub enum Mode {
Normal,
/// Active text entry mode for modifying values.
Insert,
/// Active search mode for filtering keys.
Search,
}
/// The core application state, holding all configuration variables and UI status.
@@ -23,6 +25,8 @@ pub struct App {
pub status_message: Option<String>,
/// The active text input buffer for the selected variable.
pub input: Input,
/// The current search query for filtering keys.
pub search_query: String,
}
impl App {
@@ -36,9 +40,24 @@ impl App {
running: true,
status_message: None,
input: Input::new(initial_input),
search_query: String::new(),
}
}
/// Returns the indices of variables that match the search query.
pub fn matching_indices(&self) -> Vec<usize> {
if self.search_query.is_empty() {
return Vec::new();
}
let query = self.search_query.to_lowercase();
self.vars
.iter()
.enumerate()
.filter(|(_, v)| v.key.to_lowercase().contains(&query))
.map(|(i, _)| i)
.collect()
}
/// Moves the selection to the next variable in the list, wrapping around if necessary.
pub fn next(&mut self) {
if !self.vars.is_empty() {
@@ -59,6 +78,43 @@ impl App {
}
}
/// Jumps to the next variable that matches the search query.
pub fn jump_next_match(&mut self) {
let indices = self.matching_indices();
if indices.is_empty() {
return;
}
let next_match = indices
.iter()
.find(|&&i| i > self.selected)
.or_else(|| indices.first());
if let Some(&index) = next_match {
self.selected = index;
self.sync_input_with_selected();
}
}
/// Jumps to the previous variable that matches the search query.
pub fn jump_previous_match(&mut self) {
let indices = self.matching_indices();
if indices.is_empty() {
return;
}
let prev_match = indices
.iter()
.rev()
.find(|&&i| i < self.selected)
.or_else(|| indices.last());
if let Some(&index) = prev_match {
self.selected = index;
self.sync_input_with_selected();
}
}
/// Updates the input buffer to reflect the value of the currently selected variable.
pub fn sync_input_with_selected(&mut self) {
if let Some(var) = self.vars.get(self.selected) {