diff options
author | romkatv <roman.perepelitsa@gmail.com> | 2020-03-09 19:38:52 +0300 |
---|---|---|
committer | romkatv <roman.perepelitsa@gmail.com> | 2020-03-09 19:38:52 +0300 |
commit | db1e12bce3fdb82e38d4d9ef72b8c2a6df0d8708 (patch) | |
tree | 1c2f3153109d5b70e205cacfd02383a42dbd9bc4 /internal/p10k.zsh | |
parent | 95008fc87195bf657903ad27d41c06718521a80d (diff) |
add `package` prompt segment
Diffstat (limited to 'internal/p10k.zsh')
-rw-r--r-- | internal/p10k.zsh | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/internal/p10k.zsh b/internal/p10k.zsh index 5034f0ee..18f4161c 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -2053,6 +2053,59 @@ prompt_history() { (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1] } +prompt_package() { + unset P9K_PACKAGE_NAME P9K_PACKAGE_VERSION + _p9k_upglob package.json && return + + local file=$_p9k__parent_dirs[$?]/package.json + if ! _p9k_cache_stat_get $0 $file; then + () { + local data field + local -A found + { data="$(<$file)" || return } 2>/dev/null + data=${data##[[:space:]]#} + [[ $data == '{'* ]] || return + data[1]= + local -i depth=1 + while true; do + data=${data##[[:space:]]#} + [[ -n $data ]] || return + case $data[1] in + '{'|'[') data[1]=; (( ++depth ));; + '}'|']') data[1]=; (( --depth > 0 )) || return;; + ':') data[1]=;; + ',') data[1]=; field=;; + [[:alnum:].]) data=${data##[[:alnum:].]#};; + '"') + local tail=${data##\"([^\"\\]|\\?)#} + [[ $tail == '"'* ]] || return + local s=${data:1:-$#tail} + data=${tail:1} + (( depth == 1 )) || continue + if [[ -z $field ]]; then + field=${s:-x} + elif [[ $field == (name|version) ]]; then + (( ! $+found[$field] )) || return + [[ -n $s ]] || return + print -v 'found[$field]' -- $s # this isn't quite right but close enough + (( $#found == 2 )) && break + fi + ;; + *) return 1;; + esac + done + _p9k_cache_stat_set 1 $found[name] $found[version] + return 0 + } || _p9k_cache_stat_set 0 + fi + (( _p9k__cache_val[1] )) || return + + P9K_PACKAGE_NAME=$_p9k__cache_val[2] + P9K_PACKAGE_VERSION=$_p9k__cache_val[3] + local text="${${P9K_PACKAGE_NAME##*/}//\%/%%}@${P9K_PACKAGE_VERSION//\%/%%}" + _p9k_prompt_segment "$0" "cyan" "$_p9k_color1" PACKAGE_ICON 0 '' $text +} + ################################################################ # Detection for virtualization (systemd based systems only) prompt_detect_virt() { @@ -7442,7 +7495,7 @@ _p9k_must_init() { [[ $sig == $_p9k__param_sig ]] && return 1 _p9k_deinit fi - _p9k__param_pat=$'v70\1'${ZSH_VERSION}$'\1'${ZSH_PATCHLEVEL}$'\1' + _p9k__param_pat=$'v71\1'${ZSH_VERSION}$'\1'${ZSH_PATCHLEVEL}$'\1' _p9k__param_pat+=$'${#parameters[(I)POWERLEVEL9K_*]}\1${(%):-%n%#}\1$GITSTATUS_LOG_LEVEL\1' _p9k__param_pat+=$'$GITSTATUS_ENABLE_LOGGING\1$GITSTATUS_DAEMON\1$GITSTATUS_NUM_THREADS\1' _p9k__param_pat+=$'$DEFAULT_USER\1${ZLE_RPROMPT_INDENT:-1}\1$P9K_SSH\1$__p9k_ksh_arrays\1' |