Improve tracking code and add more tests
This commit is contained in:
parent
f480c50990
commit
bf73292fd0
2 changed files with 31 additions and 13 deletions
|
@ -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
|
||||
|
||||
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
|
||||
if [[ $elapsed -gt $AUTO_NOTIFY_THRESHOLD ]]; then
|
||||
_auto_notify_message "$AUTO_COMMAND" "$elapsed"
|
||||
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() {
|
||||
|
@ -83,4 +89,5 @@ function enable_auto_notify() {
|
|||
|
||||
autoload -Uz add-zsh-hook
|
||||
|
||||
_reset_tracking
|
||||
enable_auto_notify
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Add table
Reference in a new issue