aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKoichi Murase <myoga.murase@gmail.com>2024-01-07 01:45:19 +0900
committerGitHub <noreply@github.com>2024-01-06 16:45:19 +0000
commit86f2c8e58806020bc04999a5039e18ff10409e59 (patch)
tree77fdbdd1a2307cc6c6b1dfc967c3c2ff0e32cab9
parentfeat: rework record sync for improved reliability (#1478) (diff)
downloadatuin-86f2c8e58806020bc04999a5039e18ff10409e59.zip
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.
-rw-r--r--atuin/src/shell/atuin.bash8
1 files changed, 8 insertions, 0 deletions
diff --git a/atuin/src/shell/atuin.bash b/atuin/src/shell/atuin.bash
index 725b85c4..ff34aea8 100644
--- a/atuin/src/shell/atuin.bash
+++ b/atuin/src/shell/atuin.bash
@@ -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}"