-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprompt.sh
241 lines (206 loc) · 5.86 KB
/
prompt.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
function under_bash () {
[[ "`ps -p $$ | tail -1|awk '{print $NF}'`" == "-bash" ]] 2>&1 && return
return 1
}
function under_zsh () {
[[ "`ps -p $$ | tail -1|awk '{print $NF}'`" == "-zsh" ]] 2>&1 && return
return 1
}
msgp_color_red=$'\e[31m'
msgp_color_yellow=$'\e[33m'
msgp_color_green=$'\e[32m'
msgp_color_cyan=$'\e[36m'
msgp_color_blue=$'\e[34m'
msgp_color_magenta=$'\e[35m'
msgp_color_white=$'\e[37m'
msgp_color_bold_red=$'\e[31;1m'
msgp_color_bold_yellow=$'\e[33;1m'
msgp_color_bold_green=$'\e[36;1m'
msgp_color_bold_cyan=$'\e[32;1m'
msgp_color_bold_blue=$'\e[34;1m'
msgp_color_bold_magenta=$'\e[35;1m'
msgp_color_bold_white=$'\e[37;1m'
msgp_reset_color=$'\e[37m'
# edit colours and characters here
#############################################
if under_bash; then
# msgp_user_color=$msgp_color_green - change the user to green
msgp_user_color=$msgp_color_cyan
msgp_host_color=$msgp_color_yellow
msgp_root_color=$msgp_color_green
msgp_repo_color=$msgp_color_red
msgp_branch_color=$msgp_color_white
msgp_dirty_color=$msgp_color_red
msgp_preposition_color=$msgp_color_white
msgp_promptchar_color=$msgp_color_magenta
elif under_zsh; then
msgp_user_color=$msgp_color_magenta
msgp_host_color=$msgp_color_yellow
msgp_root_color=$msgp_color_green
msgp_repo_color=$msgp_color_red
msgp_branch_color=$msgp_color_white
msgp_dirty_color=$msgp_color_red
msgp_preposition_color=$msgp_color_white
msgp_promptchar_color=$msgp_color_cyan
fi
msgp_promptchar_bash='$'
msgp_promptchar_zsh='%%' # the % character is special in zsh - escape with a preceding %
msgp_promptchar_git='±'
#############################################
function in_git_repo {
git branch > /dev/null 2>&1 && return
return 1
}
function in_repo {
in_git_repo && return
return 1
}
function prompt_char {
in_git_repo && echo -ne $msgp_promptchar_git && return
if under_bash; then
echo $msgp_promptchar_bash
elif under_zsh; then
echo $msgp_promptchar_zsh
fi
}
function location_title {
if in_repo; then
local root=$(get_repo_root)
local uroot="$(get_unversioned_repo_root)/"
echo "${root/$uroot/} ($(get_repo_type))"
else
echo "${PWD/$HOME/~}"
fi
}
function get_repo_type {
in_git_repo && echo -ne "git" && return
return 1
}
function get_repo_branch {
in_git_repo && echo $(git branch | grep '*' | cut -d ' ' -f 2) && return
return 1
}
function get_main_branch_name () {
in_git_repo && echo "master" && return
return 1
}
function get_repo_status {
in_git_repo && git status --porcelain && return
return 1
}
function get_repo_root {
in_git_repo && echo $(git rev-parse --show-toplevel) && return
return 1
}
function get_unversioned_repo_root {
local lpath="$1"
local cPWD=`echo $PWD`
# see if $lpath is non-existent or empty, and if so, assign
if test ! -s "$lpath"; then
local lpath=`echo $PWD`
fi
cd "$lpath" &> /dev/null
local repo_root="$(get_repo_root)"
# see if $repo_root is non-existent or empty, and if so, assign
if test ! -s "$repo_root"; then
echo $lpath
else
local parent="${lpath%/*}"
get_unversioned_repo_root "$parent"
fi
cd "$cPWD" &> /dev/null
}
# display current path
function ps_status {
in_repo && repo_status && return
echo -e "$msgp_root_color${PWD/#$HOME/~} $msgp_reset_color"
}
function repo_status {
# set locations
local here="$PWD"
local user_root="$HOME"
local repo_root="$(get_repo_root)"
local root="`get_unversioned_repo_root`/"
local lpath="${here/$root/}"
if [[ "`echo $root`" =~ ^$user_root ]]; then
root=`echo "$root" | sed "s:^$user_root:~:g"`
fi
# get branch information - empty if no (or default) branch
local branch=$(get_repo_branch)
# underline branch name
if [[ $branch != '' ]]; then
if under_zsh; then
local branch=" on %{\033[4m%}${branch}%{\033[0m%}"
elif under_bash; then
local branch=" on \033[4m${branch}\033[0m"
fi
fi
# status of current repo
if in_git_repo; then
local lstatus="`get_repo_status | sed 's/^ */g/'`"
else
local lstatus=''
fi
local status_count=`echo "$lstatus" | wc -l | awk '{print $1}'`
# if there's anything to report on...
if [[ "$status_count" -gt 0 ]]; then
local changes=""
# modified file count
local modified="$(echo "$lstatus" | grep -c '^[gm]M')"
if [[ "$modified" -gt 0 ]]; then
changes="$modified changed"
fi
# added file count
local added="$(echo "$lstatus" | grep -c '^[gm]A')"
if [[ "$added" -gt 0 ]]; then
if [[ "$changes" != "" ]]; then
changes="${changes}, "
fi
changes="${changes}${added} added"
fi
# removed file count
local removed="$(echo "$lstatus" | grep -c '^(mR|gD)')"
if [[ "$removed" -gt 0 ]]; then
if [[ "$changes" != "" ]]; then
changes="${changes}, "
fi
changes="${changes}${removed} removed"
fi
# renamed file count
local renamed="$(echo "$lstatus" | grep -c '^gR')"
if [[ "$renamed" -gt 0 ]]; then
if [[ "$changes" != "" ]]; then
changes="${changes}, "
fi
changes="${changes}${removed} renamed"
fi
# missing file count
local missing="$(echo "$lstatus" | grep -c '^m!')"
if [[ "$missing" -gt 0 ]]; then
if [[ "$changes" != "" ]]; then
changes="${changes}, "
fi
changes="${changes}${missing} missing"
fi
# untracked file count
local untracked="$(echo "$lstatus" | grep -c '^[gm]?')"
if [[ "$untracked" -gt 0 ]]; then
if [[ "$changes" != "" ]]; then
changes="${changes}, "
fi
changes="${changes}${untracked} untracked"
fi
if [[ "$changes" != "" ]]; then
changes=" (${changes})"
fi
fi
echo -e "$msgp_root_color$root$msgp_repo_color$lpath$msgp_branch_color$branch$msgp_dirty_color$update$changes"
}
function construct_prompt () {
echo -e "$msgp_user_color$USER ${msgp_preposition_color}at $msgp_host_color`hostname -s` ${msgp_preposition_color}in $(ps_status)$msgp_promptchar_color\n$(prompt_char)"
}
if under_bash; then
export PS1='$(construct_prompt)\[$msgp_reset_color\] '
elif under_zsh; then
PROMPT='$(construct_prompt)%{$msgp_reset_color%} '
fi