| @@ -1,66 +1,53 @@ |
| 1 | 1 | | use mpd::song::Song; |
| 2 | 2 | | use mpd::Client; |
| 3 | + | use simple_dmenu::dmenu; |
| 4 | + | use std::process::Command; |
| 3 | 5 | | |
| 4 | 6 | | pub struct Connection { |
| 5 | 7 | | pub conn: Client, |
| 8 | + | pub songs_filenames: Vec<String>, |
| 6 | 9 | | } |
| 7 | 10 | | |
| 8 | 11 | | impl Connection { |
| 9 | 12 | | pub fn new(addrs: &str) -> Result<Self, mpd::error::Error> { |
| 10 | | − | let conn = Client::connect(addrs)?; |
| 11 | | − | Ok(Self { conn }) |
| 13 | + | let mut conn = Client::connect(addrs)?; |
| 14 | + | let mut songs_filenames: Vec<String> = Vec::new(); |
| 15 | + | get_file_tree_into_vec(&mut conn, &mut songs_filenames, ".", "."); |
| 16 | + | |
| 17 | + | Ok(Self { |
| 18 | + | conn, |
| 19 | + | songs_filenames, |
| 20 | + | }) |
| 12 | 21 | | } |
| 13 | 22 | | |
| 14 | 23 | | 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; |
| 24 | + | let ss = &self.songs_filenames; |
| 19 | 25 | | 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 | | − | } |
| 26 | + | let index = get_choice_index(&self.songs_filenames, fzf_choice.get(0).unwrap()); |
| 27 | + | let song = self.get_song_with_only_filename(ss.get(index).unwrap()); |
| 28 | + | self.push(&song); |
| 29 | + | } |
| 25 | 30 | | |
| 26 | | − | let song = self.get_song(ss.get(choice).unwrap()); |
| 31 | + | pub fn play_dmenu(&mut self) { |
| 32 | + | let ss: Vec<&str> = self.songs_filenames.iter().map(|x| x.as_str()).collect(); |
| 33 | + | let op = dmenu!(iter &ss; args "-l", "30"); |
| 34 | + | let index = get_choice_index(&self.songs_filenames, &op); |
| 35 | + | let song = self.get_song_with_only_filename(ss.get(index).unwrap()); |
| 36 | + | self.push(&song); |
| 37 | + | } |
| 27 | 38 | | |
| 39 | + | fn push(&mut self, song: &Song) { |
| 40 | + | self.conn.push(song).unwrap(); |
| 28 | 41 | | if self.conn.queue().unwrap().is_empty() { |
| 29 | | − | self.push(song); |
| 42 | + | self.conn.push(song).unwrap(); |
| 30 | 43 | | self.conn.play().unwrap(); |
| 31 | 44 | | } else { |
| 32 | | − | self.push(song); |
| 45 | + | self.conn.push(song).unwrap(); |
| 33 | 46 | | self.conn.next().unwrap(); |
| 34 | 47 | | } |
| 35 | 48 | | } |
| 36 | 49 | | |
| 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 { |
| 50 | + | fn get_song_with_only_filename(&self, filename: &str) -> Song { |
| 64 | 51 | | Song { |
| 65 | 52 | | file: filename.to_string(), |
| 66 | 53 | | artist: None, |
| @@ -89,4 +76,54 @@ impl Connection { |
| 89 | 76 | | status.volume, status.repeat, status.random, status.single, status.consume |
| 90 | 77 | | ); |
| 91 | 78 | | } |
| 79 | + | |
| 80 | + | // Play controls |
| 81 | + | pub fn pause(&mut self) { |
| 82 | + | self.conn.pause(true).unwrap(); |
| 83 | + | } |
| 84 | + | |
| 85 | + | pub fn toggle_pause(&mut self) { |
| 86 | + | self.conn.toggle_pause().unwrap(); |
| 87 | + | } |
| 88 | + | |
| 89 | + | // Volume controls |
| 90 | + | pub fn inc_volume(&mut self, inc: i8) { |
| 91 | + | let cur = self.conn.status().unwrap().volume; |
| 92 | + | self.conn.volume(cur + inc).unwrap(); |
| 93 | + | } |
| 94 | + | |
| 95 | + | pub fn dec_volume(&mut self, dec: i8) { |
| 96 | + | let cur = self.conn.status().unwrap().volume; |
| 97 | + | self.conn.volume(cur - dec).unwrap(); |
| 98 | + | } |
| 99 | + | } |
| 100 | + | |
| 101 | + | fn get_file_tree_into_vec(conn: &mut Client, vec: &mut Vec<String>, path: &str, dir_append: &str) { |
| 102 | + | let songs = conn.listfiles(path).unwrap_or_default(); |
| 103 | + | for (i, s) in songs { |
| 104 | + | // Output of listfiles contains the last-modified thing, we dont want that |
| 105 | + | if i != "Last-Modified" { |
| 106 | + | if i == "directory" { |
| 107 | + | get_file_tree_into_vec(conn, vec, &s, &s); |
| 108 | + | } else { |
| 109 | + | // We parse the string as float because the output of listfiles contains some random numbers, we dont want that |
| 110 | + | if !s.parse::<f32>().is_ok() { |
| 111 | + | let mut sam: String = String::new(); |
| 112 | + | sam.push_str(dir_append); |
| 113 | + | sam.push_str(r"/"); |
| 114 | + | sam.push_str(s.as_str()); |
| 115 | + | vec.push(sam); |
| 116 | + | } |
| 117 | + | } |
| 118 | + | } |
| 119 | + | } |
| 120 | + | } |
| 121 | + | |
| 122 | + | fn get_choice_index(ss: &Vec<String>, selection: &str) -> usize { |
| 123 | + | let mut choice: usize = 0; |
| 124 | + | if let Some(index) = ss.iter().position(|s| s == selection) { |
| 125 | + | choice = index; |
| 126 | + | } |
| 127 | + | |
| 128 | + | choice |
| 92 | 129 | | } |