From b56ab6400c8f86ffe6bfeba29d04d5d4388ae463 Mon Sep 17 00:00:00 2001
From: romkatv <roman.perepelitsa@gmail.com>
Date: Tue, 8 Oct 2019 16:03:39 +0200
Subject: revamp aws_eb_env. it returns the starred entry from the output of
 "eb list"

---
 config/p10k-classic.zsh |  7 +++++++
 config/p10k-lean.zsh    |  7 +++++++
 internal/p10k.zsh       | 24 ++++++++++++++++++++----
 3 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/config/p10k-classic.zsh b/config/p10k-classic.zsh
index 5ea2cdba..7b9ae002 100644
--- a/config/p10k-classic.zsh
+++ b/config/p10k-classic.zsh
@@ -59,6 +59,7 @@
       kubecontext             # current kubernetes context (https://kubernetes.io/)
       terraform               # terraform workspace (https://www.terraform.io)
       aws                     # aws profile (https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html)
+      # aws_eb_env            # aws elastic beanstalk environment (https://aws.amazon.com/elasticbeanstalk/)
       # azure                 # azure account name (https://docs.microsoft.com/en-us/cli/azure)
       context                 # user@hostname
       nordvpn                 # nordvpn connection status, linux only (https://nordvpn.com/)
@@ -659,6 +660,12 @@
   # Custom icon.
   # typeset -g POWERLEVEL9K_AWS_VISUAL_IDENTIFIER_EXPANSION='⭐'
 
+  #[ aws_eb_env: aws elastic beanstalk environment (https://aws.amazon.com/elasticbeanstalk/) ]#
+  # AWS Elastic Beanstalk environment color.
+  typeset -g POWERLEVEL9K_AWS_EB_ENV_FOREGROUND=70
+  # Custom icon.
+  # typeset -g POWERLEVEL9K_AWS_EB_ENV_VISUAL_IDENTIFIER_EXPANSION='⭐'
+
   ##########[ azure: azure account name (https://docs.microsoft.com/en-us/cli/azure) ]##########
   # Azure account name color.
   typeset -g POWERLEVEL9K_AZURE_FOREGROUND=32
diff --git a/config/p10k-lean.zsh b/config/p10k-lean.zsh
index 999738d9..d350785b 100644
--- a/config/p10k-lean.zsh
+++ b/config/p10k-lean.zsh
@@ -59,6 +59,7 @@
       kubecontext             # current kubernetes context (https://kubernetes.io/)
       terraform               # terraform workspace (https://www.terraform.io)
       aws                     # aws profile (https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html)
+      # aws_eb_env            # aws elastic beanstalk environment (https://aws.amazon.com/elasticbeanstalk/)
       # azure                 # azure account name (https://docs.microsoft.com/en-us/cli/azure)
       context                 # user@hostname
       nordvpn                 # nordvpn connection status, linux only (https://nordvpn.com/)
@@ -719,6 +720,12 @@
   # Custom icon.
   # typeset -g POWERLEVEL9K_AWS_VISUAL_IDENTIFIER_EXPANSION='⭐'
 
+  #[ aws_eb_env: aws elastic beanstalk environment (https://aws.amazon.com/elasticbeanstalk/) ]#
+  # AWS Elastic Beanstalk environment color.
+  typeset -g POWERLEVEL9K_AWS_EB_ENV_FOREGROUND=70
+  # Custom icon.
+  # typeset -g POWERLEVEL9K_AWS_EB_ENV_VISUAL_IDENTIFIER_EXPANSION='⭐'
+
   ##########[ azure: azure account name (https://docs.microsoft.com/en-us/cli/azure) ]##########
   # Azure account name color.
   typeset -g POWERLEVEL9K_AZURE_FOREGROUND=32
diff --git a/internal/p10k.zsh b/internal/p10k.zsh
index 93c11b3b..bf5fd840 100755
--- a/internal/p10k.zsh
+++ b/internal/p10k.zsh
@@ -885,10 +885,26 @@ prompt_aws() {
 ################################################################
 # Current Elastic Beanstalk environment
 prompt_aws_eb_env() {
-  [[ -r .elasticbeanstalk/config.yml ]] || return
-  local v=($(grep environment .elasticbeanstalk/config.yml 2>/dev/null))
-  [[ $#v > 1 && -n $v[2] ]] || return
-  [[ -n $v ]] && _p9k_prompt_segment "$0" black green 'AWS_EB_ICON' 0 '' "${v[2]//\%/%%}"
+  (( $+commands[eb] )) || return
+
+  local dir=$_p9k_pwd
+  while true; do
+    [[ $dir == / ]] && return
+    [[ -d $dir/.elasticbeanstalk ]] && break
+    dir=${dir:h}
+  done
+
+  local -H stat
+  zstat -H stat -- $dir/.elasticbeanstalk/config.yml 2>/dev/null || return
+  local sig="$stat[inode].$stat[mtime].$stat[size].$stat[mode]"
+  if ! _p9k_cache_get $0 $dir || [[ $_p9k_cache_val[1] != $sig ]]; then
+    local env
+    env="$(command eb list 2>/dev/null)" || env=
+    env="${${(@M)${(@f)env}:#\* *}#\* }"
+    _p9k_cache_set "$sig" "$env"
+  fi
+  [[ -n $_p9k_cache_val[2] ]] || return
+  _p9k_prompt_segment "$0" black green 'AWS_EB_ICON' 0 '' "${_p9k_cache_val[2]//\%/%%}"
 }
 
 ################################################################
-- 
cgit v1.2.3