mirror of
https://github.com/Zedfrigg/ironbar.git
synced 2025-07-03 03:31:03 +02:00
chore: initial commit
This commit is contained in:
commit
e37d8f2b14
36 changed files with 4948 additions and 0 deletions
58
src/modules/mpd/client.rs
Normal file
58
src/modules/mpd/client.rs
Normal file
|
@ -0,0 +1,58 @@
|
|||
use mpd_client::commands::responses::Status;
|
||||
use mpd_client::raw::MpdProtocolError;
|
||||
use mpd_client::{Client, Connection};
|
||||
use std::path::PathBuf;
|
||||
use tokio::net::{TcpStream, UnixStream};
|
||||
|
||||
fn is_unix_socket(host: &String) -> bool {
|
||||
PathBuf::from(host).is_file()
|
||||
}
|
||||
|
||||
pub async fn get_connection(host: &String) -> Result<Connection, MpdProtocolError> {
|
||||
if is_unix_socket(host) {
|
||||
connect_unix(host).await
|
||||
} else {
|
||||
connect_tcp(host).await
|
||||
}
|
||||
}
|
||||
|
||||
async fn connect_unix(host: &String) -> Result<Connection, MpdProtocolError> {
|
||||
let connection = UnixStream::connect(host)
|
||||
.await
|
||||
.unwrap_or_else(|_| panic!("Error connecting to unix socket: {}", host));
|
||||
|
||||
Client::connect(connection).await
|
||||
}
|
||||
|
||||
async fn connect_tcp(host: &String) -> Result<Connection, MpdProtocolError> {
|
||||
let connection = TcpStream::connect(host)
|
||||
.await
|
||||
.unwrap_or_else(|_| panic!("Error connecting to unix socket: {}", host));
|
||||
|
||||
Client::connect(connection).await
|
||||
}
|
||||
|
||||
// /// Gets MPD server status.
|
||||
// /// Panics on error.
|
||||
// pub async fn get_status(client: &Client) -> Status {
|
||||
// client
|
||||
// .command(commands::Status)
|
||||
// .await
|
||||
// .expect("Failed to get MPD server status")
|
||||
// }
|
||||
|
||||
/// Gets the duration of the current song
|
||||
pub fn get_duration(status: &Status) -> u64 {
|
||||
status
|
||||
.duration
|
||||
.expect("Failed to get duration from MPD status")
|
||||
.as_secs()
|
||||
}
|
||||
|
||||
/// Gets the elapsed time of the current song
|
||||
pub fn get_elapsed(status: &Status) -> u64 {
|
||||
status
|
||||
.elapsed
|
||||
.expect("Failed to get elapsed time from MPD status")
|
||||
.as_secs()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue