-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_prompt
119 lines (106 loc) · 3.29 KB
/
bash_prompt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Colors
# Reset
Color_Off='\[\033[0m\]' # Text Reset
# Regular Colors
Black='\[\033[0;30m\]' # Black
Red='\[\033[0;31m\]' # Red
Green='\[\033[0;32m\]' # Green
Yellow='\[\033[0;33m\]' # Yellow
Blue='\[\033[0;34m\]' # Blue
Purple='\[\033[0;35m\]' # Purple
Cyan='\[\033[0;36m\]' # Cyan
White='\[\033[0;37m\]' # White
# High Intensity
IBlack='\[\033[0;90m\]' # Black
IRed='\[\033[0;91m\]' # Red
IGreen='\[\033[0;92m\]' # Green
IYellow='\[\033[0;93m\]' # Yellow
IBlue='\[\033[0;94m\]' # Blue
IPurple='\[\033[0;95m\]' # Purple
ICyan='\[\033[0;96m\]' # Cyan
IWhite='\[\033[0;97m\]' # White
# Colorized Git branch and status
# ###############################
function parse_git_branch {
GIT_PROMPT=""
state=""
remote=""
ahead=""
behind=""
branch=""
stash=""
git_dir=`git rev-parse --git-dir &> /dev/null`
is_git_dir=$?
git_status=`git status 2> /dev/null`
branch_pattern="^On branch ([^${IFS}]*)"
no_branch_pattern="^# Not currently on any branch."
remote_pattern="Your branch is (ahead|behind).*by ([[:digit:]]*) commit"
diverge_pattern="Your branch and (.*) have diverged"
diverge_counts_pattern="and have ([[:digit:]]*) and ([[:digit:]]*) different"
if [[ ${is_git_dir} -eq 0 ]]; then
if [[ ! ${git_status}} =~ "working tree clean" ]]; then
state="${IRed} X"
else
state="${Blue} C"
fi
# add an else if or two here if you want to get more specific
if [[ ${git_status} =~ ${remote_pattern} ]]; then
if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
remote="${Yellow} ^${IYellow}[${Yellow}${BASH_REMATCH[2]}${IYellow}]"
else
remote="${Yellow} v${IYellow}[${Yellow}${BASH_REMATCH[2]}${IYellow}]"
fi
fi
if [[ ${git_status} =~ ${diverge_pattern} ]]; then
if [[ ${git_status} =~ ${diverge_counts_pattern} ]]; then
ahead="${Yellow} ^${IYellow}[${Yellow}${BASH_REMATCH[1]}${IYellow}]"
behind="${Yellow} v${IYellow}[${Yellow}${BASH_REMATCH[2]}${IYellow}]"
remote="${ahead}${behind}"
else
remote="${Yellow} <>"
fi
fi
if [[ ${git_status} =~ ${branch_pattern} ]]; then
branch=${BASH_REMATCH[1]}
fi
if [[ ${git_status} =~ ${no_branch_pattern} ]]; then
git_head=`cat .git/HEAD`
branch="${git_head}"
fi
stash_size=`git stash list | wc -l 2> /dev/null | sed -e 's/^[ \t]*//'`
if [[ "${stash_size}" != "0" ]]; then
stash="${Purple} |${Blue}${stash_size}${Purple}|"
fi
GIT_PROMPT=" ${Blue}(${Cyan}${branch}${Blue})${remote}${stash}${state}"
else
GIT_PROMPT=""
fi
}
function chef_env() {
CHEF_PROMPT=""
if [[ -e ~/chef/current ]]; then
current_env=`cat ~/chef/current`
display_color=${Cyan}
if [[ ${current_env} == "riot" ]]; then
display_color=${Red}
fi
CHEF_PROMPT=" ${Blue}<${display_color}${current_env}${Blue}>"
else
CHEF_PROMPT=""
fi
}
# PROMPT
# ######
function prompt_func() {
previous_return_value=$?;
chef_env
parse_git_branch
prompt="\n${IBlue}[${Cyan}\u${White}:${IGreen}\w${IBlue}]$CHEF_PROMPT${IYellow}$GIT_PROMPT\n${Color_Off}"
if test $previous_return_value -eq 0
then
PS1="${prompt}\$ "
else
PS1="${prompt}${Red}\$${Color_Off} "
fi
}
PROMPT_COMMAND='prompt_func'