/
functions.zsh.j2
251 lines (200 loc) · 4.73 KB
/
functions.zsh.j2
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
{{ ansible_managed | comment }}
function command_exists() {
command -v "$1" &>/dev/null
}
function update() {
if [[ "$OSTYPE" == darwin* ]] && command_exists softwareupdate; then
echo "Updating softwareupdate..."
sudo softwareupdate -ia
echo "softwareupdate updated!"
fi
if [[ "$OSTYPE" == linux* ]] && command_exists apt; then
echo "Updating apt..."
sudo apt update --assume-yes
sudo apt upgrade --assume-yes
echo "apt updated!"
fi
if [[ "$OSTYPE" == linux* ]] && command_exists snap; then
echo "Updating snap..."
sudo snap refresh
echo "snap updated!"
fi
if [[ "$OSTYPE" == linux* ]] && command_exists flatpak; then
echo "Updating flatpak..."
flatpak update --assumeyes
echo "flatpak updated!"
fi
if command_exists brew; then
echo "Updating brew..."
brew update
brew upgrade
echo "brew updated!"
fi
if command_exists asdf; then
echo "Updating asdf..."
asdf update
asdf plugin update --all
echo "asdf updated!"
fi
if command_exists npm; then
echo "Updating npm..."
npm update --global
echo "npm updated!"
fi
if command_exists pip; then
echo "Updating pip..."
pip install -r <(pip freeze) --upgrade
echo "pip updated!"
fi
clean
}
function clean() {
if [[ "$OSTYPE" == linux* ]] && command_exists apt; then
echo "Cleaning apt..."
sudo apt autoclean --assume-yes
sudo apt autoremove --assume-yes
sudo apt purge --assume-yes
echo "apt cleaned!"
fi
if command_exists brew; then
echo "Cleaning brew..."
brew cleanup
brew autoremove
echo "brew cleaned!"
fi
}
function localip() {
if [[ "$OSTYPE" == linux* ]]; then
ip -json addr show |
jq -r '.[] |
.addr_info[] |
select(.family == "inet") |
[.label,.local] |
join(": ")'
elif [[ "$OSTYPE" == darwin* ]]; then
ifconfig -l | xargs -n1 ipconfig getifaddr || true
fi
}
function publicip() {
dig +short myip.opendns.com @resolver1.opendns.com
}
function help() {
local command \
alias_text alias_return_code \
help_text help_return_code \
man_text man_return_code
command="$*"
alias_text=$(alias "$command" 2>&1)
alias_return_code=$?
if [ $alias_return_code -eq 0 ] && [ -n "$alias_text" ]; then
echo "$alias_text"
return 0
fi
help_text=$(
eval "$command --help" 2>&1 ||
eval "$command -help" 2>&1 ||
eval "$command -h" 2>&1 ||
eval "$command help" 2>&1
)
help_return_code=$?
if [ $help_return_code -eq 0 ] && [ -n "$help_text" ]; then
echo "$help_text" | bat --plain --language=help
return 0
fi
man_text=$(man "$command" 2>&1)
man_return_code=$?
if [ $man_return_code -eq 0 ] && [ -n "$man_text" ]; then
echo "$man_text" | bat --plain --language=man
return 0
fi
echo "No help found for '$command'"
return 1
}
function cdp() {
local project_dir
project_dir=$(
fd '.git$' ~/Projects --type d --hidden --no-ignore --exec echo "{//}" |
fzfp --query "${*:-}"
)
if [ -n "$project_dir" ]; then
cd "$project_dir" || exit
fi
}
function ghprco() {
local jq_template pr_number
jq_template="\"\
#\(.number) - \(.title)\
\t\
Author: \(.user.login)
Created: \(.created_at)
Updated: \(.updated_at)
\(.body)
\""
pr_number=$(
gh api "repos/:owner/:repo/pulls" |
jq ".[] | $jq_template" |
sed -e 's/"\(.*\)"/\1/' -e "s/\\t/\t/" |
fzf \
--with-nth=1 \
--delimiter="\t" \
--preview="echo -e {2}" \
--preview-window=top:wrap |
sed -r "s/^#([0-9]+).*/\1/"
)
if [ -n "$pr_number" ]; then
gh pr checkout "$pr_number"
fi
}
function f() {
local selected
selected=$(fzfp --query "${*:-}")
echo "$selected"
}
function fcd() {
local selected
selected=$(f "$@")
if [[ -n "$selected" ]]; then
cd "$(dirname "$selected")" || exit
fi
}
function fe() {
local selected
selected=$(f "$@")
if [[ -n "$selected" ]]; then
$EDITOR "$selected"
fi
}
function ff() {
local selected
selected=$(
rg --no-heading "${*:-}" 2>/dev/null |
fzfp --ansi \
--color "hl:-1:underline,hl+:-1:underline:reverse" \
--delimiter : \
--preview "bat --highlight-line {2} {1}" \
--preview-window "+{2}+3/3,~3"
)
echo "$selected"
}
function ffcd() {
local selected
IFS=: read -rA selected <<<"$(ff "$@")"
if [[ -n "$selected" ]]; then
local file
file="${selected[1]}"
cd "$(dirname "$file")" || exit
fi
}
function ffe() {
local selected
IFS=: read -rA selected <<<"$(ff "$@")"
if [[ -n "$selected" ]]; then
local file line
file="${selected[1]}"
line="${selected[2]}"
$EDITOR "$file" +"$line"
fi
}
function logs() {
tail -f "$@" | bat --paging=never --language=log
}