summaryrefslogtreecommitdiff
path: root/accounts/gkleen@skadhi/zsh/zshrc
diff options
context:
space:
mode:
Diffstat (limited to 'accounts/gkleen@skadhi/zsh/zshrc')
-rw-r--r--accounts/gkleen@skadhi/zsh/zshrc280
1 files changed, 280 insertions, 0 deletions
diff --git a/accounts/gkleen@skadhi/zsh/zshrc b/accounts/gkleen@skadhi/zsh/zshrc
new file mode 100644
index 00000000..ea792718
--- /dev/null
+++ b/accounts/gkleen@skadhi/zsh/zshrc
@@ -0,0 +1,280 @@
1dir() {
2 curlArchive=false
3 templateArchive=""
4 repoUrl=""
5 dir=""
6 forceShell=false
7 wormhole=false
8 gitWorktree=""
9 modifyPDF=""
10
11 while getopts ':t:a:d:ir:wg:p:m' arg; do
12 case $arg in
13 "t") ;;
14 "a")
15 if [[ ${OPTARG} =~ "://" ]]; then
16 templateArchive=${OPTARG}
17 curlArchive=true
18 else
19 templateArchive=${OPTARG:a}
20 fi
21 ;;
22 "d") dir=${OPTARG} ;;
23 "i") forceShell=true ;;
24 "r") repoUrl=${OPTARG} ;;
25 "w") wormhole=true ;;
26 "g") gitWorktree=${OPTARG} ;;
27 "p") modifyPDF=${OPTARG:a} ;;
28 *) printf "Invalid option: %s\n" $arg >&2; exit 2 ;;
29 esac
30 done
31
32 shift $((OPTIND - 1))
33
34 if [[ -z ${dir} && ${#@} -ge 1 ]]; then
35 dir=${1}
36 shift
37 fi
38
39 [[ -n ${dir} ]] || return 2;
40
41 if [[ ! -e ${dir} ]]; then
42 if [[ -z "${gitWorktree}" ]]; then
43 mkdir -vp ${dir}
44 else
45 git -C ${gitWorktree} worktree add ${dir}
46 fi
47 else
48 gitWorktree=""
49 fi
50
51 cleanup()
52 {
53 if [[ -n ${modifyPDF} ]]; then
54 cd ${modifyPDF:h}
55 typeset -a pages
56 eval 'pages=(${dir}/${modifyPDF:t:r}_*.png(on))'
57 magick -verbose "${pages[@]}" $(mktemp --tmpdir=${modifyPDF:h} ${modifyPDF:t:r}_XXXXXXXXXX.${modifyPDF:e})
58 modifyPDF=""
59 fi
60 }
61
62 (
63 set -o localoptions -o localtraps
64 trap 'return 1' INT TERM
65 trap cleanup EXIT
66
67 cd ${dir}
68 export dir;
69
70 [[ -n ${repoUrl} ]] && git clone -- ${repoUrl} .
71
72 [[ -n ${modifyPDF} ]] && templateArchive=${modifyPDF}
73
74 if [[ -n ${templateArchive} ]]; then
75 (
76 archiveFile=""
77 cleanup() {
78 [[ -n "${archiveFile}" ]] && rm -fv ${archiveFile}
79 }
80 trap cleanup EXIT
81
82 if [[ $curlArchive = "true" ]]; then
83 archiveFile=$(mktemp -t "archive.XXXXXXXXXX.${templateArchive:t}")
84
85 curl -SfL -o ${archiveFile} ${templateArchive}
86
87 templateArchive=${archiveFile}
88 fi
89
90 unpack=true
91 while [[ $unpack = "true" ]]; do
92 case $(file --brief --mime-type --dereference ${templateArchive}) in
93 application/zip)
94 unzip ${templateArchive}
95 unpack=false
96 ;;
97 application/vnd.debian.binary-package)
98 ar x ${templateArchive}
99 mkdir control data
100 tar -C control -xvaf control.*
101 tar -C data -xvaf data.*
102 unpack=false
103 ;;
104 application/x-rpm)
105 cpioArchive=$(mktemp -t "archive.XXXXXXXXXX.${templateArchive:t:r}.cpio")
106 rpm2cpio ${templateArchive} > ${cpioArchive}
107 templateArchive=${cpioArchive}
108 unpack=true
109 ;;
110 application/x-cpio)
111 cpio --extract --make-directories --no-absolute-filenames -F ${templateArchive}
112 unpack=false
113 ;;
114 application/pdf)
115 magick -verbose -density 400 ${templateArchive} ${modifyPDF:t:r}_%0d.png
116 unpack=false
117 ;;
118 application/octet-stream)
119 if [[ $(file --brief --dereference ${templateArchive}) =~ Squashfs ]]; then
120 unsquashfs -d . ${templateArchive}
121 unpack=false
122 fi
123 ;;
124 application/x-iso9660-image)
125 7z x ${templateArchive}
126 unpack=false
127 ;;
128 *)
129 tar -xvaf ${templateArchive}
130 unpack=false
131 ;;
132 esac
133 done
134 )
135 fi
136
137
138 [[ $wormhole = "true" ]] && wormhole receive --accept-file
139
140
141 if [[ ${#@} -gt 0 ]]; then
142 ${@}
143 fi
144
145 cd $(pwd) # Needed for mounting to work
146
147 if [[ ${#@} -eq 0 ]] || [[ $forceShell = "true" ]]; then
148 isSingleDir() {
149 typeset -a contents
150 contents=(*(N) .*(N))
151
152 if [[ ${#contents} -eq 1 && -d ${contents[1]} ]]; then
153 print ${contents[1]}
154 return 0
155 else
156 return 1
157 fi
158 }
159 while d=$(isSingleDir); do cd ${d}; done
160
161 zsh
162 fi
163 )
164}
165
166tmpdir() {
167 cleanup()
168 {
169 cd /
170 unmount() {
171 printf "Unmounting %s\n" ${1} >&2
172 fusermount3 -u ${1} || umount ${1} || sudo umount ${1}
173 }
174
175 if [[ -n ${dir} ]]; then
176 if mountpoint -q -- ${dir}; then
177 unmount ${dir} || return $?
178 else
179 while read -d $'\0' subDir; do
180 mountpoint -q -- ${subDir} || continue
181 unmount ${subDir} || return $?
182 done <<<$(find ${dir} -xdev -type d -print0 | sort -zr)
183 fi
184
185 rm -rfv --one-file-system -- ${dir}
186 dir=""
187 fi
188 }
189
190 local tmpdir=""
191
192 while getopts ':t:a:d:ir:wg:p:m' arg; do
193 case $arg in
194 "t") tmpdir="=${OPTARG}" ;;
195 "?"|":") printf "Invalid option: %s\n" $arg >&2; exit 2 ;;
196 esac
197 done
198
199 (
200 set -o localoptions -o localtraps
201 trap 'return 1' INT TERM
202 trap cleanup EXIT
203
204
205 local dir=$(mktemp -ud --tmpdir${tmpdir} ${0}.XXXXXXXXXX || return $?)
206
207 ( dir -d ${dir} ${@} )
208 )
209}
210
211qr() {
212 qrencode -l M -o - -t ANSIUTF8 $@
213}
214
215clock() {
216 tty-clock -sSbc -C 7 -d 0 -a 100000000
217}
218
219public-ip() {
220 curl -sSf -H 'Accept: application/json' $@ ifconfig.co | jq -r '.ip'
221}
222
223swap() {
224 f1=${1}
225 f2=${2}
226
227 if [[ -z "${f1}" || ! -e "${f1}" ]]; then
228 printf "ā€˜%s’ does not exist\n" "${f1}" >&2
229 return 2
230 fi
231 if [[ -z "${f2}" || ! -e "${f2}" ]]; then
232 printf "ā€˜%s’ does not exist\n" "${f2}" >&2
233 return 2
234 fi
235
236 tmpfile=$(mktemp --dry-run --tmpdir=${f1:h} .swap.XXXXXXXXXX)
237 mv -v ${f1} ${tmpfile}
238 mv -v ${f2} ${f1}
239 mv -v ${tmpfile} ${f2}
240}
241
242ls() {
243 eza --all --group-directories-first --colour=always --sort=modified --smart-group $@ | less --mouse -FR
244}
245
246l() {
247 ls --long --binary --git --time-style=iso --header $@
248}
249
250ssh-installer() {
251 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/gkleen@sif.midgard.yggdrasil $@
252}
253
254dichotomic_search() {
255 min=$1; shift
256 max=$1; shift
257
258 while [[ $min -lt $max ]]; do
259 # Compute the mean between min and max, rounded up to the superior unit
260 current=$(( (min + max + 1 ) / 2 ))
261 if $@ $current; then
262 min=$current
263 else
264 max=$((current - 1))
265 fi
266 done
267
268 echo $min
269}
270
271while read -r -d $'\0' d _ s; do
272 hash -d ${s}=${d}
273done < <(find ~/projects ~/uni -regextype posix-extended -maxdepth 2 -type d -regex '\S+/[0-9]{2}[ws]/\S+' -print0 2>/dev/null | \
274 sed -zr 's|(.*/([0-9]{2}[ws])/(.+))|\1 \2 \3|' | \
275 sort -z -r -k2 | sort -z -s -k3 | uniq -z -f 2)
276
277alias rzadm=$'tmpdir -i sh -c \'mkdir adm; sshfs gkleen@mgmt01:/adm adm\''
278alias mathcloud=$'tmpdir -i rclone mount --daemon mathcloud:// .'
279
280export DEFAULT_USER=gkleen