From dda5fcc7844998c614e6cff873221b1860e381a9 Mon Sep 17 00:00:00 2001 From: anmiller Date: Tue, 12 Aug 2025 21:41:41 +0000 Subject: [PATCH] Update prompt manipulation with functions! --- bash/.bashrc | 198 ++++++++++++++++++++++++------------------------ bash/.functions | 10 +++ 2 files changed, 110 insertions(+), 98 deletions(-) diff --git a/bash/.bashrc b/bash/.bashrc index eb77333..f226930 100644 --- a/bash/.bashrc +++ b/bash/.bashrc @@ -1,104 +1,7 @@ #{- ~/.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 @@ -133,6 +36,104 @@ if [ -f "$HOME/.private_functions" ] && [ -r "$HOME/.private_functions" ]; then . "$HOME/.private_functions" fi +#{- 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_prepend update_prompt +fi + +# Continuation Prompt: "> " in cyan +export PS2="${cyan}>${no_color} " +# Debugging Prompt: "# " in blue +export PS4="${blue}#${no_color} " + +#{- HISTORY -}# + +prompt_command_prepend "history -r" +prompt_command_prepend "history -c" +prompt_command_prepend "history -w" +prompt_command_prepend "history -n" +# 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_prepend "log_bash_persistent_history" + + # Source bash completion, this file will then lazy load all other completions source_completions() { if [ -r "/usr/share/bash-completion/bash_completion" ]; then @@ -158,7 +159,8 @@ preserve_xtrace_reset() { fi } 2>/dev/null -PROMPT_COMMAND="preserve_xtrace; $PROMPT_COMMAND preserve_xtrace_reset" +prompt_command_prepend preserve_xtrace +prompt_command_append preserve_xtrace_reset if $(which direnv &> /dev/null); then eval "$(direnv hook bash)" diff --git a/bash/.functions b/bash/.functions index 5b3ebbe..9915fb3 100644 --- a/bash/.functions +++ b/bash/.functions @@ -64,3 +64,13 @@ git_prompt() fi fi } + +prompt_command_append() { + arg=${1} + PROMPT_COMMAND=${PROMPT_COMMAND:+"$PROMPT_COMMAND; "}"${arg}" +} + +prompt_command_prepend() { + arg=${1} + PROMPT_COMMAND="${arg}"${PROMPT_COMMAND:+"; $PROMPT_COMMAND"} +}