refactor: restructure .zshrc with array-driven init and add pyenv eval support

Replace ad-hoc conditional blocks with helper functions and arrays for PATH
extensions, sourced files, shell options, and a new eval_expressions array
that evals tool initializer output (starting with pyenv init -).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-19 17:24:15 +02:00
co-authored by Claude Sonnet 4.6
parent 2601041008
commit 7919cd5099
+131 -192
View File
@@ -1,217 +1,156 @@
# Source .env file early to export environment variables
if [ -f "$HOME/.env" ] ; then
set -a
source "$HOME/.env"
set +a
fi
fpath=(~/.zsh/completions $fpath)
if [ -d "$HOME/bin" ]; then
export PATH="$HOME/bin:$PATH"
fi
if [ -d "$HOME/.local/bin" ]; then
export PATH="$HOME/.local/bin:$PATH"
fi
if [ -d "$HOME/.rbenv/bin" ]; then
export PATH="$PATH:$HOME/.rbenv/bin"
fi
if [ -d "$HOME/.pyenv/bin" ]; then
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
fi
if [ -d "$HOME/.cargo/bin" ]; then
export PATH="$PATH:$HOME/.cargo/bin"
fi
if [ -d "$HOME/.gvm/bin" ]; then
export PATH="$PATH:$HOME/.gvm/bin"
fi
if [ -d "/usr/local/go/bin" ]; then
export PATH="$PATH:/usr/local/go/bin"
fi
if [ -d "$HOME/go/bin" ]; then
export PATH="$PATH:$HOME/go/bin"
fi
if [ -d "$HOME/node_modules/.bin" ]; then
export PATH="$PATH:$HOME/node_modules/.bin"
fi
if [ -d "$HOME/scripts" ]; then
export PATH="$HOME/scripts:$PATH"
fi
if [ -d "$HOME/stacks" ]; then
export PATH="$HOME/stacks:$PATH"
fi
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
if [ -s "$NVM_DIR/nvm.sh" ] ; then
. "$NVM_DIR/nvm.sh"
fi
if [ -s "$NVM_DIR/bash_completion" ] ; then
. "$NVM_DIR/bash_completion"
fi
if [ -s "$HOME/.rvm/scripts/rvm" ] ; then
. "$HOME/.rvm/scripts/rvm"
fi
if [ -s "$HOME/.cargo/env" ] ; then
. "$HOME/.cargo/env"
fi
if [ -s "$HOME/.gvm/scripts/gvm" ] ; then
. "$HOME/.gvm/scripts/gvm"
fi
if [ -s "$HOME/.sdkman/bin/sdkman-init.sh" ] ; then
export SDKMAN_DIR="$HOME/.sdkman"
. "$HOME/.sdkman/bin/sdkman-init.sh"
fi
# ~/.zshrc
if command -v oh-my-posh 2>&1 >/dev/null; then
eval "$(! oh-my-posh init zsh --config=~/worker.omp.json)"
fi
# =============================================================================
# HELPER FUNCTIONS
# =============================================================================
if command -v rbenv 2>&1 >/dev/null; then
eval "$(rbenv init - --no-rehash zsh)"
fi
# Add a directory to PATH with existence check
# Usage: add_to_path /path/to/add [prepend|append]
add_to_path() {
local dir="$1"
local position="${2:-prepend}"
if command -v pyenv 2>&1 >/dev/null; then
eval "$(pyenv init --path)"
fi
if [[ -d "$dir" ]]; then
case "$position" in
prepend)
export PATH="$dir:$PATH"
;;
append)
export PATH="$PATH:$dir"
;;
*)
echo "Warning: Unknown position '$position' for path '$dir'" >&2
return 1
;;
esac
fi
}
# Source a file with existence and readability check (zsh-friendly)
# Usage: source_file /path/to/file
source_file() {
local file="$1"
if [[ -f "$file" && -r "$file" ]]; then
source "$file"
else
# Silently skip missing files (common for optional tools)
return 0
fi
}
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
# =============================================================================
# PATH EXTENSIONS
# =============================================================================
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:$HOME/.local/bin:/usr/local/bin:$PATH
# Define all path extensions in an array for easy management
path_extensions=(
"$HOME/bin"
"$HOME/.local/bin"
"$HOME/.rbenv/bin"
"$HOME/.pyenv/bin"
"$HOME/.cargo/bin"
"$HOME/.gvm/bin"
"$HOME/go/bin"
"$HOME/node_modules/.bin"
"$HOME/scripts"
)
# Path to your Oh My Zsh installation.
# Apply add_to_path to each directory (prepend by default)
for path_dir in "${path_extensions[@]}"; do
add_to_path "$path_dir" prepend
done
# =============================================================================
# CONFIGURATION FILES TO SOURCE
# =============================================================================
# Define files to source in an array for easy management
source_files=(
"$HOME/.env"
"$HOME/.nvm/nvm.sh"
"$HOME/.cargo/env"
"${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
)
# Source zsh-compatible files (skip if missing, no warnings)
for config_file in "${source_files[@]}"; do
source_file "$config_file" 2>/dev/null || true
done
# =============================================================================
# EVAL EXPRESSIONS
# =============================================================================
# Commands whose output should be eval'd (tool initializers, etc.)
eval_expressions=(
'pyenv init -'
)
for expr in "${eval_expressions[@]}"; do
if output="$(eval "$expr" 2>/dev/null)"; then
eval "$output"
fi
done
# =============================================================================
# SHELL INITIALIZATION
# =============================================================================
# Oh My Zsh installation
export ZSH="$HOME/.oh-my-zsh"
# Set name of the theme to load --- if set to "random", it will
# load a random theme each time Oh My Zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
# Set theme
ZSH_THEME="powerlevel10k/powerlevel10k"
# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in $ZSH/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )
# Enable plugins
plugins=(
git pm2 gh sudo ssh ruby rust python node github rsync nvm rbenv pyenv
docker docker-compose qrcode zsh-autosuggestions zsh-syntax-highlighting
zsh-interactive-cd zsh-navigation-tools
)
# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"
# Load Oh My Zsh framework
[[ ! -f "$ZSH/oh-my-zsh.sh" ]] || source "$ZSH/oh-my-zsh.sh"
# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"
# Powerlevel10k configuration (must come after theme is set)
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
# Uncomment one of the following lines to change the auto-update behavior
# zstyle ':omz:update' mode disabled # disable automatic updates
# zstyle ':omz:update' mode auto # update automatically without asking
# zstyle ':omz:update' mode reminder # just remind me to update when it's time
# Uncomment the following line to change how often to auto-update (in days).
# zstyle ':omz:update' frequency 13
# =============================================================================
# SHELL OPTIONS
# =============================================================================
# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS="true"
zsh_options=(
appendhistory
incappendhistory
sharehistory
histignoreall_dups
histreduceblanks
complete_in_word
always_to_end
prompt_percent
prompt_subst
auto_cd
)
# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"
for opt in "${zsh_options[@]}"; do
setopt "$opt"
done
# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"
# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"
# =============================================================================
# ALIASES AND FUNCTIONS
# =============================================================================
# Uncomment the following line to display red dots whilst waiting for completion.
# You can also set it to another string to have that shown instead of the default red dots.
# e.g. COMPLETION_WAITING_DOTS="%F{yellow}waiting...%f"
# Caution: this setting can cause issues with multiline prompts in zsh < 5.7.1 (see #5765)
# COMPLETION_WAITING_DOTS="true"
# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"
# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"
# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder
# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git pm2 gh sudo ssh ruby rust python node github rsync nvm rbenv pyenv docker docker-compose qrcode zsh-autosuggestions zsh-syntax-highlighting zsh-interactive-cd zsh-navigation-tools)
[[ ! -f $ZSH/oh-my-zsh.sh ]] || source $ZSH/oh-my-zsh.sh
# User configuration
# export MANPATH="/usr/local/man:$MANPATH"
# You may need to manually set your language environment
# export LANG=en_US.UTF-8
# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
# export EDITOR='vim'
# else
# export EDITOR='nvim'
# fi
# Compilation flags
# export ARCHFLAGS="-arch $(uname -m)"
# Set personal aliases, overriding those provided by Oh My Zsh libs,
# plugins, and themes. Aliases can be placed here, though Oh My Zsh
# users are encouraged to define aliases within a top-level file in
# the $ZSH_CUSTOM folder, with .zsh extension. Examples:
# Please add your custom aliases and functions here or in:
# - $ZSH_CUSTOM/aliases.zsh
# - $ZSH_CUSTOM/macos.zsh
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
# - $ZSH_CUSTOM/functions.zsh
alias arty="arty.sh"
alias stacks="stacks.sh"
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh