Better error and notification handling
This commit is contained in:
parent
c9ac64d504
commit
eddf2231e9
5 changed files with 45 additions and 30 deletions
|
@ -3,7 +3,7 @@ extern crate mpd;
|
|||
|
||||
use libnotify::Notification;
|
||||
use mpd::{Client, Query, Song, State, Term};
|
||||
use std::time::Duration;
|
||||
use std::{env, time::Duration};
|
||||
|
||||
pub trait Noise {
|
||||
fn find(&mut self, query: &Vec<String>, max: Option<u32>, append: bool);
|
||||
|
@ -174,12 +174,23 @@ impl Noise for Client {
|
|||
State::Play => "playing",
|
||||
};
|
||||
|
||||
fn to_mins_string(dur: Duration) -> String {
|
||||
let seconds = dur.as_secs();
|
||||
|
||||
let minutes = seconds / 60;
|
||||
let rem_seconds = seconds % 60;
|
||||
|
||||
format!("{minutes:02}:{rem_seconds:02}")
|
||||
}
|
||||
|
||||
let duration = status.time.map_or("".into(), |(stamp, total)| {
|
||||
let stamp = to_mins_string(stamp);
|
||||
let total = to_mins_string(total);
|
||||
format!("{stamp}/{total}")
|
||||
});
|
||||
|
||||
// let pos_in_queue = format!("{current_pos}/{queue_lenth}");
|
||||
|
||||
output.push(format!("[{state}] {duration}"));
|
||||
|
||||
output.join("\n")
|
||||
|
@ -197,16 +208,16 @@ fn format_song(song: Song) -> String {
|
|||
// fn get_status(client: &mut Client, verbose: bool) -> String {}
|
||||
|
||||
fn send_notification(body: &str) {
|
||||
libnotify::init("noise").unwrap();
|
||||
let notify = Notification::new("Noise", Some(body), None);
|
||||
notify.show().unwrap();
|
||||
}
|
||||
|
||||
fn to_mins_string(dur: Duration) -> String {
|
||||
let seconds = dur.as_secs();
|
||||
|
||||
let minutes = seconds / 60;
|
||||
let rem_seconds = seconds % 60;
|
||||
|
||||
format!("{minutes:02}:{rem_seconds:02}")
|
||||
let key = "XDG_SESSION_TYPE";
|
||||
match env::var(key) {
|
||||
Ok(key) => {
|
||||
if key != "tty" {
|
||||
libnotify::init("noise").unwrap();
|
||||
let notify = Notification::new("Noise", Some(body), None);
|
||||
notify.show().unwrap();
|
||||
libnotify::uninit();
|
||||
}
|
||||
}
|
||||
Err(_) => return,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue