| 1 | use mpd::song::Song; |
| 2 | use mpd::{Client, State}; |
| 3 | use simple_dmenu::dmenu; |
| 4 | use std::process::Command; |
| 5 | |
| 6 | pub type Result<T> = core::result::Result<T, Error>; |
| 7 | pub type Error = Box<dyn std::error::Error>; |
| 8 | |
| 9 | #[derive(Debug)] |
| 10 | pub struct Connection { |
| 11 | pub conn: Client, |
| 12 | pub songs_filenames: Vec<String>, |
| 13 | pub state: String, |
| 14 | } |
| 15 | |
| 16 | impl Connection { |
| 17 | /// Create a new connection |
| 18 | pub fn new(addrs: &str) -> Result<Self> { |
| 19 | let mut conn = Client::connect(addrs).unwrap(); |
| 20 | let songs_filenames: Vec<String> = conn |
| 21 | .listall() |
| 22 | .unwrap() |
| 23 | .into_iter() |
| 24 | .map(|x| x.file) |
| 25 | .collect(); |
| 26 | |
| 27 | Ok(Self { |
| 28 | conn, |
| 29 | songs_filenames, |
| 30 | state: "Stopped".to_string(), |
| 31 | }) |
| 32 | } |
| 33 | |
| 34 | /// Fzf prompt for selecting song |
| 35 | pub fn play_fzf(&mut self) -> Result<()> { |
| 36 | is_installed("fzf").map_err(|ex| ex)?; |
| 37 | |
| 38 | let ss = &self.songs_filenames; |
| 39 | let fzf_choice = rust_fzf::select(ss.clone(), Vec::new()).unwrap(); |
| 40 | let index = get_choice_index(&self.songs_filenames, fzf_choice.get(0).unwrap()); |
| 41 | let song = self.get_song_with_only_filename(ss.get(index).unwrap()); |
| 42 | self.push(&song)?; |
| 43 | |
| 44 | Ok(()) |
| 45 | } |
| 46 | |
| 47 | /// Dmenu prompt for selecting songs |
| 48 | pub fn play_dmenu(&mut self) -> Result<()> { |
| 49 | is_installed("dmenu").map_err(|ex| ex)?; |
| 50 | let ss: Vec<&str> = self.songs_filenames.iter().map(|x| x.as_str()).collect(); |
| 51 | let op = dmenu!(iter &ss; args "-l", "30"); |
| 52 | let index = get_choice_index(&self.songs_filenames, &op); |
| 53 | let song = self.get_song_with_only_filename(ss.get(index).unwrap()); |
| 54 | self.push(&song)?; |
| 55 | Ok(()) |
| 56 | } |
| 57 | |
| 58 | pub fn update_state(&mut self) -> String { |
| 59 | match self.conn.status().unwrap().state { |
| 60 | State::Stop => self.state = "Stopped".to_string(), |
| 61 | State::Play => self.state = "Playing".to_string(), |
| 62 | State::Pause => self.state = "Paused".to_string(), |
| 63 | } |
| 64 | self.state.clone() |
| 65 | } |
| 66 | |
| 67 | /// push the given song to queue |
| 68 | pub fn push(&mut self, song: &Song) -> Result<()> { |
| 69 | if self.conn.queue().unwrap().is_empty() { |
| 70 | self.conn.push(song).unwrap(); |
| 71 | self.conn.play().unwrap(); |
| 72 | } else { |
| 73 | self.conn.push(song)?; |
| 74 | let len: u32 = (self.conn.queue().unwrap().len() - 1).try_into().unwrap(); |
| 75 | self.conn.switch(len)?; |
| 76 | if self.conn.status()?.state == State::Stop { |
| 77 | self.conn.play()?; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | Ok(()) |
| 82 | } |
| 83 | |
| 84 | /// Push all songs of a playlist into queue |
| 85 | pub fn push_playlist(&mut self, playlist: &str) -> Result<()> { |
| 86 | let songs: Vec<Song> = self.conn.playlist(playlist)?; |
| 87 | |
| 88 | for song in songs { |
| 89 | if self.songs_filenames.contains(&song.file) { |
| 90 | let song = self.get_song_with_only_filename(&song.file); |
| 91 | self.conn.push(&song)?; |
| 92 | self.conn.play()?; |
| 93 | } |
| 94 | } |
| 95 | Ok(()) |
| 96 | } |
| 97 | |
| 98 | /// Given a filename, get instance of Song with only filename |
| 99 | pub fn get_song_with_only_filename(&self, filename: &str) -> Song { |
| 100 | Song { |
| 101 | file: filename.to_string(), |
| 102 | artist: None, |
| 103 | title: None, |
| 104 | duration: None, |
| 105 | last_mod: None, |
| 106 | name: None, |
| 107 | place: None, |
| 108 | range: None, |
| 109 | tags: vec![("".to_string(), "".to_string())], |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /// get current playing song |
| 114 | pub fn get_current_song(&mut self) -> Option<String> { |
| 115 | self.conn.currentsong().unwrap().unwrap_or_default().title |
| 116 | } |
| 117 | |
| 118 | /// Print status to stdout |
| 119 | pub fn status(&mut self) { |
| 120 | let current_song = self.conn.currentsong(); |
| 121 | let status = self.conn.status().unwrap(); |
| 122 | |
| 123 | if current_song.is_ok() && status.state != State::Stop { |
| 124 | let song = current_song.unwrap(); |
| 125 | if let Some(s) = song { |
| 126 | println!("{} - {}", s.artist.unwrap(), s.title.unwrap()); |
| 127 | } |
| 128 | } |
| 129 | println!( |
| 130 | "volume: {}\trepeat: {}\trandom: {}\tsingle: {}\tconsume: {}", |
| 131 | status.volume, status.repeat, status.random, status.single, status.consume |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | /// Gives title of current playing song |
| 136 | pub fn now_playing(&mut self) -> Result<Option<String>> { |
| 137 | let song = self.conn.currentsong()?.unwrap_or_default(); |
| 138 | if let Some(s) = song.title { |
| 139 | if let Some(a) = song.artist { |
| 140 | return Ok(Some(format!("\"{}\" By {}", a, s))); |
| 141 | } else { |
| 142 | return Ok(Some(s)); |
| 143 | } |
| 144 | } else { |
| 145 | return Ok(Some(song.file)); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // Playback controls |
| 150 | /// Pause playback |
| 151 | pub fn pause(&mut self) { |
| 152 | self.conn.pause(true).unwrap(); |
| 153 | } |
| 154 | |
| 155 | /// Toggles playback |
| 156 | pub fn toggle_pause(&mut self) { |
| 157 | self.conn.toggle_pause().unwrap(); |
| 158 | } |
| 159 | |
| 160 | // Volume controls |
| 161 | pub fn inc_volume(&mut self, v: i8) { |
| 162 | let cur = self.conn.status().unwrap().volume; |
| 163 | if cur + v <= 100 { |
| 164 | self.conn.volume(cur + v).unwrap(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | pub fn dec_volume(&mut self, v: i8) { |
| 169 | let cur = self.conn.status().unwrap().volume; |
| 170 | if cur - v >= 0 { |
| 171 | self.conn.volume(cur - v).unwrap(); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /// Gets the index of the string from the Vector |
| 177 | fn get_choice_index(ss: &Vec<String>, selection: &str) -> usize { |
| 178 | let mut choice: usize = 0; |
| 179 | if let Some(index) = ss.iter().position(|s| s == selection) { |
| 180 | choice = index; |
| 181 | } |
| 182 | |
| 183 | choice |
| 184 | } |
| 185 | |
| 186 | /// Checks if given program is installed in your system |
| 187 | fn is_installed(ss: &str) -> Result<()> { |
| 188 | let output = Command::new("which") |
| 189 | .arg(ss) |
| 190 | .output() |
| 191 | .expect("Failed to execute command"); |
| 192 | if output.status.success() { |
| 193 | Ok(()) |
| 194 | } else { |
| 195 | let err = format!("{} not installed", ss); |
| 196 | Err(err.into()) |
| 197 | } |
| 198 | } |