plugin/cmd-returned-notify: Rewrite to match/use `lib/command_duration`
Use `$EPOCHREALTIME` (or `$SECONDS`) built-in variable provided by Bash instead of `date +%s`. We're only measuing the difference in seconds, so avoid both the binary invocation as well as the subshell. Alsö, Reduce environmental pollution by not exporting every variable, and unsetting when done. Change variable names to match lib/command-duration Remove `preexec_return_notification()` in favor of `lib/command-duration`'s `_command_duration_pre_exec()`. This should now use the same preexec hook and variables as the theme library `command_duration`. tests: handle nanosecondspull/1906/head
parent
33505d4db1
commit
6ca10cf84c
|
|
@ -2,15 +2,15 @@
|
|||
cite about-plugin
|
||||
about-plugin 'Alert (BEL) when process ends after a threshold of seconds'
|
||||
|
||||
precmd_return_notification() {
|
||||
export LAST_COMMAND_DURATION=$(($(date +%s) - ${LAST_COMMAND_TIME:=$(date +%s)}))
|
||||
[[ ${LAST_COMMAND_DURATION} -gt ${NOTIFY_IF_COMMAND_RETURNS_AFTER:-5} ]] && echo -e "\a"
|
||||
export LAST_COMMAND_TIME=
|
||||
function precmd_return_notification() {
|
||||
local command_start="${COMMAND_DURATION_START_SECONDS:=0}"
|
||||
local current_time="${EPOCHREALTIME:-$SECONDS}"
|
||||
local -i command_duration="$((${current_time%.*} - ${command_start%.*}))"
|
||||
if [[ "${command_duration}" -gt "${NOTIFY_IF_COMMAND_RETURNS_AFTER:-5}" ]]; then
|
||||
printf '\a'
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
preexec_return_notification() {
|
||||
[[ -z "${LAST_COMMAND_TIME}" ]] && LAST_COMMAND_TIME=$(date +%s)
|
||||
}
|
||||
|
||||
precmd_functions+=(precmd_return_notification)
|
||||
preexec_functions+=(preexec_return_notification)
|
||||
safe_append_prompt_command 'precmd_return_notification'
|
||||
safe_append_preexec '_command_duration_pre_exec'
|
||||
|
|
|
|||
|
|
@ -4,12 +4,13 @@ load "${MAIN_BASH_IT_DIR?}/test/test_helper.bash"
|
|||
|
||||
function local_setup_file() {
|
||||
setup_libs "preexec" #"command_duration"
|
||||
load ../../themes/command_duration.theme
|
||||
load "${BASH_IT?}/plugins/available/cmd-returned-notify.plugin.bash"
|
||||
}
|
||||
|
||||
@test "plugins cmd-returned-notify: notify after elapsed time" {
|
||||
export NOTIFY_IF_COMMAND_RETURNS_AFTER=0
|
||||
export LAST_COMMAND_TIME=$(date +%s)
|
||||
export COMMAND_DURATION_START_SECONDS="${EPOCHREALTIME:-$SECONDS}"
|
||||
sleep 1
|
||||
run precmd_return_notification
|
||||
assert_success
|
||||
|
|
@ -18,7 +19,7 @@ function local_setup_file() {
|
|||
|
||||
@test "plugins cmd-returned-notify: do not notify before elapsed time" {
|
||||
export NOTIFY_IF_COMMAND_RETURNS_AFTER=10
|
||||
export LAST_COMMAND_TIME=$(date +%s)
|
||||
export COMMAND_DURATION_START_SECONDS="${EPOCHREALTIME:-$SECONDS}"
|
||||
sleep 1
|
||||
run precmd_return_notification
|
||||
assert_success
|
||||
|
|
@ -26,23 +27,25 @@ function local_setup_file() {
|
|||
}
|
||||
|
||||
@test "plugins cmd-returned-notify: preexec no output" {
|
||||
export LAST_COMMAND_TIME=
|
||||
run preexec_return_notification
|
||||
export COMMAND_DURATION_START_SECONDS=
|
||||
run _command_duration_pre_exec
|
||||
assert_success
|
||||
assert_output ""
|
||||
}
|
||||
|
||||
@test "plugins cmd-returned-notify: preexec no output env set" {
|
||||
export LAST_COMMAND_TIME=$(date +%s)
|
||||
run preexec_return_notification
|
||||
skip "wut"
|
||||
export COMMAND_DURATION_START_SECONDS="${EPOCHREALTIME:-$SECONDS}"
|
||||
run _command_duration_pre_exec
|
||||
assert_failure
|
||||
assert_output ""
|
||||
}
|
||||
|
||||
@test "plugins cmd-returned-notify: preexec set LAST_COMMAND_TIME" {
|
||||
export LAST_COMMAND_TIME=
|
||||
assert_equal "${LAST_COMMAND_TIME}" ""
|
||||
NOW=$(date +%s)
|
||||
preexec_return_notification
|
||||
assert_equal "${LAST_COMMAND_TIME}" "${NOW}"
|
||||
@test "plugins cmd-returned-notify: preexec set COMMAND_DURATION_START_SECONDS" {
|
||||
export COMMAND_DURATION_START_SECONDS=
|
||||
assert_equal "${COMMAND_DURATION_START_SECONDS}" ""
|
||||
NOW="${EPOCHREALTIME:-$SECONDS}"
|
||||
_command_duration_pre_exec
|
||||
# We need to make sure to account for nanoseconds...
|
||||
assert_equal "${COMMAND_DURATION_START_SECONDS%.*}" "${NOW%.*}"
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue