Improve tracking code and add more tests

This commit is contained in:
Michael Aquilina 2019-07-17 21:36:30 +01:00
parent f480c50990
commit bf73292fd0
No known key found for this signature in database
GPG key ID: 636066730B056BD1
2 changed files with 31 additions and 13 deletions

View file

@ -1,11 +1,5 @@
export AUTO_NOTIFY_VERSION="0.1.0" export AUTO_NOTIFY_VERSION="0.1.0"
# Command that the user has executed
AUTO_COMMAND=""
# Full command that the user has executed after alias expansion
AUTO_COMMAND_FULL=""
# Command start time in seconds since epoch
AUTO_COMMAND_START=0
# Threshold in seconds for when to automatically show a notification # Threshold in seconds for when to automatically show a notification
export AUTO_NOTIFY_THRESHOLD=10 export AUTO_NOTIFY_THRESHOLD=10
# List of commands/programs to ignore sending notifications for # List of commands/programs to ignore sending notifications for
@ -13,6 +7,14 @@ 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 _reset_tracking() {
# Command start time in seconds since epoch
unset AUTO_COMMAND_START
# Full command that the user has executed after alias expansion
unset AUTO_COMMAND_FULL
# Command that the user has executed
unset AUTO_COMMAND
}
function _auto_notify_message() { function _auto_notify_message() {
local command="$1" local command="$1"
@ -51,18 +53,22 @@ function _is_auto_notify_ignored() {
} }
function _auto_notify_send() { function _auto_notify_send() {
local current="$(date +"%s")" if [[ -z "$AUTO_COMMAND" && -z "$AUTO_COMMAND_START" ]]; then
if [[ "$(_is_auto_notify_ignored "$AUTO_COMMAND_FULL")" == "yes" ]]; then
return return
fi fi
if [[ "$(_is_auto_notify_ignored "$AUTO_COMMAND_FULL")" == "no" ]]; then
local current="$(date +"%s")"
let "elapsed = current - AUTO_COMMAND_START" let "elapsed = current - AUTO_COMMAND_START"
if [[ -n "$AUTO_COMMAND" && $elapsed -gt $AUTO_NOTIFY_THRESHOLD ]]; then if [[ $elapsed -gt $AUTO_NOTIFY_THRESHOLD ]]; then
_auto_notify_message "$AUTO_COMMAND" "$elapsed" _auto_notify_message "$AUTO_COMMAND" "$elapsed"
fi fi
AUTO_COMMAND="" fi
# Empty tracking so that notifications are not
# triggered for any commands not run (e.g ctrl+C when typing)
_reset_tracking
} }
function _auto_notify_track() { function _auto_notify_track() {
@ -83,4 +89,5 @@ function enable_auto_notify() {
autoload -Uz add-zsh-hook autoload -Uz add-zsh-hook
_reset_tracking
enable_auto_notify enable_auto_notify

View file

@ -44,6 +44,17 @@
assert '_auto_notify_send' in $precmd_functions assert '_auto_notify_send' in $precmd_functions
} }
@test 'auto-notify-send does not send notification if tracking not set' {
unset AUTO_COMMAND
unset AUTO_COMMAND_FULL
unset AUTO_COMMAND_START
run _auto_notify_send
assert $state equals 0
assert "$output" is_empty
}
@test 'auto-notify-send does not send notification for short task' { @test 'auto-notify-send does not send notification for short task' {
AUTO_COMMAND="foo bar -r" AUTO_COMMAND="foo bar -r"
AUTO_COMMAND_FULL="foo bar -r" AUTO_COMMAND_FULL="foo bar -r"