Skip to content

Commit

Permalink
Merge pull request #29 from lautarodragan/taro/remember-selection
Browse files Browse the repository at this point in the history
feat: remember selected path
  • Loading branch information
TrevorSatori authored Jul 28, 2024
2 parents 0b6efe1 + 9090d20 commit 1f65ce4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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(),
}
}

Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions src/helpers/stateful_list.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::path::PathBuf;
use tui::widgets::ListState;

// TODO encapsulation
Expand Down Expand Up @@ -77,4 +78,26 @@ impl<T> StatefulList<T> {
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<T: ToString> StatefulList<T> {
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));
}
}

0 comments on commit 1f65ce4

Please sign in to comment.