From bf73292fd010298b9c12e0d5d6a112dd3a29b539 Mon Sep 17 00:00:00 2001 From: Michael Aquilina Date: Wed, 17 Jul 2019 21:36:30 +0100 Subject: [PATCH] Improve tracking code and add more tests --- auto-notify.plugin.zsh | 33 ++++++++++++++++++++------------- tests/test_auto_notify.zunit | 11 +++++++++++ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/auto-notify.plugin.zsh b/auto-notify.plugin.zsh index debb707..868c4f5 100644 --- a/auto-notify.plugin.zsh +++ b/auto-notify.plugin.zsh @@ -1,11 +1,5 @@ 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 export AUTO_NOTIFY_THRESHOLD=10 # 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" ) +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() { local command="$1" @@ -51,18 +53,22 @@ function _is_auto_notify_ignored() { } function _auto_notify_send() { - local current="$(date +"%s")" - - if [[ "$(_is_auto_notify_ignored "$AUTO_COMMAND_FULL")" == "yes" ]]; then + if [[ -z "$AUTO_COMMAND" && -z "$AUTO_COMMAND_START" ]]; then return fi - let "elapsed = current - AUTO_COMMAND_START" + if [[ "$(_is_auto_notify_ignored "$AUTO_COMMAND_FULL")" == "no" ]]; then + local current="$(date +"%s")" + let "elapsed = current - AUTO_COMMAND_START" - if [[ -n "$AUTO_COMMAND" && $elapsed -gt $AUTO_NOTIFY_THRESHOLD ]]; then - _auto_notify_message "$AUTO_COMMAND" "$elapsed" + if [[ $elapsed -gt $AUTO_NOTIFY_THRESHOLD ]]; then + _auto_notify_message "$AUTO_COMMAND" "$elapsed" + fi fi - AUTO_COMMAND="" + + # 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() { @@ -83,4 +89,5 @@ function enable_auto_notify() { autoload -Uz add-zsh-hook +_reset_tracking enable_auto_notify diff --git a/tests/test_auto_notify.zunit b/tests/test_auto_notify.zunit index bd99aff..ab9c4b9 100644 --- a/tests/test_auto_notify.zunit +++ b/tests/test_auto_notify.zunit @@ -44,6 +44,17 @@ 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' { AUTO_COMMAND="foo bar -r" AUTO_COMMAND_FULL="foo bar -r"