This commit is contained in:
Roeniss Moon 2024-03-23 23:23:57 +09:00 committed by GitHub
commit e8df9c2176
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 71 additions and 5 deletions

View file

@ -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
-----------------------------------

View file

@ -57,12 +57,22 @@ 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"
notification_command="display notification (item 1 of argv) with title (item 2 of argv)"
notification_body=($body $title)
if [[ ! -z "$AUTO_NOTIFY_SOUND" ]]; then
notification_command+=" sound name (item 3 of argv)"
notification_body+=("$AUTO_NOTIFY_SOUND")
fi
osascript \
-e 'on run argv' \
-e ${notification_command[@]} \
-e 'end run' \
${notification_body[@]}
else
printf "Unknown platform for sending notifications: $platform\n"
printf "Please post an issue on gitub.com/MichaelAquilina/zsh-auto-notify/issues/\n"

View file

@ -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"