| 1 | use mpd::song::Song; |
| 2 | use mpd::Client; |
| 3 | |
| 4 | pub struct Connection { |
| 5 | pub conn: Client, |
| 6 | } |
| 7 | |
| 8 | impl Connection { |
| 9 | pub fn new(addrs: &str) -> Result<Self, mpd::error::Error> { |
| 10 | let conn = Client::connect(addrs)?; |
| 11 | Ok(Self { conn }) |
| 12 | } |
| 13 | |
| 14 | pub fn play_fzf(&mut self) { |
| 15 | let mut ss: Vec<String> = Vec::new(); |
| 16 | self.get_file_tree_into_vec(&mut ss, ".", "."); |
| 17 | |
| 18 | let choice: usize; |
| 19 | let fzf_choice = rust_fzf::select(ss.clone(), Vec::new()).unwrap(); |
| 20 | if let Some(index) = ss.iter().position(|s| s == fzf_choice.get(0).unwrap()) { |
| 21 | choice = index; |
| 22 | } else { |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | let song = self.get_song(ss.get(choice).unwrap()); |
| 27 | |
| 28 | if self.conn.queue().unwrap().is_empty() { |
| 29 | self.push(song); |
| 30 | self.conn.play().unwrap(); |
| 31 | } else { |
| 32 | self.push(song); |
| 33 | self.conn.next().unwrap(); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | fn get_file_tree_into_vec(&mut self, vec: &mut Vec<String>, path: &str, dir_append: &str) { |
| 38 | let songs = self.conn.listfiles(path).unwrap_or_default(); |
| 39 | for (i, s) in songs { |
| 40 | // Output of listfiles contains the last-modified thing, we dont want that |
| 41 | if i != "Last-Modified" { |
| 42 | if i == "directory" { |
| 43 | self.get_file_tree_into_vec(vec, &s, &s); |
| 44 | } else { |
| 45 | // We parse the string as float because the output of listfiles contains some random numbers, we dont want that |
| 46 | if !s.parse::<f32>().is_ok() { |
| 47 | let mut sam: String = String::new(); |
| 48 | sam.push_str(dir_append); |
| 49 | sam.push_str(r"/"); |
| 50 | sam.push_str(s.as_str()); |
| 51 | vec.push(sam); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | fn push(&mut self, song: Song) { |
| 59 | self.conn.push(song).unwrap(); |
| 60 | |
| 61 | } |
| 62 | |
| 63 | fn get_song(&self, filename: &str) -> Song { |
| 64 | Song { |
| 65 | file: filename.to_string(), |
| 66 | artist: None, |
| 67 | title: None, |
| 68 | duration: None, |
| 69 | last_mod: None, |
| 70 | name: None, |
| 71 | place: None, |
| 72 | range: None, |
| 73 | tags: vec![("".to_string(), "".to_string())], |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | pub fn status(&mut self) { |
| 78 | let current_song = self.conn.currentsong(); |
| 79 | let status = self.conn.status().unwrap(); |
| 80 | |
| 81 | if current_song.is_ok() { |
| 82 | let song = current_song.unwrap(); |
| 83 | if let Some(s) = song { |
| 84 | println!("{} - {}", s.artist.unwrap(), s.title.unwrap()); |
| 85 | } |
| 86 | } |
| 87 | println!( |
| 88 | "volume: {}\trepeat: {}\trandom: {}\tsingle: {}\tconsume: {}", |
| 89 | status.volume, status.repeat, status.random, status.single, status.consume |
| 90 | ); |
| 91 | } |
| 92 | } |