From 18701f4e4964c7ff46f87b392bd69215531e4709 Mon Sep 17 00:00:00 2001 From: romkatv Date: Sat, 29 Feb 2020 12:10:58 +0100 Subject: [PATCH] add haskell_stack prompt segment (#535) --- README.md | 1 + config/p10k-classic.zsh | 15 ++++++++++ config/p10k-lean-8colors.zsh | 15 ++++++++++ config/p10k-lean.zsh | 15 ++++++++++ config/p10k-rainbow.zsh | 17 ++++++++++++ internal/icons.zsh | 6 ++++ internal/p10k.zsh | 53 +++++++++++++++++++++++++++++++++++- 7 files changed, 121 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d045b2ac..824fe38c 100644 --- a/README.md +++ b/README.md @@ -281,6 +281,7 @@ enable as many segments as you like. It won't slow down your prompt or Zsh start | `jenv` | java environment from [jenv](https://github.com/jenv/jenv) | | `plenv` | perl environment from [plenv](https://github.com/tokuhirom/plenv) | | `phpenv` | php environment from [phpenv](https://github.com/phpenv/phpenv) | +| `haskell_stack` | haskell version from [stack](https://haskellstack.org/) | | `node_version` | [node.js](https://nodejs.org/) version | | `go_version` | [go](https://golang.org) version | | `rust_version` | [rustc](https://www.rust-lang.org) version | diff --git a/config/p10k-classic.zsh b/config/p10k-classic.zsh index 139dff13..2cf39d9e 100644 --- a/config/p10k-classic.zsh +++ b/config/p10k-classic.zsh @@ -72,6 +72,7 @@ jenv # java version from jenv (https://github.com/jenv/jenv) plenv # perl version from plenv (https://github.com/tokuhirom/plenv) phpenv # php version from phpenv (https://github.com/phpenv/phpenv) + haskell_stack # haskell version from stack (https://haskellstack.org/) 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) @@ -1061,6 +1062,20 @@ # Custom icon. # typeset -g POWERLEVEL9K_PHPENV_VISUAL_IDENTIFIER_EXPANSION='⭐' + ##########[ haskell_stack: haskell version from stack (https://haskellstack.org/) ]########### + # Haskell color. + typeset -g POWERLEVEL9K_HASKELL_STACK_FOREGROUND=172 + # Hide haskell version if it doesn't come from one of these sources. + # + # shell: version is set by STACK_YAML + # local: version is set by stack.yaml up the directory tree + # global: version is set by the implicit global project (~/.stack/global-project/stack.yaml) + typeset -g POWERLEVEL9K_HASKELL_STACK_SOURCES=(shell local) + # If set to false, hide haskell version if it's the same as in the implicit global project. + typeset -g POWERLEVEL9K_HASKELL_STACK_ALWAYS_SHOW=true + # Custom icon. + # typeset -g POWERLEVEL9K_HASKELL_STACK_VISUAL_IDENTIFIER_EXPANSION='⭐' + ################[ terraform: terraform workspace (https://www.terraform.io) ]################# # POWERLEVEL9K_TERRAFORM_CLASSES is an array with even number of elements. The first element # in each pair defines a pattern against which the current terraform workspace gets matched. diff --git a/config/p10k-lean-8colors.zsh b/config/p10k-lean-8colors.zsh index 358566ef..8ca84717 100644 --- a/config/p10k-lean-8colors.zsh +++ b/config/p10k-lean-8colors.zsh @@ -72,6 +72,7 @@ jenv # java version from jenv (https://github.com/jenv/jenv) plenv # perl version from plenv (https://github.com/tokuhirom/plenv) phpenv # php version from phpenv (https://github.com/phpenv/phpenv) + haskell_stack # haskell version from stack (https://haskellstack.org/) 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) @@ -1040,6 +1041,20 @@ # Custom icon. # typeset -g POWERLEVEL9K_PHPENV_VISUAL_IDENTIFIER_EXPANSION='⭐' + ##########[ haskell_stack: haskell version from stack (https://haskellstack.org/) ]########### + # Haskell color. + typeset -g POWERLEVEL9K_HASKELL_STACK_FOREGROUND=3 + # Hide haskell version if it doesn't come from one of these sources. + # + # shell: version is set by STACK_YAML + # local: version is set by stack.yaml up the directory tree + # global: version is set by the implicit global project (~/.stack/global-project/stack.yaml) + typeset -g POWERLEVEL9K_HASKELL_STACK_SOURCES=(shell local) + # If set to false, hide haskell version if it's the same as in the implicit global project. + typeset -g POWERLEVEL9K_HASKELL_STACK_ALWAYS_SHOW=true + # Custom icon. + # typeset -g POWERLEVEL9K_HASKELL_STACK_VISUAL_IDENTIFIER_EXPANSION='⭐' + #############[ kubecontext: current kubernetes context (https://kubernetes.io/) ]############# # Show kubecontext only when the the command you are typing invokes one of these tools. # Tip: Remove the next line to always show kubecontext. diff --git a/config/p10k-lean.zsh b/config/p10k-lean.zsh index b569071d..d2420d26 100644 --- a/config/p10k-lean.zsh +++ b/config/p10k-lean.zsh @@ -72,6 +72,7 @@ jenv # java version from jenv (https://github.com/jenv/jenv) plenv # perl version from plenv (https://github.com/tokuhirom/plenv) phpenv # php version from phpenv (https://github.com/phpenv/phpenv) + haskell_stack # haskell version from stack (https://haskellstack.org/) 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) @@ -1040,6 +1041,20 @@ # Custom icon. # typeset -g POWERLEVEL9K_PHPENV_VISUAL_IDENTIFIER_EXPANSION='⭐' + ##########[ haskell_stack: haskell version from stack (https://haskellstack.org/) ]########### + # Haskell color. + typeset -g POWERLEVEL9K_HASKELL_STACK_FOREGROUND=172 + # Hide haskell version if it doesn't come from one of these sources. + # + # shell: version is set by STACK_YAML + # local: version is set by stack.yaml up the directory tree + # global: version is set by the implicit global project (~/.stack/global-project/stack.yaml) + typeset -g POWERLEVEL9K_HASKELL_STACK_SOURCES=(shell local) + # If set to false, hide haskell version if it's the same as in the implicit global project. + typeset -g POWERLEVEL9K_HASKELL_STACK_ALWAYS_SHOW=true + # Custom icon. + # typeset -g POWERLEVEL9K_HASKELL_STACK_VISUAL_IDENTIFIER_EXPANSION='⭐' + #############[ kubecontext: current kubernetes context (https://kubernetes.io/) ]############# # Show kubecontext only when the the command you are typing invokes one of these tools. # Tip: Remove the next line to always show kubecontext. diff --git a/config/p10k-rainbow.zsh b/config/p10k-rainbow.zsh index 578146f2..97dc3ab1 100644 --- a/config/p10k-rainbow.zsh +++ b/config/p10k-rainbow.zsh @@ -72,6 +72,7 @@ jenv # java version from jenv (https://github.com/jenv/jenv) plenv # perl version from plenv (https://github.com/tokuhirom/plenv) phpenv # php version from phpenv (https://github.com/phpenv/phpenv) + haskell_stack # haskell version from stack (https://haskellstack.org/) 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) @@ -1114,6 +1115,22 @@ # Custom icon. # typeset -g POWERLEVEL9K_PHPENV_VISUAL_IDENTIFIER_EXPANSION='⭐' + ##########[ haskell_stack: haskell version from stack (https://haskellstack.org/) ]########### + # Haskell color. + # typeset -g POWERLEVEL9K_HASKELL_STACK_FOREGROUND=0 + # typeset -g POWERLEVEL9K_HASKELL_STACK_BACKGROUND=3 + + # Hide haskell version if it doesn't come from one of these sources. + # + # shell: version is set by STACK_YAML + # local: version is set by stack.yaml up the directory tree + # global: version is set by the implicit global project (~/.stack/global-project/stack.yaml) + typeset -g POWERLEVEL9K_HASKELL_STACK_SOURCES=(shell local) + # If set to false, hide haskell version if it's the same as in the implicit global project. + typeset -g POWERLEVEL9K_HASKELL_STACK_ALWAYS_SHOW=true + # Custom icon. + # typeset -g POWERLEVEL9K_HASKELL_STACK_VISUAL_IDENTIFIER_EXPANSION='⭐' + ################[ terraform: terraform workspace (https://www.terraform.io) ]################# # POWERLEVEL9K_TERRAFORM_CLASSES is an array with even number of elements. The first element # in each pair defines a pattern against which the current terraform workspace gets matched. diff --git a/internal/icons.zsh b/internal/icons.zsh index d7d336ce..f1d54fd7 100644 --- a/internal/icons.zsh +++ b/internal/icons.zsh @@ -138,6 +138,7 @@ function _p9k_init_icons() { ELIXIR_ICON 'elixir' POSTGRES_ICON 'postgres' PHP_ICON 'php' + HASKELL_ICON 'hs' ) ;; 'awesome-fontconfig') @@ -261,6 +262,7 @@ function _p9k_init_icons() { ELIXIR_ICON 'elixir' POSTGRES_ICON 'postgres' PHP_ICON 'php' + HASKELL_ICON 'hs' ) ;; 'awesome-mapped-fontconfig') @@ -387,6 +389,7 @@ function _p9k_init_icons() { ELIXIR_ICON 'elixir' POSTGRES_ICON 'postgres' PHP_ICON 'php' + HASKELL_ICON 'hs' ) ;; 'nerdfont-complete'|'nerdfont-fontconfig') @@ -511,6 +514,7 @@ function _p9k_init_icons() { ELIXIR_ICON '\uE62D' #  POSTGRES_ICON '\uE76E' #  PHP_ICON '\uE608' #  + HASKELL_ICON '\uE61F' #  ) ;; ascii) @@ -632,6 +636,7 @@ function _p9k_init_icons() { ELIXIR_ICON 'elixir' POSTGRES_ICON 'postgres' PHP_ICON 'php' + HASKELL_ICON 'hs' ) ;; *) @@ -755,6 +760,7 @@ function _p9k_init_icons() { ELIXIR_ICON 'elixir' POSTGRES_ICON 'postgres' PHP_ICON 'php' + HASKELL_ICON 'hs' ) ;; esac diff --git a/internal/p10k.zsh b/internal/p10k.zsh index c9361db9..de75c559 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -4968,6 +4968,55 @@ _p9k_prompt_asdf_init() { typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${commands[asdf]:-${${+functions[asdf]}:#0}}' } +_p9k_haskell_stack_version() { + if ! _p9k_cache_stat_get $0 $1 ${STACK_ROOT:-~/.stack}/{pantry/pantry.sqlite3,stack.sqlite3}; then + local v + v="$(STACK_YAML=$1 stack \ + --silent \ + --no-install-ghc \ + --skip-ghc-check \ + --no-terminal \ + --color=never \ + --lock-file=read-only \ + query compiler actual)" || v= + _p9k_cache_stat_set "$v" + fi + _p9k__ret=$_p9k__cache_val[1] +} + +prompt_haskell_stack() { + if [[ -n $STACK_YAML ]]; then + (( ${_POWERLEVEL9K_HASKELL_STACK_SOURCES[(I)shell]} )) || return + _p9k_haskell_stack_version $STACK_YAML + else + (( ${_POWERLEVEL9K_HASKELL_STACK_SOURCES[(I)local|global]} )) || return + if _p9k_upglob stack.yaml; then + (( _POWERLEVEL9K_HASKELL_STACK_PROMPT_ALWAYS_SHOW )) || return + (( ${_POWERLEVEL9K_HASKELL_STACK_SOURCES[(I)global]} )) || return + _p9k_haskell_stack_version ${STACK_ROOT:-~/.stack}/global-project/stack.yaml + else + local -i idx=$? + (( ${_POWERLEVEL9K_HASKELL_STACK_SOURCES[(I)local]} )) || return + _p9k_haskell_stack_version $_p9k__parent_dirs[idx]/stack.yaml + fi + fi + + [[ -n $_p9k__ret ]] || return + + local v=$_p9k__ret + + if (( !_POWERLEVEL9K_HASKELL_STACK_PROMPT_ALWAYS_SHOW )); then + _p9k_haskell_stack_version ${STACK_ROOT:-~/.stack}/global-project/stack.yaml + [[ $v == $_p9k__ret ]] && return + fi + + _p9k_prompt_segment "$0" "yellow" "$_p9k_color1" 'HASKELL_ICON' 0 '' "${v//\%/%%}" +} + +_p9k_prompt_haskell_stack_init() { + typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[stack]' +} + # Use two preexec hooks to survive https://github.com/MichaelAquilina/zsh-you-should-use with # YSU_HARDCORE=1. See https://github.com/romkatv/powerlevel10k/issues/427. _p9k_preexec1() { @@ -6609,6 +6658,8 @@ _p9k_init_params() { _p9k_declare -b POWERLEVEL9K_JAVA_VERSION_PROJECT_ONLY 0 _p9k_declare -b POWERLEVEL9K_RBENV_PROMPT_ALWAYS_SHOW 0 _p9k_declare -a POWERLEVEL9K_RBENV_SOURCES -- shell local global + _p9k_declare -b POWERLEVEL9K_HASKELL_STACK_PROMPT_ALWAYS_SHOW 1 + _p9k_declare -a POWERLEVEL9K_HASKELL_STACK_SOURCES -- shell local _p9k_declare -b POWERLEVEL9K_PHPENV_PROMPT_ALWAYS_SHOW 0 _p9k_declare -a POWERLEVEL9K_PHPENV_SOURCES -- shell local global _p9k_declare -b POWERLEVEL9K_LUAENV_PROMPT_ALWAYS_SHOW 0 @@ -7286,7 +7337,7 @@ _p9k_must_init() { [[ $sig == $_p9k__param_sig ]] && return 1 _p9k_deinit fi - _p9k__param_pat=$'v64\1'${ZSH_VERSION}$'\1'${ZSH_PATCHLEVEL}$'\1' + _p9k__param_pat=$'v65\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'