omg/prompt.sh

99 lines
3.0 KiB
Bash
Raw Normal View History

2013-09-02 02:31:05 -04:00
function enrich {
flag=$1
symbol=$2
2013-09-02 13:00:26 -04:00
if [[ -n $3 ]]
then
coloron=$3
else
coloron=${on}
fi
if [[ $flag == true ]]; then color="${coloron}"; else color="${off}"; fi
2013-09-02 02:31:05 -04:00
PS1="${PS1}${color}${symbol} "
}
function enrich_if_greater_than_zero {
symbol=$1
variable=$2
if [[ $variable -gt 0 ]]
then
2013-09-02 02:31:05 -04:00
flag="true"
else
2013-09-02 02:31:05 -04:00
flag="false"
fi
2013-09-02 02:31:05 -04:00
enrich "${flag}" "${symbol}"
}
2013-09-02 02:31:05 -04:00
function build_prompt {
PS1=""
2013-09-02 13:00:26 -04:00
# Colors
2013-09-02 02:49:50 -04:00
on="\[\033[0;37m\]"
2013-09-02 02:31:05 -04:00
off="\[\033[1;30m\]"
2013-09-02 13:00:26 -04:00
alert="\[\033[0;31m\]"
2013-09-02 02:31:05 -04:00
branch_color="\[\033[0;34m\]"
blinking="\[\033[1;5;17m\]"
reset="\[\033[0m\]"
2013-09-02 13:00:26 -04:00
# Git info
echo 1
current_commit_hash=$(git rev-parse HEAD 2> /dev/null)
current_commit_hash_abbrev=$(git rev-parse --short HEAD 2> /dev/null)
if [[ -n $current_commit_hash ]]; then is_a_git_repo=true; else is_a_git_repo=false; fi
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
2013-09-02 13:12:27 -04:00
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
number_of_untracked_files=$(git status --short 2> /dev/null|grep --count -e ^\?\?)
if [[ ${number_of_untracked_files} -gt 0 ]] ; then has_untracked_files=true; else has_untracked_files=false; fi
2013-09-02 02:49:50 -04:00
fi
2013-09-02 13:12:27 -04:00
echo "is a git repo: ${is_a_git_repo}"
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 "Has mofications: ${has_modifications}"
echo "Has mofications_cached: ${has_modifications_cached}"
echo "Has untracked files: ${has_untracked_files}"
2013-09-02 13:00:26 -04:00
echo "-------------"
enrich ${is_a_git_repo} "❤"
enrich ${detached} "⚯" "${alert}"
2013-09-02 13:12:27 -04:00
enrich ${has_modifications} "✎"
enrich ${has_modifications_cached} "→"
enrich ${has_untracked_files} "∿"
2013-09-02 02:49:50 -04:00
if [[ ${is_a_git_repo} == true ]]
then
2013-09-02 13:00:26 -04:00
if [[ ${detached} == true ]]
then
PS1="${PS1} ${on}($current_commit_hash_abbrev)"
else
2013-09-02 13:15:33 -04:00
if [[ $has_upstream == true ]]
then
PS1="${PS1} ${on}(${current_branch} => ${upstream})"
else
PS1="${PS1} ${on}(${current_branch})"
fi
2013-09-02 13:00:26 -04:00
fi
2013-09-02 02:49:50 -04:00
fi
2013-09-02 02:31:05 -04:00
PS1="${PS1}${reset} :"
}
#PREVIOUS_PROMPT=$PS1
PROMPT_COMMAND=build_prompt