dotfiles/.bashrc

225 lines
7.5 KiB
Bash

# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
init () {
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
shopt -s globstar
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
# === Configure bash history ===
# From https://stackoverflow.com/a/19533853/2759427
# Undocumented feature which sets the size to "unlimited".
# http://stackoverflow.com/questions/9457233/unlimited-bash-history
export HISTFILESIZE=
export HISTSIZE=
export HISTTIMEFORMAT="[%F %T] "
# Change the file location because certain bash sessions truncate .bash_history file upon close.
# http://superuser.com/questions/575479/bash-history-truncated-to-500-lines-on-each-login
# (but DONT export, otherwise child shells might truncate and do stuff we dont want with it)
HISTFILE=~/.bash_eternal_history
# Force prompt to write history after every command.
# http://superuser.com/questions/20900/bash-history-loss
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
HISTCONTROL=ignoredups
# append to the history file, don't overwrite it
shopt -s histappend
# === Set prompt and terminal title ===
# Prompt which is basically user@host:$PWD ending in a literal $
# \u is username
# \h is hostname up to the first '.'
# \w is $PWD
PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*|alacritty)
PS1="\[\e]0;\u@\h: \w\a\]$PS1"
;;
*)
;;
esac
# Meta (dotfiles related)
cobconf="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
export cobconf
alias cobconf="cd ${cobconf}"
# Git aliases
alias gitkcon="git log --all --graph --date=short --pretty=format:'%C(yellow)%h %C(green)%ad %C(cyan)%an%C(auto)%d%C(reset) %C(reset)%s'"
alias xclip="xclip -selection clipboard"
alias dos2unix="dos2unix --keepdate"
alias lsblk="lsblk -o name,mountpoint,model,size,type,ro,rm,maj:min" # Show model and serial by default
alias dmesgless="dmesg --color=always | less -R"
alias l="ls -lah"
# MapCast
alias mc="node --max-old-space-size=8192 ${HOME}/Seafile/projects/mapcast/monorepo/pkg/core-app-multitool/entry.ts"
alias dispose="node ${HOME}/Seafile/projects/shred-and-record/main.ts"
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
#alias dir='dir --color=auto'
#alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
# Add $HOME/bin to $PATH
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
# For nix
if [ -d "$HOME/.nix-profile/bin" ] ; then
PATH="$HOME/.nix-profile/bin:$PATH"
fi
# Old stuff on main machine where I'm not ready to use nix yet so I need to support
# old shit
if [ "$(hostname)" = "bepis" ]; then
source "${cobconf}/bepis-old-fix.sh"
fi
# Setup DOTFILES_LOG_SHELL_TO_DOCKER (when enabled), which logs the history fo the shell to
# docker's stdout (PID 1 fd 1) on every PROMPT_COMMAND.
if [ -t 0 ] && [ -n "$DOTFILES_LOG_SHELL_TO_DOCKER" ] && [ -z "$DOTFILES_HAS_SETUP_LOG_SHELL_TO_DOCKER" ]; then
export DOTFILES_HAS_SETUP_LOG_SHELL_TO_DOCKER=1
_log_cmd_first=1
# Log the command via history to docker logs (PID 1's stdout)
log_cmd() {
local rc=$? # Capture IMMEDIATELY so we don't lose it
if [ -n "$_log_cmd_first" ]; then
unset _log_cmd_first
echo "[$(date -Is)] [pid $$] entering new shell" > /proc/1/fd/1
return
fi
local cmd
cmd=$(history -w /dev/stdout | tail -n1)
echo "[$(date -Is)] [exit with $rc] [pid $$] [cwd $PWD] $cmd" > /proc/1/fd/1
}
PROMPT_COMMAND="log_cmd${PROMPT_COMMAND:+; $PROMPT_COMMAND}"
# Trap the exit to fire a separate shell exiting command
trap 'echo "[$(date -Is)] [pid $$] exiting shell" > /proc/1/fd/1' EXIT
fi
# Secrets to not save to github
if [[ -f "${cobconf}/secrets.sh" ]]; then
source ${cobconf}/secrets.sh
fi
# Tools
findcode() {
# Finds string in all code files path or contents
# egrep -I is don't match binary data
fd . ~/Seafile/projects \
--type f \
--exclude '*{.min.js,.js.map,.css.map,.min.css,-lock.json,.fbx,.dll,.exe,.mp4,.png,.jpg,.jpeg,.kra,.pdn,.zip,.7z,.meta,.gif,.tif,.tiff,.ogg,.svg}' \
--exclude '{FORKED,reveal.js-dependencies}' \
| xargs -d '\n' egrep --color --line-number -I "$1"
# Then just look at filenames (-H including hidden)
fd -H "$1" ~/Seafile/projects \
--exclude '{FORKED,reveal.js-dependencies}'
# Old iteration - find + xargs egrep
# Prune sections will prune those subtrees from search but always evaluate to false
# find ~/Seafile/projects \
# -type d \( \
# -name node_modules -o -name .git -o -name .pytest_cache -o -name dist -o \
# -name __pycache__ -o -name FORKED -o -name venv -o -name Library -o -name _nuxt -o \
# -name reveal.js-dependencies -o -name .nuxt -o -path dotfiles/deps \
# \) -prune -false -o \
# -type f -a -not \( \
# -name '*.min.js' -o -name '*.js.map' -o -name '*-lock.json' -o -name '*.fbx' -o \
# -name '*.dll' -o -name '*.exe' -o -name '*.mp4' -o -name '*.png' -o -name '*.jpg' -o \
# -name '*.jpeg' -o -name '*.kra' -o -name '*.pdn' -o -name '*.zip' -o -name '*.7z' -o \
# -name '*.meta' -o -name '*.gif' -o -name '*.tif' -o -name '*.ogg' \
# \) | xargs -d '\n' egrep -n "$1"
# Old iteration - grep
# grep --exclude-dir={node_modules,.git,dist,FORKED,venv,Library,_nuxt} --exclude={*.min.js,*.js.map,*-lock.json,*.fbx} -RnI ~/Seafile/projects -e "$1"
}
findnotes() {
grep --include={*.md,*.csv,*.pdf} -RnI ~/Seafile/notes -e "$1"
}
cdp() {
# Opens a project in the current terminal
if [[ -d "${HOME}/Seafile/projects/${1}" ]]; then
local PRJ_PATH="${HOME}/Seafile/projects/${1}"
elif [[ -d "~/Seafile/projects/COMMISIONED/${1}" ]]; then
local PRJ_PATH="${HOME}/Seafile/projects/COMMISIONED/${1}"
elif [[ -d "~/Seafile/projects/FORKED/${1}" ]]; then
echo "Project was a fork"
local PRJ_PATH="${HOME}/Seafile/projects/FORKED/${1}"
else
echo "No project '${1}' found in ~/Seafile/projects folders"
return
fi
# Got a project path, but does it have a repo/ or repository/
if [[ -d "${PRJ_PATH}/repo" ]]; then
local PRJ_PATH="${PRJ_PATH}/repo"
elif [[ -d "${PRJ_PATH}/repository" ]]; then
local PRJ_PATH="${PRJ_PATH}/repository"
fi
# Finally, cd and try to subl the project file if there is one
cd ${PRJ_PATH}
# local PRJ_FILE=$(find *.sublime-project)
# if [[ ! -z "${PRJ_FILE}" ]]; then
# subl ${PRJ_FILE}
# else
# subl .
# fi
}
# Tiny replacement for *fetch
sysinfo() {
echo "Host: $(hostname)"
echo "Distro: $(. /etc/os-release && echo "$PRETTY_NAME")"
echo "OS: $(uname -sr)"
echo "Uptime: $(uptime -p)"
echo "CPU: $(awk -F: '/model name/ {print $2; exit}' /proc/cpuinfo | sed 's/^ //')"
echo "Cores: $(nproc)"
echo "RAM: $(free -h | awk '/^Mem:/ {print $3 " / " $2}')"
echo "Disk: $(df -h / | awk 'NR==2 {print $3 " / " $2}')"
}
} # end init()
init
unset init