return user to their previous position if they enter a subdir

This commit is contained in:
nyx 2024-10-01 23:09:30 -04:00
parent d3a801d49b
commit edae301047
No known key found for this signature in database
GPG Key ID: 9924455A3BB7D06C

View File

@ -60,6 +60,7 @@ pub struct AppState {
drawable: bool, drawable: bool,
#[cfg(feature = "tips")] #[cfg(feature = "tips")]
tip: &'static str, tip: &'static str,
position_stack: Vec<usize>,
} }
pub enum Focus { pub enum Focus {
@ -94,6 +95,7 @@ impl AppState {
drawable: false, drawable: false,
#[cfg(feature = "tips")] #[cfg(feature = "tips")]
tip: get_random_tip(), tip: get_random_tip(),
position_stack: vec![0],
}; };
state.update_items(); state.update_items();
@ -572,9 +574,12 @@ impl AppState {
} }
fn enter_parent_directory(&mut self) { fn enter_parent_directory(&mut self) {
self.visit_stack.pop(); if self.visit_stack.len() > 1 {
self.selection.select(Some(0)); self.visit_stack.pop();
self.update_items(); let previous_position = self.position_stack.pop().unwrap_or(0);
self.selection.select(Some(previous_position));
self.update_items();
}
} }
fn get_selected_node(&self) -> Option<Rc<ListNode>> { fn get_selected_node(&self) -> Option<Rc<ListNode>> {
@ -601,20 +606,19 @@ impl AppState {
} }
pub fn go_to_selected_dir(&mut self) { pub fn go_to_selected_dir(&mut self) {
let mut selected_index = self.selection.selected().unwrap_or(0); let selected_index = self.selection.selected().unwrap_or(0);
if !self.at_root() && selected_index == 0 { if !self.at_root() && selected_index == 0 {
self.enter_parent_directory(); self.enter_parent_directory();
return; return;
} }
if !self.at_root() { let actual_index = if self.at_root() { selected_index } else { selected_index - 1 };
selected_index = selected_index.saturating_sub(1);
}
if let Some(item) = self.filter.item_list().get(selected_index) { if let Some(item) = self.filter.item_list().get(actual_index) {
if item.has_children { if item.has_children {
self.visit_stack.push(item.id); self.visit_stack.push(item.id);
self.position_stack.push(selected_index);
self.selection.select(Some(0)); self.selection.select(Some(0));
self.update_items(); self.update_items();
} }
@ -646,7 +650,6 @@ impl AppState {
pub fn selected_item_is_up_dir(&self) -> bool { pub fn selected_item_is_up_dir(&self) -> bool {
let selected_index = self.selection.selected().unwrap_or(0); let selected_index = self.selection.selected().unwrap_or(0);
!self.at_root() && selected_index == 0 !self.at_root() && selected_index == 0
} }
@ -668,7 +671,11 @@ impl AppState {
} }
fn handle_enter(&mut self) { fn handle_enter(&mut self) {
if self.selected_item_is_cmd() { if self.selected_item_is_up_dir() {
self.enter_parent_directory();
} else if self.selected_item_is_dir() {
self.go_to_selected_dir();
} else if self.selected_item_is_cmd() {
if self.selected_commands.is_empty() { if self.selected_commands.is_empty() {
if let Some(node) = self.get_selected_node() { if let Some(node) = self.get_selected_node() {
self.selected_commands.push(node); self.selected_commands.push(node);
@ -683,8 +690,6 @@ impl AppState {
let prompt = ConfirmPrompt::new(&cmd_names[..]); let prompt = ConfirmPrompt::new(&cmd_names[..]);
self.focus = Focus::ConfirmationPrompt(Float::new(Box::new(prompt), 40, 40)); self.focus = Focus::ConfirmationPrompt(Float::new(Box::new(prompt), 40, 40));
} else {
self.go_to_selected_dir();
} }
} }