syntactical suger
This commit is contained in:
parent
1d70e28c4e
commit
35db5fb07d
|
|
@ -41,7 +41,7 @@ pub enum SelectedTab {
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
pub fn builder(addrs: &str) -> AppResult<Self> {
|
pub fn builder(addrs: &str) -> AppResult<Self> {
|
||||||
let mut conn = Connection::new(addrs).unwrap();
|
let mut conn = Connection::builder(addrs)?;
|
||||||
let mut queue_list = ContentList::new();
|
let mut queue_list = ContentList::new();
|
||||||
let mut pl_list = ContentList::new();
|
let mut pl_list = ContentList::new();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,10 @@
|
||||||
|
use crate::app::AppResult;
|
||||||
use mpd::song::Song;
|
use mpd::song::Song;
|
||||||
use mpd::{Client, State};
|
use mpd::{Client, State};
|
||||||
use simple_dmenu::dmenu;
|
use simple_dmenu::dmenu;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
pub type Result<T> = core::result::Result<T, Error>;
|
|
||||||
pub type Error = Box<dyn std::error::Error>;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
/// struct storing the mpd Client related stuff
|
/// struct storing the mpd Client related stuff
|
||||||
pub struct Connection {
|
pub struct Connection {
|
||||||
|
|
@ -24,7 +22,7 @@ pub struct Connection {
|
||||||
|
|
||||||
impl Connection {
|
impl Connection {
|
||||||
/// Create a new connection
|
/// Create a new connection
|
||||||
pub fn new(addrs: &str) -> Result<Self> {
|
pub fn builder(addrs: &str) -> AppResult<Self> {
|
||||||
let mut conn = Client::connect(addrs).unwrap_or_else(|_| {
|
let mut conn = Client::connect(addrs).unwrap_or_else(|_| {
|
||||||
eprintln!("Error connecting to mpd server, Make sure mpd is running");
|
eprintln!("Error connecting to mpd server, Make sure mpd is running");
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
|
|
@ -36,8 +34,7 @@ impl Connection {
|
||||||
};
|
};
|
||||||
|
|
||||||
let songs_filenames: Vec<String> = conn
|
let songs_filenames: Vec<String> = conn
|
||||||
.listall()
|
.listall()?
|
||||||
.unwrap()
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|x| x.file)
|
.map(|x| x.file)
|
||||||
.collect();
|
.collect();
|
||||||
|
|
@ -68,7 +65,7 @@ impl Connection {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Dmenu prompt for selecting songs
|
/// Dmenu prompt for selecting songs
|
||||||
pub fn play_dmenu(&mut self) -> Result<()> {
|
pub fn play_dmenu(&mut self) -> AppResult<()> {
|
||||||
if is_installed("dmenu") {
|
if is_installed("dmenu") {
|
||||||
let ss: Vec<&str> = self.songs_filenames.iter().map(|x| x.as_str()).collect();
|
let ss: Vec<&str> = self.songs_filenames.iter().map(|x| x.as_str()).collect();
|
||||||
let op = dmenu!(iter &ss; args "-p", "Choose a song: ", "-l", "30");
|
let op = dmenu!(iter &ss; args "-p", "Choose a song: ", "-l", "30");
|
||||||
|
|
@ -94,11 +91,11 @@ impl Connection {
|
||||||
let stats = self.conn.stats().unwrap_or_default();
|
let stats = self.conn.stats().unwrap_or_default();
|
||||||
|
|
||||||
// Playback State
|
// Playback State
|
||||||
match status.state {
|
self.state = match status.state {
|
||||||
State::Stop => self.state = "Stopped".to_string(),
|
State::Stop => "Stopped".to_string(),
|
||||||
State::Play => self.state = "Playing".to_string(),
|
State::Play => "Playing".to_string(),
|
||||||
State::Pause => self.state = "Paused".to_string(),
|
State::Pause => "Paused".to_string(),
|
||||||
}
|
};
|
||||||
|
|
||||||
// Progress
|
// Progress
|
||||||
let (elapsed, total) = status.time.unwrap_or_default();
|
let (elapsed, total) = status.time.unwrap_or_default();
|
||||||
|
|
@ -137,7 +134,7 @@ impl Connection {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// push the given song to queue
|
/// push the given song to queue
|
||||||
pub fn push(&mut self, song: &Song) -> Result<()> {
|
pub fn push(&mut self, song: &Song) -> AppResult<()> {
|
||||||
if self.conn.queue().unwrap().is_empty() {
|
if self.conn.queue().unwrap().is_empty() {
|
||||||
self.conn.push(song).unwrap();
|
self.conn.push(song).unwrap();
|
||||||
self.conn.play().unwrap();
|
self.conn.play().unwrap();
|
||||||
|
|
@ -154,14 +151,14 @@ impl Connection {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Push all songs of a playlist into queue
|
/// Push all songs of a playlist into queue
|
||||||
pub fn load_playlist(&mut self, playlist: &str) -> Result<()> {
|
pub fn load_playlist(&mut self, playlist: &str) -> AppResult<()> {
|
||||||
self.conn.load(playlist, ..)?;
|
self.conn.load(playlist, ..)?;
|
||||||
self.conn.play()?;
|
self.conn.play()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add given song to playlist
|
/// Add given song to playlist
|
||||||
pub fn add_to_playlist(&mut self, playlist: &str, song: &Song) -> Result<()> {
|
pub fn add_to_playlist(&mut self, playlist: &str, song: &Song) -> AppResult<()> {
|
||||||
self.conn.pl_push(playlist, song)?;
|
self.conn.pl_push(playlist, song)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
@ -185,7 +182,7 @@ impl Connection {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gives title of current playing song
|
/// Gives title of current playing song
|
||||||
pub fn now_playing(&mut self) -> Result<Option<String>> {
|
pub fn now_playing(&mut self) -> AppResult<Option<String>> {
|
||||||
if let Some(s) = &self.current_song.title {
|
if let Some(s) = &self.current_song.title {
|
||||||
if let Some(a) = &self.current_song.artist {
|
if let Some(a) = &self.current_song.artist {
|
||||||
Ok(Some(format!("\"{}\" By {}", s, a)))
|
Ok(Some(format!("\"{}\" By {}", s, a)))
|
||||||
|
|
@ -210,20 +207,14 @@ impl Connection {
|
||||||
|
|
||||||
/// Toggle Repeat mode
|
/// Toggle Repeat mode
|
||||||
pub fn toggle_repeat(&mut self) {
|
pub fn toggle_repeat(&mut self) {
|
||||||
if self.conn.status().unwrap().repeat {
|
let mode = self.conn.status().unwrap().repeat;
|
||||||
self.conn.repeat(false).unwrap();
|
self.conn.repeat(!mode).unwrap();
|
||||||
} else {
|
|
||||||
self.conn.repeat(true).unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Toggle random mode
|
/// Toggle random mode
|
||||||
pub fn toggle_random(&mut self) {
|
pub fn toggle_random(&mut self) {
|
||||||
if self.conn.status().unwrap().random {
|
let mode = self.conn.status().unwrap().random;
|
||||||
self.conn.random(false).unwrap();
|
self.conn.random(!mode).unwrap();
|
||||||
} else {
|
|
||||||
self.conn.random(true).unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Volume controls
|
// Volume controls
|
||||||
|
|
|
||||||
|
|
@ -137,8 +137,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
|
||||||
app.conn.songs_filenames = app
|
app.conn.songs_filenames = app
|
||||||
.conn
|
.conn
|
||||||
.conn
|
.conn
|
||||||
.listall()
|
.listall()?
|
||||||
.unwrap()
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|x| x.file)
|
.map(|x| x.file)
|
||||||
.collect();
|
.collect();
|
||||||
|
|
|
||||||
Reference in New Issue