From 8b31cb43709e91e57c5f2df77ec963c68f8ec13b Mon Sep 17 00:00:00 2001 From: skanehira Date: Fri, 12 Aug 2022 04:27:50 +0900 Subject: [PATCH 1/2] feat: Support sound option(macOS and Linux) --- README.md | 1 + notifiers/notifier.ts | 1 + notifiers/notify_send.ts | 5 ++++- notifiers/notify_send_test.ts | 16 +++++++++++++ notifiers/osascript.ts | 7 +++++- notifiers/osascript_test.ts | 42 +++++++++++++++++++++++++---------- 6 files changed, 58 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 5499ba6..7acb0ab 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); }); From 466f3f87fdf5b1e17885466c6e49ce86072e4ffc Mon Sep 17 00:00:00 2001 From: skanehira Date: Sat, 13 Aug 2022 00:12:17 +0900 Subject: [PATCH 2/2] fix: deno fmt error --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7acb0ab..b4a2fc8 100644 --- a/README.md +++ b/README.md @@ -28,7 +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 + sound: "device-added", // only support Linux and macOS }); ```