summaryrefslogtreecommitdiff
path: root/accounts/gkleen@sif/zshrc
blob: 83e862707d26a17920890d3e4ffd64d4ed097196 (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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
filebin() {
    basePath=/srv/www/files
    ssh ymir find /srv/www/files -type f -printf "$'%T@ %TY-%Tm-%TdT%TH:%TM %P\\\\0'" | sort -zn | cut -z -d ' ' -f 2- \
	| while IFS= read -r -d $'\0' l; do
	    IFS=' ' read -r t p <<<"${l}"
	    printf "%s https://f.141.li/%s\n" "${t}" "${p}"
	  done
}

push2bin() {
    if [[ ${#@} -eq 1 && ! -r ${1} ]]; then
        uux -p 'ymir!push2bin' $(echo -n "${1:t}" | tr -c $'[:alnum:]+-=.' '_')
    else
        for f (${@}); do
	    uux -p 'ymir!push2bin' $(echo -n "${f:t}" | tr -c $'[:alnum:]+-=.' '_') <${f}
	done
    fi
}

genmail() {
    local baseName=""
    local target=""
    if [[ ${#@} -ge 1 ]]; then
        target=${1}
        shift
    fi

    if [[ ${#@} -ge 1 ]]; then
        baseName=$(pwgen ${@})
    else
        baseName=$(pwgen -v -A -0 16 1)
    fi
    baseName=$(tr -cd $'[:alnum:]!#$%&*+-/=?^_{|}~.' <<<${baseName})
    address=${baseName}@141.li
    insertAddr() {
      echo "${baseName} gkleen" | ssh ymir tee -a /srv/mail/spm 1>/dev/null \
    }

    printf "%s\n" ${address}
    read -q 'cont?Continue [y/N]? ' || return

    insertAddr
}

s() {
    dir=$(pwd)
    [[ ${#@} -ge 1 ]] && dir=$1

    shellFile=$(findNix ${@})
    [[ ${#@} -ge 1 ]] && shift

    typeset -a cmd
    if [[ -d ${dir}/.nix-gc-roots ]]; then
        cmd=(persistent-nix-shell ${shellFile} ${S_EXTRA_ARGS} ${@})
    else
        cmd=(nix-shell ${shellFile} ${S_EXTRA_ARGS} ${@})
    fi

    if [[ -n "${S_SYSTEMD}" ]]; then
        systemd-run --user --slice=development.slice --collect -E PATH=${PATH} -p WorkingDirectory=${dir} -- ${cmd}
    else
        (
            cd ${dir}

            exec ${cmd}
        )
    fi
}

sz() {
    typeset -a S_EXTRA_ARGS
    S_EXTRA_ARGS=(--run "env __ETC_ZSHENV_SOURCED=1 zsh") s ${@}
}
st() {
    typeset -a S_EXTRA_ARGS
    S_EXTRA_ARGS=(--run "tmux new-session env __ETC_ZSHENV_SOURCED=1 zsh") s ${@}
}
stt() {
    typeset -a S_EXTRA_ARGS
    S_SYSTEMD=true S_EXTRA_ARGS=(--run "urxvt -e tmux -S .tmux.sock new-session env __ETC_ZSHENV_SOURCED=1 zsh") s ${@}
}
se() {
    typeset -a S_EXTRA_ARGS
    S_SYSTEMD=true S_EXTRA_ARGS=(--run "emacs") s ${@}
}

findNix() {
    if [[ $#@ -eq 0 ]]; then
        findNix $(pwd)
    elif [[ -f "$1" ]]; then
        print ${1:a}
    elif [[ -d "$1" && -f "$1"/shell.nix ]]; then
        print ${1:a}/shell.nix
    elif [[ -d "$1" && -f "$1"/default.nix ]]; then
        print ${1:a}/default.nix
    elif [[ -d "$1" && "$1" != "/" ]]; then
        findNix ${1:h}
    else
        printf "Traversed directories to ‘/’ and found no shell specification\n" >&2
        return 1
    fi
}

dir() {
    curlArchive=false
    templateArchive=""
    repoUrl=""
    nixShell=""
    findNix=false
    dir=""
    forceShell=false
    wormhole=false
    gitWorktree=""
    # notmuchMsg=""
    quickserve=false
    modifyPDF=""

    while getopts ':t:a:s:Sd:ir:wqg:p:' arg; do
        case $arg in
            "t") ;;
            "a")
                if [[ ${OPTARG} =~ "://" ]]; then
                    templateArchive=${OPTARG}
                    curlArchive=true
                else
                    templateArchive=${OPTARG:a}
                fi
                ;;
            "s") nixShell=${OPTARG:a} ;;
            "S") findNix=true ;;
            "d") dir=${OPTARG} ;;
            "i") forceShell=true ;;
            "r") repoUrl=${OPTARG} ;;
            "w") wormhole=true ;;
            "g") gitWorktree=${OPTARG} ;;
            # "n") notmuchMsg=${OPTARG} ;;
	    "q") quickserve=true ;;
	    "p") modifyPDF=${OPTARG:a} ;;
            *) printf "Invalid option: %s\n" $arg >&2; exit 2 ;;
        esac
    done

    shift $((OPTIND - 1))

    if [[ -z ${dir} && ${#@} -ge 1 ]]; then
        dir=${1}
        shift
    fi

    [[ -n ${dir} ]] || return 2;

    if [[ ! -e ${dir} ]]; then
        if [[ -z "${gitWorktree}" ]]; then
            mkdir -vp ${dir}
        else
            git -C ${gitWorktree} worktree add ${dir}
        fi
    else
        gitWorktree=""
    fi

    cleanup()
    {
	cd ${modifyPDF:h}
	[[ -n ${modifyPDF} ]] && nix shell nixos#imagemagick -c convert -verbose ${dir}/${modifyPDF:t:r}_*.png(on) ${modifyPDF}
    }

    (
        trap cleanup EXIT

        cd ${dir}
        export dir;

        ${findNix} && { nixShell=$(findNix) || return $? }

        [[ -n ${repoUrl} ]] && git clone -- ${repoUrl} .

	[[ -n ${modifyPDF} ]] && templateArchive=${modifyPDF}

        if [[ -n ${templateArchive} ]]; then
            (
                archiveFile=""
                cleanup() {
                    [[ -n "${archiveFile}" ]] && rm -fv ${archiveFile}
                }
                trap cleanup EXIT

                if ${curlArchive}; then
                    archiveFile=$(mktemp -t "archive.XXXXXXXXXX.${templateArchive:t}")

                    curl -L -o ${archiveFile} ${templateArchive}

                    templateArchive=${archiveFile}
                fi

		unpack=true
		while ${unpack}; do
		    case $(file --brief --mime-type --dereference ${templateArchive}) in
			application/zip)
			    unzip ${templateArchive}
			    unpack=false
			;;
			application/vnd.debian.binary-package)
			    nix shell nixos#binutils --command ar x ${templateArchive}
			    mkdir control data
			    tar -C control -xvaf control.*
			    tar -C data -xvaf data.*
			    unpack=false
			;;
			application/x-rpm)
			    cpioArchive=$(mktemp -t "archive.XXXXXXXXXX.${templateArchive:t:r}.cpio")
			    nix shell nixos#busybox --command rpm2cpio ${templateArchive} > ${cpioArchive}
			    templateArchive=${cpioArchive}
			    unpack=true
			;;
			application/x-cpio)
			    cpio --extract --make-directories --no-absolute-filenames -F ${templateArchive}
			    unpack=false
			;;
			application/pdf)
			    nix shell nixos#ghostscript nixos#imagemagick -c convert -verbose -density 400 ${templateArchive} ${modifyPDF:t:r}_%0d.png
			    unpack=false
			;;
			*)
			    tar -xvaf ${templateArchive}
			    unpack=false
			;;
		    esac
		done
            )
        fi


        # if [[ -n ${notmuchMsg} ]]; then
        #     getMimeTypes() {
        #         nix-shell -p mailcap --run "find \${buildInputs} -path '*/etc/mime.types' | head -n 1 | xargs -- cat"
        #     }

        #     typeset -a messages
        #     messages=(${(z)$(notmuch search --output=messages ${notmuchMsg})})

        #     for message (${messages}); do
        #       typeset -A notmuchAtts
        #       notmuchAtts=()

        #       while IFS= read -r -d $'\n' line; do
        #           [[ ${line} =~ '(attachment|part)\{ ID: ([0-9]+)' ]] || continue
        #           attId=${match[2]}

        #           [[ ${line} =~ 'Content-type: multipart/' ]] && continue

        #           fName="part_${attId}"
        #           [[ ${line} =~ 'Filename: (([^,]|,[^ ])+)' ]] && fName=${match[1]}

        #           if [[ ${#messages} -gt 1 ]]; then
        #               fName="${message}/${fName}"
        #           fi

        #           fExt="${fName:e}"
        #           [[ -n "${fExt}" ]] && fName="${fName:r}"

        #           if [[ -z "${fExt}" && ${line} =~ 'Content-type: (([^,]|,[^ ])+)$' ]]; then
        #               fExt=$(getMimeTypes | grep ${match[1]}$'\t' | head -n 1 | awk '{ print $2; }')
        #           fi

        #           mkdir -p ${fName:h}
        #           if [[ -n "${fExt}" ]]; then
        #               fName=$(mktemp -p . "${fName}.XXXXXX.${fExt}")
        #           else
        #               fName=$(mktemp -p . "${fName}.XXXXXX")
        #           fi

        #           notmuchAtts[${attId}]=${fName}
        #       done <<(notmuch show --decrypt=false -- ${message} | tr -d $'\f')

        #       for attId fName in ${(kv)notmuchAtts}; do
        #           [[ -d ${fName:h} ]] || mkdir -p ${fName:h}
        #           printf "#%d → ‘%s’\n" "${attId}" "${fName}" >&2

        #           notmuch show --decrypt=false --part=${attId} -- ${message} | pv -W -D 2 -i 0.1 >${fName}
        #       done
        #     done
        # fi


        ${wormhole} && wormhole receive

	if ${quickserve}; then
	  quickserve --root . --upload . --show-hidden --tar gz
	fi


        if [[ ${#@} -eq 0 ]] || ${forceShell}; then
            if [[ ${#@} -gt 0 ]]; then
                if [[ -z ${nixShell} ]]; then
                    ${@}
                else
                    nix-shell ${nixShell} --run "${@}"
                fi
            fi

            cd $(pwd) # Needed for mounting to work

            isSingleDir() {
                typeset -a contents
                contents=(*(N) .*(N))

                if [[ ${#contents} -eq 1 && -d ${contents[1]} ]]; then
                    print ${contents[1]}
                    return 0
                else
                    return 1
                fi
            }
            while d=$(isSingleDir); do cd ${d}; done


            if [[ -z ${nixShell} ]]; then
                zsh
            else
                nix-shell ${nixShell} --run zsh
            fi
        else
            if [[ -z ${nixShell} ]]; then
                ${@}
            else
                nix-shell ${nixShell} --run "${@}"
            fi
        fi
    )
}

tmpdir() {
    cleanup()
    {
      cd /
        unmount() {
            printf "Unmounting %s\n" ${1} >&2
            fusermount -u ${1} || umount ${1} || sudo umount ${1}
        }

        if mountpoint -q -- ${dir}; then
            unmount ${dir} || return $?
        else
            while read -d $'\0' subDir; do
                mountpoint -q -- ${subDir} || continue
                unmount ${subDir} || return $?
            done <<<$(find ${dir} -xdev -type d -print0 | sort -zr)
        fi

        rm -rfv --one-file-system -- ${dir}
    }

    local tmpdir=""

    while getopts ':t:a:s:Sd:ir:wqg:p:' arg; do
        case $arg in
            "t") tmpdir="=${OPTARG}" ;;
            "?"|":") printf "Invalid option: %s\n" $arg >&2; exit 2 ;;
        esac
    done

    (
        trap cleanup EXIT


        local dir=$(mktemp -ud --tmpdir${tmpdir} ${0}.XXXXXXXXXX || return $?)

        ( dir -d ${dir} ${@} )
    )
}

inhibit-sleep() {
    if systemctl --user is-active prevent-suspend.service 1>/dev/null; then
        echo "Allowing suspend"
        systemctl --user stop prevent-suspend.service
    else
        echo "Inhibiting suspend"
        systemctl --user start prevent-suspend.service
    fi
}

qr() {
    qrencode -l M -o - -t ANSIUTF8 $@
}

clock() {
    tty-clock -sSbc -C 7 -d 0 -a 100000000
}

public-ip() {
    curl -s -H 'Accept: application/json' $@ ifconfig.co | jq -r '.ip'
}

nix-ghci() {
    pkgExpr=""
    if [[ ${#@} -gt 0 ]]; then
        pkgExpr="${1}"
        shift
    fi

    nix-shell -p "with (import <nixpkgs> {}); pkgs.haskellPackages.ghcWithPackages (p: with p; [${pkgExpr}])" --run "ghci ${@}"
}

swap() {
    f1=${1}
    f2=${2}

    if [[ -z "${f1}" || ! -e "${f1}" ]]; then
        printf "‘%s’ does not exist\n" "${f1}" >&2
        return 2
    fi
    if [[ -z "${f2}" || ! -e "${f2}" ]]; then
        printf "‘%s’ does not exist\n" "${f2}" >&2
        return 2
    fi

    tmpfile=$(mktemp --dry-run --tmpdir=${f1:h} .swap.XXXXXXXXXX)
    mv -v ${f1} ${tmpfile}
    mv -v ${f2} ${f1}
    mv -v ${tmpfile} ${f2}
}

ls() {
  exa --all --group-directories-first --colour=always $@ | less --mouse -FR
}

l() {
  ls --long --binary --git --time-style=iso --header $@
}

re() {
  systemctl --restart $@
}

ure() {
  systemctl --user --restart $@
}

u2wdb() {
  ssh -t postgres@uniworxdb2 psql uni2work
}

ssh-installer() {
  ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/gkleen@sif.midgard.yggdrasil $@
}

dichotomic_search() {
  min=$1; shift
  max=$1; shift

  while [[ $min -lt $max ]]; do
    # Compute the mean between min and max, rounded up to the superior unit
    current=$(( (min + max + 1 ) / 2 ))
    if $@ $current; then
      min=$current
    else
      max=$((current - 1))
    fi
  done

  echo $min
}

while read -r -d $'\0' d _ s; do
  hash -d ${s}=${d}
done < <(find ~/projects ~/uni -regextype posix-extended -maxdepth 2 -type d -regex '\S+/[0-9]{2}[ws]/\S+' -print0 2>/dev/null | \
       sed -zr 's|(.*/([0-9]{2}[ws])/(.+))|\1 \2 \3|' | \
       sort -z -r -k2 | sort -z -s -k3 | uniq -z -f 2)

alias '..'='cd ..'
alias rzadm=$'tmpdir -i sh -c \'mkdir adm; sshfs gkleen@mgmt01:/adm adm\''
alias -g L='| less'
alias -g S='&> /dev/null'
alias -g G='| grep'
alias -g B='&> /dev/null &'
alias -g BB='&> /dev/null &!'

export DEFAULT_USER=gkleen
export EDITOR=emacsclient

bindkey -e
bindkey ';5C' emacs-forward-word
bindkey ';5D' emacs-backward-word
bindkey '^[[1;5C' emacs-forward-word
bindkey '^[[1;5D' emacs-backward-word