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
|
||||
#
|
||||
# Functions for measuring and reporting how long a command takes to run.
|
||||
|
||||
if [[ "${BASH_IT_COMMAND_DURATION:-false}" != true ]]; then
|
||||
_command_duration() {
|
||||
echo -n
|
||||
}
|
||||
return
|
||||
fi
|
||||
: "${COMMAND_DURATION_START_SECONDS:=${EPOCHREALTIME:-$SECONDS}}"
|
||||
: "${COMMAND_DURATION_ICON:=🕘}"
|
||||
: "${COMMAND_DURATION_MIN_SECONDS:=1}"
|
||||
|
||||
COMMAND_DURATION_START_TIME=
|
||||
|
||||
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 _command_duration_pre_exec() {
|
||||
COMMAND_DURATION_START_SECONDS="${EPOCHREALTIME:-$SECONDS}"
|
||||
}
|
||||
|
||||
function _dynamic_clock_icon {
|
||||
|
|
@ -23,43 +15,37 @@ function _dynamic_clock_icon {
|
|||
printf -v 'COMMAND_DURATION_ICON' '%b' "\xf0\x9f\x95\x$clock_hand"
|
||||
}
|
||||
|
||||
_command_duration() {
|
||||
local command_duration command_start current_time
|
||||
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}"
|
||||
function _command_duration() {
|
||||
[[ -n "${BASH_IT_COMMAND_DURATION:-}" ]] || return
|
||||
|
||||
if [[ -n "${COMMAND_DURATION_START_TIME:-}" ]]; then
|
||||
_bash_it_log_section="command_duration" _log_debug "calculating start time"
|
||||
command_start_sseconds=${COMMAND_DURATION_START_TIME%.*}
|
||||
current_time_seconds=${current_time%.*}
|
||||
|
||||
command_start_deciseconds=$((10#${command_start#*.}))
|
||||
current_time_deciseconds=$((10#${current_time#*.}))
|
||||
local command_duration=0 command_start="${COMMAND_DURATION_START_SECONDS:-0}"
|
||||
local -i minutes=0 seconds=0 deciseconds=0
|
||||
local -i command_start_seconds="${command_start%.*}"
|
||||
local -i command_start_deciseconds=$((10#${command_start##*.}))
|
||||
local current_time="${EPOCHREALTIME:-$SECONDS}"
|
||||
local -i current_time_seconds="${current_time%.*}"
|
||||
local -i current_time_deciseconds="$((10#${current_time##*.}))"
|
||||
|
||||
if [[ "${command_start_seconds:-0}" -gt 0 ]]; then
|
||||
# seconds
|
||||
command_duration=$((current_time_seconds - command_start_sseconds))
|
||||
_bash_it_log_section="command_duration" _log_debug "duration: $command_duration (from $COMMAND_DURATION_START_TIME to $current_time)"
|
||||
command_duration="$((current_time_seconds - command_start_seconds))"
|
||||
|
||||
if ((current_time_deciseconds >= command_start_deciseconds)); then
|
||||
deciseconds=$(((current_time_deciseconds - command_start_deciseconds)))
|
||||
deciseconds="$((current_time_deciseconds - command_start_deciseconds))"
|
||||
else
|
||||
((command_duration -= 1))
|
||||
deciseconds=$((10 - ((command_start_deciseconds - current_time_deciseconds))))
|
||||
deciseconds="$((10 - (command_start_deciseconds - current_time_deciseconds)))"
|
||||
fi
|
||||
else
|
||||
command_duration=0
|
||||
fi
|
||||
|
||||
if ((command_duration > 0)); then
|
||||
_bash_it_log_section="command_duration" _log_debug "calculating minutes and seconds"
|
||||
minutes=$((command_duration / 60))
|
||||
seconds=$((command_duration % 60))
|
||||
fi
|
||||
|
||||
_dynamic_clock_icon
|
||||
_dynamic_clock_icon "${command_duration}"
|
||||
if ((minutes > 0)); then
|
||||
printf "%s%s%dm %ds" "${COMMAND_DURATION_ICON:-}" "${COMMAND_DURATION_COLOR:-}" "$minutes" "$seconds"
|
||||
elif ((seconds >= COMMAND_DURATION_MIN_SECONDS)); then
|
||||
|
|
@ -67,4 +53,4 @@ _command_duration() {
|
|||
fi
|
||||
}
|
||||
|
||||
preexec_functions+=(_command_duration_pre_exec)
|
||||
safe_append_preexec '_command_duration_pre_exec'
|
||||
|
|
|
|||
Loading…
Reference in New Issue