This repository has been archived by the owner on Nov 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fancy-bash-prompt.sh
executable file
·290 lines (205 loc) · 8.09 KB
/
fancy-bash-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
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
#!/usr/bin/env bash
## +-----------------------------------+-----------------------------------+
## | |
## | Copyright (c) 2018-2019, Andres Gongora <mail@andresgongora.com>. |
## | |
## | This program is free software: you can redistribute it and/or modify |
## | it under the terms of the GNU 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 General Public License for more details. |
## | |
## | You should have received a copy of the GNU General Public License |
## | along with this program. If not, see <http://www.gnu.org/licenses/>. |
## | |
## +-----------------------------------------------------------------------+
##
## DESCRIPTION
##
## This script updates your "PS1" environment variable to display colors.
## Additionally, it also shortens the name of your current path to a maximum
## 25 characters, which is quite useful when working in deeply nested folders.
##
##
##
## REFFERENCES
##
## * http://tldp.org/HOWTO/Bash-Prompt-HOWTO/index.html
##
##
for file in $HOME/.bashrc.d/fancy-bash-prompt/*.sh
do
source $file
done
fancy_bash_prompt()
{
##==============================================================================
## FUNCTIONS
##==============================================================================
##------------------------------------------------------------------------------
## getGitBranch
## Returns current git branch for current directory, if (and only if)
## the current directory is part of a git repository, and git is installed.
## Returns an empty string otherwise.
##
getGitBranch()
{
if ( which git > /dev/null 2>&1 ); then
git branch 2> /dev/null | sed -n '/^[^*]/d;s/*\s*\(.*\)/\1/p'
else
echo ""
fi
}
##------------------------------------------------------------------------------
##
printSegment()
{
## GET PARAMETERS
local text=$1
local font_color=$2
local background_color=$3
local next_background_color=$4
local font_effect=$5
if [ -z "$separator_char" ]; then local separator_char='\uE0B0'; fi
## COMPUTE COLOR FORMAT CODES
local no_color="\[$(getFormatCode -e reset)\]"
local text_format="\[$(getFormatCode -c $font_color -b $background_color -e $font_effect)\]"
local separator_format="\[$(getFormatCode -c $background_color -b $next_background_color)\]"
## GENERATE TEXT
printf "${text_format}${text}${separator_format}${separator_char}${no_color}"
}
##------------------------------------------------------------------------------
##
prompt_command_hook()
{
## LOAD EXTERNAL DEPENENCIES
## Only if the functions are not available
## If not, search in `common` folder
if [ "$(type -t shortenPath)" != 'function' ];
then
local dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
source "$dir/../bash-tools/bash-tools/shorten_path.sh"
fi
if [ "$(type -t removeColorCodes)" != 'function' ];
then
local dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
source "$dir/../bash-tools/bash-tools/color.sh"
fi
## GET PARAMETERS
local user=$USER
local host=$HOSTNAME
local path="$(shortenPath "$PWD" 20)"
local git_branch="$(shortenPath "$(getGitBranch)" 15)"
## UPDATE BASH PROMPT ELEMENTS
FBP_USER="$user"
FBP_HOST="$host"
FBP_PWD="$path"
if [ -z "$git_branch" ]; then
FBP_GIT=""
else
FBP_GIT="$git_branch"
fi
## CHOOSE PS1 FORMAT IF INSIDE GIT REPO
if [ ! -z "$(getGitBranch)" ] && $FBP_SHOW_GIT; then
PS1=$FBP_PS1_GIT
else
PS1=$FBP_PS1
fi
}
##------------------------------------------------------------------------------
##
## INCLUDE EXTERNAL DEPENDENCIES
## Only if the functions are not available
## If not, search in `common` folder
local dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
if [ "$(type -t getFormatCode)" != 'function' ]; then
source "$dir/../bash-tools/bash-tools/color.sh"
fi
## DEFAULT CONFIGURATION
## WARNING! Do not edit directly, use configuration files instead
local font_color_user="white"
local background_user="blue"
local texteffect_user="bold"
local font_color_host="white"
local background_host="light-blue"
local texteffect_host="bold"
local font_color_pwd="dark-gray"
local background_pwd="white"
local texteffect_pwd="bold"
local font_color_git="light-gray"
local background_git="dark-gray"
local texteffect_git="bold"
local font_color_input="cyan"
local background_input="none"
local texteffect_input="bold"
local separator_char='\uE0B0'
local enable_vertical_padding=true
local show_git=true
## LOAD USER CONFIGURATION
local user_config_file="$HOME/.config/synth-shell/fancy-bash-prompt.config"
local sys_config_file="/etc/andresgongora/synth-shell/fancy-bash-prompt.config"
if [ -f $user_config_file ]; then
source $user_config_file
elif [ -f $sys_config_file ]; then
source $sys_config_file
fi
## GENERATE COLOR FORMATTING SEQUENCES
## The sequences will confuse the bash prompt. To tell the terminal that they are non-printing
## characters, we must surround them by \[ and \]
local no_color="\[$(getFormatCode -e reset)\]"
local ps1_input_format="\[$(getFormatCode -c $font_color_input -b $background_input -e $texteffect_input)\]"
local ps1_input="${ps1_input_format} "
local ps1_user_git=$(printSegment " \${FBP_USER} " $font_color_user $background_user $background_host $texteffect_user)
local ps1_host_git=$(printSegment " \${FBP_HOST} " $font_color_host $background_host $background_pwd $texteffect_host)
local ps1_pwd_git=$(printSegment " \${FBP_PWD} " $font_color_pwd $background_pwd $background_git $texteffect_pwd)
local ps1_git_git=$(printSegment " \${FBP_GIT} " $font_color_git $background_git $background_input $texteffect_git)
local ps1_user=$(printSegment " \${FBP_USER} " $font_color_user $background_user $background_host $texteffect_user)
local ps1_host=$(printSegment " \${FBP_HOST} " $font_color_host $background_host $background_pwd $texteffect_host)
local ps1_pwd=$(printSegment " \${FBP_PWD} " $font_color_pwd $background_pwd $background_input $texteffect_pwd)
local ps1_git=""
## ENABLE GIT BRANCH VISIBILITY ACCORDING TO USER CONFIG
FBP_SHOW_GIT=$show_git
## Add extra new line on top of prompt
if $enable_vertical_padding; then
local vertical_padding="\n"
else
local vertical_padding=""
fi
## WINDOW TITLE
## Prevent messed up terminal-window titles
## Must be set in PS1
case $TERM in
xterm*|rxvt*)
local titlebar="\[\033]0;\${FBP_USER}@\${FBP_HOST}: \${FBP_PWD}\007\]"
;;
*)
local titlebar=""
;;
esac
## BASH PROMPT - Generate prompt and remove format from the rest
FBP_PS1="${titlebar}${vertical_padding}${ps1_user}${ps1_host}${ps1_pwd}${ps1_git}${ps1_input}"
FBP_PS1_GIT="${titlebar}${vertical_padding}${ps1_user_git}${ps1_host_git}${ps1_pwd_git}${ps1_git_git}${ps1_input}"
## For terminal line coloring, leaving the rest standard
none="$(tput sgr0)"
trap 'echo -ne "${none}"' DEBUG
## ADD HOOK TO UPDATE PS1 AFTER EACH COMMAND
## Bash provides an environment variable called PROMPT_COMMAND.
## The contents of this variable are executed as a regular Bash command
## just before Bash displays a prompt.
## We want it to call our own command to truncate PWD and store it in NEW_PWD
PROMPT_COMMAND=prompt_command_hook
}
## CALL SCRIPT
## CHECK IF COLOR SUPPORTED
## - Check if compliant with Ecma-48 (ISO/IEC-6429)
## - Call script
## - Unset script
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
fancy_bash_prompt
unset fancy_bash_prompt
fi
### EOF ###