From 210978ab8f19229f9e3c07f4cfbb2567c6d620ad Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Wed, 28 Sep 2022 13:02:49 +0200 Subject: [PATCH] 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. --- home/.zshenv | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/home/.zshenv b/home/.zshenv index 1804103..88dbe9e 100644 --- a/home/.zshenv +++ b/home/.zshenv @@ -48,12 +48,18 @@ export ZDOTDIR="$XDG_CONFIG_HOME/zsh" # [ "$(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 check if running in WSL: https://stackoverflow.com/a/61014411 -if [ $(uname -r | sed -n 's/.*\( *Microsoft *\).*/\1/ip') ]; then - export DISPLAY=$(awk '/nameserver/ {print $2; exit}' /etc/resolv.conf 2>/dev/null):0.0 - export LIBGL_ALWAYS_INDIRECT=1 +if [ -n "$(uname -r | sed -n 's/.*\( *Microsoft *\).*/\1/ip')" ]; then + 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 + fi + fi + unset xdisplay fi