diff --git a/README.md b/README.md index 5499ba6..b4a2fc8 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ import { Notification } from "https://deno.land/x/notifier/whatwg/mod.ts"; new Notification("Hello", { body: "World", icon: "/path/to/icon.png", + sound: "device-added", // only support Linux and macOS }); ``` diff --git a/notifiers/notifier.ts b/notifiers/notifier.ts index 3bf2ab0..76c79fb 100644 --- a/notifiers/notifier.ts +++ b/notifiers/notifier.ts @@ -8,4 +8,5 @@ export interface Notification { title: string; message: string; icon?: string; + sound?: string; } diff --git a/notifiers/notify_send.ts b/notifiers/notify_send.ts index 69cf58f..52f053b 100644 --- a/notifiers/notify_send.ts +++ b/notifiers/notify_send.ts @@ -3,11 +3,14 @@ import type { Notification } from "./notifier.ts"; export class NotifySendNotifier extends SubprocessNotifier { buildCmd(notification: Notification): string[] { - const { title, message, icon } = notification; + const { title, message, icon, sound } = notification; const cmd = ["notify-send", title, message]; if (icon) { cmd.push("--icon", icon); } + if (sound) { + cmd.push(`--hint=string:sound-name:${sound}`); + } return cmd; } } diff --git a/notifiers/notify_send_test.ts b/notifiers/notify_send_test.ts index 9a61fb4..486bfbb 100644 --- a/notifiers/notify_send_test.ts +++ b/notifiers/notify_send_test.ts @@ -29,6 +29,22 @@ Deno.test("NotifySendNotifier#buildCmd", () => { "/path/to/icon.png", ], }, + { + notification: { + title: "Hello", + message: "World", + icon: "/path/to/icon.png", + sound: "device-added", + }, + expected: [ + "notify-send", + "Hello", + "World", + "--icon", + "/path/to/icon.png", + "--hint=string:sound-name:device-added", + ], + }, ] ) { const actual = notifier.buildCmd(Object.freeze(notification)); diff --git a/notifiers/osascript.ts b/notifiers/osascript.ts index 56178e0..a0bfec6 100644 --- a/notifiers/osascript.ts +++ b/notifiers/osascript.ts @@ -6,10 +6,15 @@ export class OsascriptNotifier extends SubprocessNotifier { buildCmd(notification: Notification): string[] { const message = escapeDoubleQuotes(notification.message); const title = escapeDoubleQuotes(notification.title); + let script = `display notification "${message}" with title "${title}"`; + if (notification.sound) { + const sound = escapeDoubleQuotes(notification.sound); + script += ` sound name "${sound}"`; + } return [ "osascript", "-e", - `display notification "${message}" with title "${title}"`, + script, ]; } } diff --git a/notifiers/osascript_test.ts b/notifiers/osascript_test.ts index c0a8211..fa7c21e 100644 --- a/notifiers/osascript_test.ts +++ b/notifiers/osascript_test.ts @@ -1,17 +1,35 @@ import { OsascriptNotifier } from "./osascript.ts"; import { assertEquals } from "../test_deps.ts"; -Deno.test("OsascriptNotifier#buildCmd", () => { - const notifier = new OsascriptNotifier(); - const notification = Object.freeze({ - title: "Hello", - message: "World", +Deno.test("OsascriptNotifier#buildCmd", async (t) => { + await t.step("basic", () => { + const notifier = new OsascriptNotifier(); + const notification = Object.freeze({ + title: "Hello", + message: "World", + }); + const actual = notifier.buildCmd(notification); + const expected = [ + "osascript", + "-e", + 'display notification "World" with title "Hello"', + ]; + assertEquals(actual, expected); + }); + + await t.step("with sound", () => { + const notifier = new OsascriptNotifier(); + const notification = Object.freeze({ + title: "Hello", + message: "World", + sound: "Submarine", + }); + const actual = notifier.buildCmd(notification); + const expected = [ + "osascript", + "-e", + 'display notification "World" with title "Hello" sound name "Submarine"', + ]; + assertEquals(actual, expected); }); - const actual = notifier.buildCmd(notification); - const expected = [ - "osascript", - "-e", - 'display notification "World" with title "Hello"', - ]; - assertEquals(actual, expected); });