lib/command_duration: Refactor using `$EPOCHREALTIME`
Fallback to `$SECONDS` for older versions of _Bash_. Instead of shortcircuiting the definition, just short-circuit the function. This allows the variable to be set later, e.g. on theme change.pull/1906/head
parent
09e8c25b64
commit
33505d4db1
|
|
@ -1,21 +1,13 @@
|
||||||
# shellcheck shell=bash
|
# shellcheck shell=bash
|
||||||
|
#
|
||||||
|
# Functions for measuring and reporting how long a command takes to run.
|
||||||
|
|
||||||
if [[ "${BASH_IT_COMMAND_DURATION:-false}" != true ]]; then
|
: "${COMMAND_DURATION_START_SECONDS:=${EPOCHREALTIME:-$SECONDS}}"
|
||||||
_command_duration() {
|
: "${COMMAND_DURATION_ICON:=🕘}"
|
||||||
echo -n
|
: "${COMMAND_DURATION_MIN_SECONDS:=1}"
|
||||||
}
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
|
|
||||||
COMMAND_DURATION_START_TIME=
|
function _command_duration_pre_exec() {
|
||||||
|
COMMAND_DURATION_START_SECONDS="${EPOCHREALTIME:-$SECONDS}"
|
||||||
COMMAND_DURATION_ICON=${COMMAND_DURATION_ICON:-' '} 🕘
|
|
||||||
COMMAND_DURATION_MIN_SECONDS=${COMMAND_DURATION_MIN_SECONDS:-'1'}
|
|
||||||
|
|
||||||
_command_duration_pre_exec() {
|
|
||||||
local command_nano_now="$(date +%1N)"
|
|
||||||
[[ "$command_nano_now" == "1N" ]] && command_nano_now=1
|
|
||||||
COMMAND_DURATION_START_TIME="$(date "+%s").${command_nano_now}"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function _dynamic_clock_icon {
|
function _dynamic_clock_icon {
|
||||||
|
|
@ -23,43 +15,37 @@ function _dynamic_clock_icon {
|
||||||
printf -v 'COMMAND_DURATION_ICON' '%b' "\xf0\x9f\x95\x$clock_hand"
|
printf -v 'COMMAND_DURATION_ICON' '%b' "\xf0\x9f\x95\x$clock_hand"
|
||||||
}
|
}
|
||||||
|
|
||||||
_command_duration() {
|
function _command_duration() {
|
||||||
local command_duration command_start current_time
|
[[ -n "${BASH_IT_COMMAND_DURATION:-}" ]] || return
|
||||||
local minutes seconds deciseconds
|
|
||||||
local command_start_sseconds current_time_seconds command_start_deciseconds current_time_deciseconds
|
|
||||||
local command_nano_now="$(date +%1N)"
|
|
||||||
[[ "$command_nano_now" == "1N" ]] && command_nano_now=1
|
|
||||||
current_time="$(date "+%s").${command_nano_now}"
|
|
||||||
|
|
||||||
if [[ -n "${COMMAND_DURATION_START_TIME:-}" ]]; then
|
local command_duration=0 command_start="${COMMAND_DURATION_START_SECONDS:-0}"
|
||||||
_bash_it_log_section="command_duration" _log_debug "calculating start time"
|
local -i minutes=0 seconds=0 deciseconds=0
|
||||||
command_start_sseconds=${COMMAND_DURATION_START_TIME%.*}
|
local -i command_start_seconds="${command_start%.*}"
|
||||||
current_time_seconds=${current_time%.*}
|
local -i command_start_deciseconds=$((10#${command_start##*.}))
|
||||||
|
local current_time="${EPOCHREALTIME:-$SECONDS}"
|
||||||
command_start_deciseconds=$((10#${command_start#*.}))
|
local -i current_time_seconds="${current_time%.*}"
|
||||||
current_time_deciseconds=$((10#${current_time#*.}))
|
local -i current_time_deciseconds="$((10#${current_time##*.}))"
|
||||||
|
|
||||||
|
if [[ "${command_start_seconds:-0}" -gt 0 ]]; then
|
||||||
# seconds
|
# seconds
|
||||||
command_duration=$((current_time_seconds - command_start_sseconds))
|
command_duration="$((current_time_seconds - command_start_seconds))"
|
||||||
_bash_it_log_section="command_duration" _log_debug "duration: $command_duration (from $COMMAND_DURATION_START_TIME to $current_time)"
|
|
||||||
|
|
||||||
if ((current_time_deciseconds >= command_start_deciseconds)); then
|
if ((current_time_deciseconds >= command_start_deciseconds)); then
|
||||||
deciseconds=$(((current_time_deciseconds - command_start_deciseconds)))
|
deciseconds="$((current_time_deciseconds - command_start_deciseconds))"
|
||||||
else
|
else
|
||||||
((command_duration -= 1))
|
((command_duration -= 1))
|
||||||
deciseconds=$((10 - ((command_start_deciseconds - current_time_deciseconds))))
|
deciseconds="$((10 - (command_start_deciseconds - current_time_deciseconds)))"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
command_duration=0
|
command_duration=0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ((command_duration > 0)); then
|
if ((command_duration > 0)); then
|
||||||
_bash_it_log_section="command_duration" _log_debug "calculating minutes and seconds"
|
|
||||||
minutes=$((command_duration / 60))
|
minutes=$((command_duration / 60))
|
||||||
seconds=$((command_duration % 60))
|
seconds=$((command_duration % 60))
|
||||||
fi
|
fi
|
||||||
|
|
||||||
_dynamic_clock_icon
|
_dynamic_clock_icon "${command_duration}"
|
||||||
if ((minutes > 0)); then
|
if ((minutes > 0)); then
|
||||||
printf "%s%s%dm %ds" "${COMMAND_DURATION_ICON:-}" "${COMMAND_DURATION_COLOR:-}" "$minutes" "$seconds"
|
printf "%s%s%dm %ds" "${COMMAND_DURATION_ICON:-}" "${COMMAND_DURATION_COLOR:-}" "$minutes" "$seconds"
|
||||||
elif ((seconds >= COMMAND_DURATION_MIN_SECONDS)); then
|
elif ((seconds >= COMMAND_DURATION_MIN_SECONDS)); then
|
||||||
|
|
@ -67,4 +53,4 @@ _command_duration() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
preexec_functions+=(_command_duration_pre_exec)
|
safe_append_preexec '_command_duration_pre_exec'
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue