GWSL-Source
GWSL-Source copied to clipboard
[Suggestion] Add activate and deactivate shell scripts
Hi! Below is a feature request/idea for more control over environment variables set by GWSL.
GWSL auto-exporting the necessary environment variables can be very convenient, but it also has its downsides. For example, if the $DISPLAY env var is set, but GWSL is not running, the matplotlib plotting package will just hang when saving a figure, even though it would not need to open a visible window. (I imagine the issue is that it decide what backend to use based on $DISPLAY.) This means that I have to unset the $DISPLAY variable before running scripts creating matplotlib plots. As I use matplotlib more than GUI apps, it is quite cumbersome, and I came up with the following solution.
- Remove the two lines added by GWSL from
.bashrcand.profile - Create two shell scripts to for "activating and deactivating" GWSL (similarly to how virtualenv or conda works).
The two scripts are
/usr/local/bin/gwsl-activate
# save original state
export DISPLAY_OLD_GWSL=$DISPLAY
export PULSE_SERVER_OLD_GWSL=$PULSE_SERVER
# export env vars
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0 #GWSL
export PULSE_SERVER=tcp:$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}') #GWSL
# modify prompt to show changes
export PS1="${PS1//\[GWSL\] /}"
export PS1="[GWSL] $PS1"
/usr/local/bin/gwsl-deactivate
# restore original state
export DISPLAY=$DISPLAY_OLD_GWSL
export PULSE_SERVER=$PULSE_SERVER_OLD_GWSL
# restore prompt
export PS1="${PS1//\[GWSL\] /}"
It seems to work very well. I can simply source gwsl-activate before launching a gui app from the terminal, and I also have some visual indication in the prompt that the environment variables for it are set. Also, GWSL must be doing some magic in the background, as it does not break starting Linux apps using the tray icon.
It might be worth considering to implement something like it in the tray menu itself. For example, besides Dispaly/Audio Auto-Exporting, there could be a button for setting up these two shell scripts (or perhaps some more refined versions of them) to provide an alternative. Also, it would be very nice to have a button to reverse Dispaly/Audio Auto-Exporting, as well.
Hmm. This is a good idea but will be confusing to implement into the UI. Users have a tendency to press every button regardless of knowing what it does, so I like to keep that to a minimum. However, we could put something in the docs. It is not ideal but might help people who look deep enough. And yes, GWSL is doing some magic with the tray icon and shortcuts. Anything GWSL directly launches has the display variable prepended to the command. The auto export variables only apply to launching apps directly from bash. Hmm.. well now that I think more about it, it may be feasible. I'll think about it and look into it.
Thanks for the reply! I totally agree that including it in the docs might be enough. It is an advanced feature after all, and users that need it can be expected to read the docs.
In the meantime, here is a slightly more thought through shell function to achieve the same result:
gwsl() {
if [ "$#" -eq 0 ]; then
echo "Set and unset environment variables for GWSL."
echo "Usage: gwsl activate|deactivate"
elif [ "$#" -ne 1 ]; then
echo "Invalid command. Usage: gwsl activate|deactivate"
else
\local cmd="$1"
case "$cmd" in
activate)
# save original state
export DISPLAY_OLD_GWSL=$DISPLAY
export PULSE_SERVER_OLD_GWSL=$PULSE_SERVER
# export env vars
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0
export PULSE_SERVER=tcp:$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}')
# modify prompt to show changes
export PS1="${PS1//\[GWSL\] /}"
export PS1="[GWSL] $PS1"
;;
deactivate)
# restore original state
if ! [ -z ${DISPLAY_OLD_GWSL+x} ]; then
export DISPLAY=$DISPLAY_OLD_GWSL
unset DISPLAY_OLD_GWSL
fi
if ! [ -z ${PULSE_SERVER_OLD_GWSL+x} ]; then
export PULSE_SERVER=$PULSE_SERVER_OLD_GWSL
unset PULSE_SERVER_OLD_GWSL
fi
# restore prompt
export PS1="${PS1//\[GWSL\] /}"
;;
*)
"Invalid command. Usage: wgsl activate|deactivate"
;;
esac
fi
}
Wow nice. I'll add this to my todo to add to the docs. (and credit you ofc XD)