env: only set DISPLAY on WSL if an Xserver is reachable

Neovim always uses `xclip` as the clipboard provider if DISPLAY is set.
Therefore, if we set it unconditionally, `xclip` hangs while trying to
reach the server if this hasn't been started, causing all sorts of
chaos.
This commit is contained in:
Fernando Schauenburg 2022-09-28 13:02:49 +02:00
parent f174612d03
commit 210978ab8f

View file

@ -48,12 +48,18 @@ export ZDOTDIR="$XDG_CONFIG_HOME/zsh"
# #
[ "$(uname -s)" = "Darwin" ] || source "$XDG_CONFIG_HOME/shell/path.sh" [ "$(uname -s)" = "Darwin" ] || source "$XDG_CONFIG_HOME/shell/path.sh"
# Set $DISPLAY if running in WSL # Set $DISPLAY if running in WSL and an Xserver is reachable
# #
# How to configure Windows Firewall for Xserver: https://skeptric.com/wsl2-xserver/ # How to configure Windows Firewall for Xserver: https://skeptric.com/wsl2-xserver/
# How to check if running in WSL: https://stackoverflow.com/a/61014411 # How to check if running in WSL: https://stackoverflow.com/a/61014411
if [ $(uname -r | sed -n 's/.*\( *Microsoft *\).*/\1/ip') ]; then if [ -n "$(uname -r | sed -n 's/.*\( *Microsoft *\).*/\1/ip')" ]; then
export DISPLAY=$(awk '/nameserver/ {print $2; exit}' /etc/resolv.conf 2>/dev/null):0.0 xdisplay="$(awk '/nameserver/ {print $2; exit}' /etc/resolv.conf 2>/dev/null):0.0"
if command -v xset >/dev/null 2>&1; then
if DISPLAY="$xdisplay" timeout '0.2s' xset q >/dev/null 2>&1; then
export DISPLAY="$xdisplay"
export LIBGL_ALWAYS_INDIRECT=1 export LIBGL_ALWAYS_INDIRECT=1
fi
fi
unset xdisplay
fi fi