Cleaned up installation directory detection

- Used ZSH built-in magic to get the installation directory.
- Works with symlinks.
- No longer calls out to external program 'dirname'.
- Fixed issue with spaces in the path.
- No longer leak the variables 'filename' and 'script_location'.
pull/22/head
Christian Höltje 7 years ago committed by Christian Höltje
parent 3c3a86a42f
commit 3bf0f18e26

@ -17,74 +17,57 @@
################################################################ ################################################################
## Turn on for Debugging ## Turn on for Debugging
#PS4='%s%f%b%k%F{blue}%{λ%}%L %F{240}%N:%i%(?.. %F{red}%?) %1(_.%F{yellow}%-1_ .)%s%f%b%k '
#zstyle ':vcs_info:*+*:*' debug true #zstyle ':vcs_info:*+*:*' debug true
#set -o xtrace #set -o xtrace
# Try to set the installation path # Try to set the installation path
if [[ -n "$POWERLEVEL9K_INSTALLATION_PATH" ]]; then if [[ -n "$POWERLEVEL9K_INSTALLATION_DIR" ]]; then
# If an installation path was set manually, p9k_directory=${POWERLEVEL9K_INSTALLATION_DIR:A}
# it should trump any other location found.
# Do nothing. This is all right, as we use the
# POWERLEVEL9K_INSTALLATION_PATH for further processing.
elif [[ $(whence -w prompt_powerlevel9k_setup) =~ "function" ]]; then
# Check if the theme was called as a function (e.g., from prezto)
autoload -U is-at-least
if is-at-least 5.0.8; then
# Try to find the correct path of the script.
POWERLEVEL9K_INSTALLATION_PATH=$(whence -v $0 | sed "s/$0 is a shell function from //")
elif [[ -f "${ZDOTDIR:-$HOME}/.zprezto/modules/prompt/init.zsh" ]]; then
# If there is an prezto installation, we assume that powerlevel9k is linked there.
POWERLEVEL9K_INSTALLATION_PATH="${ZDOTDIR:-$HOME}/.zprezto/modules/prompt/functions/prompt_powerlevel9k_setup"
fi
else else
# Last resort: Set installation path is script path if [[ "${(%):-%N}" == '(eval)' ]]; then
POWERLEVEL9K_INSTALLATION_PATH="$0" if [[ "$0" == '-antigen-load' ]] && [[ -r "${PWD}/powerlevel9k.zsh-theme" ]]; then
fi # Antigen uses eval to load things so it can change the plugin (!!)
# https://github.com/zsh-users/antigen/issues/581
# Resolve the installation path p9k_directory=$PWD
if [[ -L "$POWERLEVEL9K_INSTALLATION_PATH" ]]; then
# If this theme is sourced as a symlink, we need to locate the real URL
filename="${POWERLEVEL9K_INSTALLATION_PATH:A}"
elif [[ -d "$POWERLEVEL9K_INSTALLATION_PATH" ]]; then
# Directory
filename="${POWERLEVEL9K_INSTALLATION_PATH}/powerlevel9k.zsh-theme"
elif [[ -f "$POWERLEVEL9K_INSTALLATION_PATH" ]]; then
# Script is a file
filename="$POWERLEVEL9K_INSTALLATION_PATH"
elif [[ -z "$POWERLEVEL9K_INSTALLATION_PATH" ]]; then
# Fallback: specify an installation path!
print -P "%F{red}We could not locate the installation path of powerlevel9k.%f"
print -P "Please specify by setting %F{blue}POWERLEVEL9K_INSTALLATION_PATH%f (full path incl. file name) at the very beginning of your ~/.zshrc"
return 1
else else
print -P "%F{red}Script location could not be found! Maybe your %F{blue}POWERLEVEL9K_INSTALLATION_PATH%F{red} is not correct?%f" print -P "%F{red}You must set POWERLEVEL9K_INSTALLATION_DIR work from within an (eval).%f"
return 1 return 1
fi fi
script_location="$(dirname $filename)" else
# Get the path to file this code is executing in; then
# get the absolute path and strip the filename.
# See https://stackoverflow.com/a/28336473/108857
p9k_directory=${${(%):-%x}:A:h}
fi
fi
################################################################ ################################################################
# Source icon functions # Source icon functions
################################################################ ################################################################
source $script_location/functions/icons.zsh source "${p9k_directory}/functions/icons.zsh"
################################################################ ################################################################
# Source utility functions # Source utility functions
################################################################ ################################################################
source $script_location/functions/utilities.zsh source "${p9k_directory}/functions/utilities.zsh"
################################################################ ################################################################
# Source color functions # Source color functions
################################################################ ################################################################
source $script_location/functions/colors.zsh source "${p9k_directory}/functions/colors.zsh"
################################################################ ################################################################
# Source VCS_INFO hooks / helper functions # Source VCS_INFO hooks / helper functions
################################################################ ################################################################
source $script_location/functions/vcs.zsh source "${p9k_directory}/functions/vcs.zsh"
# cleanup temporary variables.
unset p9k_directory
################################################################ ################################################################
# Color Scheme # Color Scheme

Loading…
Cancel
Save