small_bin/
notification.rs

1use crate::{
2    config::{NotificationSettings, SoundSettings},
3    *,
4};
5use anyhow::Result;
6use std::process::Command;
7
8
9pub fn send(message: &str, sound_name: Option<&str>) -> Result<()> {
10    let sound_command = if let Some(sound) = sound_name {
11        format!("sound name \"{sound}\"")
12    } else {
13        String::new()
14    };
15
16    let script =
17        format!("display notification \"{message}\" {sound_command} with title \"Small\"");
18    let output = Command::new("/usr/bin/osascript")
19        .args(["-e", &script])
20        .output()?;
21
22    if output.status.success() {
23        Ok(())
24    } else {
25        anyhow::bail!("Failed to send notification")
26    }
27}
28
29
30pub fn notification(
31    message: &str,
32    notification_type: &str,
33    notifications: &NotificationSettings,
34    sounds: &SoundSettings,
35) -> Result<()> {
36    let should_notify = match notification_type {
37        "start" => notifications.start,
38        "clipboard" => notifications.clipboard,
39        "upload" => notifications.upload,
40        "error" => notifications.error,
41        _ => false,
42    };
43
44    if !should_notify {
45        return Ok(());
46    }
47
48    debug!("Notification of type {notification_type} with result: {should_notify}");
49    let sound_name = match notification_type {
50        "start" if sounds.start => Some(sounds.start_sound.as_str()),
51        "clipboard" if sounds.clipboard => Some(sounds.clipboard_sound.as_str()),
52        "upload" if sounds.upload => Some(sounds.upload_sound.as_str()),
53        "error" if sounds.error => Some(sounds.error_sound.as_str()),
54        _ => None,
55    };
56
57    send(message, sound_name)?;
58    if notification_type == "error" {
59        anyhow::bail!("{message}");
60    }
61
62    Ok(())
63}