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)
|
||||
# 0: reset, 1: bold, 4: underline, 22: no bold, 24: no underline, 31: red, 33: yellow
|
||||
omz_f() {
|
||||
[ $# -gt 0 ] || return
|
||||
IFS=";" printf "\033[%sm" $*
|
||||
# ANSI formatting function
|
||||
omz_format() {
|
||||
[[ $# -gt 0 ]] || return
|
||||
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)
|
||||
[ -n "$ZSH_VERSION" ] || {
|
||||
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
|
||||
# Disable formatting if stdout is not a terminal
|
||||
[[ -t 1 ]] || omz_format() { :; }
|
||||
|
||||
# Show process tree
|
||||
case "$(uname)" in
|
||||
Linux) ps -o ppid,pid,command -f -p $pids 2>/dev/null ;;
|
||||
Darwin|*) ps -o ppid,pid,command -p $pids 2>/dev/null ;;
|
||||
esac
|
||||
|
||||
# If ps command failed, try Busybox ps
|
||||
[ $? -eq 0 ] || ps -o ppid,pid,comm | awk "NR == 1 || index(\"$pids\", \$2) != 0"
|
||||
# Check for Zsh
|
||||
if [[ -z "$ZSH_VERSION" ]]; then
|
||||
display_error() {
|
||||
printf "%sError:%s %s\n" "$(omz_format 1 31)" "$(omz_format 22)" "$1"
|
||||
}
|
||||
|
||||
{
|
||||
shell=$(ps -o pid,comm | awk "\$1 == $$ { print \$2 }")
|
||||
printf "$(omz_f 1 31)Error:$(omz_f 22) Oh My Zsh can't be loaded from: $(omz_f 1)${shell}$(omz_f 22). "
|
||||
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
|
||||
|
||||
display_error "Oh My Zsh can't be loaded from: $SHELL. Run zsh instead."
|
||||
display_error "Process tree:"
|
||||
ps -o ppid,pid,command -p $$ 2>/dev/null || ps -o ppid,pid,comm | awk "NR == 1 || index(\"$$\", \$2) != 0"
|
||||
return 1
|
||||
}
|
||||
fi
|
||||
|
||||
# Check if in emulation mode, if so early return
|
||||
# https://github.com/ohmyzsh/ohmyzsh/issues/11686
|
||||
[[ "$(emulate)" = zsh ]] || {
|
||||
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
|
||||
# Check emulation mode
|
||||
if [[ "$(emulate)" != zsh ]]; then
|
||||
printf "%sError:%s Oh My Zsh can't be loaded in \`%s\` emulation mode.\n" "$(omz_format 1 31)" "$(omz_format 22)" "$(emulate)"
|
||||
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.
|
||||
[[ -n "$ZSH" ]] || export ZSH="${${(%):-%x}:a:h}"
|
||||
# Set custom and cache directories
|
||||
: ${ZSH_CUSTOM:=$ZSH/custom}
|
||||
: ${ZSH_CACHE_DIR:=$ZSH/cache}
|
||||
|
||||
# Set ZSH_CUSTOM to the path where your custom config files
|
||||
# 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
|
||||
# Ensure cache directory is writable
|
||||
if [[ ! -w "$ZSH_CACHE_DIR" ]]; then
|
||||
ZSH_CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/oh-my-zsh"
|
||||
fi
|
||||
|
||||
# Create cache and completions dir and add to $fpath
|
||||
# Create cache and completions directories
|
||||
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...
|
||||
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.
|
||||
# Load necessary Zsh functions
|
||||
autoload -U compaudit compinit zrecompile
|
||||
|
||||
# Plugin handling
|
||||
is_plugin() {
|
||||
local base_dir=$1
|
||||
local name=$2
|
||||
builtin test -f $base_dir/plugins/$name/$name.plugin.zsh \
|
||||
|| builtin test -f $base_dir/plugins/$name/_$name
|
||||
local base_dir=$1 name=$2
|
||||
[[ -f $base_dir/plugins/$name/$name.plugin.zsh ]] || [[ -f $base_dir/plugins/$name/_$name ]]
|
||||
}
|
||||
|
||||
# Add all defined plugins to fpath. This must be done
|
||||
# before running compinit.
|
||||
for plugin ($plugins); do
|
||||
# Add plugins to fpath
|
||||
for plugin in $plugins; do
|
||||
if is_plugin "$ZSH_CUSTOM" "$plugin"; then
|
||||
fpath=("$ZSH_CUSTOM/plugins/$plugin" $fpath)
|
||||
elif is_plugin "$ZSH" "$plugin"; then
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue