Krolyxon
Commit 9d388a6

rmptui9d388a6

syntactical suger

Author
krolxon
Date
2024-05-16 18:08 +0530
Parent
5b6a79c
Commit
9d388a665b5e1bfed1f01bd64edf1c270ac8f0b4

+19 −29

3 files changed

  1. M src/app.rs +1 −1
  2. M src/connection.rs +17 −26
  3. M src/event/handler.rs +1 −2
M src/app.rs View
@@ -41,7 +41,7 @@ pub enum SelectedTab {
4141
4242 impl App {
4343 pub fn builder(addrs: &str) -> AppResult<Self> {
44− let mut conn = Connection::new(addrs).unwrap();
44+ let mut conn = Connection::builder(addrs)?;
4545 let mut queue_list = ContentList::new();
4646 let mut pl_list = ContentList::new();
4747
M src/connection.rs View
@@ -1,12 +1,10 @@
1+use crate::app::AppResult;
12 use mpd::song::Song;
23 use mpd::{Client, State};
34 use simple_dmenu::dmenu;
45 use std::process::Command;
56 use std::time::Duration;
67
7−pub type Result<T> = core::result::Result<T, Error>;
8−pub type Error = Box<dyn std::error::Error>;
9−
108 #[derive(Debug)]
119 /// struct storing the mpd Client related stuff
1210 pub struct Connection {
@@ -24,7 +22,7 @@ pub struct Connection {
2422
2523 impl Connection {
2624 /// Create a new connection
27− pub fn new(addrs: &str) -> Result<Self> {
25+ pub fn builder(addrs: &str) -> AppResult<Self> {
2826 let mut conn = Client::connect(addrs).unwrap_or_else(|_| {
2927 eprintln!("Error connecting to mpd server, Make sure mpd is running");
3028 std::process::exit(1);
@@ -36,8 +34,7 @@ impl Connection {
3634 };
3735
3836 let songs_filenames: Vec<String> = conn
39− .listall()
40− .unwrap()
37+ .listall()?
4138 .into_iter()
4239 .map(|x| x.file)
4340 .collect();
@@ -68,7 +65,7 @@ impl Connection {
6865 }
6966
7067 /// Dmenu prompt for selecting songs
71− pub fn play_dmenu(&mut self) -> Result<()> {
68+ pub fn play_dmenu(&mut self) -> AppResult<()> {
7269 if is_installed("dmenu") {
7370 let ss: Vec<&str> = self.songs_filenames.iter().map(|x| x.as_str()).collect();
7471 let op = dmenu!(iter &ss; args "-p", "Choose a song: ", "-l", "30");
@@ -94,11 +91,11 @@ impl Connection {
9491 let stats = self.conn.stats().unwrap_or_default();
9592
9693 // 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+ };
10299
103100 // Progress
104101 let (elapsed, total) = status.time.unwrap_or_default();
@@ -137,7 +134,7 @@ impl Connection {
137134 }
138135
139136 /// push the given song to queue
140− pub fn push(&mut self, song: &Song) -> Result<()> {
137+ pub fn push(&mut self, song: &Song) -> AppResult<()> {
141138 if self.conn.queue().unwrap().is_empty() {
142139 self.conn.push(song).unwrap();
143140 self.conn.play().unwrap();
@@ -154,14 +151,14 @@ impl Connection {
154151 }
155152
156153 /// 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<()> {
158155 self.conn.load(playlist, ..)?;
159156 self.conn.play()?;
160157 Ok(())
161158 }
162159
163160 /// 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<()> {
165162 self.conn.pl_push(playlist, song)?;
166163 Ok(())
167164 }
@@ -185,7 +182,7 @@ impl Connection {
185182 }
186183
187184 /// 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>> {
189186 if let Some(s) = &self.current_song.title {
190187 if let Some(a) = &self.current_song.artist {
191188 Ok(Some(format!("\"{}\" By {}", s, a)))
@@ -210,20 +207,14 @@ impl Connection {
210207
211208 /// Toggle Repeat mode
212209 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();
218212 }
219213
220214 /// Toggle random mode
221215 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();
227218 }
228219
229220 // Volume controls
M src/event/handler.rs View
@@ -137,8 +137,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
137137 app.conn.songs_filenames = app
138138 .conn
139139 .conn
140− .listall()
141− .unwrap()
140+ .listall()?
142141 .into_iter()
143142 .map(|x| x.file)
144143 .collect();