You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rwxrob-dot/.bashrc

393 lines
12 KiB
Bash

2 years ago
#!bash
2 years ago
# shellcheck disable=SC1090
case $- in
*i*) ;; # interactive
*) return ;;
2 years ago
esac
# ------------------------- distro detection -------------------------
export DISTRO
[[ $(uname -r) =~ Microsoft ]] && DISTRO=WSL2 #TODO distinguish WSL1
#TODO add the rest
# ---------------------- local utility functions ---------------------
_have() { type "$1" &>/dev/null; }
2 years ago
_source_if() { [[ -r "$1" ]] && source "$1"; }
# ----------------------- environment variables ----------------------
# (also see envx)
export LANG=en_US.UTF-8 # assuming apt install language-pack-en done
export USER="${USER:-$(whoami)}"
2 years ago
export GITUSER="$USER"
export FTP=242
export WEIGHT=83.7
export HEIGHT=174
export TZ=America/New_York
2 years ago
export REPOS="$HOME/Repos"
export GHREPOS="$REPOS/github.com/$GITUSER"
export DOTFILES="$GHREPOS/dot"
export SNIPPETS="$DOTFILES/snippets"
export HELP_BROWSER=lynx
export DESKTOP="$HOME/Desktop"
export DOCUMENTS="$HOME/Documents"
export DOWNLOADS="$HOME/Downloads"
export TEMPLATES="$HOME/Templates"
export SCRIPTS="$HOME/Scripts"
2 years ago
export PUBLIC="$HOME/Public"
export PRIVATE="$HOME/Private"
export PICTURES="$HOME/Pictures"
export MUSIC="$HOME/Music"
export VIDEOS="$HOME/Videos"
export PDFS="$HOME/usb/pdfs"
2 years ago
export VIRTUALMACHINES="$HOME/VirtualMachines"
export WORKSPACES="$HOME/Workspaces" # container home dirs for mounting
export ZETDIR="$GHREPOS/zet"
export ZETTELCASTS="$VIDEOS/ZettelCasts"
export CLIP_DIR="$VIDEOS/Clips"
export CLIP_DATA="$GHREPOS/cmd-clip/data"
export CLIP_VOLUME=0
export CLIP_SCREEN=0
export TERM=xterm-256color
export HRULEWIDTH=73
export EDITOR=vi
export VISUAL=vi
export EDITOR_PREFIX=vi
export GOPRIVATE="github.com/$GITUSER/*,gitlab.com/$GITUSER/*"
#export GOPATH="$HOME/.local/go"
2 years ago
export GOBIN="$HOME/.local/bin"
export GOPROXY=direct
export CGO_ENABLED=0
export PYTHONDONTWRITEBYTECODE=2
2 years ago
export LC_COLLATE=C
export CFLAGS="-Wall -Wextra -Werror -O0 -g -fsanitize=address -fno-omit-frame-pointer -finstrument-functions"
1 year ago
export LESS="-FXR"
2 years ago
export LESS_TERMCAP_mb="" # magenta
export LESS_TERMCAP_md="" # yellow
export LESS_TERMCAP_me="" # "0m"
export LESS_TERMCAP_se="" # "0m"
2 years ago
export LESS_TERMCAP_so="" # blue
export LESS_TERMCAP_ue="" # "0m"
2 years ago
export LESS_TERMCAP_us="" # underline
export ANSIBLE_CONFIG="$HOME/.config/ansible/config.ini"
export ANSIBLE_INVENTORY="$HOME/.config/ansible/inventory.yaml"
export ANSIBLE_LOAD_CALLBACK_PLUGINS=1
#export ANSIBLE_STDOUT_CALLBACK=json
export DOCKER_HOST=unix:///run/user/$(id -u)/docker.sock
2 years ago
[[ -d /.vim/spell ]] && export VIMSPELL=("$HOME/.vim/spell/*.add")
# ----------------------------- PostgreSQL ----------------------------
export PGDATABASE=cowork
2 years ago
# -------------------------------- gpg -------------------------------
export GPG_TTY=$(tty)
2 years ago
# ------------------------------- pager ------------------------------
if [[ -x /usr/bin/lesspipe ]]; then
export LESSOPEN="| /usr/bin/lesspipe %s"
export LESSCLOSE="/usr/bin/lesspipe %s %s"
2 years ago
fi
# ----------------------------- dircolors ----------------------------
if _have dircolors; then
if [[ -r "$HOME/.dircolors" ]]; then
eval "$(dircolors -b "$HOME/.dircolors")"
else
eval "$(dircolors -b)"
fi
2 years ago
fi
# ------------------------------- path -------------------------------
pathappend() {
declare arg
for arg in "$@"; do
test -d "$arg" || continue
PATH=${PATH//":$arg:"/:}
PATH=${PATH/#"$arg:"/}
PATH=${PATH/%":$arg"/}
export PATH="${PATH:+"$PATH:"}$arg"
done
} && export -f pathappend
2 years ago
pathprepend() {
for arg in "$@"; do
test -d "$arg" || continue
PATH=${PATH//:"$arg:"/:}
PATH=${PATH/#"$arg:"/}
PATH=${PATH/%":$arg"/}
export PATH="$arg${PATH:+":${PATH}"}"
done
} && export -f pathprepend
2 years ago
# remember last arg will be first in path
pathprepend \
"$HOME/.local/bin" \
"$HOME/.local/go/bin" \
"$HOME/.nimble/bin" \
"$GHREPOS/cmd-"* \
/usr/local/go/bin \
/usr/local/opt/openjdk/bin \
/usr/local/bin \
"$SCRIPTS"
2 years ago
pathappend \
/usr/local/opt/coreutils/libexec/gnubin \
'/mnt/c/Windows' \
'/mnt/c/Program Files (x86)/VMware/VMware Workstation' \
/mingw64/bin \
/usr/local/bin \
/usr/local/sbin \
/usr/local/games \
/usr/games \
/usr/sbin \
/usr/bin \
/snap/bin \
/sbin \
/bin
2 years ago
# ------------------------------ cdpath ------------------------------
export CDPATH=".:$GHREPOS:$DOTFILES:$REPOS:/media/$USER:$HOME"
2 years ago
# ------------------------ bash shell options ------------------------
# shopt is for BASHOPTS, set is for SHELLOPTS
shopt -s checkwinsize # enables $COLUMNS and $ROWS
2 years ago
shopt -s expand_aliases
shopt -s globstar
shopt -s dotglob
shopt -s extglob
#shopt -s nullglob # bug kills completion for some
#set -o noclobber
# -------------------------- stty annoyances -------------------------
#stty stop undef # disable control-s accidental terminal stops
stty -ixon # disable control-s/control-q tty flow control
2 years ago
# ------------------------------ history -----------------------------
export HISTCONTROL=ignoreboth
export HISTSIZE=5000
export HISTFILESIZE=10000
set -o vi
shopt -s histappend
# --------------------------- smart prompt ---------------------------
# (keeping in bashrc for portability)
PROMPT_LONG=20
PROMPT_MAX=95
PROMPT_AT=@
__ps1() {
local P='$' dir="${PWD##*/}" B countme short long double \
r='\[\e[31m\]' g='\[\e[30m\]' h='\[\e[34m\]' \
u='\[\e[33m\]' p='\[\e[34m\]' w='\[\e[35m\]' \
b='\[\e[36m\]' x='\[\e[0m\]'
[[ $EUID == 0 ]] && P='#' && u=$r && p=$u # root
[[ $PWD = / ]] && dir=/
[[ $PWD = "$HOME" ]] && dir='~'
B=$(git branch --show-current 2>/dev/null)
[[ $dir = "$B" ]] && B=.
countme="$USER$PROMPT_AT$(hostname):$dir($B)\$ "
[[ $B == master || $B == main ]] && b="$r"
[[ -n "$B" ]] && B="$g($b$B$g)"
short="$u\u$g$PROMPT_AT$h\h$g:$w$dir$B$p$P$x "
long="$g$u\u$g$PROMPT_AT$h\h$g:$w$dir$B\n$g$p$P$x "
double="$g$u\u$g$PROMPT_AT$h\h$g:$w$dir\n$g$B\n$g$p$P$x "
if ((${#countme} > PROMPT_MAX)); then
PS1="$double"
elif ((${#countme} > PROMPT_LONG)); then
PS1="$long"
else
PS1="$short"
fi
2 years ago
}
PROMPT_COMMAND="__ps1"
# ----------------------------- keyboard -----------------------------
# only works if you have X and are using graphic Linux desktop
_have setxkbmap && test -n "$DISPLAY" &&
setxkbmap -option caps:escape &>/dev/null
2 years ago
# ------------------------------ aliases -----------------------------
# (use exec scripts instead, which work from vim and subprocs)
unalias -a
8 months ago
alias todo='vi ~/.todo'
2 years ago
alias ip='ip -c'
10 months ago
alias '?'=duck
alias '??'=gpt
alias '???'=google
2 years ago
alias dot='cd $DOTFILES'
alias scripts='cd $SCRIPTS'
alias snippets='cd $SNIPPETS'
alias ls='ls -h --color=auto'
alias free='free -h'
alias tree='tree -a'
2 years ago
alias df='df -h'
alias chmox='chmod +x'
alias diff='diff --color'
2 years ago
alias sshh='sshpass -f $HOME/.sshpass ssh '
alias temp='cd $(mktemp -d)'
alias view='vi -R' # which is usually linked to vim
alias clear='printf "\e[H\e[2J"'
alias c='printf "\e[H\e[2J"'
2 years ago
alias coin="clip '(yes|no)'"
alias iam=live
2 years ago
alias neo="neo -D -c gold"
alias more="less"
alias disclaimer="clear; now; zet view disclaimer"
alias main="obs scene Main"
alias tight="obs scene Closeup"
8 months ago
alias pixel="scrcpy -t -s 1A141FDF600AJ4"
2 years ago
_have vim && alias vi=vim
# ----------------------------- functions ----------------------------
# lesscoloroff() {
# while IFS= read -r line; do
# unset ${line%%=*}
# done < <(env | grep LESS_TERM)
# } && export -f lesscoloroff
2 years ago
envx() {
local envfile="${1:-"$HOME/.env"}"
[[ ! -e "$envfile" ]] && echo "$envfile not found" && return 1
while IFS= read -r line; do
name=${line%%=*}
value=${line#*=}
[[ -z "${name}" || $name =~ ^# ]] && continue
export "$name"="$value"
done <"$envfile"
2 years ago
} && export -f envx
[[ -e "$HOME/.env" ]] && envx "$HOME/.env"
new-from() {
local template="$1"
local name="$2"
! _have gh && echo "gh command not found" && return 1
[[ -z "$name" ]] && echo "usage: $0 <name>" && return 1
[[ -z "$GHREPOS" ]] && echo "GHREPOS not set" && return 1
[[ ! -d "$GHREPOS" ]] && echo "Not found: $GHREPOS" && return 1
cd "$GHREPOS" || return 1
[[ -e "$name" ]] && echo "exists: $name" && return 1
gh repo create -p "$template" --public "$name"
gh repo clone "$name"
cd "$name" || return 1
2 years ago
}
new-bonzai() { new-from rwxrob/bonzai-example "$1"; }
2 years ago
new-cmd() { new-from rwxrob/template-bash-command "cmd-$1"; }
cdz() { cd $(zet get "$@"); }
2 years ago
2 years ago
export -f new-from new-bonzai new-cmd
2 years ago
clone() {
local repo="$1" user
local repo="${repo#https://github.com/}"
local repo="${repo#git@github.com:}"
if [[ $repo =~ / ]]; then
user="${repo%%/*}"
else
user="$GITUSER"
[[ -z "$user" ]] && user="$USER"
fi
local name="${repo##*/}"
local userd="$REPOS/github.com/$user"
local path="$userd/$name"
[[ -d "$path" ]] && cd "$path" && return
mkdir -p "$userd"
cd "$userd"
echo gh repo clone "$user/$name" -- --recurse-submodule
gh repo clone "$user/$name" -- --recurse-submodule
cd "$name"
2 years ago
} && export -f clone
# ------------- source external dependencies / completion ------------
# for mac
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
2 years ago
owncomp=(
pdf zet keg kn yt gl auth pomo config live iam sshkey ws x clip
./build build b ./k8sapp k8sapp ./setup ./cmd run ./run
foo ./foo cmds ./cmds z bonzai openapi obs
2 years ago
)
for i in "${owncomp[@]}"; do complete -C "$i" "$i"; done
_have gh && . <(gh completion -s bash)
_have z && . <(z completion bash)
_have glow && . <(glow completion bash)
_have goreleaser && . <(goreleaser completion bash 2>/dev/null)
_have klogin && . <(klogin completion bash 2>/dev/null)
2 years ago
_have pandoc && . <(pandoc --bash-completion)
_have kubectl && . <(kubectl completion bash 2>/dev/null)
_have k && complete -o default -F __start_kubectl k
8 months ago
_have istioctl && . <(istioctl completion bash 2>/dev/null)
2 years ago
#_have clusterctl && . <(clusterctl completion bash)
_have kind && . <(kind completion bash)
_have cobra && . <(cobra completion bash)
2 years ago
_have kompose && . <(kompose completion bash)
_have helm && . <(helm completion bash)
_have minikube && . <(minikube completion bash)
_have conftest && . <(conftest completion bash)
_have mk && complete -o default -F __start_minikube mk
_have podman && _source_if "$HOME/.local/share/podman/completion" # d
_have docker && _source_if "$HOME/.local/share/docker/completion" # d
_have docker-compose && complete -F _docker_compose dc # dc
2 years ago
_have ansible && . <(register-python-argcomplete3 ansible)
_have ansible-config && . <(register-python-argcomplete3 ansible-config)
_have ansible-console && . <(register-python-argcomplete3 ansible-console)
_have ansible-doc && . <(register-python-argcomplete3 ansible-doc)
_have ansible-galaxy && . <(register-python-argcomplete3 ansible-galaxy)
_have ansible-inventory && . <(register-python-argcomplete3 ansible-inventory)
_have ansible-playbook && . <(register-python-argcomplete3 ansible-playbook)
_have ansible-pull && . <(register-python-argcomplete3 ansible-pull)
_have ansible-vault && . <(register-python-argcomplete3 ansible-vault)
#_have ssh-agent && test -z "$SSH_AGENT_PID" && . <(ssh-agent)
2 years ago
# -------------------- personalized configuration --------------------
2 years ago
_source_if "$HOME/.bash_personal"
_source_if "$HOME/.bash_private"
_source_if "$HOME/.bash_work"
_have terraform && complete -C /usr/bin/terraform terraform
_have terraform && complete -C /usr/bin/terraform tf
1 month ago
# ------------------------- NVM bullshit ahead ------------------------
# (keep as is or nvm idiotic installer will re-add to bashrc next time)
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion