-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.bash_prompt_command
executable file
·171 lines (143 loc) · 4.71 KB
/
.bash_prompt_command
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
# Define colors
RESET='\[\033[00m\]'
FGBK='\[\033[0;30m\]' # foreground black
FGRD='\[\033[0;31m\]' # foreground red
FGGN='\[\033[0;32m\]' # foreground green
FGYL='\[\033[0;33m\]' # foreground yellow
FGBL='\[\033[0;34m\]' # foreground blue
FGMG='\[\033[0;35m\]' # foreground magenta
FGCY='\[\033[0;36m\]' # foreground cyan
FGWT='\[\033[0;37m\]' # foreground white
FGBKBT='\[\033[1;30m\]' # foreground black bright
FGRDBT='\[\033[1;31m\]' # foreground red bright
FGGNBT='\[\033[1;32m\]' # foreground green bright
FGYLBT='\[\033[1;33m\]' # foreground yellow bright
FGBLBT='\[\033[1;34m\]' # foreground blue bright
FGMGBT='\[\033[1;35m\]' # foreground magenta bright
FGCYBT='\[\033[1;36m\]' # foreground cyan bright
FGWTBT='\[\033[1;37m\]' # foreground white bright
BGBK='\[\033[40m\]' # background black
BGRD='\[\033[41m\]' # background red
BGGN='\[\033[42m\]' # background green
BGYL='\[\033[43m\]' # background yellow
BGBL='\[\033[44m\]' # background blue
BGMG='\[\033[45m\]' # background magenta
BGCY='\[\033[46m\]' # background cyan
BGWT='\[\033[47m\]' # background white
# Give the hostname an emphasized color
HOSTCOLOR="$FGRD"
# Date Format
DATEFORMAT='%a %b %d %T'
# set the prompt symbol character differently for root
function set_prompt_character () {
if [[ ${EUID} == 0 ]]; then
PROMPT_CHARACTER='#'
else
PROMPT_CHARACTER='$'
fi
}
# Return the prompt symbol to use, colorized based on the return value of the
# previous command.
function set_prompt_symbol () {
if [[ ${1} -eq 0 ]]; then
PROMPT_SYMBOL="${PROMPT_CHARACTER}"
else
PROMPT_SYMBOL="${FGRD}${PROMPT_CHARACTER}${RESET}"
fi
}
# Change the color of the username if root
function prompt_set_user_color () {
if [[ ${EUID} == 0 ]]; then
USER_COLOR="${FGWT}${BGRD}"
else
USER_COLOR="${FGBL}"
fi
}
# Detect whether the current directory is a git repository.
function is_git_repository {
git branch > /dev/null 2>&1
}
# Determine the branch/state information for this git repository.
function set_git_branch {
# Capture the output of the "git status" command.
git_status="$(git status 2> /dev/null)"
# Set color based on clean/staged/dirty.
if [[ ${git_status} =~ "working tree clean" ]]; then # clean
state=$FGGN
elif [[ ${git_status} =~ "Changes to be committed" ]]; then # staged
state=$FGYL
elif [[ ${git_status} =~ "Changes not staged" ]]; then # unstaged
state=$FGYL
elif [[ ${git_status} =~ "Untracked files" ]]; then # untracked
state=$FGYL
else # unknown
state=$FGRD
fi
# Set arrow icon based on status against remote.
remote=""
remote_pattern="Your branch is ([[:graph:]]+)"
diverge_pattern="Your branch and (.*) have diverged"
if [[ ${git_status} =~ ${remote_pattern} ]]; then
if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
remote="↑"
elif [[ ${BASH_REMATCH[1]} == "behind" ]]; then
remote="↓"
fi
elif [[ ${git_status} =~ ${diverge_pattern} ]]; then
remote="↕"
fi
# Get the name of the branch.
branch_pattern="On branch ([^${IFS}]*)"
detached_pattern="HEAD detached at ([^${IFS}]*)"
if [[ ${git_status} =~ ${branch_pattern} ]]; then
branch=${BASH_REMATCH[1]}
elif [[ ${git_status} =~ ${detached_pattern} ]]; then
branch=${BASH_REMATCH[1]}
else
branch="<???>"
fi
# Set the final branch string.
BRANCH_COLOR="${state}[${branch}]${remote}$RESET"
BRANCH_PLAIN="[${branch}]${remote}"
}
# Set the full bash prompt.
function set_bash_prompt () {
# Set the BRANCH variable.
if is_git_repository ; then
set_git_branch
else
BRANCH_COLOR=''
BRANCH_PLAIN=''
fi
# Set USER_COLOR
prompt_set_user_color
ps1_username=$(whoami)
ps1_username="${ps1_username:0:10}"
ps1_hostname=$(hostname -s)
ps1_hostname="${ps1_hostname:0:20}"
ps1_pwd=$(pwd | sed "s|^$HOME|~|")
ps1_datetime=$(date "+${DATEFORMAT}")
user_at_host="${ps1_username}@${ps1_hostname}"
lefthalf="${user_at_host} ${ps1_pwd} ${BRANCH_PLAIN}"
righthalf="${ps1_datetime}"
# Fill spaces between the left and right halves
termwidth=${COLUMNS:-$(tput cols 2>&-||echo 80)}
let fillsize=${termwidth}-${#lefthalf}-${#righthalf}
if [[ $fillsize -gt 1 ]]; then
fill=$(printf ' %.0s' {1..300}) # 300 spaces
fill=${fill:0:$fillsize}
else
ps1_pwd="${ps1_pwd:0:20}..${ps1_pwd:(23+${#lefthalf}+${#righthalf}-${termwidth})}"
fill=' '
fi
# Set the bash prompt variable.
PS1="\n${USER_COLOR}${ps1_username}${FGCY}@${HOSTCOLOR}${ps1_hostname}${RESET} ${FGWT}${ps1_pwd}${RESET} ${BRANCH_COLOR}${fill}${FGBL}${ps1_datetime}${RESET}\n${PROMPT_SYMBOL} "
}
# Set PROMPT_CHARACTER
set_prompt_character
# Set PROMPT_SYMBOL -- caller must pass ${?} to properly set result code
set_prompt_symbol "${1}"
# Set PS1
set_bash_prompt
# Output PS1
echo "${PS1}"