From b47b986ae848627ded5929e07ce38e3b9b80ef2e Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Fri, 26 Jul 2024 18:54:08 +0200 Subject: [PATCH] zsh: make profiling .zprofile easier by using functions --- config/zsh/.zprofile | 156 ++++++++++++++++++++++++------------------- 1 file changed, 88 insertions(+), 68 deletions(-) diff --git a/config/zsh/.zprofile b/config/zsh/.zprofile index af2473b..d9f0c0e 100644 --- a/config/zsh/.zprofile +++ b/config/zsh/.zprofile @@ -1,21 +1,23 @@ # General environment settings. -export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}" -export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}" -export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" +setup_environment() { + export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}" + export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}" + export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" -export LANG="en_US.UTF-8" -export LANGUAGE="en_US" -export LC_ALL="en_US.UTF-8" -export LC_CTYPE="en_US.UTF-8" -export LC_COLLATE="C" + export LANG="en_US.UTF-8" + export LANGUAGE="en_US" + export LC_ALL="en_US.UTF-8" + export LC_CTYPE="en_US.UTF-8" + export LC_COLLATE="C" -export INPUTRC="$XDG_CONFIG_HOME/readline/inputrc" -export LESS="-i -j.49 -M -R -z-2" -export LESSHISTFILE="$XDG_DATA_HOME/less/history" -export LESSHISTSIZE=1000 -export LOCAL_PREFIX="/usr/local" -export PAGER=less -export PYTHONSTARTUP="$XDG_CONFIG_HOME/python/startup.py" + export INPUTRC="$XDG_CONFIG_HOME/readline/inputrc" + export LESS="-i -j.49 -M -R -z-2" + export LESSHISTFILE="$XDG_DATA_HOME/less/history" + export LESSHISTSIZE=1000 + export LOCAL_PREFIX="/usr/local" + export PAGER=less + export PYTHONSTARTUP="$XDG_CONFIG_HOME/python/startup.py" +} have() { command -v "$1" >/dev/null 2>&1 @@ -29,7 +31,9 @@ have() { # # For details see: https://superuser.com/a/583502 # -[ "$(uname -s)" = "Darwin" ] && { PATH=""; source /etc/profile; } +setup_macos_path_helper() { + [ "$(uname -s)" = "Darwin" ] && { PATH=""; source /etc/profile; } +} # Prepend $2 to variable $1 using $3 as separator, but only if $2 is not already # contained in $1. This is useful for prepending to PATH while avoiding @@ -53,75 +57,91 @@ prepend_unique() { } # Add custom bin dirs to PATH if they exist and are not already in PATH. -while read -r dir; do - [ -d "$dir" ] && prepend_unique PATH "${dir}" -done </dev/null 2>&1; then - export DISPLAY="$xdisplay" - export LIBGL_ALWAYS_INDIRECT=1 +setup_wsl_display() { + if [ -n "$(uname -r | sed -n 's/.*\( *Microsoft *\).*/\1/ip')" ] && have xset; then + local xdisplay="$(awk '/nameserver/ {print $2; exit}' /etc/resolv.conf 2>/dev/null):0.0" + 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 +} # Load additional local configuration if present. -local_profile="$HOME/.local/etc/zsh/zprofile" -[ -r "$local_profile" ] && source "$local_profile" -unset local_profile +setup_local_profile() { + local profile="$HOME/.local/etc/zsh/zprofile" + [ -r "$profile" ] && source "$profile" +} + +# zmodload zsh/zprof +setup_environment +setup_macos_path_helper +setup_path && export PATH +setup_manpath && export MANPATH +setup_tools +setup_wsl_display +setup_local_profile +# zprof