From 727d42ff489f9be998df97e90f4356b2faaffbc5 Mon Sep 17 00:00:00 2001 From: romkatv Date: Tue, 20 Aug 2019 11:19:11 +0200 Subject: [PATCH] expose P9K_SSH and make it available as soon as p10k is sourced --- internal/p10k.zsh | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/internal/p10k.zsh b/internal/p10k.zsh index 437a87cf..467985d7 100755 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -1062,7 +1062,7 @@ prompt_context() { if ! _p9k_cache_get $0 "${(%):-%#}"; then local -i enabled=1 local content - if [[ $_POWERLEVEL9K_ALWAYS_SHOW_CONTEXT == 0 && -n $DEFAULT_USER && $_P9K_SSH == 0 ]]; then + if [[ $_POWERLEVEL9K_ALWAYS_SHOW_CONTEXT == 0 && -n $DEFAULT_USER && $P9K_SSH == 0 ]]; then local user="$(whoami)" if [[ $user == $DEFAULT_USER ]]; then if (( _POWERLEVEL9K_ALWAYS_SHOW_USER )); then @@ -1078,7 +1078,7 @@ prompt_context() { state="DEFAULT" if [[ "${(%):-%#}" == '#' ]]; then state="ROOT" - elif (( _P9K_SSH )); then + elif (( P9K_SSH )); then if [[ -n "$SUDO_COMMAND" ]]; then state="REMOTE_SUDO" else @@ -1128,7 +1128,7 @@ prompt_user() { ################################################################ # Host: machine (where am I) prompt_host() { - if (( _P9K_SSH )); then + if (( P9K_SSH )); then _p9k_prompt_segment "$0_REMOTE" "${_p9k_color1}" yellow SSH_ICON 0 '' "$_POWERLEVEL9K_HOST_TEMPLATE" else _p9k_prompt_segment "$0_LOCAL" "${_p9k_color1}" yellow HOST_ICON 0 '' "$_POWERLEVEL9K_HOST_TEMPLATE" @@ -1994,7 +1994,7 @@ prompt_rvm() { ################################################################ # Segment to display SSH icon when connected prompt_ssh() { - if (( _P9K_SSH )); then + if (( P9K_SSH )); then _p9k_prompt_segment "$0" "$_p9k_color1" "yellow" 'SSH_ICON' 0 '' '' fi } @@ -4039,25 +4039,27 @@ _p9k_init_ssh() { # # License: https://github.com/sindresorhus/pure/blob/e8abf9d37185ec9b7b4398ca9c5eba555a1028eb/license. - [[ -n $_P9K_SSH ]] && return - export _P9K_SSH=0 + [[ -n $P9K_SSH ]] && return + typeset -gix P9K_SSH=0 if [[ -n $SSH_CLIENT || -n $SSH_TTY || -n $SSH_CONNECTION ]]; then - _P9K_SSH=1 + P9K_SSH=1 return fi # When changing user on a remote system, the $SSH_CONNECTION environment variable can be lost. # Attempt detection via `who`. (( $+commands[who] )) || return - local w && w="$(who -m 2>/dev/null)" || w=${(@M)${(f)"$(who 2>/dev/null)"}:#*[[:space:]]${TTY#/dev/}[[:space:]]*} local ipv6='(([0-9a-fA-F]+:)|:){2,}[0-9a-fA-F]+' # Simplified, only checks partial pattern. local ipv4='([0-9]{1,3}\.){3}[0-9]+' # Simplified, allows invalid ranges. # Assume two non-consecutive periods represents a hostname. Matches `x.y.z`, but not `x.y`. local hostname='([.][^. ]+){2}' + local w + w="$(who -m 2>/dev/null)" || w=${(@M)${(f)"$(who 2>/dev/null)"}:#*[[:space:]]${TTY#/dev/}[[:space:]]*} + # Usually the remote address is surrounded by parenthesis but not on all systems (e.g., Busybox). - [[ $w =~ "\(?($ipv4|$ipv6|$hostname)\)?\$" ]] && _P9K_SSH=1 + [[ $w =~ "\(?($ipv4|$ipv6|$hostname)\)?\$" ]] && P9K_SSH=1 } _p9k_must_init() { @@ -4086,7 +4088,6 @@ _p9k_init() { _p9k_init_vars _p9k_init_params _p9k_init_prompt - _p9k_init_ssh local uname="$(uname)" if [[ $uname == Linux && "$(uname -o 2>/dev/null)" == Android ]]; then @@ -4505,4 +4506,5 @@ zmodload zsh/system zmodload -F zsh/stat b:zstat zmodload -F zsh/net/socket b:zsocket +_p9k_init_ssh prompt_powerlevel9k_setup