Care about terminals not responding to DSR in Bash integration

The current method of using the DSR request (\e[5n) and response
(\e[0n) for the operating status relies on the assumption that the
terminal supports the DSR request for the operating status and always
responds with the good status (\e[0n) instead of the bad one (\e[3n).
However, the terminal may not support \e[5n or may return another
status in principle.

Moreover, when a non-trivial shell framework is involved, the DSR
response might be processed by an unintended part.  For example, when
ble.sh (https://github.com/akinomyoga/ble.sh) is loaded in the Bash
session, since we use "\builtin bind" instead of "bind" replaced by
ble.sh, the response would be processed by Readline (which is
deactivated by ble.sh) instead of ble.sh (i.e., the line editor in
action).  As a result, the attempt to replace the command line will
fail.

As a fallback for such a situation, it is still worth inserting the
target directory through the command line.  This patch restores an
older logic to insert the completion.

This patch provides a fallback to the issue reported in the following
places:

[1] https://github.com/ajeetdsouza/zoxide/issues/1043#issuecomment-2845790391
[2] https://github.com/akinomyoga/ble.sh/issues/580#issuecomment-2845818748
This commit is contained in:
Koichi Murase 2025-05-03 20:55:49 +09:00
parent 21c3ecdb2f
commit 3de6f2ca58
1 changed files with 11 additions and 0 deletions

View File

@ -86,6 +86,8 @@ function __zoxide_doctor() {
# When using zoxide with --no-cmd, alias these internal functions as desired.
#
__zoxide_z_prefix='z#'
# Jump to a directory using only keywords.
function __zoxide_z() {
__zoxide_doctor
@ -99,6 +101,10 @@ function __zoxide_z() {
__zoxide_cd "$1"
elif [[ $# -eq 2 && $1 == '--' ]]; then
__zoxide_cd "$2"
elif [[ ${@: -1} == "${__zoxide_z_prefix}"?* ]]; then
# shellcheck disable=SC2124
\builtin local result="${@: -1}"
__zoxide_cd "{{ "${result:${#__zoxide_z_prefix}}" }}"
else
\builtin local result
# shellcheck disable=SC2312
@ -157,6 +163,11 @@ if [[ ${BASH_VERSINFO[0]:-0} -eq 4 && ${BASH_VERSINFO[1]:-0} -ge 4 || ${BASH_VER
elif [[ -z ${COMP_WORDS[-1]} ]]; then
# shellcheck disable=SC2312
__zoxide_result="$(\command zoxide query --exclude "$(__zoxide_pwd)" --interactive -- "{{ "${COMP_WORDS[@]:1:${#COMP_WORDS[@]}-2}" }}")" && {
# In case the terminal does not respond to \e[5n or another
# mechanism steals the response, it is still worth completing
# the directory in the command line.
COMPREPLY=("${__zoxide_z_prefix}${__zoxide_result}/")
\builtin bind -x '"\e[0n": __zoxide_z_complete_helper'
\builtin printf '\e[5n'
}