2021-07-30 12:37:31 -07:00
|
|
|
#!/usr/bin/env false
|
2024-02-24 09:02:36 -08:00
|
|
|
# for busybox ash, dash, bash, and zsh.
|
2021-10-29 06:03:39 -07:00
|
|
|
|
2024-02-24 09:00:39 -08:00
|
|
|
unset FANCY; _=: && [ -z $_ ] && FANCY=1 || FANCY=0 # detect zsh and bash
|
|
|
|
if [ "$FANCY" = 0 ]; then . ~/.prep; fi # handle stuff like /etc/profile and $PATH
|
2013-06-28 05:22:14 -07:00
|
|
|
|
2024-07-21 13:24:19 -07:00
|
|
|
# combine and load everything containing "YES_MYSHELL" in ~/sh/:
|
2024-07-21 13:45:16 -07:00
|
|
|
[ -z "${0##/*}" ] && sh=zsh || sh="${0#-}" # zsh replaces $0, so hardcode it
|
2024-07-21 13:24:19 -07:00
|
|
|
{ [ -d "${XDG_CACHE_HOME:="$HOME/.cache"}" ] || mkdir "$XDG_CACHE_HOME"; } &&
|
|
|
|
{ [ -d "$XDG_CACHE_HOME/shrc" ] || mkdir "$XDG_CACHE_HOME/shrc"; } &&
|
|
|
|
cache="$XDG_CACHE_HOME/shrc/$sh-$$" &&
|
|
|
|
if [ -s ~/sh/recombine ] && (. ~/sh/recombine) > "$cache"; then
|
|
|
|
#wc -l "$cache" >&2
|
|
|
|
preload=:; . "$cache"; unset preload
|
2024-07-21 13:05:32 -07:00
|
|
|
fi
|
2024-07-21 13:24:19 -07:00
|
|
|
! [ -f "$cache" ] || rm "$cache"
|
|
|
|
unset cache
|
2024-07-21 13:05:32 -07:00
|
|
|
|
2019-06-12 18:11:23 -07:00
|
|
|
# {{{1 utilities
|
2013-06-28 05:22:14 -07:00
|
|
|
|
2024-02-24 08:34:23 -08:00
|
|
|
have() { if [ -z "$ZSH_VERSION" ]; then which -- "$1"; else whence -p -- "$1"; fi; } 2>/dev/null
|
|
|
|
has() { have "$@"; } >/dev/null
|
2021-10-14 13:56:10 -07:00
|
|
|
|
2024-07-21 21:48:49 -07:00
|
|
|
if has sudo; then
|
|
|
|
doas() {
|
|
|
|
printf '\033[7m %s \033[m\n' 'warning: you ran sudo when you meant doas!'
|
|
|
|
sudo "$@"
|
|
|
|
}
|
|
|
|
maybesudo() { sudo "$@"; }
|
|
|
|
elif has doas; then
|
|
|
|
sudo() {
|
|
|
|
printf '\033[7m %s \033[m\n' 'warning: you ran doas when you meant sudo!'
|
|
|
|
doas "$@"
|
|
|
|
}
|
|
|
|
maybesudo() { doas "$@"; }
|
|
|
|
else
|
|
|
|
maybesudo() (__maybesudo "$@")
|
|
|
|
fi
|
2019-06-12 18:11:23 -07:00
|
|
|
|
2021-09-25 04:04:41 -07:00
|
|
|
ADDPATH() { ### @- append a directory to `$PATH` if it isn't already present.
|
2024-02-24 02:44:42 -08:00
|
|
|
[ $# = 1 ] || { printf >&2 'ADDPATH: expected exactly 1 argument, got %s\n' $#; return 64; }
|
|
|
|
[ "$1" = "${1#[A-Z]:\\}" ] || set -- "$(cygpath -u "$1" || printf '%s\n' "$1")"
|
2022-10-09 14:52:44 -07:00
|
|
|
set -- "$(readlink -f "$1")" "$1"
|
|
|
|
if ! [ -d "$1" ]; then
|
|
|
|
if [ -e "$1" ]; then
|
|
|
|
printf 'ADDPATH: not a directory: %s\n' "$1" >&2
|
|
|
|
else
|
|
|
|
set -- "$2" "$( (true <"$2") 2>&1)"
|
|
|
|
[ -z "$ZSH_VERSION" ] || 2="${2%: *}"
|
|
|
|
printf 'ADDPATH: %s: %s\n' "$1" "${2##*: }" >&2
|
|
|
|
fi
|
2021-07-21 15:30:05 -07:00
|
|
|
return 1
|
|
|
|
fi
|
2022-10-09 14:52:44 -07:00
|
|
|
if [ ${#PATH} = 0 ]; then
|
|
|
|
PATH="$1"
|
|
|
|
return 0
|
|
|
|
fi
|
2021-10-20 12:01:09 -07:00
|
|
|
case ":$PATH:" in
|
2022-10-09 14:52:44 -07:00
|
|
|
(*":$1:"*) :;;
|
|
|
|
(*) PATH="$PATH:$1";;
|
2021-10-20 12:01:09 -07:00
|
|
|
esac
|
2019-06-12 18:11:23 -07:00
|
|
|
}
|
|
|
|
|
2022-08-29 22:06:15 -07:00
|
|
|
ROPATH() {
|
2024-02-24 02:44:42 -08:00
|
|
|
[ $# = 0 ] || { printf >&2 'ROPATH: expected exactly 0 arguments, got %s\n' $#; return 64; }
|
2022-08-29 22:06:15 -07:00
|
|
|
local segment= previous= newpath= oldpath="$PATH:"
|
|
|
|
while segment="${oldpath%%:*}"; [ "$segment" != "$oldpath" ]; do
|
|
|
|
[ -z "$segment" ] || [ -z "$previous" ] || newpath="$newpath$previous:"
|
|
|
|
[ -z "$segment" ] || previous="$segment"
|
|
|
|
oldpath="${oldpath#*:}"
|
|
|
|
done
|
|
|
|
[ -z "$previous" ] || printf 'rotated %s\n' "$previous" >&2
|
|
|
|
[ -z "$previous" ] || newpath="$previous:$newpath"
|
|
|
|
export PATH="${newpath%:}"
|
|
|
|
}
|
|
|
|
|
2019-06-12 18:11:23 -07:00
|
|
|
# {{{1 configurations
|
|
|
|
|
2021-09-25 04:04:41 -07:00
|
|
|
umask 022 # umask should be reset else pip might make faulty installations.
|
2019-06-12 18:11:23 -07:00
|
|
|
|
2024-03-25 15:32:23 -07:00
|
|
|
if [ -d "$HOME/opt/local/bin" ]; then
|
|
|
|
printf 'Warning: You have a %s directory\n consider moving it to %s\n' \
|
|
|
|
'~/opt/local/bin' '~/.local/bin' >&2
|
|
|
|
ADDPATH "$HOME/opt/local/bin"
|
|
|
|
else
|
|
|
|
ADDPATH "$HOME/.local/bin"
|
|
|
|
fi
|
2021-07-29 00:37:35 -07:00
|
|
|
|
2024-02-24 08:41:58 -08:00
|
|
|
# clean up problematic, potentially-inherited exports:
|
2016-12-09 17:19:28 -08:00
|
|
|
unset PREFIX CC CPP CXX LD CFLAGS CPPFLAGS CXXFLAGS LDFLAGS
|
2019-05-30 01:25:49 -07:00
|
|
|
unset AR RANLIB RC WINDRES OBJDUMP OBJCOPY
|
2017-01-30 13:59:28 -08:00
|
|
|
unset LD_LIBRARY_PATH
|
2024-02-24 08:56:28 -08:00
|
|
|
unset CDPATH # grief.
|
2013-06-28 05:22:14 -07:00
|
|
|
|
2021-09-25 04:04:41 -07:00
|
|
|
[ "$DESKTOP_SESSION" != xfce ] || export QT_QPA_PLATFORMTHEME="" # unhide menu bar
|
|
|
|
#[ "$TERM" != vt102 ] || export TERM="xterm" # PuTTY over serial
|
2021-10-13 21:08:31 -07:00
|
|
|
export EA_AUTH="auth"
|
|
|
|
export EA_DIR="t"
|
|
|
|
export EA_DOMAIN="https://eaguru.guru"
|
2019-07-13 08:29:03 -07:00
|
|
|
export EDITOR=vim
|
2021-10-29 06:23:25 -07:00
|
|
|
export ENV="$HOME/.shrc" # for dash and ash
|
2024-07-19 23:00:31 -07:00
|
|
|
export LESS='-QRSci'
|
2019-07-13 08:29:45 -07:00
|
|
|
export PAGER=less
|
2021-09-25 04:04:41 -07:00
|
|
|
export PYTHONIOENCODING=utf-8 # damnit python!
|
|
|
|
export SSH_AUTH_SOCK=0 # more info: https://0x0.st/NUnw
|
|
|
|
export TZ=':/etc/localtime' # more info: https://0x0.st/NUnv
|
2013-06-28 05:22:14 -07:00
|
|
|
|
2021-09-25 04:04:41 -07:00
|
|
|
# $LANG has been my bane whenever i forget to set it properly, so check it.
|
2019-06-12 18:11:23 -07:00
|
|
|
if [ "$LANG" != "en_US.UTF-8" ] && [ "$LANG" != "en_CA.UTF-8" ]; then
|
|
|
|
if [ -z "$MSYSTEM" ] || [ "$LANG" != "C.UTF-8" ]; then
|
2021-09-25 04:04:41 -07:00
|
|
|
printf 'Warning: LANG is %s\n' "${LANG:-empty!}" >&2
|
2019-06-12 18:11:23 -07:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2024-02-24 02:46:31 -08:00
|
|
|
# simple commands and aliases {{{1
|
|
|
|
|
2024-03-21 13:45:42 -07:00
|
|
|
refresh() ### @- invoke `hash -r`.
|
|
|
|
{ hash -r; }
|
|
|
|
|
2024-02-24 08:19:22 -08:00
|
|
|
pl() ### @- print each argument on its own line.
|
|
|
|
{ printf '%s\n' "$@"; }
|
|
|
|
|
2024-02-24 02:46:31 -08:00
|
|
|
### @ ll - list files verbosely, fancily, ordered, but not recursively.
|
2024-03-25 02:49:37 -07:00
|
|
|
alias ll=; unalias ll # unalias without spitting an error
|
2024-02-24 02:46:31 -08:00
|
|
|
if has lr; then
|
|
|
|
ll() { lr -1lshGG -o tev "$@" | less; }
|
|
|
|
elif has lr.com; then # https://eaguru.guru/t/lr.com
|
|
|
|
ll() { lr.com -1lshGG -o tuev "$@" | less; }
|
|
|
|
else
|
|
|
|
ll() { ls -lAX --group-directories-first --color=force "$@" | less; }
|
|
|
|
fi
|
2019-06-12 18:11:23 -07:00
|
|
|
|
2024-07-06 04:50:54 -07:00
|
|
|
if ! has busybox && has busybox-static; then
|
|
|
|
busybox() { busybox-static "$@"; }
|
|
|
|
fi
|
|
|
|
|
2024-02-24 02:49:15 -08:00
|
|
|
gdp() { ### @- invoke `gd` to diff a commit from its parent. the commit defaults to "HEAD".
|
|
|
|
local commit="${1:-HEAD}"
|
|
|
|
[ $# -le 1 ] || { printf '%s: %s\n' gdp "too many arguments" >&2; return 64; }
|
|
|
|
gd "$commit~" "$commit"
|
|
|
|
}
|
|
|
|
|
2024-02-24 08:19:22 -08:00
|
|
|
gd() ### @- invoke git's diff subcommand with fewer lines of context.
|
|
|
|
{ git diff -U2 "$@"; }
|
|
|
|
|
|
|
|
rgn() ### @- invoke ripgrep without respecting `.gitignore` files.
|
|
|
|
{ rg -u "$@"; }
|
|
|
|
|
|
|
|
ryp()
|
|
|
|
{ rg -M99 -g '*.py' "$@"; }
|
|
|
|
|
|
|
|
curls() ### @- invoke curl with less noise.
|
|
|
|
{ curl -sS "$@"; }
|
|
|
|
|
|
|
|
curLs()
|
|
|
|
{ curl -L --no-progress-meter "$@"; }
|
|
|
|
|
|
|
|
ash()
|
|
|
|
{ PS1='$ ' busybox ash "$@"; }
|
|
|
|
|
|
|
|
cort()
|
|
|
|
{ LC_ALL=C sort "$@"; }
|
|
|
|
|
|
|
|
revend() ### @- reverse the 4-byte endianness of a single file. *this is an in-place operation!*
|
|
|
|
{ objcopy -I binary -O binary --reverse-bytes=4 "$@"; }
|
|
|
|
|
|
|
|
clone() ### @- invoke rsync suitably for creating virtually indistinguishable copies of files.
|
|
|
|
{ maybesudo rsync -aHA --info=progress2 --no-i-r "$@"; }
|
|
|
|
|
|
|
|
aligntabs() ### @- align tab-delimited fields in stdin.
|
|
|
|
{ column -t -s$'\t' "$@"; }
|
|
|
|
|
2024-03-04 10:24:27 -08:00
|
|
|
__crawl() (
|
|
|
|
unset CDPATH && cd "${HOME:?\$HOME is unset}" || return
|
|
|
|
[ -d .ssh ] || install -m700 -d .ssh || return # TODO: use umask instead?
|
|
|
|
cd .ssh || return
|
|
|
|
if ! [ -s crawl ]; then grab crawl && chmod 0600 crawl || return; fi
|
2024-07-14 02:19:15 -07:00
|
|
|
TERM=xterm-256color exec ssh -F none -i crawl "$@"
|
2024-03-04 10:24:27 -08:00
|
|
|
)
|
|
|
|
|
2024-02-24 08:19:22 -08:00
|
|
|
crawla() ### @- play Dungeon Crawl: Stone Soup through ssh on the akrasiac server.
|
2024-03-04 10:24:27 -08:00
|
|
|
{ __crawl joshua@crawl.akrasiac.org "$@"; } # password is joshua
|
2024-02-24 08:19:22 -08:00
|
|
|
|
|
|
|
crawlz() ### @- play Dungeon Crawl: Stone Soup through ssh on the develz server.
|
2024-03-04 10:24:27 -08:00
|
|
|
{ __crawl crawl@crawl.develz.org "$@"; }
|
2024-02-24 08:19:22 -08:00
|
|
|
|
|
|
|
diff() ### @- use git's diff subcommand for general diffing.
|
|
|
|
{ git diff --color=auto --no-ext-diff --no-index --no-prefix "$@"; }
|
|
|
|
|
|
|
|
gc() ### @- columnize text by using git's column subcommand.
|
|
|
|
### **TODO:** consider renaming because gc(1) already exists.
|
|
|
|
{ git column --mode=dense --padding=2 "$@"; }
|
|
|
|
|
|
|
|
counts() ### @- count files in the current directory, including files found recursively.
|
|
|
|
{ find . | wc -l "$@"; }
|
|
|
|
|
|
|
|
exts() ### @- count and sort file extensions in the current directory, including files found recursively.
|
2024-05-17 20:06:30 -07:00
|
|
|
{ find -type f | sed -e 's@.*/@@' -e 's@.*\.@@' | tr A-Z a-z | scount; }
|
2024-02-24 08:19:22 -08:00
|
|
|
|
2024-07-21 21:48:49 -07:00
|
|
|
# TODO: don't use GNU options when busybox is detected!
|
2024-02-24 08:19:22 -08:00
|
|
|
nocom() ### @- strip single-line C-like and shell-like comments.
|
|
|
|
{ grep -Ev --line-buffered --color=never "^[[:space:]]*(//|#)" "$@"; }
|
|
|
|
|
|
|
|
jrep() ### @- extract strings comprised of basic ASCII or Japanese codepoints.
|
2024-05-17 20:05:57 -07:00
|
|
|
{ grep -aPo '[\x{20}-\x{7E}\x{4E00}-\x{9FFF}\x{3040}-\x{30FF}]+' "$@"; }
|
2024-02-24 08:19:22 -08:00
|
|
|
|
|
|
|
bomb() ### @- add a Byte-Order Mark to a file.
|
|
|
|
{ uconv -f utf-8 -t utf-8 --add-signature "$@"; }
|
|
|
|
|
|
|
|
cleanse() ### @- strip unprintable and non-ASCII characters.
|
2024-05-17 20:05:57 -07:00
|
|
|
{ tr -cd '\11\12\15\40-\176' "$@"; }
|
2024-02-24 08:19:22 -08:00
|
|
|
|
|
|
|
double() ### @- print every line twice. <br/> print every line twice.
|
|
|
|
### **NOTE:** there also exists a double(1) program provided by
|
|
|
|
### the *plotutils* package that i don't use.
|
|
|
|
{ awk "{print;print}" "$@"; }
|
|
|
|
|
|
|
|
katagana() ### @- convert katakana codepoints to their equivalent hiragana.
|
|
|
|
### this is occasionally useful when translating [debug text from ancient games.](https://tcrf.net/)
|
|
|
|
{ perlu -MUnicode::Normalize -pe\''$_=NFKD($_)=~y/ァ-ヶ /ぁ-ゖ /r''\' "$@"; }
|
|
|
|
|
|
|
|
makepkgf() ### @- make the freakin' package!
|
|
|
|
{ makepkg -Af --skipchecksums --skippgpcheck "$@"; }
|
|
|
|
|
|
|
|
rakef() ### @- make the freakin' gem!
|
|
|
|
{ rake && gem build *.gemspec && gem install *.gem "$@"; }
|
|
|
|
|
2024-07-01 21:58:55 -07:00
|
|
|
relog() ### @- log on again to refresh your unix groups, etc.
|
|
|
|
{ exec su - "${USER:?}"; }
|
|
|
|
|
|
|
|
carry() ### @- copy files in a plain way using rsync. affected by umask.
|
2024-07-03 00:49:26 -07:00
|
|
|
{ rsync -HOlrt --no-p --no-g --chmod=ugo=rwX "$@"; }
|
2024-07-01 21:58:55 -07:00
|
|
|
|
2024-03-21 05:24:22 -07:00
|
|
|
alias 0x0.st="oxo"
|
2024-03-21 06:35:38 -07:00
|
|
|
alias oshi.at="oshi"
|
2024-03-25 02:50:16 -07:00
|
|
|
alias pastel='pastel -f' # why would i want to use a color utility without colors?
|
2024-07-01 21:59:41 -07:00
|
|
|
alias poo="pueue"
|
2024-02-24 08:19:22 -08:00
|
|
|
alias rg="rg -M200"
|
|
|
|
|
2019-06-12 18:11:23 -07:00
|
|
|
# enable colors {{{2
|
|
|
|
|
2021-10-29 06:03:39 -07:00
|
|
|
if [ "$FANCY" = 1 ]; then
|
2021-09-25 04:04:41 -07:00
|
|
|
# busybox is smart enough to ignore --color flags when unsupported.
|
2021-09-25 19:09:30 -07:00
|
|
|
alias grep='grep --color=auto'
|
2021-09-25 04:04:41 -07:00
|
|
|
alias ls='ls --color=auto'
|
|
|
|
alias lr="lr -G"
|
2021-10-14 13:56:10 -07:00
|
|
|
#alias make="$(have colormake || have make)"
|
2021-09-25 04:04:41 -07:00
|
|
|
fi
|