Commit 9a9bdb4862

9a9bdb4862646da3b03490ab250feb62df7ea468

parent: e40a8a8d2e

Unregistered key

Nemo157 <git@nemo157.com> · 2024-09-17 05:15 UTC
committer: <noreply@github.com>

Support enabling multiple notification sinks (#344)

Layout: unified · split

docs/iamb.5 +2
@@ -269,6 +269,8 @@ to use the desktop mechanism (default).
269Setting this field to 269Setting this field to
270.Dq Sy bell 270.Dq Sy bell
271will use the terminal bell instead. 271will use the terminal bell instead.
272Both can be used via
273.Dq Sy desktop|bell .
272 274
273.It Sy show_message 275.It Sy show_message
274controls whether to show the message in the desktop notification, and defaults to 276controls whether to show the message in the desktop notification, and defaults to
src/config.rs +79 −9
@@ -398,23 +398,70 @@ pub enum UserDisplayStyle {
398 DisplayName, 398 DisplayName,
399} 399}
400 400
401#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)] 401#[derive(Clone, Copy, Debug, Eq, PartialEq)]
402#[serde(rename_all = "lowercase")] 402pub struct NotifyVia {
403pub enum NotifyVia {
404 /// Deliver notifications via terminal bell. 403 /// Deliver notifications via terminal bell.
405 Bell, 404 pub bell: bool,
406 /// Deliver notifications via desktop mechanism. 405 /// Deliver notifications via desktop mechanism.
407 #[cfg(feature = "desktop")] 406 #[cfg(feature = "desktop")]
408 Desktop, 407 pub desktop: bool,
409} 408}
409pub struct NotifyViaVisitor;
410 410
411impl Default for NotifyVia { 411impl Default for NotifyVia {
412 fn default() -> Self { 412 fn default() -> Self {
413 #[cfg(not(feature = "desktop"))] 413 Self {
414 return NotifyVia::Bell; 414 bell: cfg!(not(feature = "desktop")),
415 #[cfg(feature = "desktop")]
416 desktop: true,
417 }
418 }
419}
420
421impl<'de> Visitor<'de> for NotifyViaVisitor {
422 type Value = NotifyVia;
423
424 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
425 formatter.write_str("a valid notify destination (e.g. \"bell\" or \"desktop\")")
426 }
427
428 fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
429 where
430 E: SerdeError,
431 {
432 let mut via = NotifyVia {
433 bell: false,
434 #[cfg(feature = "desktop")]
435 desktop: false,
436 };
415 437
416 #[cfg(feature = "desktop")] 438 for value in value.split('|') {
417 return NotifyVia::Desktop; 439 match value.to_ascii_lowercase().as_str() {
440 "bell" => {
441 via.bell = true;
442 },
443 #[cfg(feature = "desktop")]
444 "desktop" => {
445 via.desktop = true;
446 },
447 #[cfg(not(feature = "desktop"))]
448 "desktop" => {
449 return Err(E::custom("desktop notification support was compiled out"))
450 },
451 _ => return Err(E::custom("could not parse into a notify destination")),
452 };
453 }
454
455 Ok(via)
456 }
457}
458
459impl<'de> Deserialize<'de> for NotifyVia {
460 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
461 where
462 D: Deserializer<'de>,
463 {
464 deserializer.deserialize_str(NotifyViaVisitor)
418 } 465 }
419} 466}
420 467
@@ -1189,6 +1236,29 @@ mod tests {
1189 assert_eq!(run, &exp); 1236 assert_eq!(run, &exp);
1190 } 1237 }
1191 1238
1239 #[test]
1240 fn test_parse_notify_via() {
1241 assert_eq!(NotifyVia { bell: false, desktop: true }, NotifyVia::default());
1242 assert_eq!(
1243 NotifyVia { bell: false, desktop: true },
1244 serde_json::from_str(r#""desktop""#).unwrap()
1245 );
1246 assert_eq!(
1247 NotifyVia { bell: true, desktop: false },
1248 serde_json::from_str(r#""bell""#).unwrap()
1249 );
1250 assert_eq!(
1251 NotifyVia { bell: true, desktop: true },
1252 serde_json::from_str(r#""bell|desktop""#).unwrap()
1253 );
1254 assert_eq!(
1255 NotifyVia { bell: true, desktop: true },
1256 serde_json::from_str(r#""desktop|bell""#).unwrap()
1257 );
1258 assert!(serde_json::from_str::<NotifyVia>(r#""other""#).is_err());
1259 assert!(serde_json::from_str::<NotifyVia>(r#""""#).is_err());
1260 }
1261
1192 #[test] 1262 #[test]
1193 fn test_load_example_config_toml() { 1263 fn test_load_example_config_toml() {
1194 let path = PathBuf::from("config.example.toml"); 1264 let path = PathBuf::from("config.example.toml");
src/notifications.rs +24 −8
@@ -63,11 +63,7 @@ pub async fn register_notifications(
63 return; 63 return;
64 } 64 }
65 65
66 match notify_via { 66 send_notification(&notify_via, &store, &summary, body.as_deref()).await;
67 #[cfg(feature = "desktop")]
68 NotifyVia::Desktop => send_notification_desktop(summary, body),
69 NotifyVia::Bell => send_notification_bell(&store).await,
70 }
71 }, 67 },
72 Err(err) => { 68 Err(err) => {
73 tracing::error!("Failed to extract notification data: {err}") 69 tracing::error!("Failed to extract notification data: {err}")
@@ -78,22 +74,42 @@ pub async fn register_notifications(
78 .await; 74 .await;
79} 75}
80 76
77async fn send_notification(
78 via: &NotifyVia,
79 store: &AsyncProgramStore,
80 summary: &str,
81 body: Option<&str>,
82) {
83 #[cfg(feature = "desktop")]
84 if via.desktop {
85 send_notification_desktop(summary, body);
86 }
87 #[cfg(not(feature = "desktop"))]
88 {
89 let _ = (summary, body, IAMB_XDG_NAME);
90 }
91
92 if via.bell {
93 send_notification_bell(store).await;
94 }
95}
96
81async fn send_notification_bell(store: &AsyncProgramStore) { 97async fn send_notification_bell(store: &AsyncProgramStore) {
82 let mut locked = store.lock().await; 98 let mut locked = store.lock().await;
83 locked.application.ring_bell = true; 99 locked.application.ring_bell = true;
84} 100}
85 101
86#[cfg(feature = "desktop")] 102#[cfg(feature = "desktop")]
87fn send_notification_desktop(summary: String, body: Option<String>) { 103fn send_notification_desktop(summary: &str, body: Option<&str>) {
88 let mut desktop_notification = notify_rust::Notification::new(); 104 let mut desktop_notification = notify_rust::Notification::new();
89 desktop_notification 105 desktop_notification
90 .summary(&summary) 106 .summary(summary)
91 .appname(IAMB_XDG_NAME) 107 .appname(IAMB_XDG_NAME)
92 .icon(IAMB_XDG_NAME) 108 .icon(IAMB_XDG_NAME)
93 .action("default", "default"); 109 .action("default", "default");
94 110
95 if let Some(body) = body { 111 if let Some(body) = body {
96 desktop_notification.body(&body); 112 desktop_notification.body(body);
97 } 113 }
98 114
99 if let Err(err) = desktop_notification.show() { 115 if let Err(err) = desktop_notification.show() {
src/tests.rs +1 −1
@@ -191,7 +191,7 @@ pub fn mock_tunables() -> TunableValues {
191 message_user_color: false, 191 message_user_color: false,
192 notifications: Notifications { 192 notifications: Notifications {
193 enabled: false, 193 enabled: false,
194 via: NotifyVia::Desktop, 194 via: NotifyVia::default(),
195 show_message: true, 195 show_message: true,
196 }, 196 },
197 image_preview: None, 197 image_preview: None,