From db1e12bce3fdb82e38d4d9ef72b8c2a6df0d8708 Mon Sep 17 00:00:00 2001
From: romkatv <roman.perepelitsa@gmail.com>
Date: Mon, 9 Mar 2020 17:38:52 +0100
Subject: add `package` prompt segment

---
 internal/icons.zsh |  6 ++++++
 internal/notes.txt |  2 --
 internal/p10k.zsh  | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 60 insertions(+), 3 deletions(-)

(limited to 'internal')

diff --git a/internal/icons.zsh b/internal/icons.zsh
index 7bd794b2..2165510f 100644
--- a/internal/icons.zsh
+++ b/internal/icons.zsh
@@ -139,6 +139,7 @@ function _p9k_init_icons() {
         POSTGRES_ICON                  'postgres'
         PHP_ICON                       'php'
         HASKELL_ICON                   'hs'
+        PACKAGE_ICON                   'pkg'
       )
     ;;
     'awesome-fontconfig')
@@ -263,6 +264,7 @@ function _p9k_init_icons() {
         POSTGRES_ICON                  'postgres'
         PHP_ICON                       'php'
         HASKELL_ICON                   'hs'
+        PACKAGE_ICON                   'pkg'
       )
     ;;
     'awesome-mapped-fontconfig')
@@ -390,6 +392,7 @@ function _p9k_init_icons() {
         POSTGRES_ICON                  'postgres'
         PHP_ICON                       'php'
         HASKELL_ICON                   'hs'
+        PACKAGE_ICON                   'pkg'
       )
     ;;
     'nerdfont-complete'|'nerdfont-fontconfig')
@@ -515,6 +518,7 @@ function _p9k_init_icons() {
         POSTGRES_ICON                  '\uE76E'               # 
         PHP_ICON                       '\uE608'               # 
         HASKELL_ICON                   '\uE61F'               # 
+        PACKAGE_ICON                   '\uF8D6'               # 
       )
     ;;
     ascii)
@@ -637,6 +641,7 @@ function _p9k_init_icons() {
         POSTGRES_ICON                  'postgres'
         PHP_ICON                       'php'
         HASKELL_ICON                   'hs'
+        PACKAGE_ICON                   'pkg'
       )
     ;;
     *)
@@ -761,6 +766,7 @@ function _p9k_init_icons() {
         POSTGRES_ICON                  'postgres'
         PHP_ICON                       'php'
         HASKELL_ICON                   'hs'
+        PACKAGE_ICON                   'pkg'
       )
     ;;
   esac
diff --git a/internal/notes.txt b/internal/notes.txt
index 82d84ea1..4bc7ff65 100644
--- a/internal/notes.txt
+++ b/internal/notes.txt
@@ -40,5 +40,3 @@
   segments would have "unknown" icons by default (yellow and not bold); can allow them to
   participate by defining `explainprompt_foo` that populates array `reply` with strings like this:
   '-s STATE -i LOCK_ICON +r'; the first element must be segment description.
-- add `package` segment that displays name@version where name is `.name` and version is `.version`
-  from package.json. Expose them as P9K_PACKAGE_NAME and P9K_PACKAGE_VERSION.
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'
-- 
cgit v1.2.3