-
Notifications
You must be signed in to change notification settings - Fork 1
/
.zsh.functions
207 lines (169 loc) · 5.96 KB
/
.zsh.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
#!/usr/bin/env bash
# Functions -----------------------------------------------
# override fzf history widget to use history from mariadb
fzf-history-widget() {
selected=( $(mariadb -ucli-history -B -e "select distinct command from sh.history group by command order by max(created) desc, command;" |
FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort $FZF_CTRL_R_OPTS --query=${(qqq)LBUFFER} +m" fzf) )
local ret=$?
BUFFER="$selected" # populate input prompt
CURSOR=$#RBUFFER # set cursor position at the end of the command inserted
zle reset-prompt
return $ret
}
zle -N fzf-history-widget
bindkey '^R' fzf-history-widget
# wrapper over frequently used kubectl subcommands
kr() {
POD_ID=$(kubectl get pods | fzf --reverse | awk '{print $1}')
case $1 in
--delete|-d)
CMD="kubectl delete pod '${POD_ID}'"
echo "${CMD}"; eval "${CMD}"
if [[ $? != "0" ]]
then
echo "unable to kill pod"
fi
;;
--exec|-e)
CONTAINER=$(echo "${POD_ID}" | sed 's/\-[a-zA-Z0-9]\{8,10\}.*//g')
CMD="kubectl exec '${POD_ID}' --container '${CONTAINER}' --stdin --tty -- sh"
echo "${CMD}"; eval "${CMD}"
if [[ $? != "0" ]]
then
echo "unable to exec into pod"
fi
;;
--logs|-l)
CONTAINER=$(echo "${POD_ID}" | sed 's/\-[a-zA-Z0-9]\{8,10\}.*//g')
CMD="kubectl logs --follow '${POD_ID}' --container '${CONTAINER}'"
echo "${CMD}"; eval "${CMD}"
if [[ $? != "0" ]]
then
echo "unable to fetch logs for pod"
fi
;;
--tag|-t)
CMD="kubectl describe pod '${POD_ID}' | grep Image | grep -i coderunner:git | awk -F'-' '{print \$NF}'"
echo "${CMD}"; eval "${CMD}"
if [[ $? != "0" ]]
then
echo "unable to fetch image tag for pod"
fi
esac
}
# create a temporary file and open it for editing
tmpf() {
dir=$(mktemp /var/tmp/"tmp.$(date +'%Y%m%d-%H%M%S').XXXXX")
${EDITOR} +"set filetype=$1" + "$dir"
}
# create a temporary directory and enter it
tmpd() {
local dir
if [ $# -eq 0 ]; then
dir=$(mktemp -d)
else
dir=$(mktemp -d -t "${1}.XXXXXXXXXX")
fi
cd "$dir" || exit
}
# exec into docker container
de() {
local cid
cid=$(docker ps -a --format "{{.ID}} \t{{.Names}}\t {{.Status}}\t{{.Image}}" | sed 1d | fzf -1 -q "$1" | awk '{print $1}')
[ -n "$cid" ] && docker exec -it "$cid" sh
}
# serve current directory over http
serve() {
local port="${1:-8000}"
sleep 1 && open "http://localhost:${port}/" &
# Set the default Content-Type to `text/plain` instead of `application/octet-stream`
# And serve everything as UTF-8 (although not technically correct, this doesn’t break anything for binary files)
python -c $'import SimpleHTTPServer;\nmap = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\nmap[""] = "text/plain";\nfor key, value in map.items():\n\tmap[key] = value + ";charset=UTF-8";\nSimpleHTTPServer.test();' "$port"
}
# better history command
fh() {
print -z $( ([ -n "$ZSH_NAME" ] && fc -l 1 || history | uniq) | fzf +s --tac | sed 's/ *[0-9]* *//')
}
# exec into compile, execute container
dex() {
PS=$(docker ps)
CID=$(echo $PS | grep $1 | awk '{print $1}')
CNAME=$(echo $PS | grep $1 | awk '{print $3}')
echo "Exec(ing) into $CNAME:$CID"
docker exec -it $CID sh
}
# move to the top-level parent directory
cdp() {
TEMP_PWD=`pwd`
while ! [ -d .git ]; do
cd ..
done
OLDPWD=$TEMP_PWD
}
# search for a pattern and open the file
rf() {
result=$(rg --no-heading -n $1 . | fzf --reverse)
file_path=$(echo $result | awk -F ':' '{print $1}')
line_number=$(echo $result | awk -F ':' '{print $2}')
${EDITOR} $file_path +$line_number
}
# find and kill process by pid
kp() {
kill -9 $(ps -ef | fzf --reverse | awk '{print $2}') &> /dev/null
if [[ $? != "0" ]]
then
echo "Unable to kill process"
fi
}
# facilitate new git repo
create_repo() {
git init &> /dev/null && echo "Created repo"
echo -n "Add remote (y/n): "
read choice
if [[ $choice =~ ^[Yy]$ ]]
then
echo -n "Enter remote: "
read remote
git remote add origin $remote
fi
}
# open fzf window with dirs and cd into it
quick_find() {
dir=$(find . -not -path '*/\.*' -type d -maxdepth 2 | fzf --layout=reverse --preview "ls -FG {}")
if [[ "$?" != "0" ]]; then return; fi;
cd $dir
zle reset-prompt
}
# zle -N quick_find_widget quick_find # define a widget for the func above
# bindkey "^o" quick_find_widget # remap ^i to the widget -> func
# list all files in current dir tree wit preview
# select one to open in vim
edit_files() {
file=$(find . -type f -not -path '*/\.git/*' | fzf --layout=reverse --preview "cat {}")
if [[ "$?" != "0" ]]; then
return
fi
${EDITOR} $file
}
zle -N quick_find_widget edit_files # define a widget for the func above
bindkey "^o" quick_find_widget # remap ^i to the widget -> func
# function to start a timer in bg / pomodoro
doro() {
if [[ $# == 0 ]]; then
let duration=1500 # no arguments -> 25 minutes
else
let duration=$(($1*60))
fi
sleep $duration
(osascript -e 'tell application "System Events" to display dialog "Time for a water break!" buttons "OK" default button "OK"' && say "time up. STOP") &
}
# open man with `less` pager directly at a given switch
# e.g. mans ls -G -> will open man page for ls at -G option
mans() {
man -P "less -p \"^ +$2\"" $1
}
# prints the complete command referred by the alias
# kubectl() { echo "+ kubectl $@"; command kubectl $@; }
# echo aws profile in use when executing aws or sam commands
# sam() { echo "+ using profile: $AWS_DEFAULT_PROFILE"; command sam $@; }
# aws() { echo "+ using profile: $AWS_DEFAULT_PROFILE"; command aws $@; }