mirror of
https://github.com/romkatv/powerlevel10k.git
synced 2024-12-18 13:41:56 +00:00
speed up node_version and nvm prompts; fixes #12
This commit is contained in:
parent
4e81de5322
commit
5800b79f51
1 changed files with 123 additions and 31 deletions
|
@ -1130,36 +1130,133 @@ prompt_load() {
|
||||||
"$1_prompt_segment" "${0}_${current_state}" "$2" "${load_states[$current_state]}" "$DEFAULT_COLOR" 'LOAD_ICON' 0 '' "$load_avg"
|
"$1_prompt_segment" "${0}_${current_state}" "$2" "${load_states[$current_state]}" "$DEFAULT_COLOR" 'LOAD_ICON' 0 '' "$load_avg"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function _p9k_cached_cmd_stdout() {
|
||||||
|
local cmd=$commands[$1]
|
||||||
|
[[ -n $cmd ]] || return
|
||||||
|
shift
|
||||||
|
local -H stat
|
||||||
|
zstat -H stat -- $cmd 2>/dev/null || return
|
||||||
|
if ! _p9k_cache_get "$0" "$stat[inode]" "$stat[mtime]" "$stat[size]" "$cmd" "$@"; then
|
||||||
|
local out
|
||||||
|
out=$($cmd "$@" 2>/dev/null)
|
||||||
|
_p9k_cache_set $(( ! $? )) "$out"
|
||||||
|
fi
|
||||||
|
(( $_P9K_CACHE_VAL[1] )) || return
|
||||||
|
_P9K_RETVAL=$_P9K_CACHE_VAL[2]
|
||||||
|
}
|
||||||
|
|
||||||
################################################################
|
################################################################
|
||||||
# Segment to diplay Node version
|
# Segment to diplay Node version
|
||||||
set_default P9K_NODE_VERSION_PROJECT_ONLY false
|
set_default P9K_NODE_VERSION_PROJECT_ONLY false
|
||||||
prompt_node_version() {
|
prompt_node_version() {
|
||||||
if [ "$P9K_NODE_VERSION_PROJECT_ONLY" = true ] ; then
|
(( $+commands[node] )) || return
|
||||||
local foundProject=false # Variable to stop searching if a project is found
|
|
||||||
local currentDir=$(pwd) # Variable to iterate through the path ancestry tree
|
|
||||||
|
|
||||||
# Search as long as no project could been found or until the root directory
|
if [[ $P9K_NODE_VERSION_PROJECT_ONLY == true ]] ; then
|
||||||
# has been reached
|
local dir=$PWD
|
||||||
while [ "$foundProject" = false -a ! "$currentDir" = "/" ] ; do
|
while true; do
|
||||||
|
[[ $dir == / ]] && return
|
||||||
# Check if directory contains a project description
|
[[ -e $dir/package.json ]] && break
|
||||||
if [[ -e "$currentDir/package.json" ]] ; then
|
dir=${dir:h}
|
||||||
foundProject=true
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
# Go to the parent directory
|
|
||||||
currentDir="$(dirname "$currentDir")"
|
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Show version if a project has been found, or set to always show
|
_p9k_cached_cmd_stdout node --version && [[ $_P9K_RETVAL == v?* ]] || return
|
||||||
if [ "$P9K_NODE_VERSION_PROJECT_ONLY" != true -o "$foundProject" = true ] ; then
|
"$1_prompt_segment" "$0" "$2" "green" "white" 'NODE_ICON' 0 '' "${_P9K_RETVAL#v}"
|
||||||
# Get the node version
|
}
|
||||||
local node_version=$(node -v 2>/dev/null)
|
|
||||||
|
|
||||||
# Return if node is not installed
|
# Almost the same as `nvm_version default` but faster. The differences shouldn't affect
|
||||||
[[ -z "${node_version}" ]] && return
|
# the observable behavior of Powerlevel10k.
|
||||||
"$1_prompt_segment" "$0" "$2" "green" "white" 'NODE_ICON' 0 '' "${${node_version:1}//\%/%%}"
|
function _p9k_nvm_ls_default() {
|
||||||
|
local v=default
|
||||||
|
local -a seen=($v)
|
||||||
|
local target
|
||||||
|
while [[ -r $NVM_DIR/alias/$v ]] && read target <$NVM_DIR/alias/$v; do
|
||||||
|
[[ -n $target && ${seen[(I)$target]} == 0 ]] || return
|
||||||
|
seen+=$target
|
||||||
|
v=$target
|
||||||
|
done
|
||||||
|
|
||||||
|
case $v in
|
||||||
|
default|N/A)
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
system|v)
|
||||||
|
_P9K_RETVAL=system
|
||||||
|
return
|
||||||
|
;;
|
||||||
|
iojs-[0-9]*)
|
||||||
|
v=iojs-v${v#iojs-}
|
||||||
|
;;
|
||||||
|
[0-9]*)
|
||||||
|
v=v$v
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
if [[ $v == v*.*.* ]]; then
|
||||||
|
if [[ -x $NVM_DIR/versions/node/$v/bin/node || -x $NVM_DIR/$v/bin/node ]]; then
|
||||||
|
_P9K_RETVAL=$v
|
||||||
|
return
|
||||||
|
elif [[ -x $NVM_DIR/versions/io.js/$v/bin/node ]]; then
|
||||||
|
_P9K_RETVAL=iojs-$v
|
||||||
|
return
|
||||||
|
else
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
local -a dirs=()
|
||||||
|
case $v in
|
||||||
|
node|node-|stable)
|
||||||
|
dirs=($NVM_DIR/versions/node $NVM_DIR)
|
||||||
|
v='(v[1-9]*|v0.*[02468].*)'
|
||||||
|
;;
|
||||||
|
unstable)
|
||||||
|
dirs=($NVM_DIR/versions/node $NVM_DIR)
|
||||||
|
v='v0.*[13579].*'
|
||||||
|
;;
|
||||||
|
iojs*)
|
||||||
|
dirs=($NVM_DIR/versions/io.js)
|
||||||
|
v=v${${${v#iojs}#-}#v}'*'
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
dirs=($NVM_DIR/versions/node $NVM_DIR $NVM_DIR/versions/io.js)
|
||||||
|
v=v${v#v}'*'
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
local -a matches=(${^dirs}/${~v}(/N))
|
||||||
|
(( $#matches )) || return
|
||||||
|
|
||||||
|
emulate -L zsh && setopt extendedglob
|
||||||
|
|
||||||
|
local max path
|
||||||
|
local -a match
|
||||||
|
for path in ${(Oa)matches}; do
|
||||||
|
[[ ${path:t} == (#b)v(*).(*).(*) ]] || continue
|
||||||
|
v=${(j::)${(@l:6::0:)match}}
|
||||||
|
[[ $v > $max ]] || continue
|
||||||
|
max=$v
|
||||||
|
_P9K_RETVAL=${path:t}
|
||||||
|
[[ ${path:h:t} != io.js ]] || _P9K_RETVAL=iojs-$_P9K_RETVAL
|
||||||
|
done
|
||||||
|
|
||||||
|
[[ -n $max ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
# The same as `nvm_version current` but faster.
|
||||||
|
_p9k_nvm_ls_current() {
|
||||||
|
local node_path=${commands[node]:A}
|
||||||
|
[[ -n $node_path ]] || return
|
||||||
|
|
||||||
|
local nvm_dir=${NVM_DIR:A}
|
||||||
|
if [[ -n $nvm_dir && $node_path == $nvm_dir/versions/io.js/* ]]; then
|
||||||
|
_p9k_cached_cmd_stdout iojs --version || return
|
||||||
|
_P9K_RETVAL=iojs-v${_P9K_RETVAL#v}
|
||||||
|
elif [[ -n $nvm_dir && $node_path == $nvm_dir/* ]]; then
|
||||||
|
_p9k_cached_cmd_stdout node --version || return
|
||||||
|
_P9K_RETVAL=v${_P9K_RETVAL#v}
|
||||||
|
else
|
||||||
|
_P9K_RETVAL=system
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1167,16 +1264,10 @@ prompt_node_version() {
|
||||||
# Segment to display Node version from NVM
|
# Segment to display Node version from NVM
|
||||||
# Only prints the segment if different than the default value
|
# Only prints the segment if different than the default value
|
||||||
prompt_nvm() {
|
prompt_nvm() {
|
||||||
local node_version nvm_default
|
[[ -n $NVM_DIR ]] && _p9k_nvm_ls_current || return
|
||||||
(( $+functions[nvm_version] )) || return
|
local current=$_P9K_RETVAL
|
||||||
|
! _p9k_nvm_ls_default || [[ $_P9K_RETVAL != $current ]] || return
|
||||||
node_version=$(nvm_version current)
|
$1_prompt_segment "$0" "$2" "magenta" "black" 'NODE_ICON' 0 '' "${${current#v}//\%/%%}"
|
||||||
[[ -z "${node_version}" || ${node_version} == "none" ]] && return
|
|
||||||
|
|
||||||
nvm_default=$(nvm_version default)
|
|
||||||
[[ "$node_version" =~ "$nvm_default" ]] && return
|
|
||||||
|
|
||||||
$1_prompt_segment "$0" "$2" "magenta" "black" 'NODE_ICON' 0 '' "${${node_version:1}//\%/%%}"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
################################################################
|
################################################################
|
||||||
|
@ -2405,5 +2496,6 @@ autoload -Uz add-zsh-hook
|
||||||
zmodload zsh/datetime
|
zmodload zsh/datetime
|
||||||
zmodload zsh/mathfunc
|
zmodload zsh/mathfunc
|
||||||
zmodload zsh/system
|
zmodload zsh/system
|
||||||
|
zmodload -F zsh/stat b:zstat
|
||||||
|
|
||||||
prompt_powerlevel9k_setup "$@"
|
prompt_powerlevel9k_setup "$@"
|
||||||
|
|
Loading…
Reference in a new issue