use cmus_notify::{
cmus::{self, query::CmusQueryResponse},
notification,
settings::Settings,
};
#[cfg(feature = "debug")]
extern crate pretty_env_logger;
#[cfg(feature = "debug")]
#[macro_use]
extern crate log;
macro_rules! sleep {
($time: expr) => {
#[cfg(feature = "debug")]
info!("sleeping for {} ms...", $time);
std::thread::sleep(std::time::Duration::from_millis($time));
};
}
fn main() {
#[cfg(feature = "debug")]
{
pretty_env_logger::init();
info!("Starting cmus-notify...");
info!("Debug mode is enabled. (feature \"debug\")");
info!("Binary path: {}", file!());
info!("Parsing the arguments and loading the configs...")
}
let settings = Settings::load_config_and_parse_args();
#[cfg(feature = "debug")]
{
info!("Configs loaded, and arguments parsed.");
info!("Settings: {:#?}", settings);
}
let mut query_command = cmus::build_query_command(
settings.remote_bin_path().as_str(),
&settings.cmus_socket_address,
&settings.cmus_socket_password,
);
#[cfg(feature = "debug")]
info!("Query command built: {:?}", query_command);
let interval = settings.interval();
let link = settings.link;
let mut notifications_handler = notification::NotificationsHandler::new(settings);
let mut previous_response = CmusQueryResponse::default();
sleep!(300);
loop {
let Ok(response) = cmus::ping_cmus(&mut query_command) else {
if link {
std::process::exit(0)
} else {
sleep!(interval);
continue;
}
};
if response != previous_response {
if let Ok(events) = previous_response.events(&response) {
previous_response = response;
if !events.is_empty() {
match notifications_handler.show_notification(events, &previous_response) {
Ok(_) => {}
Err(e) => {
eprintln!("Error: {}", e);
}
}
}
}
}
sleep!(interval);
}
}