Skip to content

Commit a4122f0

Browse files
authored
fix(bash): improve the support for enter_accept with ble.sh (#1465)
* feat(bash): check version of ble.sh blehooks are only supported in ble.sh >= 0.4, so we require the ble.sh version to be larger or equal to 0.4. We also describe the version requirement in README.md. * fix(bash): use ble.sh's contrib/integration/bash-preexec ble.sh provides module "contrib/integration/bash-preexec", which can be used with the same interface as bash-preexec. This module provides "preexec_functions" and "precmd_functions" without requiring bash-preexec. This module also properly handles it when both ble.sh and bash-preexec are loaded; the module resolves the conflicts between ble.sh and bash-preexec, and the module also tries to support bash-preexec in the detached state of ble.sh. * fix(bash): use ble.sh's accept-line widget for enter_accept In ble.sh, one can directly call the widget "accept-line" from a shell script. The widget "accept-line" is the actual widget that reserves the command execution in ble.sh, so calling "accept-line" is equivalent to the normal execution. It includes all the necessary adjustments and processing including stty and history. In addition, the command will be executed at the top-level context instead in a function scope. For example, without ble.sh, running "declare -A dict=()" through enter_accept will create an associative array in the function scope unexpectedly. With ble.sh, since the command is executed at the top-level context, such a problem does not happen. When ble.sh is in a detached state, we fall back to the manual execution of the command. In this case, we cannot assume the existence of the shell function "__bp_set_ret_value", so we always use __atuin_set_ret_value.
1 parent 5401ff1 commit a4122f0

File tree

2 files changed

+42
-42
lines changed

2 files changed

+42
-42
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,16 @@ antigen bundle atuinsh/atuin@main
274274

275275
#### [ble.sh](https://github.com/akinomyoga/ble.sh)
276276

277-
Atuin works best in bash when using [ble.sh](https://github.com/akinomyoga/ble.sh).
277+
Atuin works best in bash when using [ble.sh](https://github.com/akinomyoga/ble.sh) >= 0.4.
278278

279-
With ble.sh installed, just add atuin to your .bashrc
279+
With ble.sh (>= 0.4) installed, just add atuin to your .bashrc
280280

281281
```bash
282282
echo 'eval "$(atuin init bash)"' >> ~/.bashrc
283283
```
284284

285+
Please make sure that the above line comes after sourcing ble.sh so atuin knows the presence of ble.sh.
286+
285287
#### [bash-preexec](https://github.com/rcaloras/bash-preexec)
286288

287289
[Bash-preexec](https://github.com/rcaloras/bash-preexec) can also be used, but you may experience some minor problems with the recorded duration and exit status of some commands.

atuin/src/shell/atuin.bash

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,27 @@ __atuin_history() {
2828
if [[ $HISTORY == __atuin_accept__:* ]]
2929
then
3030
HISTORY=${HISTORY#__atuin_accept__:}
31-
# Reprint the prompt, accounting for multiple lines
32-
local __atuin_prompt_offset
33-
__atuin_prompt_offset=$(echo -n "${PS1@P}" | tr -cd '\n' | wc -c)
34-
if ((__atuin_prompt_offset > 0)); then
35-
tput cuu "$__atuin_prompt_offset"
36-
fi
37-
echo "${PS1@P}$HISTORY"
3831

39-
if [[ -n "${BLE_VERSION-}" ]]; then
40-
blehook/invoke PREEXEC "$HISTORY"
32+
if [[ -n "${BLE_ATTACHED-}" ]]; then
33+
ble-edit/content/reset-and-check-dirty "$HISTORY"
34+
ble/widget/accept-line
4135
else
36+
# Reprint the prompt, accounting for multiple lines
37+
local __atuin_prompt_offset
38+
__atuin_prompt_offset=$(echo -n "${PS1@P}" | tr -cd '\n' | wc -c)
39+
if ((__atuin_prompt_offset > 0)); then
40+
tput cuu "$__atuin_prompt_offset"
41+
fi
42+
echo "${PS1@P}$HISTORY"
43+
4244
# Assuming bash-preexec
4345
# Invoke every function in the preexec array
4446
local preexec_function
4547
local preexec_function_ret_value
4648
local preexec_ret_value=0
4749
for preexec_function in "${preexec_functions[@]:-}"; do
4850
if type -t "$preexec_function" 1>/dev/null; then
49-
__bp_set_ret_value "${__bp_last_ret_value:-}"
51+
__atuin_set_ret_value "${__bp_last_ret_value:-}"
5052
"$preexec_function" "$HISTORY"
5153
preexec_function_ret_value="$?"
5254
if [[ "$preexec_function_ret_value" != 0 ]]; then
@@ -55,35 +57,32 @@ __atuin_history() {
5557
fi
5658
done
5759
# shellcheck disable=SC2154
58-
__bp_set_ret_value "$preexec_ret_value" "$__bp_last_argument_prev_command"
59-
fi
60-
# Juggle the terminal settings so that the command can be interacted with
61-
local stty_backup
62-
stty_backup=$(stty -g)
63-
stty "$ATUIN_STTY"
60+
__atuin_set_ret_value "$preexec_ret_value" "$__bp_last_argument_prev_command"
61+
62+
# Juggle the terminal settings so that the command can be interacted with
63+
local stty_backup
64+
stty_backup=$(stty -g)
65+
stty "$ATUIN_STTY"
6466

65-
eval "$HISTORY"
66-
exit_status=$?
67+
eval "$HISTORY"
68+
exit_status=$?
6769

68-
stty "$stty_backup"
70+
stty "$stty_backup"
6971

70-
# Execute preprompt commands
71-
__atuin_set_ret_value "$exit_status" "$HISTORY"
72-
eval "$PROMPT_COMMAND"
73-
# Need to reexecute the blehook
74-
if [[ -n "${BLE_VERSION-}" ]]; then
72+
# Execute preprompt commands
73+
__atuin_set_ret_value "$exit_status" "$HISTORY"
74+
eval "$PROMPT_COMMAND"
75+
# Add it to the bash history
76+
history -s "$HISTORY"
77+
# Bash will redraw only the line with the prompt after we finish,
78+
# so to work for a multiline prompt we need to print it ourselves,
79+
# then move up a line
80+
__atuin_set_ret_value "$exit_status" "$HISTORY"
81+
echo "${PS1@P}"
82+
tput cuu 1
7583
__atuin_set_ret_value "$exit_status" "$HISTORY"
76-
blehook/invoke PRECMD "$?"
7784
fi
78-
# Add it to the bash history
79-
history -s "$HISTORY"
80-
# Bash will redraw only the line with the prompt after we finish,
81-
# so to work for a multiline prompt we need to print it ourselves,
82-
# then move up a line
83-
__atuin_set_ret_value "$exit_status" "$HISTORY"
84-
echo "${PS1@P}"
85-
tput cuu 1
86-
__atuin_set_ret_value "$exit_status" "$HISTORY"
85+
8786
READLINE_LINE=""
8887
READLINE_POINT=${#READLINE_LINE}
8988
else
@@ -92,10 +91,9 @@ __atuin_history() {
9291
fi
9392
}
9493

95-
if [[ -n "${BLE_VERSION-}" ]]; then
96-
blehook PRECMD-+=__atuin_precmd
97-
blehook PREEXEC-+=__atuin_preexec
98-
else
99-
precmd_functions+=(__atuin_precmd)
100-
preexec_functions+=(__atuin_preexec)
94+
# shellcheck disable=SC2154
95+
if [[ -n "${BLE_VERSION-}" ]] && ((_ble_version >= 400)); then
96+
ble-import contrib/integration/bash-preexec
10197
fi
98+
precmd_functions+=(__atuin_precmd)
99+
preexec_functions+=(__atuin_preexec)

0 commit comments

Comments
 (0)