diff options
Diffstat (limited to 'accounts/gkleen@sif/zshrc')
| -rw-r--r-- | accounts/gkleen@sif/zshrc | 410 |
1 files changed, 410 insertions, 0 deletions
diff --git a/accounts/gkleen@sif/zshrc b/accounts/gkleen@sif/zshrc new file mode 100644 index 00000000..d4d75073 --- /dev/null +++ b/accounts/gkleen@sif/zshrc | |||
| @@ -0,0 +1,410 @@ | |||
| 1 | filebin() { | ||
| 2 | basePath=/srv/www/files | ||
| 3 | ssh ymir find /srv/www/files -type f -printf "$'%T@ %TY-%Tm-%TdT%TH:%TM %P\\\\0'" | sort -zn | cut -z -d ' ' -f 2- \ | ||
| 4 | | while IFS= read -r -d $'\0' l; do | ||
| 5 | IFS=' ' read -r t p <<<"${l}" | ||
| 6 | printf "%s https://f.141.li/%s\n" "${t}" "${p}" | ||
| 7 | done | ||
| 8 | } | ||
| 9 | |||
| 10 | push2bin() { | ||
| 11 | if [[ ${#@} -eq 1 && ! -r ${1} ]]; then | ||
| 12 | uux -p 'ymir!push2bin' $(echo -n "${1:t}" | tr -c $'[:alnum:]+-=.' '_') | ||
| 13 | else | ||
| 14 | for f (${@}); do | ||
| 15 | uux -p 'ymir!push2bin' $(echo -n "${f:t}" | tr -c $'[:alnum:]+-=.' '_') <${f} | ||
| 16 | done | ||
| 17 | fi | ||
| 18 | } | ||
| 19 | |||
| 20 | genmail() { | ||
| 21 | local baseName="" | ||
| 22 | local target="" | ||
| 23 | if [[ ${#@} -ge 1 ]]; then | ||
| 24 | target=${1} | ||
| 25 | shift | ||
| 26 | fi | ||
| 27 | |||
| 28 | if [[ ${#@} -ge 1 ]]; then | ||
| 29 | baseName=$(pwgen ${@}) | ||
| 30 | else | ||
| 31 | baseName=$(pwgen -v -A -0 16 1) | ||
| 32 | fi | ||
| 33 | baseName=$(tr -cd $'[:alnum:]!#$%&*+-/=?^_{|}~.' <<<${baseName}) | ||
| 34 | address=${baseName}@141.li | ||
| 35 | insertAddr() { | ||
| 36 | echo "${baseName} gkleen" | ssh ymir tee -a /srv/mail/spm 1>/dev/null \ | ||
| 37 | } | ||
| 38 | |||
| 39 | printf "%s\n" ${address} | ||
| 40 | read -q 'cont?Continue [y/N]? ' || return | ||
| 41 | |||
| 42 | insertAddr | ||
| 43 | } | ||
| 44 | |||
| 45 | s() { | ||
| 46 | dir=$(pwd) | ||
| 47 | [[ ${#@} -ge 1 ]] && dir=$1 | ||
| 48 | |||
| 49 | shellFile=$(findNix ${@}) | ||
| 50 | [[ ${#@} -ge 1 ]] && shift | ||
| 51 | |||
| 52 | typeset -a cmd | ||
| 53 | if [[ -d ${dir}/.nix-gc-roots ]]; then | ||
| 54 | cmd=(persistent-nix-shell ${shellFile} ${S_EXTRA_ARGS} ${@}) | ||
| 55 | else | ||
| 56 | cmd=(nix-shell ${shellFile} ${S_EXTRA_ARGS} ${@}) | ||
| 57 | fi | ||
| 58 | |||
| 59 | if [[ -n "${S_SYSTEMD}" ]]; then | ||
| 60 | systemd-run --user --slice=development.slice --collect -E PATH=${PATH} -p WorkingDirectory=${dir} -- ${cmd} | ||
| 61 | else | ||
| 62 | ( | ||
| 63 | cd ${dir} | ||
| 64 | |||
| 65 | exec ${cmd} | ||
| 66 | ) | ||
| 67 | fi | ||
| 68 | } | ||
| 69 | |||
| 70 | sz() { | ||
| 71 | typeset -a S_EXTRA_ARGS | ||
| 72 | S_EXTRA_ARGS=(--run "env __ETC_ZSHENV_SOURCED=1 zsh") s ${@} | ||
| 73 | } | ||
| 74 | st() { | ||
| 75 | typeset -a S_EXTRA_ARGS | ||
| 76 | S_EXTRA_ARGS=(--run "tmux new-session env __ETC_ZSHENV_SOURCED=1 zsh") s ${@} | ||
| 77 | } | ||
| 78 | stt() { | ||
| 79 | typeset -a S_EXTRA_ARGS | ||
| 80 | S_SYSTEMD=true S_EXTRA_ARGS=(--run "urxvt -e tmux -S .tmux.sock new-session env __ETC_ZSHENV_SOURCED=1 zsh") s ${@} | ||
| 81 | } | ||
| 82 | se() { | ||
| 83 | typeset -a S_EXTRA_ARGS | ||
| 84 | S_SYSTEMD=true S_EXTRA_ARGS=(--run "emacs") s ${@} | ||
| 85 | } | ||
| 86 | |||
| 87 | findNix() { | ||
| 88 | if [[ $#@ -eq 0 ]]; then | ||
| 89 | findNix $(pwd) | ||
| 90 | elif [[ -f "$1" ]]; then | ||
| 91 | print ${1:a} | ||
| 92 | elif [[ -d "$1" && -f "$1"/shell.nix ]]; then | ||
| 93 | print ${1:a}/shell.nix | ||
| 94 | elif [[ -d "$1" && -f "$1"/default.nix ]]; then | ||
| 95 | print ${1:a}/default.nix | ||
| 96 | elif [[ -d "$1" && "$1" != "/" ]]; then | ||
| 97 | findNix ${1:h} | ||
| 98 | else | ||
| 99 | printf "Traversed directories to ‘/’ and found no shell specification\n" >&2 | ||
| 100 | return 1 | ||
| 101 | fi | ||
| 102 | } | ||
| 103 | |||
| 104 | dir() { | ||
| 105 | curlArchive=false | ||
| 106 | templateArchive="" | ||
| 107 | repoUrl="" | ||
| 108 | nixShell="" | ||
| 109 | findNix=false | ||
| 110 | dir="" | ||
| 111 | forceShell=false | ||
| 112 | wormhole=false | ||
| 113 | gitWorktree="" | ||
| 114 | notmuchMsg="" | ||
| 115 | quickserve=false | ||
| 116 | |||
| 117 | while getopts ':t:a:s:Sd:ir:wqg:n:' arg; do | ||
| 118 | case $arg in | ||
| 119 | "t") ;; | ||
| 120 | "a") | ||
| 121 | if [[ ${OPTARG} =~ "://" ]]; then | ||
| 122 | templateArchive=${OPTARG} | ||
| 123 | curlArchive=true | ||
| 124 | else | ||
| 125 | templateArchive=${OPTARG:a} | ||
| 126 | fi | ||
| 127 | ;; | ||
| 128 | "s") nixShell=${OPTARG:a} ;; | ||
| 129 | "S") findNix=true ;; | ||
| 130 | "d") dir=${OPTARG} ;; | ||
| 131 | "i") forceShell=true ;; | ||
| 132 | "r") repoUrl=${OPTARG} ;; | ||
| 133 | "w") wormhole=true ;; | ||
| 134 | "g") gitWorktree=${OPTARG} ;; | ||
| 135 | "n") notmuchMsg=${OPTARG} ;; | ||
| 136 | "q") quickserve=true ;; | ||
| 137 | *) printf "Invalid option: %s\n" $arg >&2; exit 2 ;; | ||
| 138 | esac | ||
| 139 | done | ||
| 140 | |||
| 141 | shift $((OPTIND - 1)) | ||
| 142 | |||
| 143 | if [[ -z ${dir} && ${#@} -ge 1 ]]; then | ||
| 144 | dir=${1} | ||
| 145 | shift | ||
| 146 | fi | ||
| 147 | |||
| 148 | [[ -n ${dir} ]] || return 2; | ||
| 149 | |||
| 150 | if [[ ! -e ${dir} ]]; then | ||
| 151 | if [[ -z "${gitWorktree}" ]]; then | ||
| 152 | mkdir -vp ${dir} | ||
| 153 | else | ||
| 154 | git -C ${gitWorktree} worktree add ${dir} | ||
| 155 | fi | ||
| 156 | else | ||
| 157 | gitWorktree="" | ||
| 158 | fi | ||
| 159 | |||
| 160 | ( | ||
| 161 | cd ${dir} | ||
| 162 | export dir; | ||
| 163 | |||
| 164 | ${findNix} && { nixShell=$(findNix) || return $? } | ||
| 165 | |||
| 166 | [[ -n ${repoUrl} ]] && git clone -- ${repoUrl} . | ||
| 167 | |||
| 168 | if [[ -n ${templateArchive} ]]; then | ||
| 169 | ( | ||
| 170 | archiveFile="" | ||
| 171 | cleanup() { | ||
| 172 | [[ -n "${archiveFile}" ]] && rm -fv ${archiveFile} | ||
| 173 | } | ||
| 174 | trap cleanup EXIT | ||
| 175 | |||
| 176 | if ${curlArchive}; then | ||
| 177 | archiveFile=$(mktemp -t "archive.XXXXXXXXXX.${templateArchive:t}") | ||
| 178 | |||
| 179 | curl -L -o ${archiveFile} ${templateArchive} | ||
| 180 | |||
| 181 | templateArchive=${archiveFile} | ||
| 182 | fi | ||
| 183 | |||
| 184 | case $(file --brief --mime-type ${templateArchive}) in | ||
| 185 | application/zip) unzip ${templateArchive} ;; | ||
| 186 | *) tar -xvaf ${templateArchive} ;; | ||
| 187 | esac | ||
| 188 | ) | ||
| 189 | fi | ||
| 190 | |||
| 191 | |||
| 192 | if [[ -n ${notmuchMsg} ]]; then | ||
| 193 | getMimeTypes() { | ||
| 194 | nix-shell -p mailcap --run "find \${buildInputs} -path '*/etc/mime.types' | head -n 1 | xargs -- cat" | ||
| 195 | } | ||
| 196 | |||
| 197 | typeset -a messages | ||
| 198 | messages=(${(z)$(notmuch search --output=messages ${notmuchMsg})}) | ||
| 199 | |||
| 200 | for message (${messages}); do | ||
| 201 | typeset -A notmuchAtts | ||
| 202 | notmuchAtts=() | ||
| 203 | |||
| 204 | while IFS= read -r -d $'\n' line; do | ||
| 205 | [[ ${line} =~ '(attachment|part)\{ ID: ([0-9]+)' ]] || continue | ||
| 206 | attId=${match[2]} | ||
| 207 | |||
| 208 | [[ ${line} =~ 'Content-type: multipart/' ]] && continue | ||
| 209 | |||
| 210 | fName="part_${attId}" | ||
| 211 | [[ ${line} =~ 'Filename: (([^,]|,[^ ])+)' ]] && fName=${match[1]} | ||
| 212 | |||
| 213 | if [[ ${#messages} -gt 1 ]]; then | ||
| 214 | fName="${message}/${fName}" | ||
| 215 | fi | ||
| 216 | |||
| 217 | fExt="${fName:e}" | ||
| 218 | [[ -n "${fExt}" ]] && fName="${fName:r}" | ||
| 219 | |||
| 220 | if [[ -z "${fExt}" && ${line} =~ 'Content-type: (([^,]|,[^ ])+)$' ]]; then | ||
| 221 | fExt=$(getMimeTypes | grep ${match[1]}$'\t' | head -n 1 | awk '{ print $2; }') | ||
| 222 | fi | ||
| 223 | |||
| 224 | mkdir -p ${fName:h} | ||
| 225 | if [[ -n "${fExt}" ]]; then | ||
| 226 | fName=$(mktemp -p . "${fName}.XXXXXX.${fExt}") | ||
| 227 | else | ||
| 228 | fName=$(mktemp -p . "${fName}.XXXXXX") | ||
| 229 | fi | ||
| 230 | |||
| 231 | notmuchAtts[${attId}]=${fName} | ||
| 232 | done <<(notmuch show --decrypt=false -- ${message} | tr -d $'\f') | ||
| 233 | |||
| 234 | for attId fName in ${(kv)notmuchAtts}; do | ||
| 235 | [[ -d ${fName:h} ]] || mkdir -p ${fName:h} | ||
| 236 | printf "#%d → ‘%s’\n" "${attId}" "${fName}" >&2 | ||
| 237 | |||
| 238 | notmuch show --decrypt=false --part=${attId} -- ${message} | pv -W -D 2 -i 0.1 >${fName} | ||
| 239 | done | ||
| 240 | done | ||
| 241 | fi | ||
| 242 | |||
| 243 | |||
| 244 | ${wormhole} && wormhole receive | ||
| 245 | |||
| 246 | if ${quickserve}; then | ||
| 247 | quickserve --root . --upload . --show-hidden --tar gz | ||
| 248 | fi | ||
| 249 | |||
| 250 | |||
| 251 | if [[ ${#@} -eq 0 ]] || ${forceShell}; then | ||
| 252 | if [[ ${#@} -gt 0 ]]; then | ||
| 253 | if [[ -z ${nixShell} ]]; then | ||
| 254 | ${@} | ||
| 255 | else | ||
| 256 | nix-shell ${nixShell} --run "${@}" | ||
| 257 | fi | ||
| 258 | fi | ||
| 259 | |||
| 260 | cd $(pwd) # Needed for mounting to work | ||
| 261 | |||
| 262 | isSingleDir() { | ||
| 263 | typeset -a contents | ||
| 264 | contents=(*(N) .*(N)) | ||
| 265 | |||
| 266 | if [[ ${#contents} -eq 1 && -d ${contents[1]} ]]; then | ||
| 267 | print ${contents[1]} | ||
| 268 | return 0 | ||
| 269 | else | ||
| 270 | return 1 | ||
| 271 | fi | ||
| 272 | } | ||
| 273 | while d=$(isSingleDir); do cd ${d}; done | ||
| 274 | |||
| 275 | |||
| 276 | if [[ -z ${nixShell} ]]; then | ||
| 277 | exec -- zsh | ||
| 278 | else | ||
| 279 | exec -- nix-shell ${nixShell} --run zsh | ||
| 280 | fi | ||
| 281 | else | ||
| 282 | if [[ -z ${nixShell} ]]; then | ||
| 283 | exec -- ${@} | ||
| 284 | else | ||
| 285 | exec -- nix-shell ${nixShell} --run "${@}" | ||
| 286 | fi | ||
| 287 | fi | ||
| 288 | ) | ||
| 289 | } | ||
| 290 | |||
| 291 | tmpdir() { | ||
| 292 | cleanup() | ||
| 293 | { | ||
| 294 | cd / | ||
| 295 | unmount() { | ||
| 296 | printf "Unmounting %s\n" ${1} >&2 | ||
| 297 | fusermount -u ${1} || umount ${1} || sudo umount ${1} | ||
| 298 | } | ||
| 299 | |||
| 300 | if mountpoint -q -- ${dir}; then | ||
| 301 | unmount ${dir} || return $? | ||
| 302 | else | ||
| 303 | while read -d $'\0' subDir; do | ||
| 304 | mountpoint -q -- ${subDir} || continue | ||
| 305 | unmount ${subDir} || return $? | ||
| 306 | done <<<$(find ${dir} -xdev -type d -print0 | sort -zr) | ||
| 307 | fi | ||
| 308 | |||
| 309 | rm -rfv --one-file-system -- ${dir} | ||
| 310 | } | ||
| 311 | |||
| 312 | local tmpdir="" | ||
| 313 | |||
| 314 | while getopts ':t:a:s:Sd:ir:wqg:n:' arg; do | ||
| 315 | case $arg in | ||
| 316 | "t") tmpdir="=${OPTARG}" ;; | ||
| 317 | "?"|":") printf "Invalid option: %s\n" $arg >&2; exit 2 ;; | ||
| 318 | esac | ||
| 319 | done | ||
| 320 | |||
| 321 | ( | ||
| 322 | trap cleanup EXIT | ||
| 323 | |||
| 324 | |||
| 325 | local dir=$(mktemp -ud --tmpdir${tmpdir} ${0}.XXXXXXXXXX || return $?) | ||
| 326 | |||
| 327 | dir -d ${dir} ${@} | ||
| 328 | ) | ||
| 329 | } | ||
| 330 | |||
| 331 | inhibit-sleep() { | ||
| 332 | if systemctl --user is-active prevent-suspend.service 1>/dev/null; then | ||
| 333 | echo "Allowing suspend" | ||
| 334 | systemctl --user stop prevent-suspend.service | ||
| 335 | else | ||
| 336 | echo "Inhibiting suspend" | ||
| 337 | systemctl --user start prevent-suspend.service | ||
| 338 | fi | ||
| 339 | } | ||
| 340 | |||
| 341 | qr() { | ||
| 342 | qrencode -l M -o - -t ANSIUTF8 $@ | ||
| 343 | } | ||
| 344 | |||
| 345 | clock() { | ||
| 346 | tty-clock -sSbc -C 7 -d 0 -a 100000000 | ||
| 347 | } | ||
| 348 | |||
| 349 | public-ip() { | ||
| 350 | curl -s -H 'Accept: application/json' $@ ifconfig.co | jq -r '.ip' | ||
| 351 | } | ||
| 352 | |||
| 353 | nix-ghci() { | ||
| 354 | pkgExpr="" | ||
| 355 | if [[ ${#@} -gt 0 ]]; then | ||
| 356 | pkgExpr="${1}" | ||
| 357 | shift | ||
| 358 | fi | ||
| 359 | |||
| 360 | nix-shell -p "with (import <nixpkgs> {}); pkgs.haskellPackages.ghcWithPackages (p: with p; [${pkgExpr}])" --run "ghci ${@}" | ||
| 361 | } | ||
| 362 | |||
| 363 | swap() { | ||
| 364 | f1=${1} | ||
| 365 | f2=${2} | ||
| 366 | |||
| 367 | if [[ -z "${f1}" || ! -e "${f1}" ]]; then | ||
| 368 | printf "‘%s’ does not exist\n" "${f1}" >&2 | ||
| 369 | return 2 | ||
| 370 | fi | ||
| 371 | if [[ -z "${f2}" || ! -e "${f2}" ]]; then | ||
| 372 | printf "‘%s’ does not exist\n" "${f2}" >&2 | ||
| 373 | return 2 | ||
| 374 | fi | ||
| 375 | |||
| 376 | tmpfile=$(mktemp --dry-run --tmpdir=${f1:h} .swap.XXXXXXXXXX) | ||
| 377 | mv -v ${f1} ${tmpfile} | ||
| 378 | mv -v ${f2} ${f1} | ||
| 379 | mv -v ${tmpfile} ${f2} | ||
| 380 | } | ||
| 381 | |||
| 382 | l() { | ||
| 383 | exa --binary --git --time-style=iso --long --all --header --group-directories-first --colour=always $@ | less --mouse -FR | ||
| 384 | } | ||
| 385 | |||
| 386 | re() { | ||
| 387 | systemctl --restart $@ | ||
| 388 | } | ||
| 389 | |||
| 390 | ure() { | ||
| 391 | systemctl --user --restart $@ | ||
| 392 | } | ||
| 393 | |||
| 394 | u2wdb() { | ||
| 395 | ssh -t postgres@uniworxdb2 psql uni2work | ||
| 396 | } | ||
| 397 | |||
| 398 | alias '..'='cd ..' | ||
| 399 | alias -g L='| less' | ||
| 400 | alias -g S='&> /dev/null' | ||
| 401 | alias -g G='| grep' | ||
| 402 | alias -g B='&> /dev/null &' | ||
| 403 | alias -g BB='&> /dev/null &!' | ||
| 404 | |||
| 405 | export DEFAULT_USER=gkleen | ||
| 406 | export EDITOR=emacsclient | ||
| 407 | |||
| 408 | bindkey -e | ||
| 409 | bindkey ';5C' emacs-forward-word | ||
| 410 | bindkey ';5D' emacs-backward-word \ No newline at end of file | ||
