summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKoichi Murase <myoga.murase@gmail.com>2024-04-10 21:02:47 +0900
committerGitHub <noreply@github.com>2024-04-10 13:02:47 +0100
commit0da8d34425ef83395759db79377025c062a69911 (patch)
tree0e75708ca0b3174da232d4301037590e292b4670
parent7ced31c354bdfb2e256de3ecc49bcc4f379f78af (diff)
feat(bash/blesh): use _ble_exec_time_ata for duration even in bash < 5 (#1940)
Bash < 5.0 doesn't support the high-resolution clock EPOCHREALTIME, so ble.sh uses the builtin command `time` to measure the execution times in Bash < 5.0. This has a lower resolution of milliseconds but still more accurate than Atuin's measurent because Atuin's measurement includes the spawn cost of Atuin, which is typically larger than a millisecond. Reported-by: Georgios Vlassis <https://github.com/gvlassis> Reference: https://github.com/atuinsh/atuin/pull/1484#issuecomment-2047068582
-rw-r--r--atuin/src/shell/atuin.bash45
1 files changed, 23 insertions, 22 deletions
diff --git a/atuin/src/shell/atuin.bash b/atuin/src/shell/atuin.bash
index 1266b8d8..885729b0 100644
--- a/atuin/src/shell/atuin.bash
+++ b/atuin/src/shell/atuin.bash
@@ -56,29 +56,30 @@ __atuin_precmd() {
fi
local duration=""
- if ((BASH_VERSINFO[0] >= 5)); then
- # We use the high-resolution duration based on EPOCHREALTIME (bash >=
- # 5.0) if available.
- # shellcheck disable=SC2154,SC2309
- if [[ ${BLE_ATTACHED-} && ${_ble_exec_time_ata-} ]]; then
- # With ble.sh, we utilize the shell variable `_ble_exec_time_ata`
- # recorded by ble.sh.
- duration=${_ble_exec_time_ata}000
+ # shellcheck disable=SC2154,SC2309
+ if [[ ${BLE_ATTACHED-} && ${_ble_exec_time_ata-} ]]; then
+ # With ble.sh, we utilize the shell variable `_ble_exec_time_ata`
+ # recorded by ble.sh. It is more accurate than the measurements by
+ # Atuin, which includes the spawn cost of Atuin. ble.sh uses the
+ # special shell variable `EPOCHREALTIME` in bash >= 5.0 with the
+ # microsecond resolution, or the builtin `time` in bash < 5.0 with the
+ # millisecond resolution.
+ duration=${_ble_exec_time_ata}000
+ elif ((BASH_VERSINFO[0] >= 5)); then
+ # We calculate the high-resolution duration based on EPOCHREALTIME
+ # (bash >= 5.0) recorded by precmd/preexec, though it might not be as
+ # accurate as `_ble_exec_time_ata` provided by ble.sh because it
+ # includes the extra time of the precmd/preexec handling. Since Bash
+ # does not offer floating-point arithmetic, we remove the non-digit
+ # characters and perform the integral arithmetic. The fraction part of
+ # EPOCHREALTIME is fixed to have 6 digits in Bash. We remove all the
+ # non-digit characters because the decimal point is not necessarily a
+ # period depending on the locale.
+ duration=$((${__atuin_precmd_time//[!0-9]} - ${__atuin_preexec_time//[!0-9]}))
+ if ((duration >= 0)); then
+ duration=${duration}000
else
- # With bash-preexec, we calculate the time duration here, though it
- # might not be as accurate as `_ble_exec_time_ata` because it also
- # includes the time for precmd/preexec handling. Bash does not
- # allow floating-point arithmetic, so we remove the non-digit
- # characters and perform the integral arithmetic. The fraction
- # part of EPOCHREALTIME is fixed to have 6 digits in Bash. We
- # remove all the non-digit characters because the decimal point is
- # not necessarily a period depending on the locale.
- duration=$((${__atuin_precmd_time//[!0-9]} - ${__atuin_preexec_time//[!0-9]}))
- if ((duration >= 0)); then
- duration=${duration}000
- else
- duration="" # clear the result on overflow
- fi
+ duration="" # clear the result on overflow
fi
fi