From f68e055f3bf0403cb4c4a9b3b79adb8471fe3872 Mon Sep 17 00:00:00 2001 From: Ben Hilburn Date: Mon, 13 Feb 2017 21:50:37 -0500 Subject: [PATCH] Merging the `ssh` detection into `context` segment, resolving conflicts. --- README.md | 20 ++++++++------------ powerlevel9k.zsh-theme | 32 +++++++++++++------------------- 2 files changed, 21 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 3b7542fc..d6de6843 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ The segments that are currently available are: **System Status Segments:** * [`background_jobs`](#background_jobs) - Indicator for background jobs. * [`battery`](#battery) - Current battery status. -* [`context`](#context) - Your username and host. +* [`context`](#context) - Your username and host, conditionalized based on $USER and SSH status. * [`dir`](#dir) - Your current working directory. * `dir_writable` - Displays a lock icon, if you do not have write permissions on the current folder. * [`disk_usage`](#disk_usage) - Disk usage of your current partition. @@ -238,29 +238,25 @@ main theme distribution so that everyone can use it! ##### context -The `context` segment (user@host string) is conditional. This lets you enable -it, but only display it if you are not your normal user or on a remote host -(basically, only print it when it's likely you need it). +The `context` segment (user@host string) is conditional. By default, it will +only print if you are not your 'normal' user (including if you are root), or if +you are SSH'd to a remote host. To use this feature, make sure the `context` segment is enabled in your prompt elements (it is by default), and define a `DEFAULT_USER` in your `~/.zshrc`: You can set the `POWERLEVEL9K_CONTEXT_HOST_DEPTH` variable to change how the hostname is displayed. See (ZSH Manual)[http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Login-information] -for details. Default is set to %m which will show the hostname up to the first ‘.’ +for details. The default is set to %m which will show the hostname up to the first ‘.’ You can set it to %{N}m where N is an integer to show that many segments of system hostname. Setting N to a negative integer will show that many segments from the end of the hostname. | Variable | Default Value | Description | |----------|---------------|-------------| -|`DEFAULT_USER`|None|Username to consider a "default context" (you can also use `$USER`)| -|`POWERLEVEL9K_CONTEXT_HOST_DEPTH`|%m|Customizable host depth on prompt| - -You can use POWERLEVEL9K_HIDE_HOST for hiding the hostname in the prompt -when you are not in a ssh session. For example: - - POWERLEVEL9K_HIDE_HOST="yes" +|`DEFAULT_USER`|None|Username to consider a "default context".| +|`POWERLEVEL9K_CONTEXT_HOST_DEPTH`|%m|Customizable host depth on prompt.| +|`POWERLEVEL9K_ALWAYS_SHOW_CONTEXT`|false|Always show this segment.| ##### dir diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index b1de8f84..49fdaf09 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -537,26 +537,27 @@ prompt_public_ip() { # Context: user@hostname (who am I and where am I) # Note that if $DEFAULT_USER is not set, this prompt segment will always print +set_default POWERLEVEL9K_ALWAYS_SHOW_CONTEXT false +set_default POWERLEVEL9K_CONTEXT_HOST_DEPTH "%m" prompt_context() { local current_state="DEFAULT" - declare -A context_states + typeset -AH context_states context_states=( "ROOT" "yellow" "DEFAULT" "011" ) + local content="$USER" - if [[ "$USER" != "$DEFAULT_USER" ]]; then - if [[ $(print -P "%#") == '#' ]]; then - current_state="ROOT" - fi - if [[ -z "$SSH_CLIENT" && -z "$SSH_TTY" ]]; then - if [[ "$POWERLEVEL9K_HIDE_HOST" == "false" || -z "$POWERLEVEL9K_HIDE_HOST" ]]; then - content="${content}@%m" + + if [[ "POWERLEVEL9K_ALWAYS_SHOW_CONTEXT" == true ]] || [[ "$USER" != "$DEFAULT_USER" ]] || [[ -n "$SSH_CLIENT" || -n "$SSH_TTY" ]]; then + + if [[ $(print -P "%#") == '#' ]]; then + current_state="ROOT" fi - else - content="${content}@%m" - fi - "$1_prompt_segment" "${0}_${current_state}" "$2" "$DEFAULT_COLOR" "${context_states[$current_state]}" "${content}" + + content="${content}@${POWERLEVEL9K_CONTEXT_HOST_DEPTH}" + + "$1_prompt_segment" "${0}_${current_state}" "$2" "$DEFAULT_COLOR" "${context_states[$current_state]}" "${content}" fi } @@ -704,13 +705,6 @@ prompt_history() { "$1_prompt_segment" "$0" "$2" "244" "$DEFAULT_COLOR" '%h' } -prompt_detect_ssh(){ - local color="yellow" - if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then - "$1_prompt_segment" "$0" "$2" "$DEFAULT_COLOR" "$color" "ssh" - fi -} - # Detection for virtualization (systemd based systems only) prompt_detect_virt() { if ! command -v systemd-detect-virt;then