Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support sound option(macOS and Linux) #9

Merged
merged 2 commits into from
Aug 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
```

Expand Down
1 change: 1 addition & 0 deletions notifiers/notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export interface Notification {
title: string;
message: string;
icon?: string;
sound?: string;
}
5 changes: 4 additions & 1 deletion notifiers/notify_send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
16 changes: 16 additions & 0 deletions notifiers/notify_send_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
7 changes: 6 additions & 1 deletion notifiers/osascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
}
42 changes: 30 additions & 12 deletions notifiers/osascript_test.ts
Original file line number Diff line number Diff line change
@@ -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);
});