fix(bash): avoid unexpected atuin history start for keybindings (#1509)

This fixes the second issue of "0s or wrong command duration" reported
at https://github.com/atuinsh/atuin/issues/1003.

The problem is that, with bash-preexec, the preexec hook may be called
even for the commands executed by the keybindings.

* In particular, the preexec is called before the command
  `__atuin_history` is executed on pressing [C-r] and [up].  In this
  case, $1 passed to `__atuin_preexec` contains the last entry in the
  command history, so `atuin history start` is called for the last
  command.  As a result, pressing [C-r] and [up] clears the duration
  of the last command.  This causes the reported 0s duration.

* Furthermore, the precmd hook corresponding to the keybinding command
  will not be called, so the duration is only filled when the next
  user command starts.  This replaces the duration of the last command
  with the time interval between the last press of [C-r] or [up] and
  the start time of the next command.  This causes the reported wrong
  duration.

There is no general and robust way to distinguish the preexec
invocation for keybindings from that for the user commands, but in
this patch we exclude the preexec invocation for Atuin's keybindings
[C-r] and [up] at least.
This commit is contained in:
Koichi Murase 2024-01-07 01:45:19 +09:00 committed by GitHub
parent 7bc6ccdd70
commit 86f2c8e588
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,6 +3,14 @@ ATUIN_STTY=$(stty -g)
export ATUIN_SESSION
__atuin_preexec() {
if [[ ! ${BLE_ATTACHED-} ]]; then
# With bash-preexec, preexec may be called even for the command run by
# keybindings. There is no general and robust way to detect the
# command for keybindings, but at least we want to exclude Atuin's
# keybindings.
[[ $BASH_COMMAND == '__atuin_history'* && $BASH_COMMAND != "$1" ]] && return 0
fi
local id
id=$(atuin history start -- "$1")
export ATUIN_HISTORY_ID="${id}"