Better error and notification handling

This commit is contained in:
Nox Sluijtman 2025-07-23 13:18:52 +02:00
parent c9ac64d504
commit eddf2231e9
Signed by: Egg
SSH key fingerprint: SHA256:2sG9X3C7Xvq2svGumz1/k7cm8l4G9+qAtAeugqB4J9M
5 changed files with 45 additions and 30 deletions

View file

@ -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,
}
}