zsh: make profiling .zprofile easier by using functions
This commit is contained in:
parent
c6750c86c5
commit
b47b986ae8
1 changed files with 88 additions and 68 deletions
|
@ -1,21 +1,23 @@
|
||||||
# General environment settings.
|
# General environment settings.
|
||||||
export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
|
setup_environment() {
|
||||||
export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
|
export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
|
||||||
export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
|
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 LANG="en_US.UTF-8"
|
||||||
export LANGUAGE="en_US"
|
export LANGUAGE="en_US"
|
||||||
export LC_ALL="en_US.UTF-8"
|
export LC_ALL="en_US.UTF-8"
|
||||||
export LC_CTYPE="en_US.UTF-8"
|
export LC_CTYPE="en_US.UTF-8"
|
||||||
export LC_COLLATE="C"
|
export LC_COLLATE="C"
|
||||||
|
|
||||||
export INPUTRC="$XDG_CONFIG_HOME/readline/inputrc"
|
export INPUTRC="$XDG_CONFIG_HOME/readline/inputrc"
|
||||||
export LESS="-i -j.49 -M -R -z-2"
|
export LESS="-i -j.49 -M -R -z-2"
|
||||||
export LESSHISTFILE="$XDG_DATA_HOME/less/history"
|
export LESSHISTFILE="$XDG_DATA_HOME/less/history"
|
||||||
export LESSHISTSIZE=1000
|
export LESSHISTSIZE=1000
|
||||||
export LOCAL_PREFIX="/usr/local"
|
export LOCAL_PREFIX="/usr/local"
|
||||||
export PAGER=less
|
export PAGER=less
|
||||||
export PYTHONSTARTUP="$XDG_CONFIG_HOME/python/startup.py"
|
export PYTHONSTARTUP="$XDG_CONFIG_HOME/python/startup.py"
|
||||||
|
}
|
||||||
|
|
||||||
have() {
|
have() {
|
||||||
command -v "$1" >/dev/null 2>&1
|
command -v "$1" >/dev/null 2>&1
|
||||||
|
@ -29,7 +31,9 @@ have() {
|
||||||
#
|
#
|
||||||
# For details see: https://superuser.com/a/583502
|
# 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
|
# 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
|
# contained in $1. This is useful for prepending to PATH while avoiding
|
||||||
|
@ -53,9 +57,11 @@ prepend_unique() {
|
||||||
}
|
}
|
||||||
|
|
||||||
# Add custom bin dirs to PATH if they exist and are not already in PATH.
|
# Add custom bin dirs to PATH if they exist and are not already in PATH.
|
||||||
while read -r dir; do
|
setup_path() {
|
||||||
|
local dir=""
|
||||||
|
while read -r dir; do
|
||||||
[ -d "$dir" ] && prepend_unique PATH "${dir}"
|
[ -d "$dir" ] && prepend_unique PATH "${dir}"
|
||||||
done <<EOL
|
done <<EOL
|
||||||
$LOCAL_PREFIX/bin
|
$LOCAL_PREFIX/bin
|
||||||
$LOCAL_PREFIX/opt/curl/bin
|
$LOCAL_PREFIX/opt/curl/bin
|
||||||
$LOCAL_PREFIX/opt/make/libexec/gnubin
|
$LOCAL_PREFIX/opt/make/libexec/gnubin
|
||||||
|
@ -67,15 +73,18 @@ done <<EOL
|
||||||
$HOME/.local/bin
|
$HOME/.local/bin
|
||||||
$HOME/.bin
|
$HOME/.bin
|
||||||
EOL
|
EOL
|
||||||
export PATH
|
}
|
||||||
|
|
||||||
# Prepend custom man directories to MANPATH if they exist, so that we get
|
# Prepend custom man directories to MANPATH if they exist, so that we get
|
||||||
# correct man page entries when multiple versions of a command are
|
# correct man page entries when multiple versions of a command are
|
||||||
# available.
|
# available.
|
||||||
have manpath && MANPATH="$(unset MANPATH; manpath)"
|
setup_manpath() {
|
||||||
while read -r dir; do
|
local dir=""
|
||||||
|
# TODO: check if I really need the next line, `manpath` add ~600ms to startup
|
||||||
|
# have manpath && MANPATH="$(unset MANPATH; manpath)"
|
||||||
|
while read -r dir; do
|
||||||
[ -d "$dir" ] && prepend_unique MANPATH "${dir}"
|
[ -d "$dir" ] && prepend_unique MANPATH "${dir}"
|
||||||
done <<EOL
|
done <<-EOL
|
||||||
$LOCAL_PREFIX/share/man
|
$LOCAL_PREFIX/share/man
|
||||||
$LOCAL_PREFIX/opt/curl/share/man
|
$LOCAL_PREFIX/opt/curl/share/man
|
||||||
$LOCAL_PREFIX/opt/make/libexec/gnuman
|
$LOCAL_PREFIX/opt/make/libexec/gnuman
|
||||||
|
@ -86,42 +95,53 @@ done <<EOL
|
||||||
$LOCAL_PREFIX/opt/coreutils/libexec/gnuman
|
$LOCAL_PREFIX/opt/coreutils/libexec/gnuman
|
||||||
$HOME/.local/share/man
|
$HOME/.local/share/man
|
||||||
EOL
|
EOL
|
||||||
export MANPATH
|
}
|
||||||
|
|
||||||
unset dir
|
|
||||||
|
|
||||||
# These checks have to be done after PATH manipulation above so we can find
|
# These checks have to be done after PATH manipulation above so we can find
|
||||||
# installed programs if they are in the added paths.
|
# installed programs if they are in the added paths.
|
||||||
|
setup_tools() {
|
||||||
|
have nvim && EDITOR="nvim"
|
||||||
|
[ -z $EDITOR ] && have vim && EDITOR="vim"
|
||||||
|
[ -z $EDITOR ] && have vi && EDITOR="vi"
|
||||||
|
[ -n $EDITOR ] && export EDITOR
|
||||||
|
|
||||||
have nvim && EDITOR="nvim"
|
have brew && {
|
||||||
[ -z $EDITOR ] && have vim && EDITOR="vim"
|
|
||||||
[ -z $EDITOR ] && have vi && EDITOR="vi"
|
|
||||||
[ -n $EDITOR ] && export EDITOR
|
|
||||||
|
|
||||||
have brew && {
|
|
||||||
export HOMEBREW_NO_ANALYTICS=1
|
export HOMEBREW_NO_ANALYTICS=1
|
||||||
export HOMEBREW_NO_AUTO_UPDATE=1
|
export HOMEBREW_NO_AUTO_UPDATE=1
|
||||||
}
|
}
|
||||||
|
|
||||||
have dotnet && {
|
have dotnet && {
|
||||||
export DOTNET_CLI_TELEMETRY_OPTOUT=1
|
export DOTNET_CLI_TELEMETRY_OPTOUT=1
|
||||||
export DOTNET_NOLOGO=1
|
export DOTNET_NOLOGO=1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Set $DISPLAY if running in WSL and an Xserver is reachable
|
# 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 [ -n "$(uname -r | sed -n 's/.*\( *Microsoft *\).*/\1/ip')" ]; then
|
setup_wsl_display() {
|
||||||
xdisplay="$(awk '/nameserver/ {print $2; exit}' /etc/resolv.conf 2>/dev/null):0.0"
|
if [ -n "$(uname -r | sed -n 's/.*\( *Microsoft *\).*/\1/ip')" ] && have xset; then
|
||||||
have xset && if DISPLAY="$xdisplay" timeout '0.2s' xset q >/dev/null 2>&1; 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 DISPLAY="$xdisplay"
|
||||||
export LIBGL_ALWAYS_INDIRECT=1
|
export LIBGL_ALWAYS_INDIRECT=1
|
||||||
fi
|
fi
|
||||||
unset xdisplay
|
fi
|
||||||
fi
|
}
|
||||||
|
|
||||||
# Load additional local configuration if present.
|
# Load additional local configuration if present.
|
||||||
local_profile="$HOME/.local/etc/zsh/zprofile"
|
setup_local_profile() {
|
||||||
[ -r "$local_profile" ] && source "$local_profile"
|
local profile="$HOME/.local/etc/zsh/zprofile"
|
||||||
unset local_profile
|
[ -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
|
||||||
|
|
Loading…
Add table
Reference in a new issue