mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2026-01-30 02:44:42 +01:00
Refactor and clean up Oh My Zsh initialization script
- Simplified the ANSI formatting function and its conditional definition. - Improved grouping of related operations with clearer comments.
This commit is contained in:
parent
00b9b62385
commit
cc5981380a
1 changed files with 34 additions and 70 deletions
100
oh-my-zsh.sh
100
oh-my-zsh.sh
|
|
@ -1,93 +1,57 @@
|
||||||
# ANSI formatting function (\033[<code>m)
|
# ANSI formatting function
|
||||||
# 0: reset, 1: bold, 4: underline, 22: no bold, 24: no underline, 31: red, 33: yellow
|
omz_format() {
|
||||||
omz_f() {
|
[[ $# -gt 0 ]] || return
|
||||||
[ $# -gt 0 ] || return
|
printf "\033[%sm" "$*"
|
||||||
IFS=";" printf "\033[%sm" $*
|
|
||||||
}
|
}
|
||||||
# If stdout is not a terminal ignore all formatting
|
|
||||||
[ -t 1 ] || omz_f() { :; }
|
|
||||||
|
|
||||||
# Protect against non-zsh execution of Oh My Zsh (use POSIX syntax here)
|
# Disable formatting if stdout is not a terminal
|
||||||
[ -n "$ZSH_VERSION" ] || {
|
[[ -t 1 ]] || omz_format() { :; }
|
||||||
omz_ptree() {
|
|
||||||
# Get process tree of the current process
|
|
||||||
pid=$$; pids="$pid"
|
|
||||||
while [ ${pid-0} -ne 1 ] && ppid=$(ps -e -o pid,ppid | awk "\$1 == $pid { print \$2 }"); do
|
|
||||||
pids="$pids $pid"; pid=$ppid
|
|
||||||
done
|
|
||||||
|
|
||||||
# Show process tree
|
# Check for Zsh
|
||||||
case "$(uname)" in
|
if [[ -z "$ZSH_VERSION" ]]; then
|
||||||
Linux) ps -o ppid,pid,command -f -p $pids 2>/dev/null ;;
|
display_error() {
|
||||||
Darwin|*) ps -o ppid,pid,command -p $pids 2>/dev/null ;;
|
printf "%sError:%s %s\n" "$(omz_format 1 31)" "$(omz_format 22)" "$1"
|
||||||
esac
|
|
||||||
|
|
||||||
# If ps command failed, try Busybox ps
|
|
||||||
[ $? -eq 0 ] || ps -o ppid,pid,comm | awk "NR == 1 || index(\"$pids\", \$2) != 0"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
display_error "Oh My Zsh can't be loaded from: $SHELL. Run zsh instead."
|
||||||
shell=$(ps -o pid,comm | awk "\$1 == $$ { print \$2 }")
|
display_error "Process tree:"
|
||||||
printf "$(omz_f 1 31)Error:$(omz_f 22) Oh My Zsh can't be loaded from: $(omz_f 1)${shell}$(omz_f 22). "
|
ps -o ppid,pid,command -p $$ 2>/dev/null || ps -o ppid,pid,comm | awk "NR == 1 || index(\"$$\", \$2) != 0"
|
||||||
printf "You need to run $(omz_f 1)zsh$(omz_f 22) instead.$(omz_f 0)\n"
|
|
||||||
printf "$(omz_f 33)Here's the process tree:$(omz_f 22)\n\n"
|
|
||||||
omz_ptree
|
|
||||||
printf "$(omz_f 0)\n"
|
|
||||||
} >&2
|
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
}
|
fi
|
||||||
|
|
||||||
# Check if in emulation mode, if so early return
|
# Check emulation mode
|
||||||
# https://github.com/ohmyzsh/ohmyzsh/issues/11686
|
if [[ "$(emulate)" != zsh ]]; then
|
||||||
[[ "$(emulate)" = zsh ]] || {
|
printf "%sError:%s Oh My Zsh can't be loaded in \`%s\` emulation mode.\n" "$(omz_format 1 31)" "$(omz_format 22)" "$(emulate)"
|
||||||
printf "$(omz_f 1 31)Error:$(omz_f 22) Oh My Zsh can't be loaded in \`$(emulate)\` emulation mode.$(omz_f 0)\n" >&2
|
|
||||||
return 1
|
return 1
|
||||||
}
|
fi
|
||||||
|
|
||||||
unset -f omz_f
|
# Set ZSH path if not defined
|
||||||
|
: ${ZSH:=${${(%):-%x}:a:h}}
|
||||||
|
|
||||||
# If ZSH is not defined, use the current script's directory.
|
# Set custom and cache directories
|
||||||
[[ -n "$ZSH" ]] || export ZSH="${${(%):-%x}:a:h}"
|
: ${ZSH_CUSTOM:=$ZSH/custom}
|
||||||
|
: ${ZSH_CACHE_DIR:=$ZSH/cache}
|
||||||
|
|
||||||
# Set ZSH_CUSTOM to the path where your custom config files
|
# Ensure cache directory is writable
|
||||||
# and plugins exists, or else we will use the default custom/
|
|
||||||
[[ -n "$ZSH_CUSTOM" ]] || ZSH_CUSTOM="$ZSH/custom"
|
|
||||||
|
|
||||||
# Set ZSH_CACHE_DIR to the path where cache files should be created
|
|
||||||
# or else we will use the default cache/
|
|
||||||
[[ -n "$ZSH_CACHE_DIR" ]] || ZSH_CACHE_DIR="$ZSH/cache"
|
|
||||||
|
|
||||||
# Make sure $ZSH_CACHE_DIR is writable, otherwise use a directory in $HOME
|
|
||||||
if [[ ! -w "$ZSH_CACHE_DIR" ]]; then
|
if [[ ! -w "$ZSH_CACHE_DIR" ]]; then
|
||||||
ZSH_CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/oh-my-zsh"
|
ZSH_CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/oh-my-zsh"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Create cache and completions dir and add to $fpath
|
# Create cache and completions directories
|
||||||
mkdir -p "$ZSH_CACHE_DIR/completions"
|
mkdir -p "$ZSH_CACHE_DIR/completions"
|
||||||
(( ${fpath[(Ie)$ZSH_CACHE_DIR/completions]} )) || fpath=("$ZSH_CACHE_DIR/completions" $fpath)
|
fpath=("$ZSH_CACHE_DIR/completions" $fpath)
|
||||||
|
|
||||||
# Check for updates on initial load...
|
# Load necessary Zsh functions
|
||||||
source "$ZSH/tools/check_for_upgrade.sh"
|
|
||||||
|
|
||||||
# Initializes Oh My Zsh
|
|
||||||
|
|
||||||
# add a function path
|
|
||||||
fpath=($ZSH/{functions,completions} $ZSH_CUSTOM/{functions,completions} $fpath)
|
|
||||||
|
|
||||||
# Load all stock functions (from $fpath files) called below.
|
|
||||||
autoload -U compaudit compinit zrecompile
|
autoload -U compaudit compinit zrecompile
|
||||||
|
|
||||||
|
# Plugin handling
|
||||||
is_plugin() {
|
is_plugin() {
|
||||||
local base_dir=$1
|
local base_dir=$1 name=$2
|
||||||
local name=$2
|
[[ -f $base_dir/plugins/$name/$name.plugin.zsh ]] || [[ -f $base_dir/plugins/$name/_$name ]]
|
||||||
builtin test -f $base_dir/plugins/$name/$name.plugin.zsh \
|
|
||||||
|| builtin test -f $base_dir/plugins/$name/_$name
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Add all defined plugins to fpath. This must be done
|
# Add plugins to fpath
|
||||||
# before running compinit.
|
for plugin in $plugins; do
|
||||||
for plugin ($plugins); do
|
|
||||||
if is_plugin "$ZSH_CUSTOM" "$plugin"; then
|
if is_plugin "$ZSH_CUSTOM" "$plugin"; then
|
||||||
fpath=("$ZSH_CUSTOM/plugins/$plugin" $fpath)
|
fpath=("$ZSH_CUSTOM/plugins/$plugin" $fpath)
|
||||||
elif is_plugin "$ZSH" "$plugin"; then
|
elif is_plugin "$ZSH" "$plugin"; then
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue