forked from bahamas10/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbashrc
278 lines (234 loc) · 6.13 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
#!/usr/bin/env bash
#
# Best bashrc in history
#
# Author: Dave Eddy <[email protected]>
# Date: Sometime in 2011
# License: MIT
# Editor: Jean Sandrin
# If not running interactively, don't do anything
[[ -n $PS1 ]] || return
# Load bics, plugins found in bics-plugins
. ~/.bics/bics || echo '> failed to load bics' >&2
# Set environment
export EDITOR='vim'
export GREP_COLOR='1;36'
export HISTCONTROL='ignoredups'
export HISTSIZE=5000
export HISTFILESIZE=5000
export LSCOLORS='ExGxbEaECxxEhEhBaDaCaD'
export PAGER='less'
export TZ='Europe/Paris'
export VISUAL='vim'
# Support colors in less
export LESS_TERMCAP_mb=$(tput bold; tput setaf 1)
export LESS_TERMCAP_md=$(tput bold; tput setaf 1)
export LESS_TERMCAP_me=$(tput sgr0)
export LESS_TERMCAP_se=$(tput sgr0)
export LESS_TERMCAP_so=$(tput bold; tput setaf 3; tput setab 4)
export LESS_TERMCAP_ue=$(tput sgr0)
export LESS_TERMCAP_us=$(tput smul; tput bold; tput setaf 2)
export LESS_TERMCAP_mr=$(tput rev)
export LESS_TERMCAP_mh=$(tput dim)
export LESS_TERMCAP_ZN=$(tput ssubm)
export LESS_TERMCAP_ZV=$(tput rsubm)
export LESS_TERMCAP_ZO=$(tput ssupm)
export LESS_TERMCAP_ZW=$(tput rsupm)
# Shell Options
shopt -s cdspell
shopt -s checkwinsize
shopt -s extglob
# Bash Version >= 4
shopt -s autocd 2>/dev/null || true
shopt -s dirspell 2>/dev/null || true
# Enable color support of ls
if ls --color=auto &>/dev/null; then
alias ls='ls -p --color=auto'
else
alias ls='ls -p -G'
fi
# Prompt
# Store `tput` colors for future use to reduce fork+exec
# the array will be 0-255 for colors, 256 will be sgr0
# and 257 will be bold
COLOR256=()
COLOR256[0]=$(tput setaf 1)
COLOR256[256]=$(tput sgr0)
COLOR256[257]=$(tput bold)
# Colors for use in PS1 that may or may not change when
# set_prompt_colors is run
PROMPT_COLORS=()
# Change the prompt colors to a theme, themes are 0-29
set_prompt_colors() {
local h=${1:-0}
local color=
local i=0
local j=0
for i in {22..231}; do
((i % 30 == h)) || continue
color=${COLOR256[$i]}
# cache the tput colors
if [[ -z $color ]]; then
COLOR256[$i]=$(tput setaf "$i")
color=${COLOR256[$i]}
fi
PROMPT_COLORS[$j]=$color
((j++))
done
}
# Construct the prompt
# [(exit code)] <user> - <hostname> <uname> <cwd> [git branch] <$|#>
# exit code of last process
#PS1='$(ret=$?;(($ret!=0)) && echo "\[${COLOR256[0]}\]($ret) \[${COLOR256[256]}\]")'
# time
PS1='\[${PROMPT_COLORS[3]}\]$(date +"%H:%M:%S") '
# username (red for root)
PS1+='\[${PROMPT_COLORS[0]}\]\[${COLOR256[257]}\]$(((UID==0)) && echo "\[${COLOR256[0]}\]")\u\[${COLOR256[256]}\] '
# zonename (global zone warning)
PS1+='\[${COLOR256[0]}\]\[${COLOR256[257]}\]'"$(zonename 2>/dev/null | grep -q '^global$' && echo 'GZ:')"'\[${COLOR256[256]}\]'
# hostname
#PS1+='\[${PROMPT_COLORS[3]}\]\h '
# uname
#PS1+='\[${PROMPT_COLORS[2]}\]'"$(uname | tr '[:upper:]' '[:lower:]')"' '
# cwd
PS1+='\[${PROMPT_COLORS[5]}\]\w '
# optional git branch
PS1+='$(branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null); [[ -n $branch ]] && echo "\[${PROMPT_COLORS[2]}\](\[${PROMPT_COLORS[3]}\]git:$branch\[${PROMPT_COLORS[2]}\]) ")'
# prompt character
PS1+='\[${PROMPT_COLORS[0]}\]\$\[${COLOR256[256]}\] '
# set the theme
set_prompt_colors 24
# Prompt command
_prompt_command() {
local user=$USER
local host=${HOSTNAME%%.*}
local pwd=${PWD/#$HOME/\~}
local ssh=
[[ -n $SSH_CLIENT ]] && ssh='[ssh] '
printf "\033]0;%s%s@%s:%s\007" "$ssh" "$user" "$host" "$pwd"
}
PROMPT_COMMAND=_prompt_command
PROMPT_DIRTRIM=6
# print a colorized diff
colordiff() {
local red=$(tput setaf 1 2>/dev/null)
local green=$(tput setaf 2 2>/dev/null)
local cyan=$(tput setaf 6 2>/dev/null)
local reset=$(tput sgr0 2>/dev/null)
diff -u "$@" | awk "
/^\-/ {
printf(\"%s\", \"$red\");
}
/^\+/ {
printf(\"%s\", \"$green\");
}
/^@/ {
printf(\"%s\", \"$cyan\");
}
{
print \$0 \"$reset\";
}"
return "${PIPESTATUS[0]}"
}
# Print all 256 colors
colors() {
local i
for i in {0..255}; do
printf "\x1b[38;5;${i}mcolor %d\n" "$i"
done
tput sgr0
}
# Copy stdin to the clipboard
copy() {
pbcopy 2>/dev/null ||
xsel 2>/dev/null ||
clip.exe
}
# Convert epoch to human readable (print current date if no args)
epoch() {
local num=${1:--1}
printf '%(%B %d, %Y %-I:%M:%S %p %Z)T\n' "$num"
}
# Platform-independent interfaces
interfaces() {
node <<-EOF
var os = require('os');
var i = os.networkInterfaces();
Object.keys(i).forEach(function(name) {
i[name].forEach(function(int) {
if (int.family === 'IPv4') {
console.log('%s: %s', name, int.address);
}
});
});
EOF
}
# Calculate CPU load / Core Count
load() {
node -p <<-EOF
var os = require('os');
var c = os.cpus().length;
os.loadavg().map(function(l) {
return (l/c).toFixed(2);
}).join(' ');
EOF
}
# Platform-independent memory usage
meminfo() {
node <<-EOF
var os = require('os');
var free = os.freemem();
var total = os.totalmem();
var used = total - free;
console.log('memory: %dmb / %dmb (%d%%)',
Math.round(used / 1024 / 1024),
Math.round(total / 1024 / 1024),
Math.round(used * 100 / total));
EOF
}
# print lines over X columns (defaults to 80)
over() {
awk -v c="${1:-80}" 'length($0) > c {
printf("%4d %s\n", NR, $0);
}'
}
# print a rainbow if truecolor is available to the terminal
rainbow() {
local i r g b
for ((i = 0; i < 77; i++)); do
r=$((255 - (i * 255 / 76)))
g=$((i * 510 / 76))
b=$((i * 255 / 76))
((g > 255)) && g=$((510 - g))
printf '\033[48;2;%d;%d;%dm ' "$r" "$g" "$b"
done
tput sgr0
echo
}
# Follow redirects to untiny a tiny url
untiny() {
local location=$1
local last_location=''
while [[ -n $location ]]; do
[[ -n $last_location ]] && echo " -> $last_location"
last_location=$location
read -r _ location < \
<(curl -sI "$location" | grep 'Location: ' | tr -d '[:cntrl:]')
done
echo "$last_location"
}
# Load external files
. ~/.bash_aliases 2>/dev/null || true
. ~/.bashrc.local 2>/dev/null || true
# load completion
. /etc/bash/bash_completion 2>/dev/null ||
. ~/.bash_completion 2>/dev/null
# no GUI errors
export $(dbus-launch)
export NO_AT_BRIDGE=1
# SVM
export ecuip="192.168.1.21"
# Rust ?!
. "$HOME/.cargo/env"
export jdo='/mnt/c/Users/jsandrin/Downloads'
true