-
Notifications
You must be signed in to change notification settings - Fork 1
/
.functions
388 lines (303 loc) · 15.2 KB
/
.functions
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/env bash
# ================================================================================================================================
#
# FUNCTION
#
# Update: 2023.01.11
#
# ================================================================================================================================
# ================================================================================================================================
# COMPRESSED PACKAGES
# ================================================================================================================================
# --------------------------------------------------------------------------------------------------------------------------------
# 进入目录
# --------------------------------------------------------------------------------------------------------------------------------
cdls() { builtin cd "$@" && ll ; }
# --------------------------------------------------------------------------------------------------------------------------------
# 解压缩
# --------------------------------------------------------------------------------------------------------------------------------
function ce() {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar -xvjf $1 ;;
*.tar.gz) tar -xvzf $1 ;;
*.tar.xz) tar -xvJf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar -xvf $1 ;;
*.tbz2) tar -xvjf $1 ;;
*.tgz) tar -xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "Don't Know How To Extract '$1'..." ;;
esac
else
echo "'$1' Is Not A Valid File !"
fi
}
# --------------------------------------------------------------------------------------------------------------------------------
# 压缩
# --------------------------------------------------------------------------------------------------------------------------------
function cc() {
if [ -n "$1" ] ; then
FILE=$1
case $FILE in
*.tar) shift && tar -cf $FILE $* ;;
*.tar.bz2) shift && tar -cjf $FILE $* ;;
*.tar.xz) shift && tar -cJf $FILE $* ;;
*.tar.gz) shift && tar -czf $FILE $* ;;
*.tgz) shift && tar -czf $FILE $* ;;
*.zip) shift && zip $FILE $* ;;
*.rar) shift && rar $FILE $* ;;
esac
else
echo "USAGE: sc <Foo.tar.gz> ./foo ./bar"
fi
}
# ================================================================================================================================
# GIT = GITHUB
# ================================================================================================================================
# --------------------------------------------------------------------------------------------------------------------------------
# Git Add Submit And Upload ( Git Push )
# --------------------------------------------------------------------------------------------------------------------------------
function gitpush() {
git add --all
git commit -a -m "$(date "+ %Y-%m-%d | %H:%M:%S")"
git push
}
# --------------------------------------------------------------------------------------------------------------------------------
# Git Commit Browser
# --------------------------------------------------------------------------------------------------------------------------------
function gitshow() {
local commit_hash="echo {} | grep -o '[a-f0-9]\{7\}' | head -1"
local view_commit="$commit_hash | xargs -I % sh -c 'git show --color=always % | diff-so-fancy'"
git log --color=always \
--format="%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ar) %C(bold blue)<%an>%Creset" "$@" | \
fzf --no-sort --tiebreak=index --no-multi --reverse --ansi \
--header="enter to view, alt-y to copy hash" --preview="$view_commit" \
--bind="enter:execute:$view_commit | less -R" \
--bind="alt-y:execute:$commit_hash | xclip -selection clipboard"
}
# --------------------------------------------------------------------------------------------------------------------------------
# Remove All Commit in Git ( Multi In One )
# --------------------------------------------------------------------------------------------------------------------------------
function git-remove-all-commit() {
local branch
branch=$(git symbolic-ref --short HEAD)
echo -e "\nDo you want to remove all your commit in \033[1m$branch\033[0m branch? [Y/n] "
read -r response
case "$response" in
[yY][eE][sS]|[yY])
git checkout --orphan latest_branch
git add -A
git commit -am "Initial commit"
git branch -D "$branch"
git branch -m "$branch"
echo -e "\nTo force update your repository, run this command:\n\n git push -fu origin $branch"
;;
*)
echo "Cancelled."
;;
esac
}
# ================================================================================================================================
# COLORS
# ================================================================================================================================
# --------------------------------------------------------------------------------------------------------------------------------
# Colors List - Style 1
# --------------------------------------------------------------------------------------------------------------------------------
function colors() {
for i in {0..255}; do
printf "\x1b[38;5;${i}mcolor%-5i\x1b[0m" $i
if ! (( ($i + 1 ) % 8 )); then
echo ""
fi
done
}
# --------------------------------------------------------------------------------------------------------------------------------
# Colors List - Style 2
# --------------------------------------------------------------------------------------------------------------------------------
function colorss() {
for x in {0..8}; do
for i in {30..37}; do
for a in {40..47}; do
echo -ne "\e[$x;$i;$a""m\\\e[$x;$i;$a""m\e[0;37;40m "
done
echo
done
done
echo ""
}
# ================================================================================================================================
# FZF = SEARCH
# ================================================================================================================================
# --------------------------------------------------------------------------------------------------------------------------------
# FZF-Ripgrep
# --------------------------------------------------------------------------------------------------------------------------------
function f() {
[[ -n $1 ]] && cd $1 # go to provided folder or noop
RG_DEFAULT_COMMAND="rg -i -l --hidden --no-ignore-vcs"
selected=$(
FZF_DEFAULT_COMMAND="rg --files" fzf \
-m \
-e \
--ansi \
--phony \
--reverse \
--bind "ctrl-a:select-all" \
--bind "f12:execute-silent:(vim {})" \
--bind "change:reload:$RG_DEFAULT_COMMAND {q} || true" \
--preview "rg -i --pretty --context 2 {q} {}" | cut -d":" -f1,2
)
[[ -n $selected ]] && vim $selected # open multiple files in editor
}
# --------------------------------------------------------------------------------------------------------------------------------
# FZF-Ripgrep
# --------------------------------------------------------------------------------------------------------------------------------
function ff() {
RG_PREFIX="rg --column --line-number --no-heading --color=always --smart-case "
INITIAL_QUERY="${*:-}"
IFS=: read -ra selected < <(
FZF_DEFAULT_COMMAND="$RG_PREFIX $(printf %q "$INITIAL_QUERY")" \
fzf --ansi \
--color "hl:-1:underline,hl+:-1:underline:reverse" \
--disabled --query "$INITIAL_QUERY" \
--bind "change:reload:sleep 0.1; $RG_PREFIX {q} || true" \
--bind "ctrl-f:unbind(change,ctrl-f)+change-prompt(2. fzf> )+enable-search+clear-query+rebind(ctrl-r)" \
--bind "ctrl-r:unbind(ctrl-r)+change-prompt(1. ripgrep> )+disable-search+reload($RG_PREFIX {q} || true)+rebind(change,ctrl-f)" \
--prompt '1. Ripgrep> ' \
--delimiter : \
--header '╱ CTRL-R (Ripgrep mode) ╱ CTRL-F (fzf mode) ╱' \
--preview 'bat --color=always {1} --highlight-line {2}' \
--preview-window 'up,60%,border-bottom,+{2}+3/3,~3'
)
[ -n "${selected[0]}" ] && vim "${selected[0]}" "+${selected[1]}"
}
# --------------------------------------------------------------------------------------------------------------------------------
# FZF-Kill-Processes
# --------------------------------------------------------------------------------------------------------------------------------
function fk() {
local pid
if [ "$UID" != "0" ]; then
pid=$(ps -f -u $UID | sed 1d | fzf -m | awk '{print $2}')
else
pid=$(ps -ef | sed 1d | fzf -m | awk '{print $2}')
fi
if [ "x$pid" != "x" ]
then
echo $pid | xargs kill -${1:-9}
fi
}
# --------------------------------------------------------------------------------------------------------------------------------
# 通过fzf搜索文件并用vim打开
# --------------------------------------------------------------------------------------------------------------------------------
function fe() {
local file
file="$(fd --type f ${1:-.} ${2:-.} \
--hidden \
--follow \
--exclude .git \
--exclude boot \
--exclude lib \
--exclude lib64 \
--exclude lost+found \
--exclude opt \
--exclude proc \
--exclude root \
--exclude run \
--exclude sbin \
--exclude srv \
--exclude sys \
--exclude tmp \
--exclude var / | fzf --multi --exit-0 --select-1)"
if [[ -n $file ]]; then
VIMRUNTIME= vim -p $file
fi
if [ -z "${file}" ]; then
return 1
fi
history -w
: > /tmp/fzf_tmp
for files in ${file[@]}; do
echo $files >> /tmp/fzf_tmp
done
arguments=$(cat /tmp/fzf_tmp)
echo vim ${arguments} >> ~/.bash_history
history -r
rm -rf /tmp/fzf_tmp
}
# ================================================================================================================================
# TMUX
# ================================================================================================================================
# --------------------------------------------------------------------------------------------------------------------------------
# TMUX
# --------------------------------------------------------------------------------------------------------------------------------
function tm() {
[[ -n "$TMUX" ]] && change="switch-client" || change="attach-session"
if [ $1 ]; then
tmux $change -t "$1" 2>/dev/null || (tmux new-session -d -s $1 && tmux $change -t "$1"); return
fi
session=$(tmux list-sessions -F "#{session_name}" 2>/dev/null | fzf --exit-0) && tmux $change -t "$session" || echo "No sessions found."
}
# ================================================================================================================================
# UNIX
# ================================================================================================================================
# --------------------------------------------------------------------------------------------------------------------------------
# Hosts Switch
# --------------------------------------------------------------------------------------------------------------------------------
function hosts(){
sudo sed -i "/# GitHub520 Host Start/Q" /etc/hosts && curl -s https://raw.hellogithub.com/hosts | sudo tee -a /etc/hosts > /dev/null 2>&1
echo -e "${GREEN}[√] Hosts Repace Successful${RESET}\n"
}
# --------------------------------------------------------------------------------------------------------------------------------
# Watch Dir
# --------------------------------------------------------------------------------------------------------------------------------
function wls(){
path=$1
inotifywait --quiet --monitor --event attrib,modify,move,delete,create --timefmt '%Y-%m-%dT%H:%M:%S' --format '%T %w %f %e' $path |
while read -r file; do
clear
ls -alhtovqF --color=always --group-directories-first --time-style +"%F %T" $path | awk '{printf "%-10s%-8s%-15s%-12s%-20s\n",$3,$4,$5,$6,$7}'
done
}
# --------------------------------------------------------------------------------------------------------------------------------
# Create A New Directory And Enter It
# --------------------------------------------------------------------------------------------------------------------------------
function mkd() {
mkdir -p "$@" && cd "$_";
}
# --------------------------------------------------------------------------------------------------------------------------------
# Check Web-Files Size
# --------------------------------------------------------------------------------------------------------------------------------
function sizeof() {
URL="$1"
humanize_size() {
local SIZE=$1
local KiB=1024
local MiB=$((1024 ** 2))
local GiB=$((1024 ** 3))
if ((SIZE > GiB)); then
echo "scale=2; $SIZE/$GiB" | bc | xargs -I{} printf "%.1fG" "{}"
elif ((SIZE > MiB)); then
echo "scale=2; $SIZE/$MiB" | bc | xargs -I{} printf "%.1fM" "{}"
elif ((SIZE > KiB)); then
echo "scale=2; $SIZE/$KiB" | bc | xargs -I{} printf "%.1fK" "{}"
else
echo "${SIZE}B"
fi
}
# Get Content-Length
local curl_size
curl_size=$(curl -sLI "$URL" | grep "[Cc]ontent-[Ll]ength" | awk 'NR==1{print $2}' | tr -d '\r')
if [ -z "$curl_size" ]; then
echo "File size not available"
exit 1
elif [[ "$curl_size" =~ ^[0-9]+$ ]]; then
humanize_size "$curl_size"
else
echo "Please provide a valid URL of a file"
exit 1
fi
}