aboutsummaryrefslogtreecommitdiff
path: root/powerlevel9k.zsh-theme
diff options
context:
space:
mode:
authorYang Tang <ytang@users.noreply.github.com>2018-10-12 18:29:50 +0300
committerGitHub <noreply@github.com>2018-10-12 18:29:50 +0300
commita98fa7ff54a0dc7b926deec99b69d5a0a0864a94 (patch)
treeb35d3df1b0ad12324f4094af98a576c6c933571e /powerlevel9k.zsh-theme
parent24bbbfc9bdbdc0666a53cd4a60a8b57551e9f0ca (diff)
parentb16c0d51d65249f0fdea86cd12cd64f6f0643b7e (diff)
Merge branch 'master' into fix-rprompt-icons-cut-off
Diffstat (limited to 'powerlevel9k.zsh-theme')
-rwxr-xr-xpowerlevel9k.zsh-theme155
1 files changed, 86 insertions, 69 deletions
diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme
index 1e9d1123..2c028a8b 100755
--- a/powerlevel9k.zsh-theme
+++ b/powerlevel9k.zsh-theme
@@ -111,45 +111,54 @@ CURRENT_BG='NONE'
set_default last_left_element_index 1
set_default POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS " "
left_prompt_segment() {
+ local segment_name="${1}"
local current_index=$2
# Check if the segment should be joined with the previous one
local joined
segmentShouldBeJoined $current_index $last_left_element_index "$POWERLEVEL9K_LEFT_PROMPT_ELEMENTS" && joined=true || joined=false
+ # Colors
+ local backgroundColor="${3}"
+ local foregroundColor="${4}"
+
# Overwrite given background-color by user defined variable for this segment.
- local BACKGROUND_USER_VARIABLE=POWERLEVEL9K_${(U)1#prompt_}_BACKGROUND
+ local BACKGROUND_USER_VARIABLE=POWERLEVEL9K_${(U)${segment_name}#prompt_}_BACKGROUND
local BG_COLOR_MODIFIER=${(P)BACKGROUND_USER_VARIABLE}
- [[ -n $BG_COLOR_MODIFIER ]] && 3="$BG_COLOR_MODIFIER"
+ [[ -n $BG_COLOR_MODIFIER ]] && backgroundColor="$BG_COLOR_MODIFIER"
# Overwrite given foreground-color by user defined variable for this segment.
- local FOREGROUND_USER_VARIABLE=POWERLEVEL9K_${(U)1#prompt_}_FOREGROUND
+ local FOREGROUND_USER_VARIABLE=POWERLEVEL9K_${(U)${segment_name}#prompt_}_FOREGROUND
local FG_COLOR_MODIFIER=${(P)FOREGROUND_USER_VARIABLE}
- [[ -n $FG_COLOR_MODIFIER ]] && 4="$FG_COLOR_MODIFIER"
+ [[ -n $FG_COLOR_MODIFIER ]] && foregroundColor="$FG_COLOR_MODIFIER"
+
+ # Get color codes here to save some calls later on
+ backgroundColor="$(getColorCode ${backgroundColor})"
+ foregroundColor="$(getColorCode ${foregroundColor})"
- local bg fg
- [[ -n "$3" ]] && bg="$(backgroundColor $3)" || bg="$(backgroundColor)"
- [[ -n "$4" ]] && fg="$(foregroundColor $4)" || fg="$(foregroundColor)"
+ local background foreground
+ [[ -n "${backgroundColor}" ]] && background="$(backgroundColor ${backgroundColor})" || background="%k"
+ [[ -n "${foregroundColor}" ]] && foreground="$(foregroundColor ${foregroundColor})" || foreground="%f"
- if [[ $CURRENT_BG != 'NONE' ]] && ! isSameColor "$3" "$CURRENT_BG"; then
- echo -n "$bg%F{$CURRENT_BG}"
+ if [[ $CURRENT_BG != 'NONE' ]] && ! isSameColor "${backgroundColor}" "$CURRENT_BG"; then
+ echo -n "${background}%F{$CURRENT_BG}"
if [[ $joined == false ]]; then
# Middle segment
echo -n "$(print_icon 'LEFT_SEGMENT_SEPARATOR')$POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS"
fi
- elif isSameColor "$CURRENT_BG" "$3"; then
+ elif isSameColor "$CURRENT_BG" "${backgroundColor}"; then
# Middle segment with same color as previous segment
# We take the current foreground color as color for our
# subsegment (or the default color). This should have
# enough contrast.
local complement
- [[ -n "$4" ]] && complement="$fg" || complement="$(foregroundColor $DEFAULT_COLOR)"
- echo -n "${bg}${complement}"
+ [[ -n "${foregroundColor}" ]] && complement="${foreground}" || complement="$(foregroundColor $DEFAULT_COLOR)"
+ echo -n "${background}${complement}"
if [[ $joined == false ]]; then
echo -n "$(print_icon 'LEFT_SUBSEGMENT_SEPARATOR')$POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS"
fi
else
# First segment
- echo -n "${bg}$POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS"
+ echo -n "${background}$POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS"
fi
local visual_identifier
@@ -161,26 +170,26 @@ left_prompt_segment() {
# we need to color both the visual identifier and the whitespace.
[[ -n "$5" ]] && visual_identifier="$visual_identifier "
# Allow users to overwrite the color for the visual identifier only.
- local visual_identifier_color_variable=POWERLEVEL9K_${(U)1#prompt_}_VISUAL_IDENTIFIER_COLOR
- set_default $visual_identifier_color_variable $4
- visual_identifier="%F{${(P)visual_identifier_color_variable}%}$visual_identifier"
+ local visual_identifier_color_variable=POWERLEVEL9K_${(U)${segment_name}#prompt_}_VISUAL_IDENTIFIER_COLOR
+ set_default $visual_identifier_color_variable "${foregroundColor}"
+ visual_identifier="$(foregroundColor ${(P)visual_identifier_color_variable})${visual_identifier}"
fi
fi
# Print the visual identifier
echo -n "${visual_identifier}"
# Print the content of the segment, if there is any
- [[ -n "$5" ]] && echo -n "${fg}${5}"
+ [[ -n "$5" ]] && echo -n "${foreground}${5}"
echo -n "${POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS}"
- CURRENT_BG=$3
+ CURRENT_BG="${backgroundColor}"
last_left_element_index=$current_index
}
# End the left prompt, closes the final segment.
left_prompt_end() {
if [[ -n $CURRENT_BG ]]; then
- echo -n "%k%F{$CURRENT_BG}$(print_icon 'LEFT_SEGMENT_SEPARATOR')"
+ echo -n "%k$(foregroundColor ${CURRENT_BG})$(print_icon 'LEFT_SEGMENT_SEPARATOR')"
else
echo -n "%k"
fi
@@ -203,25 +212,34 @@ CURRENT_RIGHT_BG='NONE'
set_default last_right_element_index 1
set_default POWERLEVEL9K_WHITESPACE_BETWEEN_RIGHT_SEGMENTS " "
right_prompt_segment() {
+ local segment_name="${1}"
local current_index=$2
# Check if the segment should be joined with the previous one
local joined
segmentShouldBeJoined $current_index $last_right_element_index "$POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS" && joined=true || joined=false
+ # Colors
+ local backgroundColor="${3}"
+ local foregroundColor="${4}"
+
# Overwrite given background-color by user defined variable for this segment.
- local BACKGROUND_USER_VARIABLE=POWERLEVEL9K_${(U)1#prompt_}_BACKGROUND
+ local BACKGROUND_USER_VARIABLE=POWERLEVEL9K_${(U)${segment_name}#prompt_}_BACKGROUND
local BG_COLOR_MODIFIER=${(P)BACKGROUND_USER_VARIABLE}
- [[ -n $BG_COLOR_MODIFIER ]] && 3="$BG_COLOR_MODIFIER"
+ [[ -n $BG_COLOR_MODIFIER ]] && backgroundColor="$BG_COLOR_MODIFIER"
# Overwrite given foreground-color by user defined variable for this segment.
- local FOREGROUND_USER_VARIABLE=POWERLEVEL9K_${(U)1#prompt_}_FOREGROUND
+ local FOREGROUND_USER_VARIABLE=POWERLEVEL9K_${(U)${segment_name}#prompt_}_FOREGROUND
local FG_COLOR_MODIFIER=${(P)FOREGROUND_USER_VARIABLE}
- [[ -n $FG_COLOR_MODIFIER ]] && 4="$FG_COLOR_MODIFIER"
+ [[ -n $FG_COLOR_MODIFIER ]] && foregroundColor="$FG_COLOR_MODIFIER"
+
+ # Get color codes here to save some calls later on
+ backgroundColor="$(getColorCode ${backgroundColor})"
+ foregroundColor="$(getColorCode ${foregroundColor})"
- local bg fg
- [[ -n "$3" ]] && bg="$(backgroundColor $3)" || bg="$(backgroundColor)"
- [[ -n "$4" ]] && fg="$(foregroundColor $4)" || fg="$(foregroundColor)"
+ local background foreground
+ [[ -n "${backgroundColor}" ]] && background="$(backgroundColor ${backgroundColor})" || background="%k"
+ [[ -n "${foregroundColor}" ]] && foreground="$(foregroundColor ${foregroundColor})" || foreground="%f"
# If CURRENT_RIGHT_BG is "NONE", we are the first right segment.
@@ -231,17 +249,17 @@ right_prompt_segment() {
fi
if [[ $joined == false ]] || [[ "$CURRENT_RIGHT_BG" == "NONE" ]]; then
- if isSameColor "$CURRENT_RIGHT_BG" "$3"; then
+ if isSameColor "$CURRENT_RIGHT_BG" "${backgroundColor}"; then
# Middle segment with same color as previous segment
# We take the current foreground color as color for our
# subsegment (or the default color). This should have
# enough contrast.
local complement
- [[ -n "$4" ]] && complement="$fg" || complement="$(foregroundColor $DEFAULT_COLOR)"
+ [[ -n "${foregroundColor}" ]] && complement="${foreground}" || complement="$(foregroundColor $DEFAULT_COLOR)"
echo -n "$complement$(print_icon 'RIGHT_SUBSEGMENT_SEPARATOR')%f"
else
- # Use the new BG color for the foreground with separator
- echo -n "$(foregroundColor $3)$(print_icon 'RIGHT_SEGMENT_SEPARATOR')%f"
+ # Use the new Background Color as the foreground of the segment separator
+ echo -n "$(foregroundColor ${backgroundColor})$(print_icon 'RIGHT_SEGMENT_SEPARATOR')%f"
fi
fi
@@ -254,13 +272,13 @@ right_prompt_segment() {
# we need to color both the visual identifier and the whitespace.
[[ -n "$5" ]] && visual_identifier=" $visual_identifier"
# Allow users to overwrite the color for the visual identifier only.
- local visual_identifier_color_variable=POWERLEVEL9K_${(U)1#prompt_}_VISUAL_IDENTIFIER_COLOR
- set_default $visual_identifier_color_variable $4
- visual_identifier="%F{${(P)visual_identifier_color_variable}%}$visual_identifier"
+ local visual_identifier_color_variable=POWERLEVEL9K_${(U)${segment_name}#prompt_}_VISUAL_IDENTIFIER_COLOR
+ set_default $visual_identifier_color_variable "${foregroundColor}"
+ visual_identifier="$(foregroundColor ${(P)visual_identifier_color_variable})${visual_identifier}"
fi
fi
- echo -n "${bg}${fg}"
+ echo -n "${background}${foreground}"
# Print whitespace only if segment is not joined or first right segment
[[ $joined == false ]] || [[ "$CURRENT_RIGHT_BG" == "NONE" ]] && echo -n "${POWERLEVEL9K_WHITESPACE_BETWEEN_RIGHT_SEGMENTS}"
@@ -270,7 +288,7 @@ right_prompt_segment() {
# Print the visual identifier
echo -n "${visual_identifier}"
- CURRENT_RIGHT_BG=$3
+ CURRENT_RIGHT_BG="${backgroundColor}"
last_right_element_index=$current_index
}
@@ -278,11 +296,6 @@ right_prompt_segment() {
# Prompt Segment Definitions
################################################################
-# The `CURRENT_BG` variable is used to remember what the last BG color used was
-# when building the left-hand prompt. Because the RPROMPT is created from
-# right-left but reads the opposite, this isn't necessary for the other side.
-CURRENT_BG='NONE'
-
################################################################
# Anaconda Environment
prompt_anaconda() {
@@ -409,12 +422,13 @@ prompt_battery() {
'charged' 'green'
'disconnected' "$DEFAULT_COLOR_INVERTED"
)
+ local ROOT_PREFIX="${4}"
# Set default values if the user did not configure them
set_default POWERLEVEL9K_BATTERY_LOW_THRESHOLD 10
- if [[ $OS =~ OSX && -f /usr/bin/pmset && -x /usr/bin/pmset ]]; then
+ if [[ $OS =~ OSX && -f "${ROOT_PREFIX}"/usr/bin/pmset && -x "${ROOT_PREFIX}"/usr/bin/pmset ]]; then
# obtain battery information from system
- local raw_data="$(pmset -g batt | awk 'FNR==2{print}')"
+ local raw_data="$(${ROOT_PREFIX}/usr/bin/pmset -g batt | awk 'FNR==2{print}')"
# return if there is no battery on system
[[ -z $(echo $raw_data | grep "InternalBattery") ]] && return
@@ -446,7 +460,7 @@ prompt_battery() {
fi
if [[ "$OS" == 'Linux' ]] || [[ "$OS" == 'Android' ]]; then
- local sysp="/sys/class/power_supply"
+ local sysp="${ROOT_PREFIX}/sys/class/power_supply"
# Reported BAT0 or BAT1 depending on kernel version
[[ -a $sysp/BAT0 ]] && local bat=$sysp/BAT0
@@ -468,8 +482,8 @@ prompt_battery() {
[[ $bat_percent =~ 100 ]] && current_state="charged"
[[ $bat_percent -lt 100 ]] && current_state="charging"
fi
- if [[ -f /usr/bin/acpi ]]; then
- local time_remaining=$(acpi | awk '{ print $5 }')
+ if [[ -f ${ROOT_PREFIX}/usr/bin/acpi ]]; then
+ local time_remaining=$(${ROOT_PREFIX}/usr/bin/acpi | awk '{ print $5 }')
if [[ $time_remaining =~ rate ]]; then
local tstring="..."
elif [[ $time_remaining =~ "[[:digit:]]+" ]]; then
@@ -623,12 +637,12 @@ prompt_context() {
if [[ $(print -P "%#") == '#' ]]; then
current_state="ROOT"
elif [[ -n "$SSH_CLIENT" || -n "$SSH_TTY" ]]; then
- if sudo -n true 2>/dev/null; then
+ if [[ -n "$SUDO_COMMAND" ]]; then
current_state="REMOTE_SUDO"
else
current_state="REMOTE"
fi
- elif sudo -n true 2>/dev/null; then
+ elif [[ -n "$SUDO_COMMAND" ]]; then
current_state="SUDO"
fi
@@ -651,7 +665,7 @@ prompt_user() {
"FOREGROUND_COLOR" "yellow"
"VISUAL_IDENTIFIER" "ROOT_ICON"
)
- elif sudo -n true 2>/dev/null; then
+ elif [[ -n "$SUDO_COMMAND" ]]; then
user_state=(
"STATE" "SUDO"
"CONTENT" "${POWERLEVEL9K_USER_TEMPLATE}"
@@ -702,11 +716,13 @@ prompt_host() {
# The 'custom` prompt provides a way for users to invoke commands and display
# the output in a segment.
prompt_custom() {
- local command=POWERLEVEL9K_CUSTOM_$3:u
+ local segment_name="${3:u}"
+ # Get content of custom segment
+ local command="POWERLEVEL9K_CUSTOM_${segment_name}"
local segment_content="$(eval ${(P)command})"
if [[ -n $segment_content ]]; then
- "$1_prompt_segment" "${0}_${3:u}" "$2" $DEFAULT_COLOR_INVERTED $DEFAULT_COLOR "$segment_content"
+ "$1_prompt_segment" "${0}_${3:u}" "$2" $DEFAULT_COLOR_INVERTED $DEFAULT_COLOR "$segment_content" "CUSTOM_${segment_name}_ICON"
fi
}
@@ -1055,18 +1071,14 @@ prompt_history() {
################################################################
# Detection for virtualization (systemd based systems only)
prompt_detect_virt() {
- if ! command -v systemd-detect-virt > /dev/null; then
- return
- fi
- local virt=$(systemd-detect-virt)
+ local virt=$(systemd-detect-virt 2> /dev/null)
if [[ "$virt" == "none" ]]; then
if [[ "$(ls -di / | grep -o 2)" != "2" ]]; then
virt="chroot"
- "$1_prompt_segment" "$0" "$2" "$DEFAULT_COLOR" "yellow" "$virt"
- else
- ;
fi
- else
+ fi
+
+ if [[ -n "${virt}" ]]; then
"$1_prompt_segment" "$0" "$2" "$DEFAULT_COLOR" "yellow" "$virt"
fi
}
@@ -1101,18 +1113,20 @@ prompt_ip() {
else
if defined POWERLEVEL9K_IP_INTERFACE; then
# Get the IP address of the specified interface.
- ip=$(ip -4 a show "$POWERLEVEL9K_IP_INTERFACE" | grep -o "inet\s*[0-9.]*" | grep -o "[0-9.]*")
+ ip=$(ip -4 a show "$POWERLEVEL9K_IP_INTERFACE" | grep -o "inet\s*[0-9.]*" | grep -o -E "[0-9.]+")
else
local interfaces callback
# Get all network interface names that are up
- interfaces=$(ip link ls up | grep -o -E ":\s+[a-z0-9]+:" | grep -v "lo" | grep -o "[a-z0-9]*")
- callback='ip -4 a show $item | grep -o "inet\s*[0-9.]*" | grep -o "[0-9.]*"'
+ interfaces=$(ip link ls up | grep -o -E ":\s+[a-z0-9]+:" | grep -v "lo" | grep -o -E "[a-z0-9]+")
+ callback='ip -4 a show $item | grep -o "inet\s*[0-9.]*" | grep -o -E "[0-9.]+"'
ip=$(getRelevantItem "$interfaces" "$callback")
fi
fi
- "$1_prompt_segment" "$0" "$2" "cyan" "$DEFAULT_COLOR" "$ip" 'NETWORK_ICON'
+ if [[ -n "$ip" ]]; then
+ "$1_prompt_segment" "$0" "$2" "cyan" "$DEFAULT_COLOR" "$ip" 'NETWORK_ICON'
+ fi
}
################################################################
@@ -1142,6 +1156,7 @@ prompt_laravel_version() {
# Segment to display load
set_default POWERLEVEL9K_LOAD_WHICH 5
prompt_load() {
+ local ROOT_PREFIX="${4}"
# The load segment can have three different states
local current_state="unknown"
local load_select=2
@@ -1177,7 +1192,7 @@ prompt_load() {
fi
;;
*)
- load_avg=$(cut -d" " -f${load_select} /proc/loadavg)
+ load_avg=$(cut -d" " -f${load_select} ${ROOT_PREFIX}/proc/loadavg)
cores=$(nproc)
esac
@@ -1250,6 +1265,7 @@ prompt_php_version() {
################################################################
# Segment to display free RAM and used Swap
prompt_ram() {
+ local ROOT_PREFIX="${4}"
local base=''
local ramfree=0
if [[ "$OS" == "OSX" ]]; then
@@ -1261,9 +1277,9 @@ prompt_ram() {
ramfree=$(( ramfree * 4096 ))
else
if [[ "$OS" == "BSD" ]]; then
- ramfree=$(grep 'avail memory' /var/run/dmesg.boot | awk '{print $4}')
+ ramfree=$(grep 'avail memory' ${ROOT_PREFIX}/var/run/dmesg.boot | awk '{print $4}')
else
- ramfree=$(grep -o -E "MemAvailable:\s+[0-9]+" /proc/meminfo | grep -o "[0-9]*")
+ ramfree=$(grep -o -E "MemAvailable:\s+[0-9]+" ${ROOT_PREFIX}/proc/meminfo | grep -o -E "[0-9]+")
base='K'
fi
fi
@@ -1428,6 +1444,7 @@ prompt_status() {
################################################################
# Segment to display Swap information
prompt_swap() {
+ local ROOT_PREFIX="${4}"
local swap_used=0
local base=''
@@ -1442,8 +1459,8 @@ prompt_swap() {
base=$(echo "$raw_swap_used" | grep -o "[A-Z]*$")
else
- swap_total=$(grep -o -E "SwapTotal:\s+[0-9]+" /proc/meminfo | grep -o "[0-9]*")
- swap_free=$(grep -o -E "SwapFree:\s+[0-9]+" /proc/meminfo | grep -o "[0-9]*")
+ swap_total=$(grep -o -E "SwapTotal:\s+[0-9]+" ${ROOT_PREFIX}/proc/meminfo | grep -o -E "[0-9]+")
+ swap_free=$(grep -o -E "SwapFree:\s+[0-9]+" ${ROOT_PREFIX}/proc/meminfo | grep -o -E "[0-9]+")
swap_used=$(( swap_total - swap_free ))
base='K'
fi
@@ -1616,7 +1633,7 @@ set_default POWERLEVEL9K_VI_COMMAND_MODE_STRING "NORMAL"
prompt_vi_mode() {
case ${KEYMAP} in
vicmd)
- "$1_prompt_segment" "$0_NORMAL" "$2" "$DEFAULT_COLOR" "default" "$POWERLEVEL9K_VI_COMMAND_MODE_STRING"
+ "$1_prompt_segment" "$0_NORMAL" "$2" "$DEFAULT_COLOR" "white" "$POWERLEVEL9K_VI_COMMAND_MODE_STRING"
;;
main|viins|*)
if [[ -z $POWERLEVEL9K_VI_INSERT_MODE_STRING ]]; then return; fi
@@ -1894,7 +1911,7 @@ prompt_powerlevel9k_setup() {
fi
fi
- defined POWERLEVEL9K_LEFT_PROMPT_ELEMENTS || POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context dir rbenv vcs)
+ defined POWERLEVEL9K_LEFT_PROMPT_ELEMENTS || POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context dir vcs)
defined POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS || POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status root_indicator background_jobs history time)
# Display a warning if deprecated segments are in use.