-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzshrc
361 lines (309 loc) · 10.4 KB
/
zshrc
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
PANDEMIC_VERSION=0
function update {
if [ "$PANDEMIC_VERSION" = "" ] || ping -W1 -c1 github.com &>/dev/null; then
tmp_file=$(mktemp -t pandemic_zsh.XXXXXX)
load_pandemic $tmp_file
if check_pandemic $tmp_file; then
if update_pandemic $tmp_file; then
echo "Please restart..."
fi
fi
rm -f $tmp_file
fi
}
function silent_update {
setopt local_options no_notify no_monitor
update &
}
function load_pandemic {
tmp_file=$1
curl -i "https://api.github.com/repos/hobbypunk90/pandemic-conf/contents/zshrc" 2>/dev/null >$tmp_file
}
function check_pandemic {
tmp_file=$1
date=$(cat $tmp_file | sed -n 's/[Ll]ast-[Mm]odified: \(.*\)/\1/p')
if [ "$?" = "0" ]; then
if where gdate &>/dev/null; then
version=$(gdate --date="$date" +"%s")
else
version=$(date --date="$date" +"%s")
fi
fi
if [[ "$version" != "" && "$PANDEMIC_VERSION" = "" || $PANDEMIC_VERSION -lt $version ]]; then
echo "New evolution stage detected."
NEW_PENDAMIC_VERSION=$version
true
else
false
fi
}
function update_pandemic {
tmp_file=$1
echo "Evolving..."
content="$(cat $tmp_file | sed -n 's/"content": "\(.*\)",/\1/p' | tr -d ' ')"
echo -e "$content" | base64 -d > $tmp_file
if ! grep "^PANDEMIC_VERSION=" $tmp_file &>/dev/null; then
false
else
sed -i".bak" "1 s/PANDEMIC_VERSION=.*/PANDEMIC_VERSION=$version/" $tmp_file
rm "${tmp_file}.bak"
# follow symbolic link 😉
echo -n "Mutate... "
cp $tmp_file /tmp/zshrc.$USER
if source /tmp/zshrc.$USER; then
cat /tmp/zshrc.$USER > ~/.zshrc
echo "accepted"
else
echo "discarded"
fi
rm /tmp/zshrc.$USER
true
fi
}
function load_zshrc {
EDITOR=nano
HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=10000
bindkey -e
function install_antigen {
echo "Install Antigen"
if cat /etc/os-release | grep -P "ID(_LIKE)?=arch"; then
echo "Arch based system detected, try to install antigen from aur..."
if where pikaur &> /dev/null; then
pikaur -S antigen
elif where yay &> /dev/null; then
yay -S antigen
fi
elif where opkg &> /dev/null; then
echo "OPKG based system detected, install curl and git with opkg, then install antigen with fallback..."
opkg install ca-certificates
opkg install ca-bundle
opkg install curl
opkg install git
install_antigen_fallback
elif [ -f /etc/debian_version ]; then
echo "Debian based system detected, install dependencies, then install antigen with fallback..."
if [ "$USER" = "root" ]; then
apt install -y ca-certificates curl git
else
sudo apt install -y ca-certificates curl git
fi
install_antigen_fallback
elif where sw_vers &> /dev/null; then
echo "Mac OS X detected, install antigen with Brew..."
brew install antigen
else
echo "Do you want to install fallback antigen(download it to ~/.antigen/antigen.zsh)? [y/N]"
read yn
case $yn in
[Yy]* ) install_antigen_fallback;;
* ) echo "Abort...";;
esac
fi
}
function install_antigen_fallback {
mkdir -p ~/.antigen
curl -L git.io/antigen > ~/.antigen/antigen.zsh
}
if [ ! -f /usr/share/zsh/share/antigen.zsh ] && [ ! -f /usr/local/share/antigen/antigen.zsh ] && [ ! -f ~/.antigen/antigen.zsh ]; then
install_antigen
if [ ! -f /usr/share/zsh/share/antigen.zsh ] && [ ! -f /usr/local/share/antigen/antigen.zsh ] && [ ! -f ~/.antigen/antigen.zsh ]; then
exit 1
fi
fi
if [ -f /usr/share/zsh/share/antigen.zsh ]; then
source /usr/share/zsh/share/antigen.zsh
elif [ -f /usr/local/share/antigen/antigen.zsh ]; then
source /usr/local/share/antigen/antigen.zsh
elif [ -f ~/.antigen/antigen.zsh ]; then
source ~/.antigen/antigen.zsh
fi
antigen use oh-my-zsh
antigen bundle git
antigen bundle git-extra
if $(where tput > /dev/null); then
antigen bundle mvn
fi
antigen bundle pip
antigen bundle rails
antigen bundle systemd
antigen bundle docker
antigen bundle macunha1/zsh-terraform
antigen bundle zsh-users/zsh-completions
antigen bundle zsh-users/zsh-autosuggestions
antigen bundle zsh-users/zsh-syntax-highlighting
antigen apply
autoload -Uz compinit
compinit
autoload -Uz vcs_info
zstyle ':vcs_info:*' enable git
zstyle ':vcs_info:*' unstagedstr "%F{red} ●%f"
zstyle ':vcs_info:*' formats "(%F{blue}%s%f:%F{yellow}%b%f)"
zstyle ':vcs_info:git*' formats "(%F{cyan}%s%f:%F{yellow}%b%f%u)"
zstyle ':vcs_info:*' check-for-changes true
zstyle ':completion:*' accept-exact '*(N)'
zstyle ':completion:*' use-cache on
zstyle ':completion:*' cache-path ~/.zsh/cache
precmd() {
vcs_info &>/dev/null
}
autoload -Uz promptinit
promptinit
prompt redhat
setopt PROMPT_SUBST
setopt extended_glob
function battery {
batid=$1
cap=$(upower -i $batid | grep -Po "(?<=percentage:).*" | grep -Po "\d+" )
adapter=$(upower -i $batid | grep -Po "(?<=state:).*" | grep -Po "[A-Za-z\-]+")
color="red"
if [[ $cap -gt 15 ]] && [[ $cap -lt 50 ]]; then color="yellow"; fi
if [[ $cap -ge 50 ]]; then color="green"; fi
rprompt="%F{$color}$cap"
if [ "$adapter" = "discharging" ]; then
rprompt="$rprompt%%%f";
else
rprompt="$rprompt%f%F{yellow}↯%f";
fi
echo $rprompt
}
function temp {
t=$(cat /sys/class/thermal/thermal_zone0/temp)
full_temp="$(( t / 1000 )).$((t % 1000 / 100))'C"
temp=$( echo $t | cut -c1,2 )
color="green"
if [[ $temp -gt 60 ]] && [[ $temp -lt 80 ]]; then color="yellow"; fi
if [[ $temp -ge 80 ]]; then color="red"; fi
echo "%F{$color}$full_temp%f"
}
function extension {
if where upower &>/dev/null && [ "$(upower -e | grep -m1 "battery")" != "" ]; then
battery $(upower -e | grep -m1 "battery")
elif [ -f "/sys/class/thermal/thermal_zone0/temp" ]; then
temp
fi
}
function version_control() {
echo ${vcs_info_msg_0_}
}
function last_exit_code() {
last_exit_code=$?
if [[ $last_exit_code -ne 0 ]]; then
echo "%F{red}$last_exit_code%f "
fi
}
if where glances &>/dev/null; then
alias glances='glances'
alias top='glances'
fi
alias vi='vim'
# create a zkbd compatible hash;
# to add other keys to this hash, see: man 5 terminfo
typeset -g -A key
key[Home]="${terminfo[khome]}"
key[End]="${terminfo[kend]}"
key[Insert]="${terminfo[kich1]}"
key[Backspace]="${terminfo[kbs]}"
key[Delete]="${terminfo[kdch1]}"
key[Up]="${terminfo[kcuu1]}"
key[Down]="${terminfo[kcud1]}"
key[Left]="${terminfo[kcub1]}"
key[Right]="${terminfo[kcuf1]}"
key[PageUp]="${terminfo[kpp]}"
key[PageDown]="${terminfo[knp]}"
key[Shift-Tab]="${terminfo[kcbt]}"
# setup key accordingly
[[ -n "${key[Home]}" ]] && bindkey -- "${key[Home]}" beginning-of-line
[[ -n "${key[End]}" ]] && bindkey -- "${key[End]}" end-of-line
[[ -n "${key[Insert]}" ]] && bindkey -- "${key[Insert]}" overwrite-mode
[[ -n "${key[Backspace]}" ]] && bindkey -- "${key[Backspace]}" backward-delete-char
[[ -n "${key[Delete]}" ]] && bindkey -- "${key[Delete]}" delete-char
[[ -n "${key[Up]}" ]] && bindkey -- "${key[Up]}" up-line-or-beginning-search
[[ -n "${key[Down]}" ]] && bindkey -- "${key[Down]}" down-line-or-history
[[ -n "${key[Left]}" ]] && bindkey -- "${key[Left]}" backward-char
[[ -n "${key[Right]}" ]] && bindkey -- "${key[Right]}" forward-char
[[ -n "${key[PageUp]}" ]] && bindkey -- "${key[PageUp]}" beginning-of-buffer-or-history
[[ -n "${key[PageDown]}" ]] && bindkey -- "${key[PageDown]}" end-of-buffer-or-history
[[ -n "${key[Shift-Tab]}" ]] && bindkey -- "${key[Shift-Tab]}" reverse-menu-complete
# Finally, make sure the terminal is in application mode, when zle is
# active. Only then are the values from $terminfo valid.
if (( ${+terminfo[smkx]} && ${+terminfo[rmkx]} )); then
autoload -Uz add-zle-hook-widget
function zle_application_mode_start { echoti smkx }
function zle_application_mode_stop { echoti rmkx }
add-zle-hook-widget -Uz zle-line-init zle_application_mode_start
add-zle-hook-widget -Uz zle-line-finish zle_application_mode_stop
fi
PROMPT="[%F{red}%n%f@%F{blue}%m%f:%F{green}%1~%f]%# "
RPROMPT='$(last_exit_code)$(version_control)$(extension)'
alias myip="curl https://www.monip.org -s | grep -Po --color=never \"(?<=IP : )[\d\.]+\""
if [ "$XDG_SESSION_DESKTOP" = "gnome" ]; then
alias afk="dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock"
elif [ "$XDG_SESSION_DESKTOP" = "KDE" ]; then
alias afk="loginctl lock-session"
fi
function rsync {
if [ -f ./.rsyncignore ]; then
/usr/bin/rsync --progress --partial -hr --exclude-from=./.rsyncignore $@
else
/usr/bin/rsync --progress --partial -hr $@
fi
}
if [ -d $HOME/.local/bin ]; then
PATH=$HOME/.local/bin:$PATH
fi
alias dd="dd status=progress"
if where terraform &>/dev/null; then
alias tfsearch='tfp -out=/tmp/tfplan && tfs -json /tmp/tfplan | jq'
fi
# Load {rb|py|nod|j}env automatically if existing
if where rbenv &>/dev/null; then
eval "$(rbenv init -)"
fi
if where pyenv &>/dev/null; then
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init - 2> /dev/null)"
fi
if where nodenv &>/dev/null; then
eval "$(nodenv init -)"
fi
if where jenv &>/dev/null; then
eval "$(jenv init -)"
fi
if where kubectl &>/dev/null && [ -d "${HOME}/.kube" ]; then
export KUBECONFIG="${HOME}/.kube/config"
if ls ${HOME}/.kube/*.kube &>/dev/null; then
for file in ${HOME}/.kube/*.kube; do
export KUBECONFIG="${file}:${KUBECONFIG}"
done
fi
if kubectl krew &>/dev/null; then
export PATH="${PATH}:${HOME}/.krew/bin"
eval "$(kubectl krew completion zsh)"
fi
if kubectl hns &>/dev/null; then
eval "$(kubectl hns completion zsh)"
fi
fi
if where codium &>/dev/null; then
alias code="codium"
fi
if where gpaste-client &>/dev/null; then
alias copy="gpaste-client"
alias paste="gpaste-client --use-index get 0"
fi
}
silent_update
load_zshrc
# load personal extra stuff
# make custom changes only to the .zshrcx file.
if [ -f ~/.zshrcx ]; then
source ~/.zshrcx
fi
if [ -f ~/.fzf.zsh ]; then
source ~/.fzf.zsh
fi