| @@ -1,12 +1,10 @@ |
| 1 | + | use crate::app::AppResult; |
| 1 | 2 | | use mpd::song::Song; |
| 2 | 3 | | use mpd::{Client, State}; |
| 3 | 4 | | use simple_dmenu::dmenu; |
| 4 | 5 | | use std::process::Command; |
| 5 | 6 | | use std::time::Duration; |
| 6 | 7 | | |
| 7 | | − | pub type Result<T> = core::result::Result<T, Error>; |
| 8 | | − | pub type Error = Box<dyn std::error::Error>; |
| 9 | | − | |
| 10 | 8 | | #[derive(Debug)] |
| 11 | 9 | | /// struct storing the mpd Client related stuff |
| 12 | 10 | | pub struct Connection { |
| @@ -24,7 +22,7 @@ pub struct Connection { |
| 24 | 22 | | |
| 25 | 23 | | impl Connection { |
| 26 | 24 | | /// Create a new connection |
| 27 | | − | pub fn new(addrs: &str) -> Result<Self> { |
| 25 | + | pub fn builder(addrs: &str) -> AppResult<Self> { |
| 28 | 26 | | let mut conn = Client::connect(addrs).unwrap_or_else(|_| { |
| 29 | 27 | | eprintln!("Error connecting to mpd server, Make sure mpd is running"); |
| 30 | 28 | | std::process::exit(1); |
| @@ -36,8 +34,7 @@ impl Connection { |
| 36 | 34 | | }; |
| 37 | 35 | | |
| 38 | 36 | | let songs_filenames: Vec<String> = conn |
| 39 | | − | .listall() |
| 40 | | − | .unwrap() |
| 37 | + | .listall()? |
| 41 | 38 | | .into_iter() |
| 42 | 39 | | .map(|x| x.file) |
| 43 | 40 | | .collect(); |
| @@ -68,7 +65,7 @@ impl Connection { |
| 68 | 65 | | } |
| 69 | 66 | | |
| 70 | 67 | | /// Dmenu prompt for selecting songs |
| 71 | | − | pub fn play_dmenu(&mut self) -> Result<()> { |
| 68 | + | pub fn play_dmenu(&mut self) -> AppResult<()> { |
| 72 | 69 | | if is_installed("dmenu") { |
| 73 | 70 | | let ss: Vec<&str> = self.songs_filenames.iter().map(|x| x.as_str()).collect(); |
| 74 | 71 | | let op = dmenu!(iter &ss; args "-p", "Choose a song: ", "-l", "30"); |
| @@ -94,11 +91,11 @@ impl Connection { |
| 94 | 91 | | let stats = self.conn.stats().unwrap_or_default(); |
| 95 | 92 | | |
| 96 | 93 | | // Playback State |
| 97 | | − | match status.state { |
| 98 | | − | State::Stop => self.state = "Stopped".to_string(), |
| 99 | | − | State::Play => self.state = "Playing".to_string(), |
| 100 | | − | State::Pause => self.state = "Paused".to_string(), |
| 101 | | − | } |
| 94 | + | self.state = match status.state { |
| 95 | + | State::Stop => "Stopped".to_string(), |
| 96 | + | State::Play => "Playing".to_string(), |
| 97 | + | State::Pause => "Paused".to_string(), |
| 98 | + | }; |
| 102 | 99 | | |
| 103 | 100 | | // Progress |
| 104 | 101 | | let (elapsed, total) = status.time.unwrap_or_default(); |
| @@ -137,7 +134,7 @@ impl Connection { |
| 137 | 134 | | } |
| 138 | 135 | | |
| 139 | 136 | | /// push the given song to queue |
| 140 | | − | pub fn push(&mut self, song: &Song) -> Result<()> { |
| 137 | + | pub fn push(&mut self, song: &Song) -> AppResult<()> { |
| 141 | 138 | | if self.conn.queue().unwrap().is_empty() { |
| 142 | 139 | | self.conn.push(song).unwrap(); |
| 143 | 140 | | self.conn.play().unwrap(); |
| @@ -154,14 +151,14 @@ impl Connection { |
| 154 | 151 | | } |
| 155 | 152 | | |
| 156 | 153 | | /// Push all songs of a playlist into queue |
| 157 | | − | pub fn load_playlist(&mut self, playlist: &str) -> Result<()> { |
| 154 | + | pub fn load_playlist(&mut self, playlist: &str) -> AppResult<()> { |
| 158 | 155 | | self.conn.load(playlist, ..)?; |
| 159 | 156 | | self.conn.play()?; |
| 160 | 157 | | Ok(()) |
| 161 | 158 | | } |
| 162 | 159 | | |
| 163 | 160 | | /// Add given song to playlist |
| 164 | | − | pub fn add_to_playlist(&mut self, playlist: &str, song: &Song) -> Result<()> { |
| 161 | + | pub fn add_to_playlist(&mut self, playlist: &str, song: &Song) -> AppResult<()> { |
| 165 | 162 | | self.conn.pl_push(playlist, song)?; |
| 166 | 163 | | Ok(()) |
| 167 | 164 | | } |
| @@ -185,7 +182,7 @@ impl Connection { |
| 185 | 182 | | } |
| 186 | 183 | | |
| 187 | 184 | | /// Gives title of current playing song |
| 188 | | − | pub fn now_playing(&mut self) -> Result<Option<String>> { |
| 185 | + | pub fn now_playing(&mut self) -> AppResult<Option<String>> { |
| 189 | 186 | | if let Some(s) = &self.current_song.title { |
| 190 | 187 | | if let Some(a) = &self.current_song.artist { |
| 191 | 188 | | Ok(Some(format!("\"{}\" By {}", s, a))) |
| @@ -210,20 +207,14 @@ impl Connection { |
| 210 | 207 | | |
| 211 | 208 | | /// Toggle Repeat mode |
| 212 | 209 | | pub fn toggle_repeat(&mut self) { |
| 213 | | − | if self.conn.status().unwrap().repeat { |
| 214 | | − | self.conn.repeat(false).unwrap(); |
| 215 | | − | } else { |
| 216 | | − | self.conn.repeat(true).unwrap(); |
| 217 | | − | } |
| 210 | + | let mode = self.conn.status().unwrap().repeat; |
| 211 | + | self.conn.repeat(!mode).unwrap(); |
| 218 | 212 | | } |
| 219 | 213 | | |
| 220 | 214 | | /// Toggle random mode |
| 221 | 215 | | pub fn toggle_random(&mut self) { |
| 222 | | − | if self.conn.status().unwrap().random { |
| 223 | | − | self.conn.random(false).unwrap(); |
| 224 | | − | } else { |
| 225 | | − | self.conn.random(true).unwrap(); |
| 226 | | − | } |
| 216 | + | let mode = self.conn.status().unwrap().random; |
| 217 | + | self.conn.random(!mode).unwrap(); |
| 227 | 218 | | } |
| 228 | 219 | | |
| 229 | 220 | | // Volume controls |