zsh-autosuggestions/src/async.zsh

36 lines
1.1 KiB
Bash
Raw Normal View History

2016-07-20 05:04:18 +02:00
#--------------------------------------------------------------------#
# Async #
#--------------------------------------------------------------------#
_zsh_autosuggest_async_request() {
typeset -g _ZSH_AUTOSUGGEST_ASYNC_FD
2016-07-20 05:04:18 +02:00
# Close the last fd to invalidate old suggestions
if [[ -n "$_ZSH_AUTOSUGGEST_ASYNC_FD" ]] && { true <&$_ZSH_AUTOSUGGEST_ASYNC_FD } 2>/dev/null; then
exec {_ZSH_AUTOSUGGEST_ASYNC_FD}<&-
fi
# Fork a process to fetch a suggestion and open a pipe to read from it
exec {_ZSH_AUTOSUGGEST_ASYNC_FD}< <(
local suggestion
_zsh_autosuggest_fetch_suggestion "$1"
echo -nE "$suggestion"
)
2016-07-20 05:04:18 +02:00
# When the fd is readable, call the response handler
zle -F "$_ZSH_AUTOSUGGEST_ASYNC_FD" _zsh_autosuggest_async_response
2017-01-27 23:18:26 +01:00
}
# Called when new data is ready to be read from the pipe
2017-01-25 03:53:26 +01:00
# First arg will be fd ready for reading
# Second arg will be passed in case of error
2017-01-27 23:18:26 +01:00
_zsh_autosuggest_async_response() {
# Read everything from the fd and give it as a suggestion
zle autosuggest-suggest -- "$(<&$1)"
2017-01-27 23:18:26 +01:00
# Remove the handler and close the fd
zle -F "$1"
exec {1}<&-
2016-07-20 05:04:18 +02:00
}