summaryrefslogtreecommitdiff
path: root/config/essentials/shell/functions.sh
blob: c7f7b3ab30da1f2797025fbba3729ef0c47763a3 (plain)
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/bin/sh

log() { >&2 printf '%s' "$@"; }
logn() { >&2 printf '%s\n' "$@"; }

vmp() {
    col -b | \
    vim -MR \
    -c 'set ft=man nolist nonu nornu'
}
nvf() {
	cache="$HOME/.cache/nvf"
	match="$(grep -m1 "$1$" "$cache" 2> /dev/null)"
	if test ! -f "$match"
	then
		logn "resetting cache..."
		match="$(goo f "$HOME" | tee "$cache" | grep -m 1 "$1$" 2> /dev/null)"
		# # Alternative:
		# match="$(goo | grep -m 1 "$1" 2> /dev/null | tee -a | "$cache")"
	fi
	if test -f "$match"
	then
		$EDITOR "$match" && return
	else
		logn "no match." && return 1
	fi
}

nnn() { test -z "$NNNLVL" && /usr/bin/nnn "$@" || exit; }
ranger() { test -z "$RANGER_LEVEL" && /usr/bin/ranger "$@" || exit; }

# googoo aliases
_googoo_fzf_opt()
{
	unset dest opt
	if [ "$1" ]
	then
		[ -d "$1" ] && dest="$1" || opt="-q $1"
	fi
}
o()
{
	_googoo_fzf_opt "$1"
	f="$(goo f "$dest" | fzf $opt)"
	test "$1" && shift
	test -f "$f" && $EDITOR $@ "$f"
}
og()
{
	_googoo_fzf_opt "$1"
	cd "$(goo d "$dest" | fzf $opt)"
}
oog()
{
	_googoo_fzf_opt "$1"
	cd "$(dirname "$(goo f "$dest" | fzf $opt)")"
}

