dotfiles/bash/.bashrc

118 lines
3.2 KiB
Bash

#{- ~/.bashrc -}#
#{- PROMPT -}#
if which tput &> /dev/null && tput bold &> /dev/null; then
red="\001$(tput bold)$(tput setaf 1)\002"
green="\001$(tput bold)$(tput setaf 2)\002"
yellow="\001$(tput bold)$(tput setaf 3)\002"
blue="\001$(tput bold)$(tput setaf 4)\002"
magenta="\001$(tput bold)$(tput setaf 5)\002"
cyan="\001$(tput bold)$(tput setaf 6)\002"
no_color="\001$(tput sgr0)\002"
else
red="\[\033[1;31m\]"
green="\[\033[1;32m\]"
yellow="\[\033[1;33m\]"
blue="\[\033[1;34m\]"
magenta="\[\033[1;35m\]"
cyan="\[\033[1;36m\]"
no_color="\[\033[0m\]"
fi
export PS1="${green}"'\$'"${no_color} "
export DIRENV_PS1="${red}"'\$'"${no_color} "
if [ "${HOSTNAME}" != "crypt" ] && [ "${HOSTNAME}" != "tomb" ] || [ "${SSH_TTY}" != "" ]; then
DIRENV_PS1="${DIRENV_PS1}${blue}${HOSTNAME} ${DIRENV_PS1}"
PS1="${PS1}${blue}${HOSTNAME} ${PS1}"
fi
export ORIG_PS1=${PS1}
if which direnv &> /dev/null; then
update_prompt() {
if direnv status | grep -iEq 'RC allowed (true|0)'; then
PS1=${DIRENV_PS1}
else
PS1=${ORIG_PS1}
fi
}
PROMPT_COMMAND="update_prompt; $PROMPT_COMMAND"
fi
# Continuation Prompt: "> " in cyan
export PS2="${cyan}>${no_color} "
# Debugging Prompt: "# " in blue
export PS4="${blue}#${no_color} "
#{- HISTORY -}#
PROMPT_COMMAND="history -n; history -w; history -c; history -r; $PROMPT_COMMAND"
# PROMPT_COMMAND="echo hist1; history; history -n; echo hist2; history; history -w; echo hist3; history; history -c; echo hist4; history; history -r; echo hist5; history; $PROMPT_COMMAND"
export HISTFILE="$HOME/.history/bash"
export HISTTIMEFORMAT="%F %T "
export HISTCONTROL="erasedups:ignoreboth"
export HISTFILESIZE=-1
export HISTSIZE=-1
export PERSISTENT_HISTFILE="${HOME}/.history/bash_persistent_history"
log_bash_persistent_history()
{
local rc=$?
[[
$(history 1) =~ ^\ *[0-9]+\ +([^\ ]+\ [^\ ]+)\ +(.*)$
]]
local date_part="${BASH_REMATCH[1]}"
local command_part="${BASH_REMATCH[2]}"
# Ensure file exists
if ! [ -w "$(dirname ${PERSISTENT_HISTFILE})" ]; then
mkdir -p $(dirname ${PERSISTENT_HISTFILE})
fi
if [ "$command_part" != "$PERSISTENT_HISTORY_LAST" ]
then
echo $date_part "|" "$rc" "|" "$command_part" >> "${PERSISTENT_HISTFILE}"
export PERSISTENT_HISTORY_LAST="$command_part"
fi
}
PROMPT_COMMAND="log_bash_persistent_history; $PROMPT_COMMAND"
#{- SOURCING -}#
# Source profile, if it hasn't already been loaded
if [ -z "$__PROFILE_DONE" ]; then
. $HOME/.profile
fi
# Source .env to extend with secret/sensitive environment variables if it exists
if [ -r "$HOME/.env" ]; then
. "$HOME/.env"
fi
# Source aliases, if they exist
if [ -f "$HOME/.aliases" ] && [ -r "$HOME/.aliases" ]; then
. "$HOME/.aliases"
fi
# Source private aliases, if they exist
if [ -f "$HOME/.private_aliases" ] && [ -r "$HOME/.private_aliases" ]; then
. "$HOME/.private_aliases"
fi
# Source functions, if they exist
if [ -f "$HOME/.functions" ] && [ -r "$HOME/.functions" ]; then
. "$HOME/.functions"
fi
# Source private functions, if they exist
if [ -f "$HOME/.private_functions" ] && [ -r "$HOME/.private_functions" ]; then
. "$HOME/.private_functions"
fi
if $(which direnv &> /dev/null); then
eval "$(direnv hook bash)"
fi