mirror of
https://github.com/atuinsh/atuin.git
synced 2025-01-27 08:40:11 +01:00
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.
This commit is contained in:
parent
5401ff12b7
commit
a4122f062a
@ -274,14 +274,16 @@ antigen bundle atuinsh/atuin@main
|
|||||||
|
|
||||||
#### [ble.sh](https://github.com/akinomyoga/ble.sh)
|
#### [ble.sh](https://github.com/akinomyoga/ble.sh)
|
||||||
|
|
||||||
Atuin works best in bash when using [ble.sh](https://github.com/akinomyoga/ble.sh).
|
Atuin works best in bash when using [ble.sh](https://github.com/akinomyoga/ble.sh) >= 0.4.
|
||||||
|
|
||||||
With ble.sh installed, just add atuin to your .bashrc
|
With ble.sh (>= 0.4) installed, just add atuin to your .bashrc
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
echo 'eval "$(atuin init bash)"' >> ~/.bashrc
|
echo 'eval "$(atuin init bash)"' >> ~/.bashrc
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Please make sure that the above line comes after sourcing ble.sh so atuin knows the presence of ble.sh.
|
||||||
|
|
||||||
#### [bash-preexec](https://github.com/rcaloras/bash-preexec)
|
#### [bash-preexec](https://github.com/rcaloras/bash-preexec)
|
||||||
|
|
||||||
[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.
|
[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.
|
||||||
|
@ -28,17 +28,19 @@ __atuin_history() {
|
|||||||
if [[ $HISTORY == __atuin_accept__:* ]]
|
if [[ $HISTORY == __atuin_accept__:* ]]
|
||||||
then
|
then
|
||||||
HISTORY=${HISTORY#__atuin_accept__:}
|
HISTORY=${HISTORY#__atuin_accept__:}
|
||||||
# Reprint the prompt, accounting for multiple lines
|
|
||||||
local __atuin_prompt_offset
|
|
||||||
__atuin_prompt_offset=$(echo -n "${PS1@P}" | tr -cd '\n' | wc -c)
|
|
||||||
if ((__atuin_prompt_offset > 0)); then
|
|
||||||
tput cuu "$__atuin_prompt_offset"
|
|
||||||
fi
|
|
||||||
echo "${PS1@P}$HISTORY"
|
|
||||||
|
|
||||||
if [[ -n "${BLE_VERSION-}" ]]; then
|
if [[ -n "${BLE_ATTACHED-}" ]]; then
|
||||||
blehook/invoke PREEXEC "$HISTORY"
|
ble-edit/content/reset-and-check-dirty "$HISTORY"
|
||||||
|
ble/widget/accept-line
|
||||||
else
|
else
|
||||||
|
# Reprint the prompt, accounting for multiple lines
|
||||||
|
local __atuin_prompt_offset
|
||||||
|
__atuin_prompt_offset=$(echo -n "${PS1@P}" | tr -cd '\n' | wc -c)
|
||||||
|
if ((__atuin_prompt_offset > 0)); then
|
||||||
|
tput cuu "$__atuin_prompt_offset"
|
||||||
|
fi
|
||||||
|
echo "${PS1@P}$HISTORY"
|
||||||
|
|
||||||
# Assuming bash-preexec
|
# Assuming bash-preexec
|
||||||
# Invoke every function in the preexec array
|
# Invoke every function in the preexec array
|
||||||
local preexec_function
|
local preexec_function
|
||||||
@ -46,7 +48,7 @@ __atuin_history() {
|
|||||||
local preexec_ret_value=0
|
local preexec_ret_value=0
|
||||||
for preexec_function in "${preexec_functions[@]:-}"; do
|
for preexec_function in "${preexec_functions[@]:-}"; do
|
||||||
if type -t "$preexec_function" 1>/dev/null; then
|
if type -t "$preexec_function" 1>/dev/null; then
|
||||||
__bp_set_ret_value "${__bp_last_ret_value:-}"
|
__atuin_set_ret_value "${__bp_last_ret_value:-}"
|
||||||
"$preexec_function" "$HISTORY"
|
"$preexec_function" "$HISTORY"
|
||||||
preexec_function_ret_value="$?"
|
preexec_function_ret_value="$?"
|
||||||
if [[ "$preexec_function_ret_value" != 0 ]]; then
|
if [[ "$preexec_function_ret_value" != 0 ]]; then
|
||||||
@ -55,35 +57,32 @@ __atuin_history() {
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
# shellcheck disable=SC2154
|
# shellcheck disable=SC2154
|
||||||
__bp_set_ret_value "$preexec_ret_value" "$__bp_last_argument_prev_command"
|
__atuin_set_ret_value "$preexec_ret_value" "$__bp_last_argument_prev_command"
|
||||||
fi
|
|
||||||
# Juggle the terminal settings so that the command can be interacted with
|
|
||||||
local stty_backup
|
|
||||||
stty_backup=$(stty -g)
|
|
||||||
stty "$ATUIN_STTY"
|
|
||||||
|
|
||||||
eval "$HISTORY"
|
# Juggle the terminal settings so that the command can be interacted with
|
||||||
exit_status=$?
|
local stty_backup
|
||||||
|
stty_backup=$(stty -g)
|
||||||
|
stty "$ATUIN_STTY"
|
||||||
|
|
||||||
stty "$stty_backup"
|
eval "$HISTORY"
|
||||||
|
exit_status=$?
|
||||||
|
|
||||||
# Execute preprompt commands
|
stty "$stty_backup"
|
||||||
__atuin_set_ret_value "$exit_status" "$HISTORY"
|
|
||||||
eval "$PROMPT_COMMAND"
|
# Execute preprompt commands
|
||||||
# Need to reexecute the blehook
|
__atuin_set_ret_value "$exit_status" "$HISTORY"
|
||||||
if [[ -n "${BLE_VERSION-}" ]]; then
|
eval "$PROMPT_COMMAND"
|
||||||
|
# Add it to the bash history
|
||||||
|
history -s "$HISTORY"
|
||||||
|
# Bash will redraw only the line with the prompt after we finish,
|
||||||
|
# so to work for a multiline prompt we need to print it ourselves,
|
||||||
|
# then move up a line
|
||||||
|
__atuin_set_ret_value "$exit_status" "$HISTORY"
|
||||||
|
echo "${PS1@P}"
|
||||||
|
tput cuu 1
|
||||||
__atuin_set_ret_value "$exit_status" "$HISTORY"
|
__atuin_set_ret_value "$exit_status" "$HISTORY"
|
||||||
blehook/invoke PRECMD "$?"
|
|
||||||
fi
|
fi
|
||||||
# Add it to the bash history
|
|
||||||
history -s "$HISTORY"
|
|
||||||
# Bash will redraw only the line with the prompt after we finish,
|
|
||||||
# so to work for a multiline prompt we need to print it ourselves,
|
|
||||||
# then move up a line
|
|
||||||
__atuin_set_ret_value "$exit_status" "$HISTORY"
|
|
||||||
echo "${PS1@P}"
|
|
||||||
tput cuu 1
|
|
||||||
__atuin_set_ret_value "$exit_status" "$HISTORY"
|
|
||||||
READLINE_LINE=""
|
READLINE_LINE=""
|
||||||
READLINE_POINT=${#READLINE_LINE}
|
READLINE_POINT=${#READLINE_LINE}
|
||||||
else
|
else
|
||||||
@ -92,10 +91,9 @@ __atuin_history() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
if [[ -n "${BLE_VERSION-}" ]]; then
|
# shellcheck disable=SC2154
|
||||||
blehook PRECMD-+=__atuin_precmd
|
if [[ -n "${BLE_VERSION-}" ]] && ((_ble_version >= 400)); then
|
||||||
blehook PREEXEC-+=__atuin_preexec
|
ble-import contrib/integration/bash-preexec
|
||||||
else
|
|
||||||
precmd_functions+=(__atuin_precmd)
|
|
||||||
preexec_functions+=(__atuin_preexec)
|
|
||||||
fi
|
fi
|
||||||
|
precmd_functions+=(__atuin_precmd)
|
||||||
|
preexec_functions+=(__atuin_preexec)
|
||||||
|
Loading…
Reference in New Issue
Block a user