zsh-autosuggestions/completion-client.zsh

42 lines
1.0 KiB
Bash
Executable File

#!/usr/bin/env zsh
zmodload zsh/net/socket
AUTOSUGGEST_SERVER_SCRIPT="${0:a:h}/completion-server.zsh"
autosuggest-ensure-server() {
setopt local_options no_hup
local server_dir="/tmp/zsh-autosuggest-$USER"
local pid_file="$server_dir/pid"
local socket_path="$server_dir/socket"
if [[ ! -S $socket_path || ! -r $pid_file ]] || ! kill -0 $(<$pid_file) &> /dev/null; then
if which setsid &> /dev/null; then
setsid zsh $AUTOSUGGEST_SERVER_SCRIPT $server_dir $pid_file $socket_path &!
else
zsh $AUTOSUGGEST_SERVER_SCRIPT $server_dir $pid_file $socket_path &!
fi
fi
autosuggest-server-connect
}
autosuggest-server-connect() {
unset ZLE_AUTOSUGGEST_CONNECTION
integer remaining_tries=10
while (( --remaining_tries )) && ! zsocket $socket_path &>/dev/null; do
sleep 0.3
done
[[ -z $REPLY ]] && return 1
ZLE_AUTOSUGGEST_CONNECTION=$REPLY
}
autosuggest-first-completion() {
[[ -z $ZLE_AUTOSUGGEST_CONNECTION ]] && return 1
setopt local_options noglob
local response
print -u $ZLE_AUTOSUGGEST_CONNECTION - $1 &> /dev/null || return 1
}