-
Notifications
You must be signed in to change notification settings - Fork 1
/
agnoster_bash.sh
172 lines (153 loc) · 4.87 KB
/
agnoster_bash.sh
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/bash
# Agnoster BASH Theme
#
# Author: H@di([email protected])
#
# Usage: 1. set a powerline-compatible font for terminal
# 2. append this file to your ~/.bash_profile
#
# • powerline-compatible fonts: https://github.com/powerline/fonts/tree/master/SourceCodePro
# • Monoco powerline-compatible font: https://gist.github.com/rogual/6824790627960fc93077
# • agnoster.zsh-theme: https://github.com/agnoster/agnoster-zsh-theme
PROMPT_COLOR="\[\e[1;2m\]"
function get_git_stat {
if ! $(git rev-parse --is-inside-git-dir 2>/dev/null) && ! $(git rev-parse --is-inside-work-tree 2>/dev/null) && ! $(git rev-parse --is-bare-repository 2>/dev/null); then
echo 0 # not a git repo
else
if [[ $(git status --porcelain 2>/dev/null | wc -l) != 0 ]]; then
echo 2 # modified
elif [[ $(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null | wc -l) != 0 ]]; then
if [[ $(git rev-list HEAD@{upstream}..HEAD 2>/dev/null | wc -l) != 0 && $(git rev-list HEAD..HEAD@{upstream} 2>/dev/null | wc -l) != 0 ]]; then
echo 5 # diverged
elif [[ $(git rev-list HEAD@{upstream}..HEAD 2>/dev/null | wc -l) != 0 ]]; then
echo 3 # ahead
elif [[ $(git rev-list HEAD..HEAD@{upstream} 2>/dev/null | wc -l) != 0 ]]; then
echo 4 # behind
else
echo 1 # clean
fi
else
echo 1 # clean
fi
fi
}
function is_git_detached {
if [[ $(git rev-parse --abbrev-ref HEAD 2>/dev/null) == "HEAD" ]]; then
echo 1
else
echo 0
fi
}
function get_fg {
echo "\[\e[$((30+$1))m\]"
}
function get_bg {
echo "\[\e[$((40+$1))m\]"
}
function get_fg_def {
if [[ $# == 0 ]]; then
echo 9
else
if [[ $1 > 60 ]]; then
echo 0
else
echo 67
fi
fi
}
function ps1_gen {
retval=$?
GIT_STAT=$(get_git_stat)
git_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
git_head_sh1=$(git rev-parse --verify HEAD 2>/dev/null)
SEGMENT_SEPARATOR=$'\ue0b0'
PLUSMINUS=$'\u00b1'
BRANCH=$'\ue0a0'
DETACHED=$'\u27a6'
CROSS=$'\u2718'
LIGHTNING=$'\u26a1'
GEAR=$'\u2699'
PUSH=$'\u21d1'
PULL=$'\u21d3'
MERGE=$'\u2694'
STAT_COLOR=0
VIRTUALENV_COLOR=64
DIR_COLOR=66
GIT_COLOR=62
NO_COLOR="\[\e[0;39;49m\]"
if [[ $retval != 0 ]]; then
prompt_stat="`get_fg 1`${CROSS}`get_fg $(get_fg_def)` "
else
prompt_stat=""
fi
if [[ $(jobs -l | wc -l) != 0 ]]; then
prompt_stat="${prompt_stat}${GEAR} "
fi
if [[ $UID == 0 ]]; then
prompt_stat="${prompt_stat}${LIGHTNING} "
fi
prompt_stat="${prompt_stat}\u"
if [[ $VIRTUAL_ENV ]]; then
prompt_virtualenv="`basename $VIRTUAL_ENV`"
fi
prompt_dir="\W"
if [[ $GIT_STAT != 0 ]]; then
if [[ $(is_git_detached) == 1 ]]; then
prompt_git="${DETACHED} ${git_head_sh1::7}"
else
prompt_git="${BRANCH} ${git_branch}"
fi
if [[ $GIT_STAT == 3 ]]; then
prompt_git="${prompt_git}${PUSH}"
elif [[ $GIT_STAT == 4 ]]; then
prompt_git="${prompt_git}${PULL}"
elif [[ $GIT_STAT == 5 ]]; then
prompt_git="${prompt_git}${MERGE} "
fi
if [[ $GIT_STAT == 1 ]]; then
GIT_COLOR=62
elif [[ $GIT_STAT == 2 ]]; then
GIT_COLOR=61
else
GIT_COLOR=63
fi
fi
if [[ $prompt_stat != "" ]]; then
prompt_stat="`get_bg $STAT_COLOR``get_fg $(get_fg_def)`${prompt_stat}`get_fg $STAT_COLOR`"
fi
if [[ $prompt_virtualenv != "" ]]; then
prompt_virtualenv="`get_bg $VIRTUALENV_COLOR`${SEGMENT_SEPARATOR}`get_fg $(get_fg_def $VIRTUALENV_COLOR)` ${prompt_virtualenv}`get_fg $VIRTUALENV_COLOR`"
fi
if [[ $prompt_dir != "" ]]; then
prompt_dir="`get_bg $DIR_COLOR`${SEGMENT_SEPARATOR}`get_fg $(get_fg_def $DIR_COLOR)` ${prompt_dir}`get_fg $DIR_COLOR`"
fi
if [[ $prompt_git != "" ]]; then
prompt_git="`get_bg $GIT_COLOR`${SEGMENT_SEPARATOR}`get_fg $(get_fg_def $GIT_COLOR)` ${prompt_git}`get_fg $GIT_COLOR`"
fi
prompt_end="`get_bg 0`${SEGMENT_SEPARATOR}${NO_COLOR} "
PS1="${prompt_stat}${prompt_virtualenv}${prompt_dir}${prompt_git}${prompt_end}"
unset SEGMENT_SEPARATOR
unset PLUSMINUS
unset BRANCH
unset DETACHED
unset CROSS
unset LIGHTNING
unset GEAR
unset retval
unset git_head_sh1
unset git_branch
unset prompt_retval
unset prompt_virtualenv
unset prompt_git
unset prompt_stat
unset prompt_end
unset STAT_COLOR
unset VIRTUALENV_COLOR
unset DIR_COLOR
unset GIT_COLOR
unset NO_COLOR
}
PS2="${PROMPT_COLOR}${PS2}\e[0m"
PS3="${PROMPT_COLOR}${PS3}\e[0m"
PS4="${PROMPT_COLOR}${PS4}\e[0m"
PROMPT_COMMAND="ps1_gen;$PROMPT_COMMAND"