refactoring

This commit is contained in:
Arialdo Martini 2013-09-02 19:00:26 +02:00
parent c1fc8c0151
commit 064812ae41

View File

@ -1,8 +1,13 @@
function enrich { function enrich {
flag=$1 flag=$1
symbol=$2 symbol=$2
if [[ -n $3 ]]
if [[ $flag == true ]]; then color="${on}"; else color="${off}"; fi then
coloron=$3
else
coloron=${on}
fi
if [[ $flag == true ]]; then color="${coloron}"; else color="${off}"; fi
PS1="${PS1}${color}${symbol} " PS1="${PS1}${color}${symbol} "
} }
function enrich_if_not_null { function enrich_if_not_null {
@ -45,22 +50,37 @@ function enrich_if_greater_than_zero {
function build_prompt { function build_prompt {
PS1="" PS1=""
# Colors
on="\[\033[0;37m\]" on="\[\033[0;37m\]"
off="\[\033[1;30m\]" off="\[\033[1;30m\]"
red="\[\033[0;31m\]" alert="\[\033[0;31m\]"
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\]"
current_branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null) # Git info
if [[ -n "${current_branch}" ]] echo 1
then current_commit_hash=$(git rev-parse HEAD 2> /dev/null)
is_a_git_repo=true current_commit_hash_abbrev=$(git rev-parse --short HEAD 2> /dev/null)
else if [[ -n $current_commit_hash ]]; then is_a_git_repo=true; else is_a_git_repo=false; fi
is_a_git_repo=false
if [[ $is_a_git_repo == true ]]; then
current_branch=$(git rev-parse --abbrev-ref HEAD);
if [[ $current_branch == "HEAD" ]]; then detached=true; else detached=false; fi
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
fi fi
enrich_if_not_null "❤" $current_branch echo "is a git repo: ${is_a_git_repo}"
enrich_if_equal "⚯" "${current_branch}" "HEAD" echo "current commit hash: ${current_commit_hash}"
echo "current branch: ${current_branch}"
echo "is detached: ${detached}"
echo "upstream branch: ${upstream}"
echo "Has upstream: ${has_upstream}"
echo "-------------"
enrich ${is_a_git_repo} "❤"
enrich ${detached} "⚯" "${alert}"
number_of_modifications=$(git status --short 2> /dev/null|grep --count -e ^\.M) 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 if [[ ${number_of_modifications} -gt 0 ]] ; then has_modifications=true; else has_modifications=false; fi
@ -74,8 +94,14 @@ function build_prompt {
if [[ ${is_a_git_repo} == true ]] if [[ ${is_a_git_repo} == true ]]
then then
if [[ ${detached} == true ]]
then
PS1="${PS1} ${on}($current_commit_hash_abbrev)"
else
PS1="${PS1} ${on}(${current_branch})" PS1="${PS1} ${on}(${current_branch})"
fi fi
fi
PS1="${PS1}${reset} :" PS1="${PS1}${reset} :"
} }