First pass at async functionality
parent
debbffc79a
commit
ab8f295225
@ -0,0 +1,53 @@
|
|||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Async #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
|
_zsh_autosuggest_async_fetch_suggestion() {
|
||||||
|
local strategy_function="_zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY"
|
||||||
|
local prefix="$(_zsh_autosuggest_escape_command "$1")"
|
||||||
|
|
||||||
|
# Send the suggestion command to the pty to fetch a suggestion
|
||||||
|
zpty -w -n $ZSH_AUTOSUGGEST_PTY_NAME "$strategy_function '$prefix'"$'\0'
|
||||||
|
}
|
||||||
|
|
||||||
|
_zsh_autosuggest_async_suggestion_worker() {
|
||||||
|
local last_pid
|
||||||
|
|
||||||
|
while read -d $'\0' cmd; do
|
||||||
|
# Kill last bg process
|
||||||
|
kill -KILL $last_pid &>/dev/null
|
||||||
|
|
||||||
|
# Run suggestion search in the background
|
||||||
|
print -n -- "$(eval "$cmd")"$'\0' &
|
||||||
|
|
||||||
|
# Save the bg process's id so we can kill later
|
||||||
|
last_pid=$!
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
_zsh_autosuggest_async_suggestion_ready() {
|
||||||
|
# while zpty -rt $ZSH_AUTOSUGGEST_PTY_NAME suggestion 2>/dev/null; do
|
||||||
|
while read -u $_ZSH_AUTOSUGGEST_PTY_FD -d $'\0' suggestion; do
|
||||||
|
zle _autosuggest-show-suggestion "${suggestion//$'\r'$'\n'/$'\n'}"
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
# Recreate the pty to get a fresh list of history events
|
||||||
|
_zsh_autosuggest_async_recreate_pty() {
|
||||||
|
typeset -g _ZSH_AUTOSUGGEST_PTY_FD
|
||||||
|
|
||||||
|
# Kill the old pty
|
||||||
|
if [ -n "$_ZSH_AUTOSUGGEST_PTY_FD" ]; then
|
||||||
|
zle -F $_ZSH_AUTOSUGGEST_PTY_FD
|
||||||
|
zpty -d $ZSH_AUTOSUGGEST_PTY_NAME &>/dev/null
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Start a new pty
|
||||||
|
typeset -h REPLY
|
||||||
|
zpty -b $ZSH_AUTOSUGGEST_PTY_NAME _zsh_autosuggest_async_suggestion_worker
|
||||||
|
_ZSH_AUTOSUGGEST_PTY_FD=$REPLY
|
||||||
|
zle -F $_ZSH_AUTOSUGGEST_PTY_FD _zsh_autosuggest_async_suggestion_ready
|
||||||
|
}
|
||||||
|
|
||||||
|
add-zsh-hook precmd _zsh_autosuggest_async_recreate_pty
|
@ -0,0 +1,10 @@
|
|||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Setup #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
|
# Precmd hooks for initializing the library and starting pty's
|
||||||
|
autoload -Uz add-zsh-hook
|
||||||
|
|
||||||
|
# Asynchronous suggestions are generated in a pty
|
||||||
|
zmodload zsh/zpty
|
@ -1,21 +0,0 @@
|
|||||||
|
|
||||||
#--------------------------------------------------------------------#
|
|
||||||
# Suggestion #
|
|
||||||
#--------------------------------------------------------------------#
|
|
||||||
|
|
||||||
# Delegate to the selected strategy to determine a suggestion
|
|
||||||
_zsh_autosuggest_suggestion() {
|
|
||||||
local escaped_prefix="$(_zsh_autosuggest_escape_command "$1")"
|
|
||||||
local strategy_function="_zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY"
|
|
||||||
|
|
||||||
if [ -n "$functions[$strategy_function]" ]; then
|
|
||||||
echo -E "$($strategy_function "$escaped_prefix")"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
_zsh_autosuggest_escape_command() {
|
|
||||||
setopt localoptions EXTENDED_GLOB
|
|
||||||
|
|
||||||
# Escape special chars in the string (requires EXTENDED_GLOB)
|
|
||||||
echo -E "${1//(#m)[\\()\[\]|*?~]/\\$MATCH}"
|
|
||||||
}
|
|
@ -0,0 +1,11 @@
|
|||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Utility Functions #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
|
_zsh_autosuggest_escape_command() {
|
||||||
|
setopt localoptions EXTENDED_GLOB
|
||||||
|
|
||||||
|
# Escape special chars in the string (requires EXTENDED_GLOB)
|
||||||
|
echo -E "${1//(#m)[\"\'\\()\[\]|*?~]/\\$MATCH}"
|
||||||
|
}
|
Loading…
Reference in New Issue