summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKoichi Murase <myoga.murase@gmail.com>2024-02-18 04:43:25 +0900
committerEllie Huxtable <ellie@elliehuxtable.com>2024-02-26 10:43:52 +0000
commit386771da5c8260c9c02304f62ddd7cce85372e2f (patch)
tree834adbd3df3b96bb1b371e6d945250ef29358bc7
parent1464cb657a47e7b5705194302532f3ecf37c7649 (diff)
fix(bash): rework #1509 to recover from the preexec failure (#1729)
In GitHub #1509, we blocked the unintended preexec event caused by the keybinding of Atuin. However, with that fix, the preexec event for the intended user command is still missing. In this patch, we try to manually run the preexec hook when we detected the unintended preexec (which means the missing intended preexec). References: https://github.com/atuinsh/atuin/pull/1509 https://forum.atuin.sh/t/atuin-bash-and-ble-sh/175 https://github.com/atuinsh/atuin/issues/1003#issuecomment-1947905213 https://github.com/atuinsh/atuin/pull/1727 https://github.com/atuinsh/atuin/pull/1728
-rw-r--r--atuin/src/shell/atuin.bash28
1 files changed, 23 insertions, 5 deletions
diff --git a/atuin/src/shell/atuin.bash b/atuin/src/shell/atuin.bash
index e40e8c56..3394da2e 100644
--- a/atuin/src/shell/atuin.bash
+++ b/atuin/src/shell/atuin.bash
@@ -17,12 +17,19 @@ export ATUIN_SESSION
ATUIN_HISTORY_ID=""
__atuin_preexec() {
+ # Workaround for old versions of bash-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
+ # In older versions of bash-preexec, the preexec hook may be called
+ # even for the commands 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. When the preexec hook is called
+ # for a keybinding, the preexec hook for the user command will not
+ # fire, so we instead set a fake ATUIN_HISTORY_ID here to notify
+ # __atuin_precmd of this failure.
+ if [[ $BASH_COMMAND == '__atuin_history'* && $BASH_COMMAND != "$1" ]]; then
+ ATUIN_HISTORY_ID=__bash_preexec_failure__
+ return 0
+ fi
fi
local id
@@ -36,6 +43,17 @@ __atuin_precmd() {
[[ ! $ATUIN_HISTORY_ID ]] && return
+ # If the previous preexec hook failed, we manually call __atuin_preexec
+ if [[ $ATUIN_HISTORY_ID == __bash_preexec_failure__ ]]; then
+ # This is the command extraction code taken from bash-preexec
+ local previous_command
+ previous_command=$(
+ export LC_ALL=C HISTTIMEFORMAT=''
+ builtin history 1 | sed '1 s/^ *[0-9][0-9]*[* ] //'
+ )
+ __atuin_preexec "$previous_command"
+ fi
+
local duration=""
if ((BASH_VERSINFO[0] >= 5)); then
# We use the high-resolution duration based on EPOCHREALTIME (bash >=