Krolyxon
Commit a459eec

rmptuisrcbrowser.rs

137 lines · 4.0 KB · Rust

1use std::path::Path;
2
3use mpd::Song;
4
5use crate::{app::AppResult, connection::Connection, utils::FileExtension};
6
7#[derive(Debug)]
8/// struct for working with directory browser tab in rmptui
9pub struct FileBrowser {
10 pub filetree: Vec<(String, String)>,
11 pub selected: usize,
12 pub prev_selected: usize,
13 pub path: String,
14 pub prev_path: String,
15 pub songs: Vec<Song>,
16}
17
18impl FileBrowser {
19 pub fn new() -> FileBrowser {
20 FileBrowser {
21 filetree: Vec::new(),
22 selected: 0,
23 prev_selected: 0,
24 path: ".".to_string(),
25 prev_path: ".".to_string(),
26 songs: vec![],
27 }
28 }
29
30 pub fn update_directory(&mut self, conn: &mut Connection) -> AppResult<()> {
31 self.filetree.clear();
32 let mut file_vec: Vec<(String, String)> = vec![];
33 let mut dir_vec: Vec<(String, String)> = vec![];
34 for (t, f) in conn.conn.listfiles(self.path.as_str())?.into_iter() {
35 if t == "directory" && !f.starts_with(".") {
36 dir_vec.push((t, f));
37 } else if t == "file"
38 && Path::new(&f).has_extension(&[
39 "mp3", "ogg", "flac", "m4a", "wav", "aac", "opus", "ape", "wma", "mpc", "aiff",
40 "dff", "mp2", "mka",
41 ])
42 {
43 file_vec.push((t, f));
44 }
45 }
46
47 // dir_vec.sort_by(|a, b| a.1.cmp(&b.1));
48 dir_vec.sort_by(|a, b| {
49 let num_a = a.1.parse::<u32>().unwrap_or(u32::MAX);
50 let num_b = b.1.parse::<u32>().unwrap_or(u32::MAX);
51 num_a
52 .cmp(&num_b)
53 .then_with(|| a.1.to_lowercase().cmp(&b.1.to_lowercase()))
54 });
55
56 file_vec.sort_by(|a, b| {
57 let num_a = a.1.parse::<u32>().unwrap_or(u32::MAX);
58 let num_b = b.1.parse::<u32>().unwrap_or(u32::MAX);
59 num_a
60 .cmp(&num_b)
61 .then_with(|| a.1.to_lowercase().cmp(&b.1.to_lowercase()))
62 });
63
64 dir_vec.extend(file_vec);
65 self.filetree = dir_vec;
66
67 // Add metadata
68 self.songs.clear();
69 for (t, song) in self.filetree.iter() {
70 if t == "file" {
71 let v = conn
72 .conn
73 .lsinfo(Song {
74 file: (self.path.clone() + "/" + song)
75 .strip_prefix("./")
76 .unwrap_or_else(|| "")
77 .to_string(),
78 ..Default::default()
79 })
80 .unwrap_or_else(|_| {
81 vec![Song {
82 file: "Not a song".to_string(),
83 ..Default::default()
84 }]
85 });
86
87 self.songs.push(v.first().unwrap().clone());
88 } else {
89 let v = Song {
90 file: "".to_string(),
91 ..Default::default()
92 };
93
94 self.songs.push(v);
95 }
96 }
97
98 Ok(())
99 }
100
101 // Go to next item in filetree
102 pub fn next(&mut self) {
103 if self.selected < self.filetree.len() - 1 {
104 self.selected += 1;
105 }
106 }
107
108 /// Go to previous item in filetree
109 pub fn prev(&mut self) {
110 if self.selected != 0 {
111 self.selected -= 1;
112 }
113 }
114
115 /// handles going back event
116 pub fn handle_go_back(&mut self, conn: &mut Connection) -> AppResult<()> {
117 if self.prev_path != "." {
118 let r = self.path.rfind('/');
119 if let Some(r) = r {
120 self.path = self.path.as_str()[..r].to_string();
121 self.update_directory(conn)?;
122 }
123 } else {
124 self.path = self.prev_path.clone();
125 self.update_directory(conn)?;
126 }
127
128 self.selected = self.prev_selected;
129 Ok(())
130 }
131}
132
133impl Default for FileBrowser {
134 fn default() -> Self {
135 Self::new()
136 }
137}