more bash magic

This commit is contained in:
Yehor Lvivski 2013-11-07 14:21:19 +02:00
parent 882aa79ebb
commit 19db503029

View File

@ -1,41 +1,41 @@
# Symbols
: ${is_a_git_repo_symbol:="❤"}
: ${has_untracked_files_symbol:="∿"}
: ${has_adds_symbol:="+"}
: ${has_deletions_symbol:="-"}
: ${has_deletions_cached_symbol:="✖"}
: ${has_modifications_symbol:="✎"}
: ${has_modifications_cached_symbol:="☲"}
: ${ready_to_commit_symbol:="→"}
: ${is_on_a_tag_symbol:="⌫"}
: ${needs_to_merge_symbol:="ᄉ"}
: ${has_upstream_symbol:="⇅"}
: ${detached_symbol:="⚯ "}
: ${can_fast_forward_symbol:="»"}
: ${has_diverged_symbol:="Ⴤ"}
: ${rebase_tracking_branch_symbol:="↶"}
: ${merge_tracking_branch_symbol:="ᄉ"}
: ${should_push_symbol:="↑"}
: ${has_stashes_symbol:="★"}
: ${is_a_git_repo_symbol:='❤'}
: ${has_untracked_files_symbol:='∿'}
: ${has_adds_symbol:='+'}
: ${has_deletions_symbol:='-'}
: ${has_deletions_cached_symbol:='✖'}
: ${has_modifications_symbol:='✎'}
: ${has_modifications_cached_symbol:='☲'}
: ${ready_to_commit_symbol:='→'}
: ${is_on_a_tag_symbol:='⌫'}
: ${needs_to_merge_symbol:='ᄉ'}
: ${has_upstream_symbol:='⇅'}
: ${detached_symbol:='⚯ '}
: ${can_fast_forward_symbol:='»'}
: ${has_diverged_symbol:='Ⴤ'}
: ${rebase_tracking_branch_symbol:='↶'}
: ${merge_tracking_branch_symbol:='ᄉ'}
: ${should_push_symbol:='↑'}
: ${has_stashes_symbol:='★'}
# Flags
: ${display_has_upstream:=false}
: ${display_tag:=false}
: ${display_tag_name:=true}
: ${two_lines:=true}
: ${finally:="\w ∙ "}
: ${finally:='\w ∙ '}
: ${use_color_off:=false}
# Colors
: ${on="\[\033[0;37m\]"}
: ${off="\[\033[1;30m\]"}
: ${red="\[\033[0;31m\]"}
: ${green="\[\033[0;32m\]"}
: ${yellow="\[\033[0;33m\]"}
: ${violet="\[\033[0;35m\]"}
: ${branch_color="\[\033[0;34m\]"}
#: ${blinking="\[\033[1;5;17m\]"}
: ${reset="\[\033[0m\]"}
: ${on='\[\033[0;37m\]'}
: ${off='\[\033[1;30m\]'}
: ${red='\[\033[0;31m\]'}
: ${green='\[\033[0;32m\]'}
: ${yellow='\[\033[0;33m\]'}
: ${violet='\[\033[0;35m\]'}
: ${branch_color='\[\033[0;34m\]'}
#: ${blinking='\[\033[1;5;17m\]'}
: ${reset='\[\033[0m\]'}
function enrich {
flag=$1
@ -61,12 +61,12 @@ function build_prompt {
number_of_logs=$(git log --pretty=oneline 2> /dev/null | wc -l)
current_branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)
if [[ $current_branch == "HEAD" ]]; then detached=true; else detached=false; fi
if [[ $current_branch == 'HEAD' ]]; then detached=true; else detached=false; fi
if [[ $is_a_git_repo == true && $number_of_logs == 0 ]]; then just_init=true; fi
if [[ $is_a_git_repo == true && $number_of_logs -gt 0 ]]; then
upstream=$(git rev-parse --symbolic-full-name --abbrev-ref @{upstream} 2> /dev/null)
if [[ $upstream != "@{upstream}" ]]; then has_upstream=true; else has_upstream=false; upstream=""; fi
if [[ $upstream != '@{upstream}' ]]; then has_upstream=true; else has_upstream=false; upstream=''; fi
git_status=$(git status --porcelain 2> /dev/null)
@ -92,13 +92,21 @@ function build_prompt {
commits_diff=$(git log --pretty=oneline --topo-order --left-right ${current_commit_hash}...${upstream} 2> /dev/null)
commits_ahead=$(echo "$commits_diff" | grep -c "^<" )
commits_behind=$(echo "$commits_diff" | grep -c "^>" )
OLD_IFS=$IFS # save ifs
IFS=$'\n'
if [[ $commits_ahead -gt 0 && $commits_behind -gt 0 ]]; then
commits_ahead=(${commits_diff//>*/})
commits_behind=(${commits_diff//<*/})
IFS=$OLD_IFS # restore ifs
commits_ahead=${#commits_ahead[@]}
commits_behind=${#commits_behind[@]}
if [[ $commits_ahead > 0 && $commits_behind > 0 ]]; then
has_diverged=true
fi
if [[ $commits_ahead -eq 0 && $commits_behind -gt 0 ]]; then
if [[ $commits_ahead == 0 && $commits_behind > 0 ]]; then
can_fast_forward=true
fi
@ -145,10 +153,10 @@ function build_prompt {
if [[ $has_diverged == true ]]; then
PS1="${PS1}-${commits_behind} ${has_diverged_symbol} +${commits_ahead} "
else
if [[ $commits_behind -gt 0 ]]; then
if [[ $commits_behind > 0 ]]; then
PS1="${PS1}${on} -${commits_behind} ${can_fast_forward_symbol} "
fi
if [[ $commits_ahead -gt 0 ]]; then
if [[ $commits_ahead > 0 ]]; then
PS1="${PS1}${on} ${should_push_symbol} +${commits_ahead} "
fi
fi
@ -167,9 +175,9 @@ function build_prompt {
fi
if [[ $two_lines == true && $is_a_git_repo == true ]]; then
break="\n"
break='\n'
else
break=""
break=''
fi
PS1="${PS1}${reset}${break}${finally}"