-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.sh
executable file
·338 lines (287 loc) · 7.99 KB
/
lib.sh
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
glow() {
command glow "$@" | less
}
sockets() {
(osqueryi --list --separator ',' |
cut -c 1-300 |
column -t -s ',' |
sed '1d' |
rg -v '(rapportd|node.mojom.NodeService|wormhole)' |
sort) <<EOF
SELECT s.pid, s.local_port, p.cmdline, p.cwd
FROM process_open_sockets AS s
INNER JOIN processes AS p
ON s.pid = p.pid
WHERE s.state = 'LISTEN'
ORDER BY p.cmdline;
EOF
}
processes() {
(osqueryi --list --separator ',') <<EOF
SELECT p.pid, p.name, p.cwd
FROM processes AS p
ORDER BY p.cmdline;
EOF
}
vscode() {
if [ -z "$1" ]; then
if ls | rg -q '\.code-workspace$'; then
code *.code-workspace
else
code .
fi
else
code -g "$@"
fi
}
which-follow() {
local p
p="$(which -a "$1" | tee /dev/stderr | tail -n1)"
if [[ -z "$p" ]]; then
:
elif [[ "$p" == *" not found" ]]; then
echo "$p"
elif [[ "$p" == *": aliased to "* ]]; then
echo "$p"
local p2
p2=$(echo "$p" | rg -r '$1' '.+aliased to ([^ ]+).*')
[ "$p" != "$p2" ] && which-follow "$p2"
else
readlink -f "$p"
fi
}
bat-files() {
local f
while read f; do bat --color=always $f; done | less -R
}
pyenv-load() {
source /Users/dan/src/devenv/shell-config/pyenv.sh
}
nvm-load() {
source /Users/dan/src/devenv/shell-config/nvm.sh
}
__dan_is_macos() {
[ -e /Applications ]
}
cat-which() {
bat --style header,grid $(which "$1")
}
open-file() {
code --new-window $(resolve-file "$1")
}
cd-site-packages() {
cd $(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")
}
delta-toggle() {
eval "export DELTA_FEATURES='$(-delta-features-toggle $1 | tee /dev/stderr)'"
}
die() {
echo "$@" >&2
return 1
}
dot-view() {
local file=$(mktemp).svg
dot -T svg -o $file <$1
echo $file
open -a "/Applications/Google Chrome.app" $file
}
ega() {
touch $1 && git add $1 && emacsclient -n $1
}
emacs-set-minimal() {
rm -f ~/.emacs
ln -s ~/devenv/emacs-config/emacs-minimal.el ~/.emacs
}
emacs-set-normal() {
rm -f ~/.emacs
ln -s ~/devenv/emacs-config/emacs.el ~/.emacs
}
fd() {
~/bin/fd --color=always --hyperlink=always "$@" |
rg -r $'\e]8;;cursor://file$1' $'^\e]8;;file://'$(hostname)'(.*)'
}
# https://gist.github.com/SlexAxton/4989674
gifify() {
if [[ -n "$1" ]]; then
if [[ $2 == '--good' ]]; then
ffmpeg -i $1 -r 10 -vcodec png out-static-%05d.png
time convert -verbose +dither -layers Optimize -resize 600x600\> out-static*.png GIF:- | gifsicle --colors 128 --delay=5 --loop --optimize=3 --multifile - >$1.gif
rm out-static*.png
else
ffmpeg -i $1 -s 600x400 -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 --delay=3 >$1.gif
fi
else
echo "proper usage: gifify <input_movie.mov>. You DO need to include extension."
fi
}
gprof2dot-and-go() {
local file=$(mktemp).svg
gprof2dot --format pstats $1 | dot -T svg -o $file
echo $file
open -a "/Applications/Google Chrome.app" $file
}
grip-chrome() {
tmpfile=$(mktemp)
grip --export $1 $tmpfile >/dev/null 2>&1 && chrome $tmpfile
}
grip-python() {
(
echo '```python'
cat
echo '```'
) | grip --export -
}
hub-commit-pr() {
head=$(git symbolic-ref --short HEAD)
EDITOR='emacsclient -n' hub pull-request -b dev:master -h dev:$head $1
}
hub-pr() {
url=$(hub browse -u)
open ${url/tree/pull}
# while chrome-cli info | grep -q "Loading: Yes"; do
# sleep 0.1
# done
# chrome-cli execute "document.getElementById('pull_request_body').value = ''"
}
hyperlink() {
local url="$1"
local text="$2"
printf '\e]8;;%s\e\\%s\e]8;;\e\\\n' "$url" "$text"
}
kill-python() {
kill $(ps aux | grep python | grep -v grep | grep "$1" | awk '{print $2}')
}
mv-downcase() {
local f=$(mktemp -u)
mv "$1" "$f" && mv "$f" $(tr "[:upper:]" "[:lower:]" <<<"$1")
}
mv-link() {
local source="$1"
local target="$2"
[ -n "$1" ] && [ -n "$2" ] || die "mv-link source target"
mv "$source" "$target" && ln -s "$target" "$source"
}
python-virtualenv-activate() {
source $DAN_VIRTUALENVS_DIRECTORY/"$1"/bin/activate
}
python-virtualenv-cd-sitepackages() {
cd $DAN_VIRTUALENVS_DIRECTORY/$(python-virtualenv-name)/lib/python*/site-packages
}
python-virtualenv-name() {
basename $(git rev-parse --show-toplevel)
}
rust-list-tests() {
# TODO: distinguish between tests that are indented and thus in a
# module probably named 'tests', and tests which are not indented.
rg -H --vimgrep 'fn test' |
sed -E s,src/\(.+\)\.rs:\[0-9\]+:\[0-9\]:\ +fn\ \(test\[^\(\]+\).\*,\\1::tests::\\2, |
sed -E s,/,::,g | sed -E s,mod::,,
}
src-grep() {
local d
find ~/src -maxdepth 1 -type d | egrep -v '(3p|counsyl)' | while read d; do
(cd $d && [ -d .git ] && git grep $@)
done
}
switchto() {
workon $1 && cdproject
}
tmux-send-all() {
tmux list-panes -a -F '#{session_name}:#{window_index}.#{pane_index}' |
xargs -I PANE tmux send-keys -t PANE "$1" Enter
}
ping-world() {
ping -c 1 www.gov.uk
}
pip-local() {
PIP_INDEX_URL="http://$(docker-machine ip $DOCKER_MACHINE_NAME):5555/simple/" \
PIP_TRUSTED_HOST=$(docker-machine ip $DOCKER_MACHINE_NAME) \
pip $@
}
pypi-local() {
local port=${1:-5555}
local package_dir=~/tmp/packages
local image_name=simple-http-server
local container_name=$image_name
local dockerfile_dir=~/devenv/dockerfiles/simple-http-server
(cd $dockerfile_dir && docker build -t $image_name .)
dir2pi $package_dir
docker rm -f $container_name 2>/dev/null
echo "Starting local PyPi server: http://$(docker-machine ip $DOCKER_MACHINE_NAME):$port"
docker run -p $port:80 --rm --name $container_name -v $package_dir:/srv simple-http-server
}
random-leetcode() {
local file=$(ls lc_* | shuf -n1)
echo $file
LEETCODE=$file
open "https://leetcode.com/problems/$(sed -E "s/lc_(.+)\.py/\1/" <<<$file | tr _ -)"
}
tail0() {
file=$1
until [[ -e $file ]]; do sleep 0.1; done
tail -f $file
}
# toggle iTerm Dock icon
# https://gist.github.com/CrazyApi/5377685
# http://apple.stackexchange.com/questions/209250/remove-iterm-from-cmdtab-apps?rq=1
# http://apple.stackexchange.com/questions/92004/is-there-a-way-to-hide-certain-apps-from-the-cmdtab-menu
toggle-iterm() {
pb='/usr/libexec/PlistBuddy'
iTerm='/Applications/iTerm.app/Contents/Info.plist'
echo "Do you wish to hide iTerm in Dock?"
select ync in "Hide" "Show" "Cancel"; do
case $ync in
'Hide')
$pb -c "Add :LSUIElement bool true" $iTerm
echo "relaunch iTerm to take effectives"
break
;;
'Show')
$pb -c "Delete :LSUIElement" $iTerm
echo "run killall 'iTerm' to exit, and then relaunch it"
break
;;
'Cancel')
break
;;
esac
done
}
function unset-env-vars() {
cut -d= -f1 | while read s; do unset $s; done
}
function hyperlink() {
local url="$1"
local text="$2"
printf '\e]8;;%s\e\\%s\e]8;;\e\\\n' "$url" "$text"
}
function fdd() {
local _path
command fd --color=always "$@" |
while read _path; do
abspath=$(readlink -f $(echo -n $_path | ansifilter))
url="vscode://file/$abspath"
hyperlink $url $_path
done
}
function whichf() {
readlink -f $(which "$1")
}
function -tempyral() {
local file=$1
local class=$2
local _path=/Users/dan/src/temporalio/tempyral/media/videos/$file/1080p60/$class.mp4
: >/tmp/log &&
manim -qh scenes/$file.py $class && pkill 'QuickTime Player' &&
sleep 1 &&
open $_path
}
function tempyral-execute-workflow() {
-tempyral execute_workflow ExecuteWorkflow
}
function tempyral-execute-update() {
-tempyral execute_update ExecuteUpdate
}
function tempyral-call-activity() {
-tempyral call_activity CallActivity
}