summaryrefslogtreecommitdiff
path: root/config/essentials/zsh/functions.zsh
blob: a63bf69649e65f832180b64134e4e678c9ed13d9 (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
#!/bin/zsh

die ()
{
	echo "$1" >&2
}

awnk() {
	awk "{print \$$1}"
}

vmp() {
    col -b | \
    vim -MR \
    -c 'set ft=man nolist nonu nornu'
}
vimh() {
	vim -c "help $1" -c 'call feedkeys("\<c-w>o")'
}
nvf() {
	local cache="$HOME/.cache/nvf"
	local match="$(grep -m1 "$1$" "$cache" 2> /dev/null)"
	if test ! -f "$match"
	then
		die "resetting cache..."
		match="$(goo | 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
		vim "$match" && return
	else
		die "no match." && return 1
	fi
}

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

# googoo aliases
ff () { goo f "$1" | fzf }
fd () { goo d "$1" | fzf }
fdf () { goo f "$1" | fzf | xargs -I {} dirname "{}" }
o ()
{
	f="$(ff $1)"
	test "$1" && shift
	test -n "$f" && $EDITOR $@ "$f"
}
go ()
{
	d="$(fd $1)"
	test -d "$d" && cd "$d"
}
ogo ()
{
	d="$(fdf $1)"
	test -d "$d" && cd "$d"
}

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
}

calc () { echo "$@" | bc -l }

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

clip () { 
	if [ "$WAYLAND_DISPLAY" ]
	then
		echo -n "$@" | wl-copy
	else
		echo -n "$@" | xclip -selection clipboard -rmlastnl
	fi
}

fzh () {
    choice="$(tac $HOME/.config/zsh/histfile | fzf)"
    test -z "${choice}" && return
    echo "${choice}" >> "${HOME}/.config/zsh/histfile"
    eval "${choice}"
}

unzipp () {
    file=$1
    shift
    unzip $file $@ || exit 1
    rm $file
}

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

esc () {
	$EDITOR "$(which $1)"
}

delfile () {
	curl ${2:-"https://up.kallipso.be/delete/$1"}
}
upfile () {
	curl -F "file=@\"$1\"" ${2:-"https://up.kallipso.be"}
}

sgd () {
	d="$PWD"
	for dir in ${1:-$HOME/src/*} 
	do 
		cd $dir
		git fetch > /dev/null 2>&1
		if [ "$(git status --short 2>/dev/null | grep -v "??" | head -1)" ]
		then
			# There are changes, and this is a git repo
			echo "$PWD \e[1;31m*changes\e[0m"
		fi
		test "$(parse_git_remote)" && 
			echo "$PWD \e[0;32m*push/pull\e[0m"
		done
	cd "$d"
	unset d
}

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

# Check if main exists and use instead of master
function git_main_branch() {
  command git rev-parse --git-dir &>/dev/null || return
  local ref
  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 || return
  local branch
  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
gpg_backup ()
{
	gpg --export-secret-keys --armor > private.asc
	gpg --export --armor > public.asc
	gpg --export-ownertrust --armor > trust.asc
	tar czf gpg_backup.tar.gz {public,private,trust}.asc
	shred -uz {public,private,trust}.asc
}

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

ngenable ()
{
	ln -sf /etc/nginx/sites-available/$1 /etc/nginx/sites-enabled/
}

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"
}
pkbs ()
{
	pkgfile -b "$1" | tee /dev/stderr | doas pacman -S -
}

mime-default ()
{
	die "Setting '$1' as default for its mimetypes"
	grep "MimeType=" /usr/share/applications/"$1" |
		cut -d '=' -f 2- |
		tr ';' '\n' |
		xargs -I {} xdg-mime default "$1" "{}"
	die "Done."
}

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