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