use crate::{core::helpers::spawn_with_args, ErrorHandler, PenroseError, Result};
use std::fmt;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum NotifyLevel {
Low,
Normal,
Critical,
}
impl fmt::Display for NotifyLevel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Self::Low => "low",
Self::Normal => "normal",
Self::Critical => "critical",
};
write!(f, "{}", s)
}
}
#[derive(Debug)]
pub struct NotifyConfig {
level: NotifyLevel,
duration: usize,
}
impl Default for NotifyConfig {
fn default() -> Self {
Self {
level: NotifyLevel::Normal,
duration: 5000,
}
}
}
pub fn notify_send(
title: impl Into<String>,
body: impl Into<String>,
config: NotifyConfig,
) -> Result<()> {
spawn_with_args(
"notify-send",
&[
"-u",
&config.level.to_string(),
"-t",
&config.duration.to_string(),
&title.into(),
&body.into(),
],
)
}
pub fn notify_send_error_handler() -> ErrorHandler {
Box::new(|e: PenroseError| {
if notify_send(
"Unhandled Error",
e.to_string(),
NotifyConfig {
level: NotifyLevel::Critical,
duration: 10000,
},
)
.is_err()
{
error!("Unable to display error via notify-send. Error was: {}", e);
}
})
}