diff options
Diffstat (limited to 'powerlevel9k.zsh-theme')
-rwxr-xr-x | powerlevel9k.zsh-theme | 156 |
1 files changed, 93 insertions, 63 deletions
diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index ff3a9466..f253bfb9 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -409,12 +409,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 +447,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 +469,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 @@ -498,7 +499,7 @@ prompt_battery() { fi fi # return if POWERLEVEL9K_BATTERY_HIDE_ABOVE_THRESHOLD is set and the battery percentage is greater or equal - if [[ -v "POWERLEVEL9K_BATTERY_HIDE_ABOVE_THRESHOLD" && "${bat_percent}" -ge $POWERLEVEL9K_BATTERY_HIDE_ABOVE_THRESHOLD ]]; then + if defined POWERLEVEL9K_BATTERY_HIDE_ABOVE_THRESHOLD && [[ "${bat_percent}" -ge $POWERLEVEL9K_BATTERY_HIDE_ABOVE_THRESHOLD ]]; then return fi @@ -623,12 +624,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 @@ -702,11 +703,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 } @@ -745,6 +748,31 @@ prompt_command_execution_time() { } ################################################################ +# Determine the unique path - this is needed for the +# truncate_to_unique strategy. +# +function getUniqueFolder() { + local trunc_path directory test_dir test_dir_length + local -a matching + local -a paths + local cur_path='/' + paths=(${(s:/:)1}) + for directory in ${paths[@]}; do + test_dir='' + for (( i=0; i < ${#directory}; i++ )); do + test_dir+="${directory:$i:1}" + matching=("$cur_path"/"$test_dir"*/) + if [[ ${#matching[@]} -eq 1 ]]; then + break + fi + done + trunc_path+="$test_dir/" + cur_path+="$directory/" + done + echo "${trunc_path: : -1}" +} + +################################################################ # Dir: current working directory # Parameters: # * $1 Alignment: string - left|right @@ -811,23 +839,10 @@ prompt_dir() { # for each parent path component find the shortest unique beginning # characters sequence. Source: https://stackoverflow.com/a/45336078 if (( ${#current_path} > 1 )); then # root and home are exceptions and won't have paths - local matching - local cur_path='/' - [[ $current_path != "~"* ]] && trunc_path='/' || trunc_path='' - for directory in ${paths[@]}; do - test_dir='' - for (( i=0; i<${#directory}; i++ )); do - test_dir+="${directory:$i:1}" - matching=("$cur_path"/"$test_dir"*/) - if [[ ${#matching[@]} -eq 1 ]]; then - break - fi - done - trunc_path+="$test_dir/" - cur_path+="$directory/" - done - [[ $current_path == "~"* ]] && trunc_path="~/$trunc_path" - current_path="${trunc_path: : -1}" + # cheating here to retain ~ as home folder + local home_path="$(getUniqueFolder $HOME)" + trunc_path="$(getUniqueFolder $PWD)" + [[ $current_path == "~"* ]] && current_path="~${trunc_path//${home_path}/}" || current_path="/${trunc_path}" fi ;; truncate_with_folder_marker) @@ -1043,18 +1058,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 } @@ -1089,18 +1100,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 } ################################################################ @@ -1118,11 +1131,10 @@ prompt_vpn_ip() { ################################################################ # Segment to display laravel version prompt_laravel_version() { - local laravel_version="$(php artisan --version 2>/dev/null)" - if [[ -n "${laravel_version}" ]]; then - # Remove unrelevant infos - laravel_version="${laravel_version//Laravel Framework version /}" - + local laravel_version="$(php artisan --version 2> /dev/null)" + if [[ -n "${laravel_version}" && "${laravel_version}" =~ "Laravel Framework" ]]; then + # Strip out everything but the version + laravel_version="${laravel_version//Laravel Framework /}" "$1_prompt_segment" "$0" "$2" "maroon" "white" "${laravel_version}" 'LARAVEL_ICON' fi } @@ -1131,6 +1143,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 @@ -1166,7 +1179,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 @@ -1239,6 +1252,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 @@ -1250,9 +1264,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 @@ -1260,21 +1274,19 @@ prompt_ram() { "$1_prompt_segment" "$0" "$2" "yellow" "$DEFAULT_COLOR" "$(printSizeHumanReadable "$ramfree" $base)" 'RAM_ICON' } - +################################################################ +# Segment to display rbenv information +# https://github.com/rbenv/rbenv#choosing-the-ruby-version set_default POWERLEVEL9K_RBENV_PROMPT_ALWAYS_SHOW false -# rbenv information prompt_rbenv() { - if command which rbenv 2>/dev/null >&2; then + if [[ -n "$RBENV_VERSION" ]]; then + "$1_prompt_segment" "$0" "$2" "red" "$DEFAULT_COLOR" "$RBENV_VERSION" 'RUBY_ICON' + elif [ $commands[rbenv] ]; then local rbenv_version_name="$(rbenv version-name)" local rbenv_global="$(rbenv global)" - - # Don't show anything if the current Ruby is the same as the global Ruby - # unless `POWERLEVEL9K_RBENV_PROMPT_ALWAYS_SHOW` is set. - if [[ $rbenv_version_name == $rbenv_global && "$POWERLEVEL9K_RBENV_PROMPT_ALWAYS_SHOW" = false ]]; then - return + if [[ "${rbenv_version_name}" != "${rbenv_global}" || "${POWERLEVEL9K_RBENV_PROMPT_ALWAYS_SHOW}" == "true" ]]; then + "$1_prompt_segment" "$0" "$2" "red" "$DEFAULT_COLOR" "$rbenv_version_name" 'RUBY_ICON' fi - - "$1_prompt_segment" "$0" "$2" "red" "$DEFAULT_COLOR" "$rbenv_version_name" 'RUBY_ICON' fi } @@ -1419,6 +1431,7 @@ prompt_status() { ################################################################ # Segment to display Swap information prompt_swap() { + local ROOT_PREFIX="${4}" local swap_used=0 local base='' @@ -1433,8 +1446,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 @@ -1607,7 +1620,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 @@ -1628,11 +1641,22 @@ prompt_virtualenv() { } ################################################################ -# pyenv: current active python version (with restrictions) +# Segment to display pyenv information # https://github.com/pyenv/pyenv#choosing-the-python-version +set_default POWERLEVEL9K_PYENV_PROMPT_ALWAYS_SHOW false prompt_pyenv() { if [[ -n "$PYENV_VERSION" ]]; then "$1_prompt_segment" "$0" "$2" "blue" "$DEFAULT_COLOR" "$PYENV_VERSION" 'PYTHON_ICON' + elif [ $commands[pyenv] ]; then + local pyenv_version_name="$(pyenv version-name)" + local pyenv_global="system" + local pyenv_root="$(pyenv root)" + if [[ -f "${pyenv_root}/version" ]]; then + pyenv_global="$(pyenv version-file-read ${pyenv_root}/version)" + fi + if [[ "${pyenv_version_name}" != "${pyenv_global}" || "${POWERLEVEL9K_PYENV_PROMPT_ALWAYS_SHOW}" == "true" ]]; then + "$1_prompt_segment" "$0" "$2" "blue" "$DEFAULT_COLOR" "$pyenv_version_name" 'PYTHON_ICON' + fi fi } @@ -1682,7 +1706,7 @@ prompt_kubecontext() { local k8s_final_text="" - if [[ "$cur_ctx" == "cur_namespace" ]]; then + if [[ "$cur_ctx" == "$cur_namespace" ]]; then # No reason to print out the same identificator twice k8s_final_text="$cur_ctx" else @@ -1779,10 +1803,16 @@ powerlevel9k_preexec() { set_default POWERLEVEL9K_PROMPT_ADD_NEWLINE false powerlevel9k_prepare_prompts() { - local RETVAL RPROMPT_PREFIX RPROMPT_SUFFIX + # Return values. These need to be global, because + # they are used in prompt_status. Also, we need + # to get the return value of the last command at + # very first in this function. Do not move the + # lines down, otherwise the last command is not + # what you expected it to be. RETVAL=$? RETVALS=( "$pipestatus[@]" ) + local RPROMPT_SUFFIX RPROMPT_PREFIX _P9K_COMMAND_DURATION=$((EPOCHREALTIME - _P9K_TIMER_START)) # Reset start time |