dotfiles/bash/.bashrc

209 lines
5.7 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} "
export NIX_SHELL_PS1="${blue}"'λ'"${no_color} "
if [ "${HOSTNAME}" != "crypt" ] && [ "${HOSTNAME}" != "tomb" ] || [ "${CONTAINER_ID}" != "" ] || [ "${SSH_TTY}" != "" ]; then
if [ "${CONTAINER_ID}" ]; then
OLD_HOSTNAME="${HOSTNAME}"
HOSTNAME="${CONTAINER_ID}"
fi
DIRENV_PS1="${DIRENV_PS1}${blue}${HOSTNAME} ${DIRENV_PS1}"
NIX_SHELL_PS1="${NIX_SHELL_PS1}${green}${HOSTNAME} ${NIX_SHELL_PS1}"
if [ "${CONTAINER_ID}" ]; then
PS1="${PS1}${magenta}${HOSTNAME} ${PS1}"
else
PS1="${PS1}${blue}${HOSTNAME} ${PS1}"
fi
if [ "${CONTAINER_ID}" ]; then
HOSTNAME="${OLD_HOSTNAME}"
fi
fi
export ORIG_PS1=${PS1}
if which direnv &> /dev/null; then
update_prompt() {
if [ -n "${IN_NIX_SHELL}" ]; then
PS1=${NIX_SHELL_PS1}
elif 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
if [ -r "$HOME/.config/xdg/env" ]; then
. "$HOME/.config/xdg/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
source_completions() {
# Source completions if they are available
completion_dirs=(
"/usr/share/bash-completion/completions"
"/etc/bash_completion.d"
)
cache_dir="${XDG_CACHE_HOME}/bash-completion-sourcing"
if [ "${BUILD_CACHE}" ]; then
mkdir -p "${cache_dir}"
fi
# Base completion sourcing, later completions will fail without this
if [ -r "/usr/share/bash-completion/bash_completion" ]; then
. "/usr/share/bash-completion/bash_completion"
elif [ -r "/etc/bash_completion" ]; then
. "/etc/bash_completion"
fi
# Completions from packages
export TIMEFORMAT="%R"
for completion_dir in "${completion_dirs[@]}"; do
if [ -d "${completion_dir}" ] && [ -r "${completion_dir}" ]; then
for completion_file in "${completion_dir}"/*; do
if [ "$BUILD_CACHE" ]; then
echo "Sourcing ${completion_file}"
. "${completion_file}"
timeavg=$(time (. "${completion_file}") 2>&1 1>/dev/null)
timeavg=$(echo $timeavg | cut -d '.' -f 2)
if [ "${timeavg}" -gt 100 ]; then
echo "This ones slow, adding to cache"
echo "${timeavg}" > "${cache_dir}/$(basename ${completion_file})"
elif [ "${timeavg}" -gt 10 ]; then
echo "This ones a little slow"
echo "${timeavg}"
fi
else
if ! [ -e "${cache_dir}/$(basename ${completion_file})" ]; then
. "${completion_file}"
else
:
fi
fi
done
fi
done
unset TIMEFORMAT
}
if [ "$BASH_COMPLETION_ENABLED" ]; then
source_completions
fi
# Preserve the usefulness of set -x by attempting to hide everything that
# happens in prompt_command
preserve_xtrace() {
if grep -q "x" <<< $-; then
XTRACE_WAS_SET=1
set +x
fi
} 2>/dev/null
preserve_xtrace_reset() {
if [ "${XTRACE_WAS_SET}" ]; then
set -x
unset XTRACE_WAS_SET
fi
} 2>/dev/null
PROMPT_COMMAND="preserve_xtrace; $PROMPT_COMMAND preserve_xtrace_reset"
if $(which direnv &> /dev/null); then
eval "$(direnv hook bash)"
fi