| 1 | use crate::{app::AppResult, connection::Connection}; |
| 2 | |
| 3 | #[derive(Debug)] |
| 4 | pub struct FileBrowser { |
| 5 | pub filetree: Vec<(String, String)>, |
| 6 | pub selected: usize, |
| 7 | pub prev_selected: usize, |
| 8 | pub path: String, |
| 9 | pub prev_path: String, |
| 10 | } |
| 11 | |
| 12 | impl FileBrowser { |
| 13 | pub fn new() -> FileBrowser { |
| 14 | FileBrowser { |
| 15 | filetree: Vec::new(), |
| 16 | selected: 0, |
| 17 | prev_selected: 0, |
| 18 | path: ".".to_string(), |
| 19 | prev_path: ".".to_string(), |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | pub fn update_directory(&mut self, conn: &mut Connection) -> AppResult<()> { |
| 24 | self.filetree.clear(); |
| 25 | self.filetree = conn |
| 26 | .conn |
| 27 | .listfiles(self.path.as_str())? |
| 28 | .into_iter() |
| 29 | .filter(|(f, _)| f == "directory" || f == "file") |
| 30 | .collect::<Vec<(String, String)>>(); |
| 31 | |
| 32 | Ok(()) |
| 33 | } |
| 34 | |
| 35 | // Go to next item in filetree |
| 36 | pub fn next(&mut self) { |
| 37 | // if self.selected < self.filetree.len() - 1 { |
| 38 | // self.selected += 1; |
| 39 | // } |
| 40 | |
| 41 | if self.selected == self.filetree.len() - 1 { |
| 42 | self.selected = 0; |
| 43 | } else { |
| 44 | self.selected += 1; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /// Go to previous item in filetree |
| 49 | pub fn prev(&mut self) { |
| 50 | if self.selected == 0 { |
| 51 | self.selected = self.filetree.len() - 1; |
| 52 | } else { |
| 53 | self.selected -= 1; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | pub fn handle_enter(&mut self, conn: &mut Connection) -> AppResult<()> { |
| 58 | let (t, path) = self.filetree.get(self.selected).unwrap(); |
| 59 | if t == "directory" { |
| 60 | if path != "." { |
| 61 | self.prev_path = self.path.clone(); |
| 62 | self.path = self.prev_path.clone() + "/" + path; |
| 63 | self.update_directory(conn)?; |
| 64 | self.prev_selected = self.selected; |
| 65 | self.selected = 0; |
| 66 | // println!("self.path: {}", self.path); |
| 67 | // println!("self.prev_pat: {}", self.prev_path); |
| 68 | } |
| 69 | } else { |
| 70 | // let list = conn |
| 71 | // .songs_filenames |
| 72 | // .iter() |
| 73 | // .map(|f| f.as_str()) |
| 74 | // .collect::<Vec<&str>>(); |
| 75 | // let (filename, _) = rust_fuzzy_search::fuzzy_search_sorted(&path, &list) |
| 76 | // .get(0) |
| 77 | // .unwrap() |
| 78 | // .clone(); |
| 79 | |
| 80 | for filename in conn.songs_filenames.clone().iter() { |
| 81 | if filename.contains(path) { |
| 82 | let song = conn.get_song_with_only_filename(filename); |
| 83 | conn.push(&song)?; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | Ok(()) |
| 88 | } |
| 89 | |
| 90 | pub fn handle_go_back(&mut self, conn: &mut Connection) -> AppResult<()> { |
| 91 | if self.prev_path != "." { |
| 92 | let r = self.path.rfind("/").unwrap(); |
| 93 | self.path = self.path.as_str()[..r].to_string(); |
| 94 | self.update_directory(conn)?; |
| 95 | } else { |
| 96 | self.path = self.prev_path.clone(); |
| 97 | self.update_directory(conn)?; |
| 98 | } |
| 99 | |
| 100 | self.selected = self.prev_selected; |
| 101 | Ok(()) |
| 102 | } |
| 103 | } |