-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash_prompt
More file actions
281 lines (243 loc) · 7.96 KB
/
bash_prompt
File metadata and controls
281 lines (243 loc) · 7.96 KB
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
#!/bin/bash
# Filename: bash_prompt
# Maintainer: Felipe Fink Grael
#
# Based on Dave Vehr's work, found at
# http://www.debian-administration.org/articles/205
# Current Format: [dynamic section] USER@HOST:PWD$
# USER: (also sets the base color for the prompt)
# Red == Root(UID 0) Login shell (i.e. sudo bash)
# Light Red == Root(UID 0) Login shell (i.e. su -l or direct login)
# Yellow == Root(UID 0) priviledges in non-login shell (i.e. su)
# Brown == SU to user other than root(UID 0)
# Green == Normal user
# @:
# Light Red == http_proxy environmental variable undefined.
# Green == http_proxy environmental variable configured.
# HOST:
# Red == Insecure remote connection (unknown type)
# Yellow == Insecure remote connection (Telnet)
# Brown == Insecure remote connection (RSH)
# Cyan == Secure remote connection (i.e. SSH)
# Green == Local session
# DYNAMIC SECTION:
# (If count is zero for any of the following, it will not appear)
# [scr:#] ==== Number of detached screen sessions
# Yellow == 1-2
# Red == 3+
# [bg:#] ==== Number of backgrounded but still running jobs
# Yellow == 1-10
# Red == 11+
# [stp:#] ==== Number of stopped (backgrounded) jobs
# Yellow == 1-2
# Red == 3+
# CURRENT DIRECTORY: (truncated to 1/4 screen width)
# Red == Current user does not have write priviledges
# Green == Current user does have write priviledges
# NOTE:
# 1. Displays message on day change at midnight on the line above the
# prompt (Day changed to...).
# 2. Command is added to the history file each time you hit enter so its
# available to all shells.
# Configure Colors:
COLOR_WHITE='\033[1;37m'
COLOR_LIGHTGRAY='\033[0;37m'
COLOR_GRAY='\033[1;30m'
COLOR_BLACK='\033[0;30m'
COLOR_RED='\033[0;31m'
COLOR_LIGHTRED='\033[1;31m'
COLOR_GREEN='\033[0;32m'
COLOR_LIGHTGREEN='\033[1;32m'
COLOR_BROWN='\033[0;33m'
COLOR_YELLOW='\033[1;33m'
COLOR_BLUE='\033[0;34m'
COLOR_LIGHTBLUE='\033[1;34m'
COLOR_PURPLE='\033[0;35m'
COLOR_PINK='\033[1;35m'
COLOR_CYAN='\033[0;36m'
COLOR_LIGHTCYAN='\033[1;36m'
COLOR_DEFAULT='\033[0m'
# Color scheme
COLOR_RETVAL="\[${COLOR_RED}\]"
COLOR_GIT="\[${COLOR_BLUE}\]"
COLOR_SCREEN_LOTS="\[${COLOR_YELLOW}\]"
COLOR_SCREEN_FEW="\[${COLOR_CYAN}\]"
COLOR_BGJOBS_LOTS="\[${COLOR_YELLOW}\]"
COLOR_BGJOBS_FEW="\[${COLOR_CYAN}\]"
COLOR_STPJOBS_LOTS="\[${COLOR_YELLOW}\]"
COLOR_STPJOBS_FEW="\[${COLOR_CYAN}\]"
COLOR_USER_ROOT="\[${COLOR_LIGHTRED}\]"
COLOR_USER_NORMAL="\[${COLOR_CYAN}\]"
COLOR_DISPLAY_OK="\[${COLOR_LIGHTCYAN}\]"
COLOR_DISPLAY_NOXAUTH="\[${COLOR_LIGHTGRAY}\]"
COLOR_DISPLAY_NODISPLAY="\[${COLOR_LIGHGRAY}\]"
COLOR_HOST_SSH="\[${COLOR_LIGHTCYAN}\]"
COLOR_HOST_LOCAL="\[${COLOR_CYAN}\]"
COLOR_USER_ROOT="\[${COLOR_LIGHTRED}\]"
COLOR_USER_NORMAL="\[${COLOR_CYAN}\]"
COLOR_COLON_ROOT="\[${COLOR_LIGHTRED}\]"
COLOR_COLON_NORMAL="\[${COLOR_CYAN}\]"
COLOR_DOLLAR_ROOT="\[${COLOR_LIGHTRED}\]"
COLOR_DOLLAR_NORMAL="\[${COLOR_CYAN}\]"
COLOR_PWD_WRITABLE="\[${COLOR_WHITE}\]"
COLOR_PWD_NONWRITABLE="\[${COLOR_LIGHTGRAY}\]"
COLOR_HISTORY="\[${COLOR_LIGHTGRAY}\]"
COLOR_VIRTUALENV="\[${COLOR_GREEN}\]"
# Function to set prompt_command to.
function promptcmd () {
local RET_VAL=$?
history -a
local SSH_FLAG=0
# local TTY=$(tty | awk -F/dev/ '{print $2}')
# if [[ ${TTY} ]]; then
# local SESS_SRC=$(who | grep "$TTY " | awk '{print $6 }')
# fi
# Titlebar - commented out as Terminal.app seems to have its own way
case ${TERM} in
xterm* )
local TITLEBAR='\[\033]0;\u@\h:\w\007\]'
;;
screen* )
local TITLEBAR='\[\033k\u@\h:\w\033\134\]'
;;
* )
local TITLEBAR=''
;;
esac
# Bash history number
local PHIST="${COLOR_HISTORY}\\!:"
# Virtualenv
local PVENV=""
if [ "x$VIRTUAL_ENV" != x ] ; then
PVENV="${COLOR_VIRTUALENV}$(basename ${VIRTUAL_ENV}): "
fi
# Last Command's Return Value
local PRETVAL=''
if [ $RET_VAL -ne 0 ] ; then
PRETVAL="${COLOR_RETVAL}[${RET_VAL}] "
fi
# Git branch
local PGIT=''
if `which git >/dev/null 2>&1`; then
local GIT_BRANCH="$(git branch 2>&1 | grep '*' | cut -f 2 -d ' ')"
if [ -n "$GIT_BRANCH" ]; then
PGIT="${COLOR_GIT}($GIT_BRANCH) "
fi
fi
# Detached Screen Sessions
if `wich screen >/dev/null 2>&1`; then
local DTCHSCRN=$(screen -ls | grep -c Detach )
local PSCREEN=''
if [ ${DTCHSCRN} -gt 2 ]; then
PSCREEN="${COLOR_SCREEN_LOTS}[scr:${DTCHSCRN}] "
elif [ ${DTCHSCRN} -gt 0 ]; then
PSCREEN="${COLOR_SCREEN_FEW}[scr:${DTCHSCRN}] "
fi
fi
# Backgrounded running jobs
local BKGJBS=$(jobs -r | wc -l | sed -e 's/ *//g' )
local PBGJOBS=''
if [ ${BKGJBS} -gt 2 ]; then
PBGJOBS="${COLOR_BGJOBS_LOTS}[bg:${BKGJBS}] "
elif [ ${BKGJBS} -gt 0 ]; then
PBGJOBS="${COLOR_BGJOBS_FEW}[bg:${BKGJBS}] "
fi
# Stopped Jobs
local STPJBS=$(jobs -s | wc -l | sed -e 's/ *//g' )
local PSTPJOBS=''
if [ ${STPJBS} -gt 2 ]; then
PSTPJOBS="${COLOR_BGJOBS_LOTS}[stp:${STPJBS}] "
elif [ ${STPJBS} -gt 0 ]; then
PSTPJOBS="${COLOR_BGJOBS_FEW}[stp:${STPJBS}] "
fi
# User dependant features (username, colon and dollar sign)
local PUSER=''
local PCOLON=''
local PDOLLAR=''
if [ ${UID} -eq 0 ] ; then
PUSER="${COLOR_USER_ROOT}\u"
PCOLON="${COLOR_COLON_ROOT}:"
PDOLLAR="${COLOR_DOLLAR_ROOT}#"
else
PUSER="${COLOR_USER_NORMAL}\u"
PCOLON="${COLOR_COLON_NORMAL}:"
PDOLLAR="${COLOR_DOLLAR_NORMAL}$"
fi
# DISPLAY situation
local PAT=''
if [ -n "$DISPLAY" ] ; then
if [ -n "$XAUTHORITY" ] ; then
if [ -f $XAUTHORITY ] ; then
PAT="${COLOR_DISPLAY_OK}@"
else
PAT="${COLOR_DISPLAY_NOXAUTH}@"
fi
else
PAT="${COLOR_DISPLAY_NOXAUTH}@"
fi
else
PAT="${COLOR_DISPLAY_NODISPLAY}@"
fi
# Host
local PHOST=''
if [[ ${SSH_CLIENT} ]] || [[ ${SSH2_CLIENT} ]]; then
SSH_FLAG=1
fi
if [ ${SSH_FLAG} -eq 1 ]; then
PHOST="${COLOR_HOST_SSH}\h"
else
PHOST="${COLOR_HOST_LOCAL}\h"
fi
# Working directory
local PPWD=''
if [ -w "${PWD}" ]; then
PPWD="${COLOR_PWD_WRITABLE}$(prompt_workingdir)"
else
PPWD="${COLOR_PWD_NONWRITABLE}$(prompt_workingdir)"
fi
PS1="${TITLEBAR}${PHIST}${PVENV}${PRETVAL}${PGIT}${PSCREEN}${PBGJOBS}${PSTPJOBS}${PUSER}${PAT}${PHOST}${PCOLON}${PPWD}${PDOLLAR}\[${COLOR_DEFAULT}\] "
}
# Trim working dir to 1/4 the screen width
function prompt_workingdir () {
# Workaround for the case where the COLUMN variable isnt set yet.
if [ -z "$COLUMNS" ]; then
local COLUMNS=80
fi
local pwdmaxlen=$(($COLUMNS/4))
local trunc_symbol="..."
if [[ $PWD == $HOME* ]]; then
newPWD="~${PWD#$HOME}"
else
newPWD=${PWD}
fi
if [ ${#newPWD} -gt $pwdmaxlen ]; then
local pwdoffset=$(( ${#newPWD} - $pwdmaxlen + 3 ))
newPWD="${trunc_symbol}${newPWD:$pwdoffset:$pwdmaxlen}"
fi
echo $newPWD
}
# Determine what prompt to display:
# 1. Display simple custom prompt for shell sessions started
# by script.
# 2. Display "bland" prompt for shell sessions within vim
# 3 Display promptcmd for all other cases.
function load_prompt () {
# Get PIDs
#local my_process=$(cat /proc/$$/cmdline | cut -d \. -f 1)
if [ `uname -s` != "Linux" ]; then
local parent_process=unknown
else
local parent_process=$(cat /proc/$PPID/cmdline | cut -d \. -f 1)
fi
if [[ $parent_process == script* ]]; then
PROMPT_COMMAND=""
PS1="\u@\H:\w\$ "
elif [[ $parent_process == vim* || $parent_process == gvim* ]]; then
PROMPT_COMMAND=""
PS1="\u@\H:\w\$ "
else
PROMPT_COMMAND=promptcmd
fi
export PS1 PROMPT_COMMAND
}
load_prompt