1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-05 15:59:04 -08:00
rc/home/shrc

246 lines
8.9 KiB
Text
Raw Normal View History

2021-07-30 12:37:31 -07:00
#!/usr/bin/env false
# for ash, dash, bash, and zsh.
_=: && [ -z $_ ] && FANCY=1 || FANCY=0 # detect zsh and bash
if [ "$FANCY" = 0 ]; then
. ~/.prep # handle boring stuff like /etc/profile and $PATH
fi
2013-06-28 05:22:14 -07:00
2019-06-12 18:11:23 -07:00
# {{{1 utilities
2013-06-28 05:22:14 -07:00
have() {
if [ -z "$ZSH_VERSION" ]; then which -- "$1"; else whence -p -- "$1"; fi
} 2>/dev/null
has() {
have "$@"
} >/dev/null
if has sudo; then
maybesudo() { sudo "$@"; }
2019-06-12 18:11:23 -07:00
else
maybesudo() { maybesudo_ "$@"; }
2019-06-12 18:11:23 -07:00
fi
ADDPATH() { ### @- append a directory to `$PATH` if it isn't already present.
[ $# = 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
return 1
fi
2022-10-09 14:52:44 -07:00
if [ ${#PATH} = 0 ]; then
PATH="$1"
return 0
fi
case ":$PATH:" in
2022-10-09 14:52:44 -07:00
(*":$1:"*) :;;
(*) PATH="$PATH:$1";;
esac
2019-06-12 18:11:23 -07:00
}
ROPATH() {
[ $# = 0 ] || { printf >&2 'ROPATH: expected exactly 0 arguments, got %s\n' $#; return 64; }
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
umask 022 # umask should be reset else pip might make faulty installations.
2019-06-12 18:11:23 -07:00
2013-07-01 02:22:36 -07:00
ADDPATH "$HOME/opt/local/bin"
2021-07-29 00:37:35 -07:00
# cleanup in case of 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
2013-06-28 05:22:14 -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"
export EDITOR=vim
export ENV="$HOME/.shrc" # for dash and ash
2024-02-24 02:47:38 -08:00
export LESS='-SRQc'
2019-07-13 08:29:45 -07:00
export PAGER=less
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
# $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
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
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.
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
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"
}
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' "$@"; }
crawla() ### @- play Dungeon Crawl: Stone Soup through ssh on the akrasiac server.
{ ssh joshua@crawl.akrasiac.org -i ~/.ssh/crawl "$@"; }
crawlz() ### @- play Dungeon Crawl: Stone Soup through ssh on the develz server.
{ ssh crawl@crawl.develz.org -i ~/.ssh/crawl "$@"; }
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.
{ find -type f | grep -o '\\.[^/.]*$' | sort | uniq -c | sort -n "$@"; }
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.
{ grep -aPo "[\x{20}-\x{7E}\x{4E00}-\x{9FFF}\x{3040}-\x{30FF}]+" "$@"; }
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.
{ tr -cd "\11\12\15\40-\176" "$@"; }
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 "$@"; }
alias rg="rg -M200"
2019-06-12 18:11:23 -07:00
# enable colors {{{2
if [ "$FANCY" = 1 ]; then
# busybox is smart enough to ignore --color flags when unsupported.
alias grep='grep --color=auto'
alias ls='ls --color=auto'
alias lr="lr -G"
#alias make="$(have colormake || have make)"
fi
2013-06-28 05:22:14 -07:00
# garbage {{{1
2024-02-24 02:54:08 -08:00
if false; then # currently in the process of nuking these
2019-06-12 18:11:23 -07:00
# just flags {{{2
2021-07-30 17:57:08 -07:00
alias fils="du -bahd1" ### @- (GNU du) display human-friendly filesizes for the files in a directory.
alias lsa="ls -A --group-directories-first" ### @- (GNU ls) list files with directories and dotfiles ordered first.
alias perlu='perl -Mopen=locale -Mutf8' ### @- invoke perl expecting files with UTF-8 encoding.
alias cms="cryptominisat5 --verb 0" ### @- invoke cryptominisat5 with less noise.
2019-06-12 18:11:23 -07:00
# git {{{2
2021-07-30 17:57:08 -07:00
alias get="git clone --single-branch --depth 1" ### @- retrieve the most recent files from the default branch of a git repository, and not much else.
alias gs='git status' ### @- invoke git's status subcommand.
### **TODO:** consider renaming because gs(1) already exists.
2021-07-30 17:57:08 -07:00
alias gds='git --no-pager diff --stat' ### @- display difference stats from git.
alias gl='git log --oneline' ### @- invoke git's log subcommand with a single line per commit.
2021-10-13 23:43:58 -07:00
### **TODO:** consider renaming because [gl(1)](https://github.com/gitless-vcs/gitless) already exists.
2021-07-30 17:57:08 -07:00
alias glo='git log --graph --decorate --pretty=oneline --abbrev-commit --all' ### @- navigate git's commit tree succinctly.
alias g1='git log --pretty=oneline --abbrev-commit --color=always | head -1' ### @- display the most recent git commit.
alias g11='git rev-parse --short HEAD'
2021-07-30 17:57:08 -07:00
alias gr='git remote -v' ### @- display remote git repositories verbosely.
alias gb='git --no-pager branch' ### @- display the current git branch.
### **NOTE:** there also exists a gb(1) program provided by
### the *gb* package that i don't use.
2013-06-28 05:22:14 -07:00
2019-06-12 18:11:23 -07:00
# being specific {{{2
2019-06-03 09:26:26 -07:00
alias pip="maybesudo -H pip3"
2019-04-29 11:47:31 -07:00
2021-08-01 10:29:38 -07:00
alias eamv='ea move' ### @- invoke [`ea move`.](#ea)
alias eacp='ea copy' ### @- invoke [`ea copy`.](#ea)
alias earm='ea delete' ### @- invoke [`ea delete`.](#ea)
2019-06-12 18:11:23 -07:00
# providing extra functionality {{{2
2021-10-06 19:20:30 -07:00
alias picky='comm -23 <(pacman -Qetq | sort -u) <(pacman -Qgq base base-devel xorg xorg-drivers xfce4 mingw-w64-x86_64-toolchain 2>/dev/null | sort -u) | tr -d "\t"' ### @-
### list pacman packages that were manually installed and are not in some common package groups.
### this list can be used to assist later reinstalls.
2022-01-08 21:15:26 -08:00
alias unused='comm -23 <(pacman -Qtq | sort) <(pacman -Qeq | sort) | tr -d "\t"' ### @-
2021-10-06 19:20:30 -07:00
### list pacman packages that weren't installed explicitly and don't satisfy any dependencies.
### this list can be used to free up some disk space.
2021-07-30 17:57:08 -07:00
fi