Skip to content

Commit

Permalink
Add option to make sound
Browse files Browse the repository at this point in the history
  • Loading branch information
roeniss committed Mar 17, 2024
1 parent f4766d6 commit fc0e16d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
14 changes: 14 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----------------------------------

Expand Down
22 changes: 17 additions & 5 deletions auto-notify.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
42 changes: 42 additions & 0 deletions tests/test_auto_notify_send.zunit
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
echo - "${@:3}"
}

function paplay {
echo - "paplay $1"
}

function uname {
echo - "Linux"
}
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down

0 comments on commit fc0e16d

Please sign in to comment.