bug: Use en_US when fetching EPOCHREALTIME

Isolates fetching of EPOCHREALTIME to a function which sets LC_ALL=en_US.UTF-8.
This ensures that the value is in decimal format, regardless of runtime locale.

bug: Hide duration when no command executed
pull/2172/head
David Farrell 2022-10-26 15:23:11 -07:00
parent 1c9cfd056b
commit 7c7e4f90ec
No known key found for this signature in database
GPG Key ID: 1CCA28D0E300B56F
3 changed files with 23 additions and 7 deletions

View File

@ -2,12 +2,24 @@
# #
# Functions for measuring and reporting how long a command takes to run. # Functions for measuring and reporting how long a command takes to run.
: "${COMMAND_DURATION_START_SECONDS:=${EPOCHREALTIME:-$SECONDS}}" # Get shell duration in decimal format regardless of runtime locale.
# Notice: This function runs as a sub-shell - notice '(' vs '{'.
function _shell_duration_en() (
# DFARREL You would think LC_NUMERIC would do it, but not working in my local
LC_ALL='en_US.UTF-8'
printf "%s" "${EPOCHREALTIME:-$SECONDS}"
)
: "${COMMAND_DURATION_START_SECONDS:=$(_shell_duration_en)}"
: "${COMMAND_DURATION_ICON:=🕘}" : "${COMMAND_DURATION_ICON:=🕘}"
: "${COMMAND_DURATION_MIN_SECONDS:=1}" : "${COMMAND_DURATION_MIN_SECONDS:=1}"
function _command_duration_pre_exec() { function _command_duration_pre_exec() {
COMMAND_DURATION_START_SECONDS="${EPOCHREALTIME:-$SECONDS}" COMMAND_DURATION_START_SECONDS="$(_shell_duration_en)"
}
function _command_duration_pre_cmd() {
COMMAND_DURATION_START_SECONDS=""
} }
function _dynamic_clock_icon { function _dynamic_clock_icon {
@ -20,13 +32,15 @@ function _dynamic_clock_icon {
function _command_duration() { function _command_duration() {
[[ -n "${BASH_IT_COMMAND_DURATION:-}" ]] || return [[ -n "${BASH_IT_COMMAND_DURATION:-}" ]] || return
[[ -n "${COMMAND_DURATION_START_SECONDS:-}" ]] || return
local command_duration=0 command_start="${COMMAND_DURATION_START_SECONDS:-0}" local command_duration=0 command_start="${COMMAND_DURATION_START_SECONDS:-0}"
local -i minutes=0 seconds=0 deciseconds=0 local -i minutes=0 seconds=0 deciseconds=0
local -i command_start_seconds="${command_start%.*}" local -i command_start_seconds="${command_start%.*}"
local -i command_start_deciseconds=$((10#${command_start##*.})) local -i command_start_deciseconds=$((10#${command_start##*.}))
command_start_deciseconds="${command_start_deciseconds:0:1}" command_start_deciseconds="${command_start_deciseconds:0:1}"
local current_time="${EPOCHREALTIME:-$SECONDS}" local current_time
current_time="$(_shell_duration_en)"
local -i current_time_seconds="${current_time%.*}" local -i current_time_seconds="${current_time%.*}"
local -i current_time_deciseconds="$((10#${current_time##*.}))" local -i current_time_deciseconds="$((10#${current_time##*.}))"
current_time_deciseconds="${current_time_deciseconds:0:1}" current_time_deciseconds="${current_time_deciseconds:0:1}"
@ -59,3 +73,4 @@ function _command_duration() {
} }
_bash_it_library_finalize_hook+=("safe_append_preexec '_command_duration_pre_exec'") _bash_it_library_finalize_hook+=("safe_append_preexec '_command_duration_pre_exec'")
_bash_it_library_finalize_hook+=("safe_append_prompt_command '_command_duration_pre_cmd'")

View File

@ -4,7 +4,8 @@ about-plugin 'Alert (BEL) when process ends after a threshold of seconds'
function precmd_return_notification() { function precmd_return_notification() {
local command_start="${COMMAND_DURATION_START_SECONDS:=0}" local command_start="${COMMAND_DURATION_START_SECONDS:=0}"
local current_time="${EPOCHREALTIME:-$SECONDS}" local current_time
current_time="$(_shell_duration_en)"
local -i command_duration="$((${current_time%.*} - ${command_start%.*}))" local -i command_duration="$((${current_time%.*} - ${command_start%.*}))"
if [[ "${command_duration}" -gt "${NOTIFY_IF_COMMAND_RETURNS_AFTER:-5}" ]]; then if [[ "${command_duration}" -gt "${NOTIFY_IF_COMMAND_RETURNS_AFTER:-5}" ]]; then
printf '\a' printf '\a'

View File

@ -9,7 +9,7 @@ function local_setup_file() {
@test "plugins cmd-returned-notify: notify after elapsed time" { @test "plugins cmd-returned-notify: notify after elapsed time" {
export NOTIFY_IF_COMMAND_RETURNS_AFTER=0 export NOTIFY_IF_COMMAND_RETURNS_AFTER=0
export COMMAND_DURATION_START_SECONDS="${EPOCHREALTIME:-$SECONDS}" export COMMAND_DURATION_START_SECONDS="$(_shell_duration_en)"
sleep 1 sleep 1
run precmd_return_notification run precmd_return_notification
assert_success assert_success
@ -18,7 +18,7 @@ function local_setup_file() {
@test "plugins cmd-returned-notify: do not notify before elapsed time" { @test "plugins cmd-returned-notify: do not notify before elapsed time" {
export NOTIFY_IF_COMMAND_RETURNS_AFTER=10 export NOTIFY_IF_COMMAND_RETURNS_AFTER=10
export COMMAND_DURATION_START_SECONDS="${EPOCHREALTIME:-$SECONDS}" export COMMAND_DURATION_START_SECONDS="$(_shell_duration_en)"
sleep 1 sleep 1
run precmd_return_notification run precmd_return_notification
assert_success assert_success
@ -34,7 +34,7 @@ function local_setup_file() {
@test "lib command_duration: preexec set COMMAND_DURATION_START_SECONDS" { @test "lib command_duration: preexec set COMMAND_DURATION_START_SECONDS" {
export COMMAND_DURATION_START_SECONDS= export COMMAND_DURATION_START_SECONDS=
assert_equal "${COMMAND_DURATION_START_SECONDS}" "" assert_equal "${COMMAND_DURATION_START_SECONDS}" ""
NOW="${EPOCHREALTIME:-$SECONDS}" NOW="$(_shell_duration_en)"
_command_duration_pre_exec _command_duration_pre_exec
# We need to make sure to account for nanoseconds... # We need to make sure to account for nanoseconds...
assert_equal "${COMMAND_DURATION_START_SECONDS%.*}" "${NOW%.*}" assert_equal "${COMMAND_DURATION_START_SECONDS%.*}" "${NOW%.*}"