forked from brandonweiss/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_profile
95 lines (74 loc) · 1.8 KB
/
bash_profile
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
. ~/.bashrc
RESTORE="\033[0m"
RED="\033[00;31m"
BLUE="\033[00;34m"
MAGENTA="\033[00;35m"
CYAN="\033[00;36m"
SPECIALGRAY="\033[38;5;242m"
function _pwd_with_tilde() {
echo $PWD | sed 's|^'$HOME'\(.*\)$|~\1|'
}
function _in_git_directory() {
git rev-parse --git-dir > /dev/null 2>&1
}
function _git_branch_name_or_revision() {
local branch=$(git symbolic-ref HEAD 2> /dev/null | sed -e 's|^refs/heads/||')
local revision=$(git rev-parse HEAD 2> /dev/null | cut -b 1-7)
if [ -n $branch ]; then
echo $branch
else
echo $revision
fi
}
function _git_upstream_configured() {
git rev-parse --abbrev-ref @"{u}" > /dev/null 2>&1
}
function _git_behind_upstream() {
local commits_behind=$(git rev-list --right-only --count HEAD...@"{u}" 2> /dev/null)
[ $commits_behind -gt 0 ]
}
function _git_ahead_of_upstream() {
local commits_ahead=$(git rev-list --left-only --count HEAD...@"{u}" 2> /dev/null)
[ $commits_ahead -gt 0 ]
}
function _git_upstream_status() {
local arrows=""
if _git_upstream_configured; then
if _git_behind_upstream; then
arrows="$arrows"⇣
fi
if _git_ahead_of_upstream; then
arrows="$arrows"⇡
fi
fi
echo $arrows
}
function _print_in_color() {
local string=$1
local color=$2
echo -en $color
echo -n $string
echo -en $RESTORE
}
function _prompt_color_for_status() {
if [ $1 -eq 0 ]; then
echo $MAGENTA
else
echo $RED
fi
}
function prompt() {
local last_status=$?
printf "\n"
_print_in_color $(_pwd_with_tilde) $BLUE
if _in_git_directory; then
printf " "
_print_in_color " $(_git_branch_name_or_revision)" $SPECIALGRAY
printf " "
_print_in_color " $(_git_upstream_status)" $CYAN
fi
printf "\n"
_print_in_color "❯ " $(_prompt_color_for_status $last_status)
printf " "
}
export PS1="\$(prompt)"