From 92643de3ad1e86ca841857bbe8e0b9a215b9ae37 Mon Sep 17 00:00:00 2001 From: nyx Date: Wed, 2 Oct 2024 00:02:14 -0400 Subject: [PATCH] implement changes proposed by adam Co-authored-by: Adam Perkowski --- tui/src/state.rs | 55 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/tui/src/state.rs b/tui/src/state.rs index e132ee15..a5218f70 100644 --- a/tui/src/state.rs +++ b/tui/src/state.rs @@ -77,6 +77,13 @@ pub struct ListEntry { pub has_children: bool, } +enum SelectedItem { + UpDir, + Directory, + Command, + None, +} + impl AppState { pub fn new(theme: Theme, override_validation: bool) -> Self { let tabs = linutil_core::get_tabs(!override_validation); @@ -574,9 +581,8 @@ impl AppState { } fn enter_parent_directory(&mut self) { - if self.visit_stack.len() > 1 { + if let Some(previous_position) = self.position_stack.pop() { self.visit_stack.pop(); - let previous_position = self.position_stack.pop().unwrap_or(0); self.selection.select(Some(previous_position)); self.update_items(); } @@ -674,26 +680,39 @@ impl AppState { } } - fn handle_enter(&mut self) { + fn get_selected_item_type(&self) -> SelectedItem { if self.selected_item_is_up_dir() { - self.enter_parent_directory(); + SelectedItem::UpDir } else if self.selected_item_is_dir() { - self.go_to_selected_dir(); + SelectedItem::Directory } else if self.selected_item_is_cmd() { - if self.selected_commands.is_empty() { - if let Some(node) = self.get_selected_node() { - self.selected_commands.push(node); + SelectedItem::Command + } else { + SelectedItem::None + } + } + + fn handle_enter(&mut self) { + match self.get_selected_item_type() { + SelectedItem::UpDir => self.enter_parent_directory(), + SelectedItem::Directory => self.go_to_selected_dir(), + SelectedItem::Command => { + if self.selected_commands.is_empty() { + if let Some(node) = self.get_selected_node() { + self.selected_commands.push(node); + } } + + let cmd_names = self + .selected_commands + .iter() + .map(|node| node.name.as_str()) + .collect::>(); + + let prompt = ConfirmPrompt::new(&cmd_names[..]); + self.focus = Focus::ConfirmationPrompt(Float::new(Box::new(prompt), 40, 40)); } - - let cmd_names = self - .selected_commands - .iter() - .map(|node| node.name.as_str()) - .collect::>(); - - let prompt = ConfirmPrompt::new(&cmd_names[..]); - self.focus = Focus::ConfirmationPrompt(Float::new(Box::new(prompt), 40, 40)); + SelectedItem::None => {} } } @@ -757,4 +776,4 @@ fn get_random_tip() -> &'static str { let mut rng = rand::thread_rng(); let random_index = rng.gen_range(0..tips.len()); tips[random_index] -} +} \ No newline at end of file