forked from nojhan/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprompt.bash
334 lines (284 loc) · 8.91 KB
/
prompt.bash
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/bin/bash
# An intelligent an non intrusive prompt for bash
# Licensed under the AGPL version 3
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# 2012 (c) nojhan <[email protected]>
# Below are function for having a flexible dynamic prompt for power user:
# - display the colored hostname when connected via ssh
# - when root, color in yellow the [username path] and in red the sharp
# - when user, print the colored git branch name (if in a git repository):
# red = some changes are not commited, yellow = some commits are not pushed,
# green = no changes or commits
# - if necessary, print a counter for jobs that are attached to the current term (e.g. xterm &)
# or that are sleeping (e.g. Ctrl-z)
# - display the load average, colored with a colormap
# - display the battery level (if necessary), with colormap
# Some colors
BLUE="\[\033[0;34m\]"
LIGHT_GRAY="\[\033[0;37m\]"
LIGHT_GREEN="\[\033[1;32m\]"
LIGHT_BLUE="\[\033[1;34m\]"
LIGHT_CYAN="\[\033[1;36m\]"
YELLOW="\[\033[1;33m\]"
WHITE="\[\033[1;37m\]"
RED="\[\033[0;31m\]"
NO_COL="\[\033[00m\]"
#################
# Where are we? #
#################
THIS_TTY=tty`ps aux | grep $$ | grep bash | awk '{ print $7 }'`
SESS_SRC=`who | grep $THIS_TTY | awk '{ print $6 }'`
# Are we in an SSH connexion?
SSH_FLAG=0
SSH_IP=`echo $SSH_CLIENT | awk '{ print $1 }'`
if [ $SSH_IP ] ; then
SSH_FLAG=1
fi
SSH2_IP=`echo $SSH2_CLIENT | awk '{ print $1 }'`
if [ $SSH2_IP ] ; then
SSH_FLAG=1
fi
if [ $SSH_FLAG -eq 1 ] ; then
CONN=ssh
elif [ -z $SESS_SRC ] ; then
CONN=lcl
elif [ $SESS_SRC = "(:0.0)" -o $SESS_SRC = "" ] ; then
CONN=lcl
else
CONN=tel
fi
###############
# Who are we? #
###############
if [ `/usr/bin/whoami` = "root" ] ; then
USR=u_root
else
USR=nou_root
fi
#####################################
# Count the number of attached jobs #
#####################################
# Either attached running jobs (started with $ myjob &)
# or attached stopped jobs (suspended with Ctrl-Z)
jobcount()
{
rep=""
running=`jobs -r | wc -l | tr -d " "`
stopped=`jobs -s | wc -l | tr -d " "`
if [ $running != "0" -a $stopped != "0" ] ; then
rep=" ${running}r/${stopped}s"
elif [ $running != "0" -a $stopped == "0" ] ; then
rep=" ${running}r"
elif [ $running == "0" -a $stopped != "0" ] ; then
rep=" ${stopped}s"
fi
echo -ne "$rep"
}
#####################
# HG branch display #
#####################
hg_branch_color()
{
if hg root >/dev/null 2>&1 ; then
red=`tput setaf 1`
green=`tput setaf 2`
yellow=`tput setaf 3`
color=""
branch="$(hg branch)"
if [ $(hg status --quiet -n | wc -l | sed -e "s/ //g") = 0 ] ; then
#has_commit=`git rev-list origin/$branch..$branch`
#if [ "$has_commit" != "" ] ; then
# color="${yellow}" # some commits to push
#else
# color="${green}" # nothing to commit or push
#fi
color=""
else
color="${red}" # changes to commit
fi
else
return 0
fi
echo -ne ${color}HG:${branch}
}
######################
# GIT branch display #
######################
# Set a color depending on the branch state:
# - green if the repository is up to date
# - yellow if there is some commits not pushed
# - red if there is changes to commit
git_branch_color()
{
if git rev-parse --git-dir >/dev/null 2>&1 ; then
red=`tput setaf 1`
green=`tput setaf 2`
yellow=`tput setaf 3`
color=""
branch=$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')
#if git diff --quiet 2>/dev/null >&2 ; then
if [ $(git status -s --untracked-files=no 2>/dev/null | wc -l | sed -e "s/ //g") = 0 ] ; then
#branch="$(git_branch)"
has_commit=`git rev-list origin/$branch..$branch`
if [ "$has_commit" != "" ] ; then
color="${yellow}" # some commits to push
else
color="${green}" # nothing to commit or push
fi
else
color="${red}" # changes to commit
fi
else
return 0
fi
echo -ne "${color}GIT:${branch}"
}
##################
# Battery status #
##################
# Get the battery status in percent
battery()
{
bat=`acpi --battery | sed "s/^Battery .*, \([0-9]*\)%.*$/\1/"`
if [ ${bat} -lt 90 ] ; then
echo -n " ${bat}%"
else
echo -n ""
fi
}
# Compute a gradient of background/forground colors depending on the battery status
battery_color()
{
gray=0
red=1
green=2
yellow=3
blue=4
magenta=5
cyan=6
white=7
bat=$(battery)
if [ "$bat" != "" ] ; then
if [ ${bat} -gt 80 ] ; then
tput bold ; tput setaf ${blue}
elif [ ${bat} -le 80 ] && [ ${bat} -gt 60 ] ; then
tput bold ; tput setaf ${green}
elif [ ${bat} -le 60 ] && [ ${bat} -gt 40 ] ; then
tput bold ; tput setaf ${yellow}
elif [ ${bat} -le 40 ] && [ ${bat} -gt 20 ] ; then
tput bold ; tput setaf ${red}
elif [ ${bat} -le 20 ] && [ ${bat} -gt 0 ] ; then
tput setaf ${gray} ; tput setab ${red}
else
tput bold ; tput setaf ${white} ; tput setab ${red}
fi
else
echo -n ""
fi
}
###############
# System load #
###############
# Get the load average
load_out()
{
#load=`uptime | sed -e "s/.*load average: \(.*\...\), \(.*\...\), \(.*\...\).*/\1/" -e "s/ //g"`
# below macos hack
load=`uptime | sed -e "s/.*load averages: \(.*\,..\) \(.*\,..\) \(.*\,..\).*/\1/" -e "s/ //g" -e "s/\,/\./g"`
tmp=$(echo $load*100 | bc)
load100=${tmp%.*}
# blow macos hack
#load100=${load%.*}
if [ ${load100} -gt 120 ] ; then
echo -n $load
else
echo -n ""
fi
}
# Compute a gradient of background/forground colors depending on the battery status
load_color()
{
gray=0
red=1
green=2
yellow=3
blue=4
magenta=5
cyan=6
white=7
# Colour progression is important ...
# bold gray -> bold green -> bold yellow -> bold red ->
# black on red -> bold white on red
#
# Then we have to choose the values at which the colours switch, with
# anything past yellow being pretty important.
load=$(load_out)
if [ "$load" != "" ] ; then
tmp=$(echo $load*100 | bc)
load100=${tmp%.*}
if [ ${load100} -lt 150 ] ; then
tput bold ; tput setaf ${blue}
elif [ ${load100} -ge 150 ] && [ ${load100} -lt 200 ] ; then
tput bold ; tput setaf ${green}
elif [ ${load100} -ge 200 ] && [ ${load100} -lt 500 ] ; then
tput bold ; tput setaf ${yellow}
elif [ ${load100} -ge 500 ] && [ ${load100} -lt 700 ] ; then
tput bold ; tput setaf ${red}
elif [ ${load100} -ge 700 ] && [ ${load100} -lt 900 ] ; then
tput setaf ${gray} ; tput setab ${red}
else
tput bold ; tput setaf ${white} ; tput setab ${red}
fi
fi
}
########################
# Construct the prompt #
########################
# different colors depending on connexion type and user
if [ $CONN = lcl -a $USR = nou_root ] ; then
PS1="${WHITE}[\u \w]${NO_COL}"
elif [ $CONN = lcl -a $USR = u_root ] ; then
PS1="${YELLOW}[\w]${NO_COL}" # no user name if we are local root
elif [ $CONN = tel -a $USR = nou_root ] ; then
PS1="[\u${LIGHT_GREEN}@\h${NO_COL} \w]${NO_COL}"
elif [ $CONN = tel -a $USR = u_root ] ; then
PS1="${RED}[\u @${YELLOW}\h${RED} \w]${NO_COL}"
elif [ $CONN = ssh -a $USR = nou_root ] ; then
PS1="[\u @${LIGHT_BLUE}\h${NO_COL} \w]${NO_COL}"
elif [ $CONN = ssh -a $USR = u_root ] ; then
PS1="${RED}[\u @${LIGHT_BLUE}\h${RED} \w]${NO_COL}"
fi
# add job count
PS1="$PS1${LIGHT_BLUE}\$(jobcount)${NO_COL}"
if [ $USR = nou_root ] ; then
# add git branch and status
#PS1="$PS1\$(git_branch_color)\$(git_branch_text)${NO_COL}"
PS1="$PS1\$(git_branch_color)${NO_COL}"
# add hg branch and status
PS1="$PS1\$(hg_branch_color)${NO_COL}"
fi
# add prompt mark
if [ $USR = nou_root ] ; then
PS1="$PS1${WHITE}\\$ ${NO_COL}"
elif [ $USR = u_root ] ; then
PS1="$PS1${RED}\\$ ${NO_COL}"
fi
# add battery status
#PS1="\[\$(battery_color)\]\$(battery) ${NO_COL}$PS1"
# add colored load average
PS1="\[\$(load_color)\]\$(load_out)${NO_COL}$PS1"
# Glue the bash prompt always go to the first column .
# Avoid glitches after interrupting a command with Ctrl-C
PS1="\[\033[G\]$PS1"