Merge pull request #2 from lvivski/master

Better code style + PS2
This commit is contained in:
Arialdo Martini 2013-11-06 06:03:02 -08:00
commit ec0d72f1af

View File

@ -1,8 +1,7 @@
function enrich {
flag=$1
symbol=$2
if [[ -n $3 ]]
then
if [[ -n $3 ]]; then
coloron=$3
else
coloron=${on}
@ -14,6 +13,7 @@ function enrich {
function build_prompt {
PS1=""
# Symbols
if [[ -z "${is_a_git_repo_symbol}" ]]; then is_a_git_repo_symbol="❤"; fi
if [[ -z "${has_untracked_files_symbol}" ]]; then has_untracked_files_symbol="∿"; fi
@ -26,7 +26,7 @@ function build_prompt {
if [[ -z "${is_on_a_tag_symbol}" ]]; then is_on_a_tag_symbol="⌫"; fi
if [[ -z "${needs_to_merge_symbol}" ]]; then needs_to_merge_symbol="ᄉ"; fi
if [[ -z "${has_upstream_symbol}" ]]; then has_upstream_symbol="⇅"; fi
if [[ -z "${detached_symbol}" ]]; then detached_symbol="⚯"; fi
if [[ -z "${detached_symbol}" ]]; then detached_symbol="⚯ "; fi
if [[ -z "${can_fast_forward_symbol}" ]]; then can_fast_forward_symbol="»"; fi
if [[ -z "${has_diverged_symbol}" ]]; then has_diverged_symbol="Ⴤ"; fi
if [[ -z "${rebase_tracking_branch_symbol}" ]]; then rebase_tracking_branch_symbol="↶"; fi
@ -34,7 +34,6 @@ function build_prompt {
if [[ -z "${should_push_symbol}" ]]; then should_push_symbol="↑"; fi
if [[ -z "${has_stashes_symbol}" ]]; then has_stashes_symbol="★"; fi
# flags
if [[ -z "${display_has_upstream}" ]]; then display_has_upstream=false; fi
if [[ -z "${display_tag}" ]]; then display_tag=false; fi
@ -51,7 +50,7 @@ function build_prompt {
yellow="\[\033[0;33m\]"
violet="\[\033[0;35m\]"
branch_color="\[\033[0;34m\]"
# blinking="\[\033[1;5;17m\]"
#blinking="\[\033[1;5;17m\]"
reset="\[\033[0m\]"
# Git info
@ -64,15 +63,14 @@ function build_prompt {
if [[ $current_branch == "HEAD" ]]; then detached=true; else detached=false; fi
if [ $is_a_git_repo == true -a $number_of_logs == 0 ]; then just_init=true; fi
if [ $is_a_git_repo == true -a $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
number_of_modifications=$(git status --short 2> /dev/null|grep --count -e ^\.M)
if [[ ${number_of_modifications} -gt 0 ]] ; then has_modifications=true; else has_modifications=false; fi
number_of_modifications_cached=$(git status --short 2> /dev/null|grep --count -e ^M)
if [[ ${number_of_modifications_cached} -gt 0 ]] ; then has_modifications_cached=true; else has_modifications_cached=false; fi
@ -81,6 +79,7 @@ function build_prompt {
number_of_deletions=$(git status --short 2> /dev/null|grep --count -e ^.D)
if [[ ${number_of_deletions} -gt 0 ]] ; then has_deletions=true; else has_deletions=false; fi
number_of_deletions_cached=$(git status --short 2> /dev/null|grep --count -e ^D)
if [[ ${number_of_deletions_cached} -gt 0 ]] ; then has_deletions_cached=true; else has_deletions_cached=false; fi
@ -92,8 +91,7 @@ function build_prompt {
if [[ ${number_of_untracked_files} -gt 0 ]] ; then has_untracked_files=true; else has_untracked_files=false; fi
tag_at_current_commit=$(git describe --exact-match --tags ${current_commit_hash} 2>/dev/null)
if [[ -n "${tag_at_current_commit}" ]]; then is_on_a_tag=true; else is_on_a_tag=false; fi;
if [[ -n "${tag_at_current_commit}" ]]; then is_on_a_tag=true; else is_on_a_tag=false; fi
commits_ahead=0
commits_behind=0
@ -104,12 +102,10 @@ function build_prompt {
commits_ahead=$(git log --pretty=oneline --topo-order --left-right ${current_commit_hash}...${upstream} | grep -c "^<" )
commits_behind=$(git log --pretty=oneline --topo-order --left-right ${current_commit_hash}...${upstream} | grep -c "^>" )
if [ ${commits_ahead} -gt 0 -a ${commits_behind} -gt 0 ];
then
has_diverged=true
if [ ${commits_ahead} -gt 0 -a ${commits_behind} -gt 0 ]; then
as_diverged=true
fi
if [ ${commits_ahead} -eq 0 -a ${commits_behind} -gt 0 ];
then
if [ ${commits_ahead} -eq 0 -a ${commits_behind} -gt 0 ]; then
can_fast_forward=true
fi
@ -121,8 +117,7 @@ function build_prompt {
is_on_a_tag=false
fi
if [[ ${is_a_git_repo} == true ]]
then
if [[ ${is_a_git_repo} == true ]]; then
enrich ${is_a_git_repo} "${is_a_git_repo_symbol}" "${violet}"
enrich ${has_stashes} "${has_stashes_symbol}" "${yellow}"
enrich ${has_untracked_files} "${has_untracked_files_symbol}" "${red}"
@ -140,63 +135,53 @@ function build_prompt {
if [[ ${display_has_upstream} == true ]]; then
enrich ${has_upstream} "${has_upstream_symbol}"
fi
if [[ ${detached} == true ]]
then
if [[ ${detached} == true ]]; then
if [[ ${just_init} == true ]]; then
PS1="${PS1} ${red}detached"
PS1="${PS1}${red}detached"
else
PS1="${PS1} ${on}(${current_commit_hash_abbrev})"
PS1="${PS1}${on}(${current_commit_hash_abbrev})"
fi
else
if [[ $has_upstream == true ]]
then
if [[ ${will_rebase} == true ]];
then
if [[ $has_upstream == true ]]; then
if [[ ${will_rebase} == true ]]; then
type_of_upstream="${rebase_tracking_branch_symbol}";
else
type_of_upstream="${merge_tracking_branch_symbol}";
fi
if [[ ${has_diverged} == true ]]; then
PS1="${PS1} -${commits_behind} ${has_diverged_symbol} +${commits_ahead}"
PS1="${PS1}-${commits_behind} ${has_diverged_symbol} +${commits_ahead} "
else
if [[ ${commits_behind} -gt 0 ]]; then
PS1="${PS1} ${on} -${commits_behind} ${can_fast_forward_symbol} "
PS1="${PS1}${on} -${commits_behind} ${can_fast_forward_symbol} "
fi
if [[ ${commits_ahead} -gt 0 ]]; then
PS1="${PS1} ${on} ${should_push_symbol} +${commits_ahead}"
PS1="${PS1}${on} ${should_push_symbol} +${commits_ahead} "
fi
fi
PS1="${PS1} (${green}${current_branch}${reset} ${type_of_upstream} ${upstream//\/$current_branch/})"
PS1="${PS1}(${green}${current_branch}${reset} ${type_of_upstream} ${upstream//\/$current_branch/})"
else
PS1="${PS1} ${on}(${green}${current_branch}${reset})"
PS1="${PS1}${on}(${green}${current_branch}${reset})"
fi
fi
if [[ ${display_tag} == true ]]; then
PS1="${PS1} ${yellow}${is_on_a_tag_symbol}${reset}"
PS1="${PS1}${yellow}${is_on_a_tag_symbol}${reset}"
fi
if [ ${display_tag_name} == true -a ${is_on_a_tag} == true ];
then
PS1="${PS1} ${yellow}[${tag_at_current_commit}]${reset}"
if [ ${display_tag_name} == true -a ${is_on_a_tag} == true ]; then
PS1="${PS1}${yellow}[${tag_at_current_commit}]${reset}"
fi
fi
if [ ${two_lines} == true -a ${is_a_git_repo} == true ];
then
if [ ${two_lines} == true -a ${is_a_git_repo} == true ]; then
break="\n";
else
break="";
fi
PS1="${PS1}${reset}${break}${finally}"
}
PROMPT_COMMAND=build_prompt
PS2="${yellow}${reset} "
PROMPT_COMMAND=build_prompt