Repository navigation
Expand file tree
/
Copy path.zshrc
More file actions
289 lines (261 loc) · 8.19 KB
/
Copy path.zshrc
File metadata and controls
289 lines (261 loc) · 8.19 KB
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
_bootmsg "Setting PROMPT"
# 20200301:
# - /etc/zshrc にPROMPT変数が定義されている。
# - ${ZDOTDIR}/.zshenv のあとに /etc/zshrcを読み込む
# - よって、.zshenv だと上書きされてしまう事象(CentOS 7)
#PROMPT="%K{black}%F{3}${HOST} %F{cyan}<"$IP_ADDRESSES"> "'${vcs_info_msg_0_}'"%F{reset}%K{reset}
#%K{0} %F{7} [%~] %#%K{reset}%F{reset} "
#
#
PREPWD=''
term_time=$(date +%s)
notifySec=4 #30 # 実行時間N秒以上の場合,処理する.
#function before_runcmd(){
# echo "BT: "
# term_time=$(date +%s)
#}
#trap 'before_runcmd' DEBUG
function precmd () {
if [[ $? -eq 0 ]]
then
STATUS_COLOR='%F{46}' # Green is true.
else
STATUS_COLOR='%F{196}' # Red is false.
fi
new_term_time=$(date +%s)
last_cmds=$(fc -l -1 | head -n1 | cut -c8-)
last_cmd=$(echo $last_cmds | cut -d' ' -f1)
# 多く処理した場合は,通知する
if [[ $(( $new_term_time - $term_time )) -gt $notifySec ]] && [[ -f ~/.slackrc ]]
then
#source ~/.slackrc
#POST_TEXT="$last_cmd is end."
#post_slack
: "current disabled."
fi
term_time=$new_term_time
if [[ "$last_cmd" == 'git' ]] ; then vcs_info; fi
if [[ "$last_cmd" =~ 'vi.*' ]] ; then vcs_info; fi
if [[ "$last_cmd" =~ '.*\>.*' ]] ; then vcs_info; fi
if [[ "$last_cmd" == 'cd' ]] ; then vcs_info; fi
PREPWD=$(pwd | perl -pe 's!^(.{10,}?/)(.+)(/.{15,})$!$1...$3!')
if [[ "$TASK" != "" ]] && [[ "${vcs_info_msg_0_}" != "" ]];then
PROMPT="%F{013}${vcs_info_msg_0_}%F{154}* $TASK
${STATUS_COLOR}${ZSH_WORKSPACE}%F{207} %* > %F{reset}"
elif [[ "$TASK" != "" ]];then
PROMPT="%F{154}* $TASK
${STATUS_COLOR}${ZSH_WORKSPACE}%F{207} %* > %F{reset}"
elif [[ "$vcs_info_msg_0_" != "" ]];then
PROMPT="%F{013}${vcs_info_msg_0_}
${STATUS_COLOR}${ZSH_WORKSPACE}%F{207} %* > %F{reset}"
else
PROMPT="${STATUS_COLOR}${ZSH_WORKSPACE}%F{013}"'${vcs_info_msg_0_}'"${task_prompt}%F{207} %* > %F{reset}"
fi
}
vcs_info
PREPWD=$(pwd | perl -pe 's!^(.{10,}?/)(.+)(/.{15,})$!$1...$3!')
PROMPT="${STATUS_COLOR}${ZSH_WORKSPACE}%F{013}"'${vcs_info_msg_0_}'"${task_prompt}%F{207}> %F{reset}"
# RPROMPT show last
if test "$(whoami)" == "root"
then
cat <<ATTENT
====================
HELLO ADMIN PROMPT!
====================
ATTENT
fi
function zsh_update() {
cd $ZDOTDIR
#echo "Latest: $(git log | head -n6 | grep 'Date' | sed 's/Date: //')"
OLD_ZSH_CONF_VERSION=$(git log -n 1 --pretty=format:"%H")
(git pull > /dev/null 2>&1) && \
ZSH_CONF_VERSION=$(git log -n 1 --pretty=format:"%H")
if [[ "$ZSH_CONF_VERSION" != "" ]]
then
if [ $OLD_ZSH_CONF_VERSION != $ZSH_CONF_VERSION ]
then
echo "[ZSH_UPDATED]"
echo
fi
fi
}
_bootmsg "Checking Update"
zsh_update &! # bash の & disown 相当
_bootmsg "Define Functions [chpwd]"
chpwd() {
l3=$(( COLUMNS / 4 ))
l4=$(( l3 * 2 ))
echo "< $(pwd | perl -pe 's;^(.{'"$l3"',}?/)(.+)(/.{'"$l4"',})$;$1...$3;')"
}
_bootmsg "Define Functions [memo]"
function @(){
if [ $# -eq 0 ]
then
# 空で指定したらリセット
unset MEMO
return
else
for text in $@
do
#頭に-がついてたら削除。
if test $(echo "$text" | grep '^-' )
then
local keywd=`echo $text | sed -e 's/^-//'`
declare -g MEMO=`echo $MEMO | sed "s/\s$keywd/ /" | sed "s/${keywd}\s//"`
echo "memo buffer removed: $keywd"
else
local exist_flag=0
for i in $(echo $MEMO | xargs)
do
test "$i" = "$text" && exist_flag=1 && break
done
if test $exist_flag -eq 0
then
declare -g MEMO="${MEMO} ${text}"
echo "memo buffer added: ${text}"
else
echo "memo buffer exist: ${text}"
fi
fi
done
fi
}
_bootmsg "Define Functions [zshaddhistory]"
zshaddhistory() {
local line=${1%%$'\n'}
local cmd=${line%% *}
test $(echo ${line} |grep -o '\n' |wc -l) -lt 10 || echo '*no Insert History'
}
_bootmsg "Define Functions [memo_write]"
function memo_write(){
if test "$MEMO" = ""
then
# 変数が空ならば、リセットする。
echo '' > ${ZDOTDIR}/MEMO.txt
else
for i in $(echo $MEMO | xargs)
do
local exist_flag=0
# 存在チェック
for x in $(/bin/cat ${ZDOTDIR}/MEMO.txt | xargs)
do
if test "$i" = "$x"
then
exist_flag=1
fi
done
if test $exist_flag -eq 0
then #存在しないならば
echo "memo: add - $i"
echo $i >> ${ZDOTDIR}/MEMO.txt
else
echo "memo: exist - $i"
fi
done
# ファイルにあるが、変数に存在しない文字列を削除
local MEMO_LIST=`echo $MEMO | tr ' ' '\n'`
for i in $(/bin/cat ${ZDOTDIR}/MEMO.txt | xargs)
do
# ファイルのMEMO_LISTをGrep(完全一致)して、完全一致しなければ(Revirse) Grepで行削除
if test ! "$( echo $MEMO_LIST | grep -x $i)"
then
grep -v "^${i}" ${ZDOTDIR}/MEMO.txt | tee ${ZDOTDIR}/MEMO.txt > /dev/null
test "$?" -eq 0 && echo "memo: removed - $i"
fi
done
fi
}
alias @write='memo_write'
_bootmsg "Define Functions [title]"
function title() { echo -e "\033]0;${1:?please specify a title}\007" ; }
_bootmsg "Define Functions [@reload]"
function @reload(){
export MEMO=$(/bin/cat ${ZDOTDIR}/MEMO.txt | xargs)
}
_bootmsg "Define Functions [wttr]"
function wttr(){
if [[ $1 = "" ]]
then
_wttr=$WTTR_LOCATION
else
_wttr=$1
fi
curl https://ja.wttr.in/${_wttr}?format="%l:+%c\n------\nReal:+%t\nFeel:+%f\nWind:+%w\nSunRise:+%S\nSun-Set:+%s\n"
}
function memclear(){
R=$(awk 'FNR==5 {print $2/1024}' /proc/meminfo)
echo "clear memory $R MiB to ..."
echo 3 | sudo tee /proc/sys/vm/drop_caches
sleep 1
awk -v r=$R 'FNR==5 {print "cleared: " r-($2/1024) " MiB"}' /proc/meminfo
}
##############################
## EXIT
_bootmsg "Define exit function"
_exit_function(){
memo_write
########################################
# check close process
_wait_proc(){
while :
do
if ps "$1" 2>&1 >/dev/null ;then
continue
fi
break
done
}
# Auto Close Terminal
if [ -v ZTERM_A3C_PID ];then
kill -15 "$ZTERM_A3C_PID"
_wait_proc "$ZTERM_A3C_PID"
unset ZTERM_A3C_PID
fi
# PPIDが`init`でRunningしているWSLの場合は、Exit 0で切らせる。
test "$(ps -o comm "$PPID" | tail -n1)" = "init" && return 0
}
trap "_exit_function" EXIT INT
_bootmsg "Loading Extensions"
for i in $(/bin/ls -1 "${ZDOTDIR}/custom-enable.d")
do
_bootmsg "Loading Extensions [${i}]"
source "${ZDOTDIR}/custom-enable.d/${i}"
done
_bootmsg "Loading Custom Extensions"
for i in $(/bin/ls -1 "${ZDOTDIR}/custom.d")
do
_bootmsg "Loading Custom Extensions [${i}]"
source "${ZDOTDIR}/custom.d/${i}"
done
_bootmsg "Loading cache MEMO"
declare -g MEMO=$(/bin/cat ${ZDOTDIR}/MEMO.txt | xargs)
# Trap exit to run .zlogout
#source $ZDOTDIR/logout.sh
alias exit="_exit_function; exit" # run _exit_function before exit
TRAPEXIT() {
memo_write
source "${HOME}/.zsh/.zlogout"
}
# fzf config
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
rm -f $ZLOCKFILE
clear
if [[ -v DIRECT_SSH_NAME ]];then
ssh "$DIRECT_SSH_NAME"
fi
for i in $(/bin/ls -1 "${ZDOTDIR}/display.d")
do
f="${ZDOTDIR}/display.d/${i}"
[[ -f "$f" ]] && source "$f"
done
# refresh 1sec.
TRAPALRM(){
zle reset-prompt
}
TMOUT=1
MODE_INDICATOR_VICMD='%F{9}<%F{1}<<%f'
MODE_INDICATOR_SEARCH='%F{13}<%F{5}<<%f'
RPROMPT="${MODE_INDICATOR_PROMPT}%F{190}"'${MEMO}'"%F{reset}"
if [[ -v DIRECT_SSH_NAME ]] && [[ -v WSL_DISTRO_NAME ]];then
ssh "$DIRECT_SSH_NAME"
fi