-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash_prompt
198 lines (172 loc) · 5.46 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env bash
# Sexy bash prompt by twolfson
# https://github.com/twolfson/sexy-bash-prompt
# Forked from gf3, https://gist.github.com/gf3/306785
# Determine what type of terminal we are using for `tput`
if [[ $COLORTERM = gnome-* && $TERM = xterm ]] && infocmp gnome-256color >/dev/null 2>&1; then export TERM=gnome-256color
elif [[ $TERM != dumb ]] && infocmp xterm-256color >/dev/null 2>&1; then export TERM=xterm-256color
fi
# If we are on a colored terminal
if tput setaf 1 &> /dev/null; then
# Reset the shell from our `if` check
tput sgr0
# If the terminal supports at least 256 colors, write out our 256 color based set
if [[ $(tput colors) -ge 256 ]] 2>/dev/null; then
USER_COLOR=$(tput setaf 27) #BLUE
PREPOSITION_COLOR=$(tput setaf 7) #WHITE
DEVICE_COLOR=$(tput setaf 39) #INDIGO
DIR_COLOR=$(tput setaf 76) #GREEN
GIT_STATUS_COLOR=$(tput setaf 154) #YELLOW
else
# Otherwise, use colors from our set of 16
# Original colors from fork
USER_COLOR=$(tput setaf 5) #MAGENTA
PREPOSITION_COLOR=$(tput setaf 7) #WHITE
DEVICE_COLOR=$(tput setaf 4) #ORANGE
DIR_COLOR=$(tput setaf 2) #GREEN
GIT_STATUS_COLOR=$(tput setaf 1) #PURPLE
fi
# Save common color actions
BOLD=$(tput bold)
NORMAL=$PREPOSITION_COLOR
RESET=$(tput sgr0)
else
# Otherwise, use ANSI escape sequences for coloring
# Original colors from fork
USER_COLOR="\033[1;31m" #MAGENTA
PREPOSITION_COLOR="\033[1;37m" #WHITE
DEVICE_COLOR="\033[1;33m" #ORANGE
DIR_COLOR="\033[1;32m" #GREEN
GIT_STATUS_COLOR="\033[1;35m" #PURPLE
BOLD=""
RESET="\033[m"
fi
function get_git_branch() {
# On branches, this will return the branch name
# On non-branches, (no branch)
REF="$(git symbolic-ref HEAD 2> /dev/null | sed -e 's/refs\/heads\///')"
if [[ $REF != "" ]]; then
echo $REF
else
echo "(no branch)"
fi
}
is_branch1_behind_branch2 () {
# $ git log origin/master..master -1
# commit 4a633f715caf26f6e9495198f89bba20f3402a32
# Author: Todd Wolfson <todd@twolfson.com>
# Date: Sun Jul 7 22:12:17 2013 -0700
#
# Unsynced commit
# Find the first log (if any) that is in branch1 but not branch2
FIRST_LOG="$(git log $1..$2 -1 2> /dev/null)"
# Exit with 0 if there is a first log, 1 if there is not
[[ -n $FIRST_LOG ]]
}
branch_exists () {
# List remote branches | # Find our branch and exit with 0 or 1 if found/not found
git branch --remote 2> /dev/null | grep --quiet "$1"
}
parse_git_ahead () {
# Grab the local and remote branch
BRANCH="$(get_git_branch)"
REMOTE_BRANCH=origin/"$BRANCH"
# $ git log origin/master..master
# commit 4a633f715caf26f6e9495198f89bba20f3402a32
# Author: Todd Wolfson <todd@twolfson.com>
# Date: Sun Jul 7 22:12:17 2013 -0700
#
# Unsynced commit
# If the remote branch is behind the local branch
# or it has not been merged into origin (remote branch doesn't exist)
if (is_branch1_behind_branch2 "$REMOTE_BRANCH" "$BRANCH" ||
! branch_exists "$REMOTE_BRANCH"); then
# echo our character
echo 1
fi
}
parse_git_behind () {
# Grab the branch
BRANCH="$(get_git_branch)"
REMOTE_BRANCH=origin/"$BRANCH"
# $ git log master..origin/master
# commit 4a633f715caf26f6e9495198f89bba20f3402a32
# Author: Todd Wolfson <todd@twolfson.com>
# Date: Sun Jul 7 22:12:17 2013 -0700
#
# Unsynced commit
# If the local branch is behind the remote branch
if is_branch1_behind_branch2 "$BRANCH" "$REMOTE_BRANCH"; then
# echo our character
echo 1
fi
}
parse_git_dirty () {
# ?? file.txt # Unstaged new files
# A file.txt # Staged new files
# M file.txt # Unstaged modified files
# M file.txt # Staged modified files
# D file.txt # Unstaged deleted files
# D file.txt # Staged deleted files
# If the git status has *any* changes (i.e. dirty)
if [[ -n "$(git status --porcelain 2> /dev/null)" ]]; then
# echo our character
echo 1
fi
}
function is_on_git() {
git rev-parse 2> /dev/null
}
function get_git_status() {
# Grab the git dirty and git behind
DIRTY_BRANCH="$(parse_git_dirty)"
BRANCH_AHEAD="$(parse_git_ahead)"
BRANCH_BEHIND="$(parse_git_behind)"
# Iterate through all the cases and if it matches, then echo
if [[ $DIRTY_BRANCH == 1 && $BRANCH_AHEAD == 1 && $BRANCH_BEHIND == 1 ]]; then
echo "⬢"
elif [[ $DIRTY_BRANCH == 1 && $BRANCH_AHEAD == 1 ]]; then
echo "▲"
elif [[ $DIRTY_BRANCH == 1 && $BRANCH_BEHIND == 1 ]]; then
echo "▼"
elif [[ $BRANCH_AHEAD == 1 && $BRANCH_BEHIND == 1 ]]; then
echo "⬡"
elif [[ $BRANCH_AHEAD == 1 ]]; then
echo "△"
elif [[ $BRANCH_BEHIND == 1 ]]; then
echo "▽"
elif [[ $DIRTY_BRANCH == 1 ]]; then
echo "*"
fi
}
get_git_info () {
# Grab the branch
BRANCH="$(get_git_branch)"
# If there are any branches
if [[ $BRANCH != "" ]]; then
# Echo the branch
OUTPUT=$BRANCH
# Add on the git status
OUTPUT=$OUTPUT"$(get_git_status)"
# Echo our output
echo $OUTPUT
fi
}
# Symbol displayed at the line of every prompt
get_prompt_symbol () {
# Some prompts display $ for a non-version control dir, ∓ for git, and § for mercurial
# I have chosen to keep it consistent
echo "\$"
}
# Define the sexy-bash-prompt
PS1="\[${BOLD}${USER_COLOR}\]\u \
\[$PREPOSITION_COLOR\]at \
\[$DEVICE_COLOR\]\h \
\[$PREPOSITION_COLOR\]in \
\[$DIR_COLOR\]\w\
\[$PREPOSITION_COLOR\]\
\$( is_on_git && \
echo -n \" on \" && \
echo -n \"\[$GIT_STATUS_COLOR\]\$(get_git_info)\" && \
echo -n \"\[$NORMAL\]\")\n\
$(get_prompt_symbol) \[$RESET\]"