Merged with #82, still need testing

This commit is contained in:
afonsofrancof 2024-07-30 21:52:12 +01:00
parent b3dedea471
commit 0ca4f09dbc
No known key found for this signature in database
2 changed files with 59 additions and 27 deletions

View File

@ -9,6 +9,7 @@ use ratatui::{
Frame, Frame,
}; };
#[derive(Clone)]
struct ListNode { struct ListNode {
name: &'static str, name: &'static str,
command: Command, command: Command,
@ -161,7 +162,8 @@ impl CustomList {
self.filtered_items self.filtered_items
.iter() .iter()
.map(|node| { .map(|node| {
Line::from(format!("{} {}", state.theme.cmd_icon, node.value().name)).style(state.theme.cmd_color) Line::from(format!("{} {}", state.theme.cmd_icon, node.name))
.style(state.theme.cmd_color)
}) })
.collect() .collect()
}; };
@ -313,6 +315,7 @@ impl CustomList {
} else { } else {
self.filtered_items.len() self.filtered_items.len()
}; };
let curr_selection = self.list_state.selected().unwrap(); let curr_selection = self.list_state.selected().unwrap();
if self.at_root() { if self.at_root() {
self.list_state self.list_state
@ -378,12 +381,15 @@ impl CustomList {
/// ///
/// Returns `Some(command)` when command is selected, othervise we returns `None` /// Returns `Some(command)` when command is selected, othervise we returns `None`
fn handle_enter(&mut self) -> Option<Command> { fn handle_enter(&mut self) -> Option<Command> {
// Get the current node (current directory) // Get the selected index
let selected = self.list_state.selected().unwrap();
if self.filter_query.is_empty() {
// No filter query, use the regular tree navigation
let curr = self let curr = self
.inner_tree .inner_tree
.get(*self.visit_stack.last().unwrap()) .get(*self.visit_stack.last().unwrap())
.unwrap(); .unwrap();
let selected = self.list_state.selected().unwrap();
// if we are not at the root, and the first element is selected, // if we are not at the root, and the first element is selected,
// we can be sure it's '..', so we go up the directory // we can be sure it's '..', so we go up the directory
@ -409,6 +415,12 @@ impl CustomList {
} }
} }
} }
} else {
// Filter query is active, use the filtered items
if let Some(filtered_node) = self.filtered_items.get(selected) {
return Some(filtered_node.command.clone());
}
}
None None
} }

View File

@ -117,7 +117,7 @@ fn run<B: Backend>(terminal: &mut Terminal<B>, state: &AppState) -> io::Result<(
//Render the search bar (First chunk of the screen) //Render the search bar (First chunk of the screen)
frame.render_widget(search_bar, chunks[0]); frame.render_widget(search_bar, chunks[0]);
//Render the command list (Second chunk of the screen) //Render the command list (Second chunk of the screen)
custom_list.draw(frame, chunks[1], search_input.clone(),state); custom_list.draw(frame, chunks[1], search_input.clone(), state);
if let Some(ref mut command) = &mut command_opt { if let Some(ref mut command) = &mut command_opt {
command.draw(frame, state); command.draw(frame, state);
@ -145,7 +145,27 @@ fn run<B: Backend>(terminal: &mut Terminal<B>, state: &AppState) -> io::Result<(
if key.code == KeyCode::Char('q') { if key.code == KeyCode::Char('q') {
return Ok(()); return Ok(());
} }
if let Some(cmd) = custom_list.handle_key(key, state) { //Activate search mode if the forward slash key gets pressed
if key.code == KeyCode::Char('/') {
// Enter search mode
in_search_mode = true;
continue;
}
//Insert user input into the search bar
if in_search_mode {
match key.code {
KeyCode::Char(c) => search_input.push(c),
KeyCode::Backspace => {
search_input.pop();
}
KeyCode::Esc => {
search_input = String::new();
in_search_mode = false
}
KeyCode::Enter => in_search_mode = false,
_ => {}
}
} else if let Some(cmd) = custom_list.handle_key(key, state) {
command_opt = Some(RunningCommand::new(cmd, state)); command_opt = Some(RunningCommand::new(cmd, state));
} }
} }