diff options
Diffstat (limited to 'powerlevel9k.zsh-theme')
-rwxr-xr-x | powerlevel9k.zsh-theme | 292 |
1 files changed, 221 insertions, 71 deletions
diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index b21e2ff4..a4469406 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -17,74 +17,57 @@ ################################################################ ## Turn on for Debugging +#PS4='%s%f%b%k%F{blue}%{λ%}%L %F{240}%N:%i%(?.. %F{red}%?) %1(_.%F{yellow}%-1_ .)%s%f%b%k ' #zstyle ':vcs_info:*+*:*' debug true #set -o xtrace # Try to set the installation path -if [[ -n "$POWERLEVEL9K_INSTALLATION_PATH" ]]; then - # If an installation path was set manually, - # it should trump any other location found. - # Do nothing. This is all right, as we use the - # POWERLEVEL9K_INSTALLATION_PATH for further processing. -elif [[ $(whence -w prompt_powerlevel9k_setup) =~ "function" ]]; then - # Check if the theme was called as a function (e.g., from prezto) - autoload -U is-at-least - if is-at-least 5.0.8; then - # Try to find the correct path of the script. - POWERLEVEL9K_INSTALLATION_PATH=$(whence -v $0 | sed "s/$0 is a shell function from //") - elif [[ -f "${ZDOTDIR:-$HOME}/.zprezto/modules/prompt/init.zsh" ]]; then - # If there is an prezto installation, we assume that powerlevel9k is linked there. - POWERLEVEL9K_INSTALLATION_PATH="${ZDOTDIR:-$HOME}/.zprezto/modules/prompt/functions/prompt_powerlevel9k_setup" - fi -else - # Last resort: Set installation path is script path - POWERLEVEL9K_INSTALLATION_PATH="$0" -fi - -# Resolve the installation path -if [[ -L "$POWERLEVEL9K_INSTALLATION_PATH" ]]; then - # If this theme is sourced as a symlink, we need to locate the real URL - filename="${POWERLEVEL9K_INSTALLATION_PATH:A}" -elif [[ -d "$POWERLEVEL9K_INSTALLATION_PATH" ]]; then - # Directory - filename="${POWERLEVEL9K_INSTALLATION_PATH}/powerlevel9k.zsh-theme" -elif [[ -f "$POWERLEVEL9K_INSTALLATION_PATH" ]]; then - # Script is a file - filename="$POWERLEVEL9K_INSTALLATION_PATH" -elif [[ -z "$POWERLEVEL9K_INSTALLATION_PATH" ]]; then - # Fallback: specify an installation path! - print -P "%F{red}We could not locate the installation path of powerlevel9k.%f" - print -P "Please specify by setting %F{blue}POWERLEVEL9K_INSTALLATION_PATH%f (full path incl. file name) at the very beginning of your ~/.zshrc" - return 1 +if [[ -n "$POWERLEVEL9K_INSTALLATION_DIR" ]]; then + p9k_directory=${POWERLEVEL9K_INSTALLATION_DIR:A} else - print -P "%F{red}Script location could not be found! Maybe your %F{blue}POWERLEVEL9K_INSTALLATION_PATH%F{red} is not correct?%f" - return 1 + if [[ "${(%):-%N}" == '(eval)' ]]; then + if [[ "$0" == '-antigen-load' ]] && [[ -r "${PWD}/powerlevel9k.zsh-theme" ]]; then + # Antigen uses eval to load things so it can change the plugin (!!) + # https://github.com/zsh-users/antigen/issues/581 + p9k_directory=$PWD + else + print -P "%F{red}You must set POWERLEVEL9K_INSTALLATION_DIR work from within an (eval).%f" + return 1 + fi + else + # Get the path to file this code is executing in; then + # get the absolute path and strip the filename. + # See https://stackoverflow.com/a/28336473/108857 + p9k_directory=${${(%):-%x}:A:h} + fi fi -script_location="$(dirname $filename)" ################################################################ # Source icon functions ################################################################ -source $script_location/functions/icons.zsh +source "${p9k_directory}/functions/icons.zsh" ################################################################ # Source utility functions ################################################################ -source $script_location/functions/utilities.zsh +source "${p9k_directory}/functions/utilities.zsh" ################################################################ # Source color functions ################################################################ -source $script_location/functions/colors.zsh +source "${p9k_directory}/functions/colors.zsh" ################################################################ # Source VCS_INFO hooks / helper functions ################################################################ -source $script_location/functions/vcs.zsh +source "${p9k_directory}/functions/vcs.zsh" + +# cleanup temporary variables. +unset p9k_directory ################################################################ # Color Scheme @@ -337,6 +320,23 @@ prompt_background_jobs() { fi } +# A newline in your prompt, so you can segments on multiple lines. +prompt_newline() { + local lws newline + [[ "$1" == "right" ]] && return + newline=$'\n' + lws=$POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS + if [[ "$POWERLEVEL9K_PROMPT_ON_NEWLINE" == true ]]; then + newline="${newline}$(print_icon 'MULTILINE_NEWLINE_PROMPT_PREFIX')" + fi + POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS= + "$1_prompt_segment" \ + "$0" \ + "$2" \ + "NONE" "NONE" "${newline}" + POWERLEVEL9K_WHITESPACE_BETWEEN_LEFT_SEGMENTS=$lws +} + # Segment that indicates usage level of current partition. set_default POWERLEVEL9K_DISK_USAGE_ONLY_WARNING false set_default POWERLEVEL9K_DISK_USAGE_WARNING_LEVEL 90 @@ -468,9 +468,24 @@ prompt_battery() { message="$bat_percent%%" fi - # Draw the prompt_segment - if [[ -n $bat_percent ]]; then - "$1_prompt_segment" "${0}_${current_state}" "$2" "$DEFAULT_COLOR" "${battery_states[$current_state]}" "$message" 'BATTERY_ICON' + # override default icon if we are using battery stages + if [[ -n "$POWERLEVEL9K_BATTERY_STAGES" ]]; then + local segment=$(( 100.0 / (${#POWERLEVEL9K_BATTERY_STAGES} - 1 ) )) + if [[ $segment > 1 ]]; then + local offset=$(( ($bat_percent / $segment) + 1 )) + # check if the stages are in an array or a string + [[ "${(t)POWERLEVEL9K_BATTERY_STAGES}" =~ "array" ]] && POWERLEVEL9K_BATTERY_ICON="$POWERLEVEL9K_BATTERY_STAGES[$offset]" || POWERLEVEL9K_BATTERY_ICON=${POWERLEVEL9K_BATTERY_STAGES:$offset:1} + fi + fi + + # override the default color if we are using a color level array + if [[ -n "$POWERLEVEL9K_BATTERY_LEVEL_BACKGROUND" ]] && [[ "${(t)POWERLEVEL9K_BATTERY_LEVEL_BACKGROUND}" =~ "array" ]]; then + local segment=$(( 100.0 / (${#POWERLEVEL9K_BATTERY_LEVEL_BACKGROUND} - 1 ) )) + local offset=$(( ($bat_percent / $segment) + 1 )) + "$1_prompt_segment" "$0_${current_state}" "$2" "${POWERLEVEL9K_BATTERY_LEVEL_BACKGROUND[$offset]}" "${battery_states[$current_state]}" "${message}" "BATTERY_ICON" + else + # Draw the prompt_segment + "$1_prompt_segment" "$0_${current_state}" "$2" "${DEFAULT_COLOR}" "${battery_states[$current_state]}" "${message}" "BATTERY_ICON" fi } @@ -572,6 +587,61 @@ prompt_context() { "$1_prompt_segment" "${0}_${current_state}" "$2" "$DEFAULT_COLOR" "${context_states[$current_state]}" "${content}" } +################################################################ +# User: user (who am I) +# Note that if $DEFAULT_USER is not set, this prompt segment will always print +set_default POWERLEVEL9K_USER_TEMPLATE "%n" +prompt_user() { + local current_state="DEFAULT" + typeset -AH user_state + if [[ "$POWERLEVEL9K_ALWAYS_SHOW_USER" == true ]] || [[ "$USER" != "$DEFAULT_USER" ]]; then + if [[ $(print -P "%#") == '#' ]]; then + user_state=( + "STATE" "ROOT" + "CONTENT" "${POWERLEVEL9K_USER_TEMPLATE}" + "BACKGROUND_COLOR" "${DEFAULT_COLOR}" + "FOREGROUND_COLOR" "yellow" + "VISUAL_IDENTIFIER" "ROOT_ICON" + ) + else + user_state=( + "STATE" "DEFAULT" + "CONTENT" "$USER" + "BACKGROUND_COLOR" "${DEFAULT_COLOR}" + "FOREGROUND_COLOR" "011" + "VISUAL_IDENTIFIER" "USER_ICON" + ) + fi + "$1_prompt_segment" "${0}_${user_state[STATE]}" "$2" "${user_state[BACKGROUND_COLOR]}" "${user_state[FOREGROUND_COLOR]}" "${user_state[CONTENT]}" "${user_state[VISUAL_IDENTIFIER]}" + fi +} + +################################################################ +# Host: machine (where am I) +set_default POWERLEVEL9K_HOST_TEMPLATE "%m" +prompt_host() { + local current_state="LOCAL" + typeset -AH host_state + if [[ -n "$SSH_CLIENT" ]] || [[ -n "$SSH_TTY" ]]; then + host_state=( + "STATE" "REMOTE" + "CONTENT" "${POWERLEVEL9K_HOST_TEMPLATE}" + "BACKGROUND_COLOR" "${DEFAULT_COLOR}" + "FOREGROUND_COLOR" "yellow" + "VISUAL_IDENTIFIER" "SSH_ICON" + ) + else + host_state=( + "STATE" "LOCAL" + "CONTENT" "${POWERLEVEL9K_HOST_TEMPLATE}" + "BACKGROUND_COLOR" "${DEFAULT_COLOR}" + "FOREGROUND_COLOR" "011" + "VISUAL_IDENTIFIER" "HOST_ICON" + ) + fi + "$1_prompt_segment" "$0_${host_state[STATE]}" "$2" "${host_state[BACKGROUND_COLOR]}" "${host_state[FOREGROUND_COLOR]}" "${host_state[CONTENT]}" "${host_state[VISUAL_IDENTIFIER]}" +} + # The 'custom` prompt provides a way for users to invoke commands and display # the output in a segment. prompt_custom() { @@ -619,6 +689,7 @@ prompt_command_execution_time() { # Dir: current working directory set_default POWERLEVEL9K_DIR_PATH_SEPARATOR "/" set_default POWERLEVEL9K_HOME_FOLDER_ABBREVIATION "~" +set_default POWERLEVEL9K_DIR_SHOW_WRITABLE false prompt_dir() { local current_path="$(print -P "%~")" if [[ -n "$POWERLEVEL9K_SHORTEN_DIR_LENGTH" || "$POWERLEVEL9K_SHORTEN_STRATEGY" == "truncate_with_folder_marker" ]]; then @@ -724,7 +795,7 @@ prompt_dir() { fi if [[ "${POWERLEVEL9K_HOME_FOLDER_ABBREVIATION}" != "~" ]]; then - current_path="$( echo "${current_path}" | sed "s/^~/${POWERLEVEL9K_HOME_FOLDER_ABBREVIATION}/")" + current_path=${current_path/#\~/${POWERLEVEL9K_HOME_FOLDER_ABBREVIATION}} fi typeset -AH dir_states @@ -732,9 +803,12 @@ prompt_dir() { "DEFAULT" "FOLDER_ICON" "HOME" "HOME_ICON" "HOME_SUBFOLDER" "HOME_SUB_ICON" + "NOT_WRITABLE" "LOCK_ICON" ) local current_state="DEFAULT" - if [[ $(print -P "%~") == '~' ]]; then + if [[ "${POWERLEVEL9K_DIR_SHOW_WRITABLE}" == true && ! -w "$PWD" ]]; then + current_state="NOT_WRITABLE" + elif [[ $(print -P "%~") == '~' ]]; then current_state="HOME" elif [[ $(print -P "%~") == '~'* ]]; then current_state="HOME_SUBFOLDER" @@ -777,12 +851,12 @@ prompt_detect_virt() { if [[ "$virt" == "none" ]]; then if [[ "$(ls -di / | grep -o 2)" != "2" ]]; then virt="chroot" - "$1_prompt_segment" "$0" "$2" "yellow" "$DEFAULT_COLOR" "$virt" + "$1_prompt_segment" "$0" "$2" "$DEFAULT_COLOR" "yellow" "$virt" else ; fi else - "$1_prompt_segment" "$0" "$2" "yellow" "$DEFAULT_COLOR" "$virt" + "$1_prompt_segment" "$0" "$2" "$DEFAULT_COLOR" "yellow" "$virt" fi } @@ -827,6 +901,16 @@ prompt_ip() { "$1_prompt_segment" "$0" "$2" "cyan" "$DEFAULT_COLOR" "$ip" 'NETWORK_ICON' } +set_default POWERLEVEL9K_VPN_IP_INTERFACE "tun" +# prompt if vpn active +prompt_vpn_ip() { + for vpn_iface in $(/sbin/ifconfig | grep -e ^"$POWERLEVEL9K_VPN_IP_INTERFACE" | cut -d":" -f1) + do + ip=$(/sbin/ifconfig "$vpn_iface" | grep -o "inet\s.*" | cut -d' ' -f2) + "$1_prompt_segment" "$0" "$2" "cyan" "$DEFAULT_COLOR" "$ip" 'VPN_ICON' + done +} + prompt_load() { # The load segment can have three different states local current_state="unknown" @@ -876,13 +960,16 @@ prompt_node_version() { # Node version from NVM # Only prints the segment if different than the default value prompt_nvm() { - [[ ! $(type nvm) =~ 'nvm is a shell function'* ]] && return - local node_version=$(nvm current) - [[ -z "${node_version}" ]] || [[ ${node_version} = "none" ]] && return - local nvm_default=$(cat $NVM_DIR/alias/default) + local node_version nvm_default + (( $+functions[nvm_version] )) || return + + node_version=$(nvm_version current) + [[ -z "${node_version}" || ${node_version} == "none" ]] && return + + nvm_default=$(nvm_version default) [[ "$node_version" =~ "$nvm_default" ]] && return - $1_prompt_segment "$0" "$2" "green" "011" "${node_version:1}" 'NODE_ICON' + $1_prompt_segment "$0" "$2" "magenta" "black" "${node_version:1}" 'NODE_ICON' } # NodeEnv Prompt @@ -987,13 +1074,10 @@ prompt_rspec_stats() { # Ruby Version Manager information prompt_rvm() { - local gemset=$(echo $GEM_HOME | awk -F'@' '{print $2}') - [ "$gemset" != "" ] && gemset="@$gemset" - - local version=$(echo $MY_RUBY_HOME | awk -F'-' '{print $NF}') + local version_and_gemset=${rvm_env_string/ruby-} - if [[ -n "$version$gemset" ]]; then - "$1_prompt_segment" "$0" "$2" "240" "$DEFAULT_COLOR" "$version$gemset" 'RUBY_ICON' + if [[ -n "$version_and_gemset" ]]; then + "$1_prompt_segment" "$0" "$2" "240" "$DEFAULT_COLOR" "$version_and_gemset" 'RUBY_ICON' fi } @@ -1003,17 +1087,57 @@ prompt_ssh() { fi } -# Status: return code if verbose, otherwise just an icon if an error occurred +# Status: When an error occur, return the error code, or a cross icon if option is set +# Display an ok icon when no error occur, or hide the segment if option is set to false +# +set_default POWERLEVEL9K_STATUS_CROSS false +set_default POWERLEVEL9K_STATUS_OK true +set_default POWERLEVEL9K_STATUS_SHOW_PIPESTATUS true +set_default POWERLEVEL9K_STATUS_HIDE_SIGNAME false +# old options, retro compatibility set_default POWERLEVEL9K_STATUS_VERBOSE true set_default POWERLEVEL9K_STATUS_OK_IN_NON_VERBOSE false + +exit_code_or_status() { + local ec=$1 + if [[ "$POWERLEVEL9K_STATUS_HIDE_SIGNAME" = true ]]; then + echo "$ec" + elif (( ec <= 128 )); then + echo "$ec" + else + local sig=$(( ec - 128 )) + local idx=$(( sig + 1 )) + echo "${signals[$idx]}(-${sig})" + fi +} + prompt_status() { - if [[ "$RETVAL" -ne 0 ]]; then - if [[ "$POWERLEVEL9K_STATUS_VERBOSE" == true ]]; then - "$1_prompt_segment" "$0_ERROR" "$2" "red" "226" "$RETVAL" 'CARRIAGE_RETURN_ICON' + local ec_text + local ec_sum + local ec + + if [[ $POWERLEVEL9K_STATUS_SHOW_PIPESTATUS == true ]]; then + ec_text=$(exit_code_or_status "${RETVALS[1]}") + ec_sum=${RETVALS[1]} + + for ec in "${(@)RETVALS[2,-1]}"; do + ec_text="${ec_text}|$(exit_code_or_status "$ec")" + ec_sum=$(( $ec_sum + $ec )) + done + else + # We use RETVAL instead of the right-most RETVALS item because + # PIPE_FAIL may be set. + ec_text=$(exit_code_or_status "${RETVAL}") + ec_sum=${RETVAL} + fi + + if (( ec_sum > 0 )); then + if [[ "$POWERLEVEL9K_STATUS_CROSS" == false && "$POWERLEVEL9K_STATUS_VERBOSE" == true ]]; then + "$1_prompt_segment" "$0_ERROR" "$2" "red" "226" "$ec_text" 'CARRIAGE_RETURN_ICON' else "$1_prompt_segment" "$0_ERROR" "$2" "$DEFAULT_COLOR" "red" "" 'FAIL_ICON' fi - elif [[ "$POWERLEVEL9K_STATUS_VERBOSE" == true || "$POWERLEVEL9K_STATUS_OK_IN_NON_VERBOSE" == true ]]; then + elif [[ "$POWERLEVEL9K_STATUS_OK" == true ]] && [[ "$POWERLEVEL9K_STATUS_VERBOSE" == true || "$POWERLEVEL9K_STATUS_OK_IN_NON_VERBOSE" == true ]]; then "$1_prompt_segment" "$0_OK" "$2" "$DEFAULT_COLOR" "green" "" 'OK_ICON' fi } @@ -1243,6 +1367,24 @@ prompt_dir_writable() { fi } +# Kubernetes Current Context +prompt_kubecontext() { + local kubectl_version=$(kubectl version --client 2>/dev/null) + + if [[ -n "$kubectl_version" ]]; then + # Get the current Kubernetes config context's namespaece + local k8s_namespace=$(kubectl config get-contexts --no-headers | grep '*' | awk '{print $5}') + # Get the current Kuberenetes context + local k8s_context=$(kubectl config current-context) + + if [[ -z "$k8s_namespace" ]]; then + k8s_namespace="default" + fi + "$1_prompt_segment" "$0" "$2" "magenta" "white" "$k8s_context/$k8s_namespace" "KUBERNETES_ICON" + fi +} + + ################################################################ # Prompt processing and drawing ################################################################ @@ -1293,15 +1435,16 @@ powerlevel9k_preexec() { set_default POWERLEVEL9K_PROMPT_ADD_NEWLINE false powerlevel9k_prepare_prompts() { RETVAL=$? + RETVALS=( "$pipestatus[@]" ) _P9K_COMMAND_DURATION=$((EPOCHREALTIME - _P9K_TIMER_START)) # Reset start time - _P9K_TIMER_START=0xFFFFFFFF + _P9K_TIMER_START=0x7FFFFFFF if [[ "$POWERLEVEL9K_PROMPT_ON_NEWLINE" == true ]]; then PROMPT='$(print_icon 'MULTILINE_FIRST_PROMPT_PREFIX')%f%b%k$(build_left_prompt) -$(print_icon 'MULTILINE_SECOND_PROMPT_PREFIX')' +$(print_icon 'MULTILINE_LAST_PROMPT_PREFIX')' if [[ "$POWERLEVEL9K_RPROMPT_ON_NEWLINE" != true ]]; then # The right prompt should be on the same line as the first line of the left # prompt. To do so, there is just a quite ugly workaround: Before zsh draws @@ -1330,12 +1473,12 @@ NEWLINE=' } prompt_powerlevel9k_setup() { - # I decided to use the value below for better supporting 32-bit CPUs, since the previous value "99999999999" was causing issues on my Android phone, which is powered by an armv7l - # We don't have to change that until 19 January of 2038! :) + # The value below was set to better support 32-bit CPUs. + # It's the maximum _signed_ integer value on 32-bit CPUs. + # Please don't change it until 19 January of 2038. ;) # Disable false display of command execution time - # Maximum integer on 32-bit CPUs - _P9K_TIMER_START=2147483647 + _P9K_TIMER_START=0x7FFFFFFF # The prompt function will set these prompt_* options after the setup function # returns. We need prompt_subst so we can safely run commands in the prompt @@ -1399,4 +1542,11 @@ prompt_powerlevel9k_setup() { add-zsh-hook preexec powerlevel9k_preexec } +prompt_powerlevel9k_teardown() { + add-zsh-hook -D precmd powerlevel9k_\* + add-zsh-hook -D preexec powerlevel9k_\* + PROMPT='%m%# ' + RPROMPT= +} + prompt_powerlevel9k_setup "$@" |