From fc0e16d0f259d2c5a7316b995bd5850b00c864a8 Mon Sep 17 00:00:00 2001 From: Roeniss Moon Date: Sun, 17 Mar 2024 22:47:08 +0900 Subject: [PATCH] Add option to make sound --- README.rst | 14 +++++++++++ auto-notify.plugin.zsh | 22 ++++++++++++---- tests/test_auto_notify_send.zunit | 42 +++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index 70ea746..b2bb118 100644 --- a/README.rst +++ b/README.rst @@ -162,6 +162,20 @@ then all the values in ``AUTO_NOTIFY_IGNORE`` are not used. export AUTO_NOTIFY_WHITELIST=("apt-get" "docker") +**Notification Sound** + +You can play sound for the notification by setting the environment variable ``AUTO_NOTIFY_SOUND``. +For Linux, you can use any sound with absolute path. For instance, Ubuntu 22.04 provides some in /usr/share/sounds. +For macOS, you can use filename of the sound in /System/Library/Sounds directory. +Notice that the letter case is important and extension is skipped. + +:: + + # example for macOS + export AUTO_NOTIFY_SOUND=Frog + # example for Linux + export AUTO_NOTIFY_SOUND=/usr/share/sounds/Yaru/stereo/bell.oga + Temporarily Disabling Notifications ----------------------------------- diff --git a/auto-notify.plugin.zsh b/auto-notify.plugin.zsh index 64174b9..71b1f2c 100644 --- a/auto-notify.plugin.zsh +++ b/auto-notify.plugin.zsh @@ -57,12 +57,24 @@ function _auto_notify_message() { transient="" fi notify-send "$title" "$body" --app-name=zsh $transient "--urgency=$urgency" "--expire-time=$AUTO_NOTIFY_EXPIRE_TIME" + if [[ -n "$AUTO_NOTIFY_SOUND" ]]; then + paplay "$AUTO_NOTIFY_SOUND" + fi + elif [[ "$platform" == "Darwin" ]]; then - osascript \ - -e 'on run argv' \ - -e 'display notification (item 1 of argv) with title (item 2 of argv)' \ - -e 'end run' \ - "$body" "$title" + if [[ -z "$AUTO_NOTIFY_SOUND" ]]; then + osascript \ + -e 'on run argv' \ + -e 'display notification (item 1 of argv) with title (item 2 of argv)' \ + -e 'end run' \ + "$body" "$title" + else + osascript \ + -e 'on run argv' \ + -e 'display notification (item 1 of argv) with title (item 2 of argv) sound name (item 3 of argv)' \ + -e 'end run' \ + "$body" "$title" "$AUTO_NOTIFY_SOUND" + fi else printf "Unknown platform for sending notifications: $platform\n" printf "Please post an issue on gitub.com/MichaelAquilina/zsh-auto-notify/issues/\n" diff --git a/tests/test_auto_notify_send.zunit b/tests/test_auto_notify_send.zunit index 4748b2b..114d3af 100644 --- a/tests/test_auto_notify_send.zunit +++ b/tests/test_auto_notify_send.zunit @@ -9,6 +9,10 @@ echo - "${@:3}" } + function paplay { + echo - "paplay $1" + } + function uname { echo - "Linux" } @@ -92,6 +96,23 @@ assert "$lines[4]" same_as "--app-name=zsh --hint=int:transient:1 --urgency=normal --expire-time=15000" } +@test 'auto-notify-send sends notification on Linux with sound' { + AUTO_COMMAND="f bar -r" + AUTO_COMMAND_FULL="foo bar -r" + AUTO_COMMAND_START=11080 + AUTO_NOTIFY_EXPIRE_TIME=15000 + AUTO_NOTIFY_SOUND=/usr/share/sounds/Yaru/stereo/bell.oga + + run _auto_notify_send + + assert $state equals 0 + assert "$lines[1]" same_as 'Notification Title: "f bar -r" Completed' + assert "$lines[2]" same_as "Notification Body: Total time: 20 seconds" + assert "$lines[3]" same_as "Exit code: 0" + assert "$lines[4]" same_as "--app-name=zsh --hint=int:transient:1 --urgency=normal --expire-time=15000" + assert "$lines[5]" same_as "paplay /usr/share/sounds/Yaru/stereo/bell.oga" +} + @test 'auto-notify-send sends notification on macOS' { AUTO_COMMAND="f bar -r" AUTO_COMMAND_FULL="foo bar -r" @@ -112,6 +133,27 @@ assert "$lines[2]" same_as 'Exit code: 0 "f bar -r" Completed' } +@test 'auto-notify-send sends notification on macOS with sound' { + AUTO_COMMAND="f bar -r" + AUTO_COMMAND_FULL="foo bar -r" + AUTO_COMMAND_START=11080 + AUTO_NOTIFY_SOUND=Frog + + function uname { + echo - "Darwin" + } + + function osascript { + echo - "${@}" + } + + run _auto_notify_send + + assert $state equals 0 + assert "$lines[1]" same_as '-e on run argv -e display notification (item 1 of argv) with title (item 2 of argv) sound name (item 3 of argv) -e end run Total time: 20 seconds' + assert "$lines[2]" same_as 'Exit code: 0 "f bar -r" Completed Frog' +} + @test 'auto-notify-send sends warning on unsupported platform' { AUTO_COMMAND="f bar -r" AUTO_COMMAND_FULL="foo bar -r"