# Onelineres
awnk() { awk "{print \$$1}"; }
vimh() { vi -c "help $1" -c 'call feedkeys("\<c-w>o")'; }
dgo() { cd "$(goo d ~ | fzf --filter "$@" | head -n 1)"; }
open() { $EDITOR "$(goo f ~ | fzf --filter "$@" | head -n 1)"; }
pkbs() { doas pacman -Sy "$(pkgfile -b "$1" | tee /dev/stderr)"; }
oclip() { printf "\033]52;c;$(printf '%s' "$@" | base64)\a"; }
oclipp() { printf "]52;c$(cat | base64)"; }
sms() { ssh -t phone sendmsg "$1" "'$2'"; }
trcp() { scp "$1" db:/media/basilisk/downloads/transmission/torrents/; }
rln() { ln -s "$(readlink -f "$1")" "$2"; }
getgit() { git clone git@db:"$1"; }
esc() { eval "$EDITOR '$(which $1)'"; }
ssh_keyadd() { ssh-keygen -f "$HOME"/.ssh/"$1" -P "$(pass generate -f keys/"$HOST"/ssh/"$1" | tail -n 1)" -t ed25519; }
delfile() { curl -s "${2:-https://upfast.cronyakatsuki.xyz/delete/$1}"; }
upfile() { curl -s -F "file=@\"$1\"" "${2:-https://0x0.st}"; }
to_webm() { ffmpeg -y -i "$1" -vcodec libvpx -cpu-used -12 -deadline realtime "${1%.*}".webm; }
ngenable() { ln -sf /etc/nginx/sites-available/$1 /etc/nginx/sites-enabled/; }
remove_audio() { ffmpeg -i "$1" -cpu-used -$(nproc) -deadline realtime -c copy -an "${2:-out.mp4}"; }
nasg() { smbclient //192.168.178.24/Public/ -D ENFANTS/Luca/tmp -N -c "get $1"; }
vidlen() { date -u -d @"$(ffprobe -show_entries format=duration -v quiet -of csv="p=0" -i "$1")" +'%T'; }
pcp() { readlink -f "$1" | tr -d '\n' | clipp; }

ipc() 
{
   if [ "$(ip link show eno1 | awk -F, 'NR=1 {print $3}')" = "UP" ]
   then
        doas ip link set eno1 down
    else
        doas ip link set eno1 up 
   fi
}

psgrep()
{
	[ $# -eq 0 ] && return 1
	pgrep "$@" | xargs ps
}

unique() {
	f="$(mktemp)"
	awk '!x[$0]++' "$1" > "$f"
	mv "$f" "$1"
}

clip() { 
	if [ "$WAYLAND_DISPLAY" ]
	then
		echo -n "$@" | wl-copy
	else
		echo -n "$@" | xsel -b
	fi
}

unzipp() {
	unzip -- "$(readlink -f -- "$1")" || return 1
    rm -- "$1"
}

# fix long waiting time
__git_files() { _wanted files expl 'local files' _files; }

# Returns current branch
git_current_branch()
{
	command git rev-parse --git-dir > /dev/null 2>&1 || return
	git branch --show-current
}

# Check if main exists and use instead of master
git_main_branch()
{
  command git rev-parse --git-dir > /dev/null 2>&1 || return
  for ref in refs/{heads,remotes/{origin,upstream}}/{main,trunk,mainline,default}
  do
    if command git show-ref -q --verify $ref; then
      echo ${ref:t}
      return
    fi
  done
  echo master
}

# Check for develop and similarly named branches
function git_develop_branch() {
  command git rev-parse --git-dir > /dev/null 2>&1 || return
  for branch in dev devel development
  do
    if command git show-ref -q --verify refs/heads/$branch
    then
      echo $branch
      return
    fi
  done
  echo develop
}

# gpg backup
# $1: key
gpg_backup()
{
    key="$(gpg --list-keys --with-colons | awk -F: '/^uid/ {print $10}' |sort|uniq | fzf)"
    [ "$key" ] || exit 1
    gpg --armor --export-secret-keys "$key" > private.asc
    gpg --armor --export "$key" > public.asc
    gpg --armor --export-ownertrust > trust.asc
    tar -czvf gpg_backup.tar.gz public.asc private.asc trust.asc
    shred -uz public.asc private.asc trust.asc
}

# $1: backup tar
gpg_import()
{
	tar xf $1
	shred -uz $1
	gpg --import public.asc
	gpg --import-ownertrust trust.asc
	gpg --import private.asc
	shred -uz public.asc private.asc trust.asc
}

vbsr()
{
	vboxmanage snapshot "$1" restore "$2" &&
		vboxmanage startvm "$1" ||
		vboxmanage controlvm "$1" poweroff
}
vbsrr()
{
	vbsr "$1" "$2"
	sleep 3
	vbsr "$1" "$2"
}
vbst()
{
	vboxmanage snapshot "$1" take "$2"
}

pacsize()
{
	if test -n "$1"; then
		packages="$@"
	elif test ! -t 0; then
		packages="$(cat)"
	else
		echo "No data provided..."
		return 1
	fi
	echo $packages |
		expac '%m %n' - |
		numfmt --to=iec-i --suffix=B --format="%.2f"
}

mime-default ()
{
	mime=
    [ "${mime:=$1}" ] ||
        mime="$(find /usr/share/applications/ -iname '*.desktop' -printf '%f\n' |
            sed 's/\.desktop$//' |
            fzf)"

	logn "Setting '$mime' as default for its mimetypes"
    [ "$mime" ] || exit 1
	grep "MimeType=" /usr/share/applications/"$mime".desktop |
		cut -d '=' -f 2- | tr ';' '\0' |
		xargs -0I{} xdg-mime default "$mime".desktop "{}"
	logn "Done."
}

addedkeys() {
	find ~/.ssh -iname "*.pub" | while read key
	do 
		fingerprint="$(ssh-keygen -lf "$key" 2>/dev/null)" 
		if ssh-add -l | grep -q "$fingerprint"
		then
		echo "$key"
		fi
	done | sed "s,$HOME/.ssh/,,"
}

fpass() {
	find $HOME/.password-store -type f -not -path ".git" |
		grep "\.gpg$" |
		sed "s,$HOME/.password-store/,,;s,\.gpg$,," |
		fzf |
		xargs pass show -c
}

muttmail()
{
	config="$HOME/.config/mutt"
	mail="$(find "$config"/configs -type f -printf '%f\n' | fzf)"
	[ "$mail" ] || return 1
    printf 'source %s\n' "$config"/configs/"$mail" > "$config"/muttrc
	mutt
}

resize()
{
	test $# -lt 2 &&
		printf "usage: %s <format> <file> [out]\n" "$0" >&2 &&
		return 1
	convert -resize $1^ -gravity center -crop $1+0+0 -- "$2" "${3:-$1}"
}

edit_in_dir() { 
	file="$1/$(goo f "$1" | sed "s@^$1@@" | fzf)"
	[ -f "$file" ] || return 1
	$EDITOR "$file"
}

# Download a file from google drive
# link like this: https://drive.usercontent.google.com/download?id=1TiJDEftTtF1KTMBI950Bj487ndYqkwpQ&export=download
gdown () {
        agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/12$(head /dev/urandom | tr -dc '0-1' | cut -c1).0.0.0 Safari/537.36"
        uuid=$(curl -sL "$1" -A "$agent" | sed -nE 's|.*(uuid=[^"]*)".*|\1|p')
        aria2c -x16 -s16 "$1&confirm=t&$uuid" -U "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36" --summary-interval=0 -d "${2:-.}"
}

# toggle wireguard vpn on $1 -> interface
wgt() { 
	d="${1:-wg0}"
	ip -br a | awk '{print $1}' | grep "$d" > /dev/null &&
        doas wg-quick down "$d" ||
        doas wg-quick up "$d"
}



fchange()
{
    [ "$1" ] || return 1
    inotifywait -m -e create,modify,delete --format "%f" "${2:-.}"  |
        while read -r EVENT
        do
            eval "$1"
        done
}

unhappy.exe() {
    [ "$1" ] &&
        smiles=("[: " ".-." " :]" "._.") ||
        smiles=("]: " ".-." " :[" "._.")

	while true
	do
		for s in $smiles
		do
			printf '\r%s' "$s"
			sleep 1
		done
	done
}

ssh_port()
{
    ssh -f -N -L 0.0.0.0:"$3":localhost:"$1" "$2"
    >&2 printf "Forwarded port '%s' on '%s' to '%s'.\n" "$1" "$2" "$3"
}
ffconcat () {
	tmp=$(mktemp -p . ffconcat.XXXXX) 
	sed 's/.*/file &/' > "$tmp"
	ffmpeg -y -f concat -safe 0 -i $tmp -c copy "$1"
	rm $tmp
}
nvim_bindings() {  
    tmp="$(mktemp)"
    nvim +":set nomore | :redir! > $tmp | :map | :redir END | :q"  > /dev/null
    eval "nvim $(fzf < "$tmp" | awk '{print $NF}' | cut -f1 -d'>' | sed 's/:/ +/')"
    rm "$tmp"
}

prj() {
    pfx="$HOME/proj"
	d="$(find "$pfx" -mindepth 1 -maxdepth 1 -type d | sed "s@$pfx/@@" |fzf)"
    { [ "$d" ] && [ -d "$pfx/$d" ]; } || return 1
    cd "$pfx/$d"
}
prje() {
    prj || return 1
    f="$(git ls-files | fzf)"
    [ "$f" ] || return 1
    if [ -f "$f" ] 
    then
        "$EDITOR" "$f"
    else
        cd -
    fi
}
nprj() {
    if [ "$1" ] 
    then
        p="$1"
    else
        printf '>'
        p="$(head -n1)"
        [ "$p" ] || exit 1
    fi

    mkdir -p "$HOME/proj/$p"
    cd "$HOME/proj/$p"
}

edit_git_file () {
    GIT_WORK_TREE="$1" GIT_DIR="$1"/.git git rev-parse --show-toplevel > /dev/null 2>&1 || return 1
    f="$(GIT_WORK_TREE="$1" GIT_DIR="$1"/.git git ls-files | fzf)"
    [ "$f" ] || return 2
    $EDITOR "$1"/"$f"
}

# Wrapper to automatically add the key
# could have been alias='SSH_ASKPASS=askpass SSH_ASKPASS_REQUIRE=prefer ssh'
# but this option is very slow for some reason
ssh() {
    for arg in $@; do
        if grep "Host $arg\s*\$" ~/.ssh/config > /dev/null 2>&1 ;
        then
           # Disable notification by unsetting the DISPLAY varibles
            keyadd "$arg" 2> /dev/null
            break
        fi
    done
    /usr/bin/ssh $@
}

ssl_req() {
    [ "$1" ] || return 1
    [ "$2" ] || return 2
    openssl req -newkey rsa:4096 -x509 -sha256 -days 365 -nodes -out "$1" -keyout "$2"
}

trclipo()
{
    magnet="$(clipo)"
    if ! printf '%s\n' "$magnet" | grep '^magnet:' > /dev/null 2>&1
    then
        >&2 printf 'Not a magnet.\n'
        return 1
    fi

    category="$(printf "music\nanime\nmovies\nshows\nother\nsoftware\ngames\nisos\nbooks\n" | fzf)"
    [ "$category" ] || return 2

    transmission-remote debuc.com -a -w "/downloads/$category" "$magnet"

}