diff --git a/src/app.rs b/src/app.rs index a5303e1..17a61ad 100644 --- a/src/app.rs +++ b/src/app.rs @@ -43,6 +43,7 @@ pub struct App<'a> { input_mode: InputMode, pub titles: Vec<&'a str>, pub active_tab: AppTab, + pub last_visited_path: PathBuf, } impl<'a> App<'a> { @@ -55,6 +56,7 @@ impl<'a> App<'a> { input_mode: InputMode::Browser, titles: vec!["Music", "Controls"], active_tab: AppTab::Music, + last_visited_path: env::current_dir().unwrap(), } } @@ -83,6 +85,7 @@ impl<'a> App<'a> { let join = self.selected_item(); // if folder enter, else play song if join.is_dir() { + self.last_visited_path = join.clone(); env::set_current_dir(join).unwrap(); self.browser_items = StatefulList::with_items(gen_funcs::scan_and_filter_directory()); self.browser_items.next(); @@ -95,7 +98,7 @@ impl<'a> App<'a> { pub fn backpedal(&mut self) { env::set_current_dir("../").unwrap(); self.browser_items = StatefulList::with_items(gen_funcs::scan_and_filter_directory()); - self.browser_items.next(); + self.browser_items.select_by_path(&self.last_visited_path); } // if queue has items and nothing playing, auto play diff --git a/src/helpers/stateful_list.rs b/src/helpers/stateful_list.rs index 0828713..d2a361f 100644 --- a/src/helpers/stateful_list.rs +++ b/src/helpers/stateful_list.rs @@ -1,3 +1,4 @@ +use std::path::PathBuf; use tui::widgets::ListState; // TODO encapsulation @@ -77,4 +78,26 @@ impl StatefulList { pub fn unselect(&mut self) { self.state.select(None); } + pub fn select(&mut self, i: usize) { + self.curr = i; + self.state.select(Some(i)); + } +} + +impl StatefulList { + pub fn find_by_path(&self, s: &PathBuf) -> usize { + let mut i = 0; + + for n in 0 .. self.items.len() { + if s.ends_with(self.items[n].to_string()) { + i = n; + break; + } + } + + i + } + pub fn select_by_path(&mut self, s: &PathBuf) { + self.select(self.find_by_path(s)); + } }