feat(shell): bash support completion in the middle of a prompt

This commit is contained in:
dkmar 2025-04-10 04:33:02 -07:00
parent 8fab2fc16e
commit 251d2e6f5f

View File

@ -1,14 +1,16 @@
function tv_smart_autocomplete() {
local current_prompt="${READLINE_LINE:0:$READLINE_POINT}"
local prompt_prefix="${READLINE_LINE:0:$READLINE_POINT}"
local prompt_suffix="${READLINE_LINE:$READLINE_POINT}"
local output=$(tv --autocomplete-prompt "$current_prompt")
local output=$(tv --autocomplete-prompt "$prompt_prefix" | tr '\n' ' ')
if [[ -n $output ]]; then
# add a space if the prompt does not end with one
[[ "${current_prompt}" != *" " ]] && current_prompt="${current_prompt} "
[[ "${prompt_prefix}" != *" " ]] && prompt_prefix="${prompt_prefix} "
READLINE_LINE=$current_prompt$output
READLINE_POINT=${#READLINE_LINE}
local lhs=$prompt_prefix$output
READLINE_LINE=$lhs$prompt_suffix
READLINE_POINT=${#lhs}
fi
}