commit
bad7321d4a
5 changed files with 83 additions and 6 deletions
|
@ -1,6 +1,10 @@
|
|||
Changelog for zsh-auto-notify
|
||||
=============================
|
||||
|
||||
0.5.0
|
||||
-----
|
||||
* Support changing notification title and body using AUTO_NOTIFY_TITLE and AUTO_NOTIFY_BODY
|
||||
|
||||
0.4.0
|
||||
-----
|
||||
* Add `AUTO_NOTIFY_EXPIRE_TIME` configuration option
|
||||
|
|
16
README.rst
16
README.rst
|
@ -93,6 +93,22 @@ can configure this value by setting the environment variable ``AUTO_NOTIFY_THRES
|
|||
# Set threshold to 20seconds
|
||||
export AUTO_NOTIFY_THRESHOLD=20
|
||||
|
||||
**Notification Formatting**
|
||||
|
||||
You can change the formatting of notifications by setting the values for ``AUTO_NOTIFY_TITLE`` and
|
||||
``AUTO_NOTIFY_BODY``. When writing these values, the following variables will be replaced according to
|
||||
the data that ``auto-notify`` has detected:
|
||||
|
||||
* ``%command`` - the command that the user executed
|
||||
* ``%elapsed`` - number of seconds that elapsed
|
||||
* ``%exit_code`` - the exit code of the command that was executed
|
||||
|
||||
An example of how these values can be set is shown below:
|
||||
|
||||
::
|
||||
|
||||
export AUTO_NOTIFY_TITLE="Hey! %command has just finished"
|
||||
export AUTO_NOTIFY_BODY="It completed in %elapsed seconds with exit code %exit_code"
|
||||
|
||||
**Notification Expiration Time**
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
export AUTO_NOTIFY_VERSION="0.4.0"
|
||||
export AUTO_NOTIFY_VERSION="0.5.0"
|
||||
|
||||
# Time it takes for a notification to expire
|
||||
export AUTO_NOTIFY_EXPIRE_TIME=8000
|
||||
|
@ -9,26 +9,43 @@ export AUTO_NOTIFY_IGNORE=(
|
|||
"vim" "nvim" "less" "more" "man" "tig" "watch" "git commit" "top" "htop" "ssh" "nano"
|
||||
)
|
||||
|
||||
function _auto_notify_format() {
|
||||
local MESSAGE="$1"
|
||||
local command="$2"
|
||||
local elapsed="$3"
|
||||
local exit_code="$4"
|
||||
MESSAGE="${MESSAGE//\%command/$command}"
|
||||
MESSAGE="${MESSAGE//\%elapsed/$elapsed}"
|
||||
MESSAGE="${MESSAGE//\%exit_code/$exit_code}"
|
||||
printf "%s" "$MESSAGE"
|
||||
}
|
||||
|
||||
function _auto_notify_message() {
|
||||
local command="$1"
|
||||
local elapsed="$2"
|
||||
local exit_code="$3"
|
||||
local platform="$(uname)"
|
||||
# Run using echo -e in order to make sure notify-send picks up new line
|
||||
local title="\"$command\" Completed"
|
||||
local text="$(echo -e "Total time: $elapsed seconds\nExit code: $exit_code")"
|
||||
local DEFAULT_TITLE="\"%command\" Completed"
|
||||
local DEFAULT_BODY="$(echo -e "Total time: %elapsed seconds\nExit code: %exit_code")"
|
||||
|
||||
local title="${AUTO_NOTIFY_TITLE:-$DEFAULT_TITLE}"
|
||||
local text="${AUTO_NOTIFY_BODY:-$DEFAULT_BODY}"
|
||||
|
||||
title="$(_auto_notify_format "$title" "$command" "$elapsed" "$exit_code")"
|
||||
body="$(_auto_notify_format "$text" "$command" "$elapsed" "$exit_code")"
|
||||
|
||||
if [[ "$platform" == "Linux" ]]; then
|
||||
local urgency="normal"
|
||||
if [[ "$exit_code" != "0" ]]; then
|
||||
urgency="critical"
|
||||
fi
|
||||
notify-send "$title" "$text" "--urgency=$urgency" "--expire-time=$AUTO_NOTIFY_EXPIRE_TIME"
|
||||
notify-send "$title" "$body" "--urgency=$urgency" "--expire-time=$AUTO_NOTIFY_EXPIRE_TIME"
|
||||
elif [[ "$platform" == "Darwin" ]]; then
|
||||
# We need to escape quotes since we are passing a script into a command
|
||||
text="${text//\"/\\\"}"
|
||||
body="${body//\"/\\\"}"
|
||||
title="${title//\"/\\\"}"
|
||||
osascript -e "display notification \"$text\" with title \"$title\""
|
||||
osascript -e "display notification \"$body\" with title \"$title\""
|
||||
else
|
||||
printf "Unknown platform for sending notifications: $platform\n"
|
||||
printf "Please post an issue on gitub.com/MichaelAquilina/zsh-auto-notify/issues/\n"
|
||||
|
|
26
tests/test_auto_notify_format.zunit
Normal file
26
tests/test_auto_notify_format.zunit
Normal file
|
@ -0,0 +1,26 @@
|
|||
#!/usr/bin/env zunit
|
||||
|
||||
@setup {
|
||||
load "../auto-notify.plugin.zsh"
|
||||
}
|
||||
|
||||
@test 'auto-notify-format no parameters' {
|
||||
run _auto_notify_format "Hello World" "foo" "bar" "baz"
|
||||
|
||||
assert $state equals 0
|
||||
assert "$output" same_as "Hello World"
|
||||
}
|
||||
|
||||
@test 'auto-notify-format correct formatting 1' {
|
||||
run _auto_notify_format "Command run is - %command (%elapsed seconds)" "zypper up" "10" "0"
|
||||
|
||||
assert $state equals 0
|
||||
assert "$output" same_as "Command run is - zypper up (10 seconds)"
|
||||
}
|
||||
|
||||
@test 'auto-notify-format correct formatting 2' {
|
||||
run _auto_notify_format "Exit code is %exit_code!" "zypper up" "10" "-101"
|
||||
|
||||
assert $state equals 0
|
||||
assert "$output" same_as "Exit code is -101!"
|
||||
}
|
|
@ -126,3 +126,17 @@
|
|||
assert "$lines[1]" same_as "Unknown platform for sending notifications: Hal9000"
|
||||
assert "$lines[2]" same_as "Please post an issue on gitub.com/MichaelAquilina/zsh-auto-notify/issues/"
|
||||
}
|
||||
|
||||
@test 'auto-notify-send sends custom message' {
|
||||
AUTO_COMMAND="doom -i"
|
||||
AUTO_COMMAND_FULL="doom -i"
|
||||
AUTO_COMMAND_START=11055
|
||||
AUTO_NOTIFY_TITLE="%command has completed in %elapseds yo"
|
||||
AUTO_NOTIFY_BODY="%command exited with code %exit_code"
|
||||
|
||||
run _auto_notify_send
|
||||
|
||||
assert $state equals 0
|
||||
assert "$lines[1]" same_as 'Notification Title: doom -i has completed in 45s yo'
|
||||
assert "$lines[2]" same_as "Notification Body: doom -i exited with code 0"
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue