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:
valknarandClaude Sonnet 4.6 committed 2026-06-19 17:24:15 +02:00
1 parent 2601041008
commit 7919cd5099
1 file changed
+124 -185
+124 -185
View File
@@ -1,217 +1,156 @@
# Source .env file early to export environment variables # ~/.zshrc
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" # HELPER FUNCTIONS
fi # =============================================================================
if [ -d "$HOME/.local/bin" ]; then # Add a directory to PATH with existence check
export PATH="$HOME/.local/bin:$PATH" # Usage: add_to_path /path/to/add [prepend|append]
fi add_to_path() {
local dir="$1"
local position="${2:-prepend}"
if [ -d "$HOME/.rbenv/bin" ]; then if [[ -d "$dir" ]]; then
export PATH="$PATH:$HOME/.rbenv/bin" case "$position" in
fi prepend)
export PATH="$dir:$PATH"
if [ -d "$HOME/.pyenv/bin" ]; then ;;
export PYENV_ROOT="$HOME/.pyenv" append)
export PATH="$PYENV_ROOT/bin:$PATH" export PATH="$PATH:$dir"
fi ;;
*)
if [ -d "$HOME/.cargo/bin" ]; then echo "Warning: Unknown position '$position' for path '$dir'" >&2
export PATH="$PATH:$HOME/.cargo/bin" return 1
fi ;;
esac
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
fi }
if [ -s "$NVM_DIR/nvm.sh" ] ; then # Source a file with existence and readability check (zsh-friendly)
. "$NVM_DIR/nvm.sh" # Usage: source_file /path/to/file
fi source_file() {
local file="$1"
if [ -s "$NVM_DIR/bash_completion" ] ; then if [[ -f "$file" && -r "$file" ]]; then
. "$NVM_DIR/bash_completion" source "$file"
fi else
# Silently skip missing files (common for optional tools)
if [ -s "$HOME/.rvm/scripts/rvm" ] ; then return 0
. "$HOME/.rvm/scripts/rvm" fi
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
if command -v oh-my-posh 2>&1 >/dev/null; then # =============================================================================
eval "$(! oh-my-posh init zsh --config=~/worker.omp.json)" # PATH EXTENSIONS
fi # =============================================================================
if command -v rbenv 2>&1 >/dev/null; then # Define all path extensions in an array for easy management
eval "$(rbenv init - --no-rehash zsh)" path_extensions=(
fi "$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"
)
if command -v pyenv 2>&1 >/dev/null; then # Apply add_to_path to each directory (prepend by default)
eval "$(pyenv init --path)" for path_dir in "${path_extensions[@]}"; do
fi add_to_path "$path_dir" prepend
done
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc. # =============================================================================
# Initialization code that may require console input (password prompts, [y/n] # CONFIGURATION FILES TO SOURCE
# 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
# If you come from bash you might have to change your $PATH. # Define files to source in an array for easy management
# export PATH=$HOME/bin:$HOME/.local/bin:/usr/local/bin:$PATH source_files=(
"$HOME/.env"
"$HOME/.nvm/nvm.sh"
"$HOME/.cargo/env"
"${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
)
# Path to your Oh My Zsh installation. # 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" export ZSH="$HOME/.oh-my-zsh"
# Set name of the theme to load --- if set to "random", it will # Set theme
# 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
ZSH_THEME="powerlevel10k/powerlevel10k" ZSH_THEME="powerlevel10k/powerlevel10k"
# Set list of themes to pick from when loading at random # Enable plugins
# Setting this variable when ZSH_THEME=random will cause zsh to load plugins=(
# a theme from this variable instead of looking in $ZSH/themes/ git pm2 gh sudo ssh ruby rust python node github rsync nvm rbenv pyenv
# If set to an empty array, this variable will have no effect. docker docker-compose qrcode zsh-autosuggestions zsh-syntax-highlighting
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" ) zsh-interactive-cd zsh-navigation-tools
)
# Uncomment the following line to use case-sensitive completion. # Load Oh My Zsh framework
# CASE_SENSITIVE="true" [[ ! -f "$ZSH/oh-my-zsh.sh" ]] || source "$ZSH/oh-my-zsh.sh"
# Uncomment the following line to use hyphen-insensitive completion. # Powerlevel10k configuration (must come after theme is set)
# Case-sensitive completion must be off. _ and - will be interchangeable. [[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
# HYPHEN_INSENSITIVE="true"
# 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. zsh_options=(
# DISABLE_MAGIC_FUNCTIONS="true" 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. for opt in "${zsh_options[@]}"; do
# DISABLE_LS_COLORS="true" 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. # Please add your custom aliases and functions here or in:
# 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:
# - $ZSH_CUSTOM/aliases.zsh # - $ZSH_CUSTOM/aliases.zsh
# - $ZSH_CUSTOM/macos.zsh # - $ZSH_CUSTOM/functions.zsh
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
alias arty="arty.sh" alias arty="arty.sh"
alias stacks="stacks.sh" alias stacks="stacks.sh"
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh