Add support for custom message formats
This commit is contained in:
parent
eaf5936f1e
commit
aed7176e73
4 changed files with 77 additions and 5 deletions
15
README.rst
15
README.rst
|
@ -93,6 +93,21 @@ can configure this value by setting the environment variable ``AUTO_NOTIFY_THRES
|
||||||
# Set threshold to 20seconds
|
# Set threshold to 20seconds
|
||||||
export AUTO_NOTIFY_THRESHOLD=20
|
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**
|
**Notification Expiration Time**
|
||||||
|
|
||||||
|
|
|
@ -9,26 +9,43 @@ export AUTO_NOTIFY_IGNORE=(
|
||||||
"vim" "nvim" "less" "more" "man" "tig" "watch" "git commit" "top" "htop" "ssh" "nano"
|
"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() {
|
function _auto_notify_message() {
|
||||||
local command="$1"
|
local command="$1"
|
||||||
local elapsed="$2"
|
local elapsed="$2"
|
||||||
local exit_code="$3"
|
local exit_code="$3"
|
||||||
local platform="$(uname)"
|
local platform="$(uname)"
|
||||||
# Run using echo -e in order to make sure notify-send picks up new line
|
# Run using echo -e in order to make sure notify-send picks up new line
|
||||||
local title="\"$command\" Completed"
|
local DEFAULT_TITLE="\"%command\" Completed"
|
||||||
local text="$(echo -e "Total time: $elapsed seconds\nExit code: $exit_code")"
|
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
|
if [[ "$platform" == "Linux" ]]; then
|
||||||
local urgency="normal"
|
local urgency="normal"
|
||||||
if [[ "$exit_code" != "0" ]]; then
|
if [[ "$exit_code" != "0" ]]; then
|
||||||
urgency="critical"
|
urgency="critical"
|
||||||
fi
|
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
|
elif [[ "$platform" == "Darwin" ]]; then
|
||||||
# We need to escape quotes since we are passing a script into a command
|
# We need to escape quotes since we are passing a script into a command
|
||||||
text="${text//\"/\\\"}"
|
body="${body//\"/\\\"}"
|
||||||
title="${title//\"/\\\"}"
|
title="${title//\"/\\\"}"
|
||||||
osascript -e "display notification \"$text\" with title \"$title\""
|
osascript -e "display notification \"$body\" with title \"$title\""
|
||||||
else
|
else
|
||||||
printf "Unknown platform for sending notifications: $platform\n"
|
printf "Unknown platform for sending notifications: $platform\n"
|
||||||
printf "Please post an issue on gitub.com/MichaelAquilina/zsh-auto-notify/issues/\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[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/"
|
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