-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.bashrc
318 lines (269 loc) · 10.4 KB
/
.bashrc
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
##############################
# .bashrc by Jason Rodriguez #############################
# #
# THIS FILE AUTOMATICALLY UPDATES ITSELF! #
# EITHER DISABLE THE UPDATE PROCESS ON THE LAST LINE #
# OR PLACE ANY CUSTOM CONFIGURATION IN .bash-config #
# #
# Prompt: #
# [hostname] (pwd) >>> #
# ||| #
# ||red if user is root #
# |red if login session is root #
# always the host color #
# #
# Functions: #
# showhost() - show the [hostname] portion #
# hidehost() - hide the [hostname] portion #
# sethost(string name) - set the value inside [hostname] #
# #
# showline() - print a separator line above the prompt #
# hideline() - do not print separator line #
# sethc(int color) - sets the color of the '>' symbols #
# aurget(string name) - fetches a package from the AUR #
# #
# External Configuration File: #
# .bash-config - put custom functions, etc. here #
# #
# Relatively Safe File Updater: #
# updatefile(string URL, string localFileName) #
# #
##########################################################
UPDATEURL="https://raw.github.com/greyhound-forty/dotfiles/master/"
UPDATEURL2="https://raw.github.com/greyhound-forty/dotfiles/master/"
updatefile ()
{
URL=$1
LOCALFILENAME=$2
TEMPFILE=$(mktemp /tmp/tmp.XXXXXX)
wget --no-check-certificate -qO${TEMPFILE} ${URL} 2>/dev/null && mv ${TEMPFILE} ${LOCALFILENAME}
}
# pretty print functions
notice() { echo -e "\e[0;34m:: \e[1;37m${*}\e[0m"; }
# placeholder functions (keeps bash from complaining loudly)
set_prompt () { :; }
custom_hook() { :; }
# === BASIC SETUP ===
# Check for an interactive session
[ -z "$PS1" ] && return
#PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\007"'
#export PROMPT_COMMAND
# Enable huge history
export HISTFILESIZE=9999999999
export HISTSIZE=9999999999
# Ignore "ls" commands
export HISTIGNORE="ls"
# Save timestamp info for every command
export HISTTIMEFORMAT="[%Y-%m-%d - %H:%M:%S] "
# Dump the history file after every command
shopt -s histappend
#export PROMPT_COMMAND="history -a; ${PROMPT_COMMAND}"
# set up autocomplete for some commands
complete -cf sudo
# set a basic prompt, just in case.
PS1='[\u@\h \W]\$ '
# adding local user's bin directory to the path
PATH=~/bin:$PATH
# I like nano, comment these for system defaults
export EDITOR="nano -w"
export VISUAL="nano -w"
# make the window resize properly
shopt -s checkwinsize
# set up custom configuration stuffs
CONFIGFILE="${HOME}/.bash-config"
if [ -f ${CONFIGFILE} ]; then
. ${CONFIGFILE}
else # config file doesn't exist, so create it
touch ${CONFIGFILE}
SYSTEM=$(uname -s)
if [ "$SYSTEM" == "Linux" ]; then
echo "# Linux system defaults" >> ${CONFIGFILE}
echo "alias ls='ls --color=auto'" >> ${CONFIGFILE}
elif [ "$SYSTEM" == "FreeBSD" ]; then
echo "# FreeBSD system defaults" >> ${CONFIGFILE}
echo "alias ls='ls -G'" >> ${CONFIGFILE}
echo "alias md5sum='md5'" >> ${CONFIGFILE}
fi
if ! which nano >/dev/null 2>&1; then
if which pico >/dev/null 2>&1; then
echo "alias nano='pico'" >> ${CONFIGFILE}
fi
fi
echo >> ${CONFIGFILE}
echo "# This function is run at the end of the bash startup" >> ${CONFIGFILE}
echo "unset custom_hook" >> ${CONFIGFILE}
echo "custom_hook () { : # this colon is necessary if there's no content
}
UPDATE_BASHRC=1 # Controls the automatic update process
UPDATE_DIRCOLORS=1 # Determines whether to update .dir_colors from github
#LINE_ENABLED=1 # Uncomment this line to enable the horizontal line above the prompt
" >> ${CONFIGFILE}
# now that we've created this, source it!
. ${CONFIGFILE}
fi
# === FUNCTIONS, ETC ===
GITBRANCH=""
get_git_branch () {
GITBRANCH=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1) /')
}
# this gets its own function for ease of use later
update_titlebar () {
echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\007"
}
# line separator is disabled by default
export PROMPT_COMMAND="history -a; update_titlebar; get_git_branch;"
# highlight text in a pipeline, ex: cat file | highlight "searchstring"
highlight() {
perl -pe "s/$1/\e[1;31;43m$&\e[0m/g"
}
netsort () { netstat -nt | awk '{ print $5 }' | egrep -o "([0-9]*\.){3}[0-9]*" | sort | uniq -c | sort -n; }
# shortcut for downloading packages from the AUR, ex: aurget packagename
aurget () { export LASTAURPKG=${1}; wget https://aur.archlinux.org/packages/${1:0:2}/${1}/${1}.tar.gz; }
aurunpack () { tar -xvzf ${LASTAURPKG}.tar.gz; }
# If we're on a dumb console, stop here, we don't want color and we don't want to update.
if [[ "$TERM" == "linux" ]]; then
unset PROMPT_COMMAND
PS1='[\u@\h \W]\$ '
return
fi
# basic colors for bash prompt!
loadcolors () {
txtblk='\e[0;30m' # Black - Regular
txtred='\e[0;31m' # Red
txtgrn='\e[0;32m' # Green
txtylw='\e[0;33m' # Yellow
txtblu='\e[0;34m' # Blue
txtpur='\e[0;35m' # Purple
txtcyn='\e[0;36m' # Cyan
txtwht='\e[0;37m' # White
bldblk='\e[1;30m' # Black - Bold
bldred='\e[1;31m' # Red
bldgrn='\e[1;32m' # Green
bldylw='\e[1;33m' # Yellow
bldblu='\e[1;34m' # Blue
bldpur='\e[1;35m' # Purple
bldcyn='\e[1;36m' # Cyan
bldwht='\e[1;37m' # White
unkblk='\e[4;30m' # Black - Underline
undred='\e[4;31m' # Red
undgrn='\e[4;32m' # Green
undylw='\e[4;33m' # Yellow
undblu='\e[4;34m' # Blue
undpur='\e[4;35m' # Purple
undcyn='\e[4;36m' # Cyan
undwht='\e[4;37m' # White
bakblk='\e[40m' # Black - Background
bakred='\e[41m' # Red
bakgrn='\e[42m' # Green
bakylw='\e[43m' # Yellow
bakblu='\e[44m' # Blue
bakpur='\e[45m' # Purple
bakcyn='\e[46m' # Cyan
bakwht='\e[47m' # White
txtrst='\e[0m' # Text Reset
}
# make the colors accessible to this script
loadcolors
# default host color (the >>> section in the prompt)
PROMPT_HOSTNAME_COLOR=${txtwht}
# be sure to have some good defaults, just in case
PROMPT_HOSTNAME="\h"
PROMPT_CWD="\w"
PROMT_HOSTNAME_BOX="\[${txtwht}\][\[${txtrst}\]${PROMPT_HOSTNAME_COLOR}\[${txtwht}\]] "
# prompt commands, to change how the current working directory is displayed
long () { PROMPT_CWD="\w"; set_prompt; }
short () { PROMPT_CWD="\W"; set_prompt; }
# show or hide the hostname of the server, or change the displayed name, in that order
showhost () { PROMPT_HOSTNAME_BOX="\[${PROMPT_HOSTNAME_COLOR}\][\[${txtrst}\]${PROMPT_HOSTNAME}\[${PROMPT_HOSTNAME_COLOR}\]] "; set_prompt; }
hidehost () { PROMPT_HOSTNAME_BOX=""; set_prompt; }
sethost () {
if [ -z "${1}" ]; then
PROMPT_HOSTNAME="\h"
showhost
else
PROMPT_HOSTNAME="${1}"
showhost
fi
set_prompt
}
# draw a line separating each command's output
draw_line () {
# like to use a different date format? Edit DATEFORMAT.
# The line length will adjust automagically.
DATEFORMAT="+%r"
DATESTRING=$(date ${DATEFORMAT})
DATELENGTH=${#DATESTRING}
((WIDTH=COLUMNS-DATELENGTH-3))
echo -ne "${bldblk}"
for (( c=1; c<=$WIDTH; c++ )); do echo -n "-"; done
echo -n "|"
echo -ne " ${DATESTRING}${txtrst}\n"
}
hideline() { export PROMPT_COMMAND="history -a; update_titlebar;"; }
showline() { export PROMPT_COMMAND="history -a; update_titlebar; draw_line;"; }
# function to set host color, accepts 256-color syntax
# ex: sethc 140
sethc ()
{
loadcolors
PROMPT_HOSTNAME_COLOR="\e[38;5;${1}m"
PROMPT_HOSTNAME_BOX="\[${PROMPT_HOSTNAME_COLOR}\][\[${txtrst}\]${PROMPT_HOSTNAME}\[${PROMPT_HOSTNAME_COLOR}\]] "
}
# Try to dynamically set the hostname color based on the hostname's md5sum
if which bc >/dev/null 2>&1;
then
sethc $(echo ${HOSTNAME} | md5sum | head -c 2 | tr a-z A-Z | xargs echo ibase=16\; | bc)
else
sethc $(printf "%d\n" 0x$(echo ${HOSTNAME} | md5sum | head -c 2))
fi
# Function set the prompt.
set_prompt ()
{
if [ `id -u` == "0" ]; then
if [ -n "${SUDO_USER}" ]; then
PS1="${PROMPT_HOSTNAME_BOX}\[${txtwht}\](\[${txtrst}\]${PROMPT_CWD}\[${txtwht}\]) \[${PROMPT_HOSTNAME_COLOR}\]>>\[${bldred}\]>\[${txtrst}\] "
else
PS1="${PROMPT_HOSTNAME_BOX}\[${txtwht}\](\[${txtrst}\]${PROMPT_CWD}\[${txtwht}\]) \[${PROMPT_HOSTNAME_COLOR}\]>\[${bldred}\]>>\[${txtrst}\] "
fi
else
PS1="${PROMPT_HOSTNAME_BOX}\[${txtwht}\](\[${txtrst}\]${PROMPT_CWD}\[${txtwht}\]) \${GITBRANCH}\[${PROMPT_HOSTNAME_COLOR}\]>>>\[${txtrst}\] "
fi
}
# Actually set it here
set_prompt
# This version of the sethc function will edit the prompt color on the fly.
# It actually replaces the previous version within this script which isn't dynamic
unset sethc
sethc ()
{
loadcolors
PROMPT_HOSTNAME_COLOR="\e[38;5;${1}m"
PROMPT_HOSTNAME_BOX="\[${PROMPT_HOSTNAME_COLOR}\][\[${txtrst}\]${PROMPT_HOSTNAME}\[${PROMPT_HOSTNAME_COLOR}\]] "
set_prompt
}
# handy function to show ALL THE COLORS
showcolors () {
echo "=== Basic Colors ==="
loadcolors
for X in {0..255}; do
[ $X == 16 ] && echo && echo "=== Extended Colors ==="
echo -e "${X}: \e[38;5;${X}m>>>${txtrst}"
done
}
# apply any custom configuration stuff
if [[ ${LINE_ENABLED} == "1" ]]; then
showline
fi
# load dir_colors if it exists
[ -f ~/.dir_colors ] && eval $(dircolors -b ~/.dir_colors)
# run the custom configuration hook
custom_hook
# This updates the .bashrc from github
updatebashcfg ()
{
[ "$UPDATE_BASHCFG" == 1 ] && updatefile ${UPDATEURL2}.bash-config ${HOME}/.bash-config
[ "$UPDATE_BASHRC" == 1 ] && updatefile ${UPDATEURL}.bashrc ${HOME}/.bashrc
[ "$UPDATE_DIRCOLORS" == 1 ] && updatefile ${UPDATEURL}.dir_colors ${HOME}/.dir_colors
}
# Make it so, but in the background
#updatebashcfg & disown >/dev/null 2>&1