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