1
0
Fork 0
mirror of https://github.com/romkatv/powerlevel10k.git synced 2024-11-22 12:20:07 +00:00

Merge branch 'jkoelndorfer-undef-var' into next

Implement defined, set_default utility functions
This commit is contained in:
Ben Hilburn 2015-09-03 12:08:40 -07:00
commit 797a953e12

View file

@ -48,17 +48,37 @@
# Utility functions # Utility functions
################################################################ ################################################################
# Exits with 0 if a variable has been previously defined (even if empty)
# Takes the name of a variable that should be checked.
function defined() {
local varname="$1"
typeset -p "$varname" > /dev/null 2>&1
}
function print_icon() { function print_icon() {
local icon_name=$1 local icon_name=$1
local ICON_USER_VARIABLE=POWERLEVEL9K_${icon_name} local ICON_USER_VARIABLE=POWERLEVEL9K_${icon_name}
local USER_ICON=${(P)ICON_USER_VARIABLE} local USER_ICON=${(P)ICON_USER_VARIABLE}
if [[ -n "$USER_ICON" ]]; then if defined "$ICON_USER_VARIABLE"; then
echo -n $USER_ICON echo -n $USER_ICON
else else
echo -n ${icons[$icon_name]} echo -n ${icons[$icon_name]}
fi fi
} }
# Given the name of a variable and a default value, sets the variable
# value to the default only if it has not been defined.
#
# typeset cannot set the value for an array, so this will only work
# for scalar values.
function set_default() {
local varname="$1"
local default_value="$2"
defined "$varname" || typeset -g "$varname"="$default_value"
}
################################################################ ################################################################
# Icons # Icons
################################################################ ################################################################
@ -221,16 +241,8 @@ else
DEFAULT_COLOR_DARK="236" DEFAULT_COLOR_DARK="236"
fi fi
VCS_FOREGROUND_COLOR=$DEFAULT_COLOR set_default POWERLEVEL9K_VCS_FOREGROUND "$DEFAULT_COLOR"
VCS_FOREGROUND_COLOR_DARK=$DEFAULT_COLOR_DARK set_default POWERLEVEL9K_VCS_DARK_FOREGROUND "$DEFAULT_COLOR_DARK"
# If user has defined custom colors for the `vcs` segment, override the defaults
if [[ -n $POWERLEVEL9K_VCS_FOREGROUND ]]; then
VCS_FOREGROUND_COLOR=$POWERLEVEL9K_VCS_FOREGROUND
fi
if [[ -n $POWERLEVEL9K_VCS_DARK_FOREGROUND ]]; then
VCS_FOREGROUND_COLOR_DARK=$POWERLEVEL9K_VCS_DARK_FOREGROUND
fi
################################################################ ################################################################
# VCS Information Settings # VCS Information Settings
@ -248,20 +260,20 @@ if [[ "$POWERLEVEL9K_SHOW_CHANGESET" == true ]]; then
VCS_CHANGESET_HASH_LENGTH="$POWERLEVEL9K_CHANGESET_HASH_LENGTH" VCS_CHANGESET_HASH_LENGTH="$POWERLEVEL9K_CHANGESET_HASH_LENGTH"
fi fi
VCS_CHANGESET_PREFIX="%F{$VCS_FOREGROUND_COLOR_DARK}$(print_icon 'VCS_COMMIT_ICON')%0.$VCS_CHANGESET_HASH_LENGTH""i%f " VCS_CHANGESET_PREFIX="%F{$POWERLEVEL9K_VCS_DARK_FOREGROUND}$(print_icon 'VCS_COMMIT_ICON')%0.$VCS_CHANGESET_HASH_LENGTH""i%f "
fi fi
zstyle ':vcs_info:*' enable git hg zstyle ':vcs_info:*' enable git hg
zstyle ':vcs_info:*' check-for-changes true zstyle ':vcs_info:*' check-for-changes true
VCS_DEFAULT_FORMAT="$VCS_CHANGESET_PREFIX%F{$VCS_FOREGROUND_COLOR}%b%c%u%m%f" VCS_DEFAULT_FORMAT="$VCS_CHANGESET_PREFIX%F{$POWERLEVEL9K_VCS_FOREGROUND}%b%c%u%m%f"
zstyle ':vcs_info:git:*' formats "%F{$VCS_FOREGROUND_COLOR}$(print_icon 'VCS_GIT_ICON')%f$VCS_DEFAULT_FORMAT" zstyle ':vcs_info:git:*' formats "%F{$POWERLEVEL9K_VCS_FOREGROUND}$(print_icon 'VCS_GIT_ICON')%f$VCS_DEFAULT_FORMAT"
zstyle ':vcs_info:hg:*' formats "%F{$VCS_FOREGROUND_COLOR}$(print_icon 'VCS_HG_ICON')%f$VCS_DEFAULT_FORMAT" zstyle ':vcs_info:hg:*' formats "%F{$POWERLEVEL9K_VCS_FOREGROUND}$(print_icon 'VCS_HG_ICON')%f$VCS_DEFAULT_FORMAT"
zstyle ':vcs_info:*' actionformats " %b %F{red}| %a%f" zstyle ':vcs_info:*' actionformats " %b %F{red}| %a%f"
zstyle ':vcs_info:*' stagedstr " %F{$VCS_FOREGROUND_COLOR}$(print_icon 'VCS_STAGED_ICON')%f" zstyle ':vcs_info:*' stagedstr " %F{$POWERLEVEL9K_VCS_FOREGROUND}$(print_icon 'VCS_STAGED_ICON')%f"
zstyle ':vcs_info:*' unstagedstr " %F{$VCS_FOREGROUND_COLOR}$(print_icon 'VCS_UNSTAGED_ICON')%f" zstyle ':vcs_info:*' unstagedstr " %F{$POWERLEVEL9K_VCS_FOREGROUND}$(print_icon 'VCS_UNSTAGED_ICON')%f"
zstyle ':vcs_info:git*+set-message:*' hooks vcs-detect-changes git-untracked git-aheadbehind git-stash git-remotebranch git-tagname zstyle ':vcs_info:git*+set-message:*' hooks vcs-detect-changes git-untracked git-aheadbehind git-stash git-remotebranch git-tagname
zstyle ':vcs_info:hg*+set-message:*' hooks vcs-detect-changes zstyle ':vcs_info:hg*+set-message:*' hooks vcs-detect-changes
@ -374,14 +386,14 @@ prompt_vcs() {
$1_prompt_segment "$0" "green" "$DEFAULT_COLOR" $1_prompt_segment "$0" "green" "$DEFAULT_COLOR"
fi fi
echo -n "%F{$VCS_FOREGROUND_COLOR}%f$vcs_prompt " echo -n "%F{$POWERLEVEL9K_VCS_FOREGROUND}%f$vcs_prompt "
fi fi
} }
function +vi-git-untracked() { function +vi-git-untracked() {
if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) == 'true' && \ if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) == 'true' && \
-n $(git ls-files --others --exclude-standard | sed q) ]]; then -n $(git ls-files --others --exclude-standard | sed q) ]]; then
hook_com[unstaged]+=" %F{$VCS_FOREGROUND_COLOR}$(print_icon 'VCS_UNTRACKED_ICON')%f" hook_com[unstaged]+=" %F{$POWERLEVEL9K_VCS_FOREGROUND}$(print_icon 'VCS_UNTRACKED_ICON')%f"
fi fi
} }
@ -394,12 +406,12 @@ function +vi-git-aheadbehind() {
# for git prior to 1.7 # for git prior to 1.7
# ahead=$(git rev-list origin/${branch_name}..HEAD | wc -l) # ahead=$(git rev-list origin/${branch_name}..HEAD | wc -l)
ahead=$(git rev-list ${branch_name}@{upstream}..HEAD 2>/dev/null | wc -l) ahead=$(git rev-list ${branch_name}@{upstream}..HEAD 2>/dev/null | wc -l)
(( $ahead )) && gitstatus+=( " %F{$VCS_FOREGROUND_COLOR}$(print_icon 'VCS_OUTGOING_CHANGES_ICON')${ahead// /}%f" ) (( $ahead )) && gitstatus+=( " %F{$POWERLEVEL9K_VCS_FOREGROUND}$(print_icon 'VCS_OUTGOING_CHANGES_ICON')${ahead// /}%f" )
# for git prior to 1.7 # for git prior to 1.7
# behind=$(git rev-list HEAD..origin/${branch_name} | wc -l) # behind=$(git rev-list HEAD..origin/${branch_name} | wc -l)
behind=$(git rev-list HEAD..${branch_name}@{upstream} 2>/dev/null | wc -l) behind=$(git rev-list HEAD..${branch_name}@{upstream} 2>/dev/null | wc -l)
(( $behind )) && gitstatus+=( " %F{$VCS_FOREGROUND_COLOR}$(print_icon 'VCS_INCOMING_CHANGES_ICON')${behind// /}%f" ) (( $behind )) && gitstatus+=( " %F{$POWERLEVEL9K_VCS_FOREGROUND}$(print_icon 'VCS_INCOMING_CHANGES_ICON')${behind// /}%f" )
hook_com[misc]+=${(j::)gitstatus} hook_com[misc]+=${(j::)gitstatus}
} }
@ -411,12 +423,12 @@ function +vi-git-remotebranch() {
remote=${$(git rev-parse --verify HEAD@{upstream} --symbolic-full-name 2>/dev/null)/refs\/(remotes|heads)\/} remote=${$(git rev-parse --verify HEAD@{upstream} --symbolic-full-name 2>/dev/null)/refs\/(remotes|heads)\/}
branch_name=${$(git symbolic-ref --short HEAD 2>/dev/null)} branch_name=${$(git symbolic-ref --short HEAD 2>/dev/null)}
hook_com[branch]="%F{$VCS_FOREGROUND_COLOR}$(print_icon 'VCS_BRANCH_ICON')${hook_com[branch]}%f" hook_com[branch]="%F{$POWERLEVEL9K_VCS_FOREGROUND}$(print_icon 'VCS_BRANCH_ICON')${hook_com[branch]}%f"
# Always show the remote # Always show the remote
#if [[ -n ${remote} ]] ; then #if [[ -n ${remote} ]] ; then
# Only show the remote if it differs from the local # Only show the remote if it differs from the local
if [[ -n ${remote} && ${remote#*/} != ${branch_name} ]] ; then if [[ -n ${remote} && ${remote#*/} != ${branch_name} ]] ; then
hook_com[branch]+="%F{$VCS_FOREGROUND_COLOR}$(print_icon 'VCS_REMOTE_BRANCH_ICON')%f%F{$VCS_FOREGROUND_COLOR}${remote// /}%f" hook_com[branch]+="%F{$POWERLEVEL9K_VCS_FOREGROUND}$(print_icon 'VCS_REMOTE_BRANCH_ICON')%f%F{$POWERLEVEL9K_VCS_FOREGROUND}${remote// /}%f"
fi fi
} }
@ -424,7 +436,7 @@ function +vi-git-tagname() {
local tag local tag
tag=$(git describe --tags --exact-match HEAD 2>/dev/null) tag=$(git describe --tags --exact-match HEAD 2>/dev/null)
[[ -n "${tag}" ]] && hook_com[branch]=" %F{$VCS_FOREGROUND_COLOR}$(print_icon 'VCS_TAG_ICON')${tag}%f" [[ -n "${tag}" ]] && hook_com[branch]=" %F{$POWERLEVEL9K_VCS_FOREGROUND}$(print_icon 'VCS_TAG_ICON')${tag}%f"
} }
# Show count of stashed changes # Show count of stashed changes
@ -434,13 +446,13 @@ function +vi-git-stash() {
if [[ -s $(git rev-parse --git-dir)/refs/stash ]] ; then if [[ -s $(git rev-parse --git-dir)/refs/stash ]] ; then
stashes=$(git stash list 2>/dev/null | wc -l) stashes=$(git stash list 2>/dev/null | wc -l)
hook_com[misc]+=" %F{$VCS_FOREGROUND_COLOR}$(print_icon 'VCS_STASH_ICON')${stashes// /}%f" hook_com[misc]+=" %F{$POWERLEVEL9K_VCS_FOREGROUND}$(print_icon 'VCS_STASH_ICON')${stashes// /}%f"
fi fi
} }
function +vi-hg-bookmarks() { function +vi-hg-bookmarks() {
if [[ -n "${hgbmarks[@]}" ]]; then if [[ -n "${hgbmarks[@]}" ]]; then
hook_com[hg-bookmark-string]=" %F{$VCS_FOREGROUND_COLOR}$(print_icon 'VCS_BOOKMARK_ICON')${hgbmarks[@]}%f" hook_com[hg-bookmark-string]=" %F{$POWERLEVEL9K_VCS_FOREGROUND}$(print_icon 'VCS_BOOKMARK_ICON')${hgbmarks[@]}%f"
# And to signal, that we want to use the sting we just generated, # And to signal, that we want to use the sting we just generated,
# set the special variable `ret' to something other than the default # set the special variable `ret' to something other than the default
@ -657,9 +669,7 @@ prompt_virtualenv() {
# Main prompt # Main prompt
build_left_prompt() { build_left_prompt() {
if [[ "${#POWERLEVEL9K_LEFT_PROMPT_ELEMENTS}" == 0 ]]; then defined POWERLEVEL9K_LEFT_PROMPT_ELEMENTS || POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context dir rbenv vcs)
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context dir rbenv vcs)
fi
for element in $POWERLEVEL9K_LEFT_PROMPT_ELEMENTS; do for element in $POWERLEVEL9K_LEFT_PROMPT_ELEMENTS; do
prompt_$element "left" prompt_$element "left"
@ -672,9 +682,7 @@ build_left_prompt() {
build_right_prompt() { build_right_prompt() {
RETVAL=$? RETVAL=$?
if [[ "${#POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS}" == 0 ]]; then defined POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS || POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(longstatus history time)
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(longstatus history time)
fi
for element in $POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS; do for element in $POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS; do
prompt_$element "right" prompt_$element "right"