Use a function to get the colour for the username part of prompt.

This commit is contained in:
Darren 'Tadgy' Austin 2023-10-01 17:57:09 +01:00
commit b0af5d148b

30
.bashrc
View file

@ -28,6 +28,7 @@ __prompt_git_status() {
# Do nothing if there's an error.
(( ERR >= 1 )) || { (( ERR == 128 )) && (( ${#GIT_REPO_INFO[@]} != 3 )); } && return "$RET"
# FIXME: Move the git dir to status below.
# Generate the prompt.
if [[ "${GIT_REPO_INFO[2]}" == "true" ]]; then
# If in the git directory, use a special branch marker.
@ -105,6 +106,21 @@ __prompt_git_status() {
return "$RET"
}
__prompt_user_colour() {
# Determine the colour of the username in the prompt.
local RET="$?"
if [[ "$LOGNAME" == "root" ]]; then
printf "%s" "1" # Red
elif [[ "$LOGNAME" == "tadgy" ]]; then
printf "%s" "2" # Green
else
printf "%s" "6" # Cyan
fi
return "$RET"
}
__find_ssh_agent_sock() {
# Find an *active* ssh agent socket.
# Returns: 0 = Found an active socket.
@ -344,15 +360,6 @@ HISTSIZE=-1
HISTTIMEFORMAT="%d/%m/%y %H:%M:%S "
history -r
# Determine the colour of the username in the prompt.
if [[ "$LOGNAME" == "root" ]]; then
COLOUR=1 # Red
elif [[ "$LOGNAME" == "tadgy" ]]; then
COLOUR=2 # Green
else
COLOUR=5 # Purple
fi
# The commands to execute before the prompt is displayed.
PROMPT_COMMAND="__ssh_agent_prompt_command"
@ -373,14 +380,13 @@ if (( BASH_VERSINFO[0] >= 4 )); then
# Trim the path in the prompt.
PROMPT_DIRTRIM=2
# Coloured username + host + directory:
PS1='[\[$(tput bold)$(tput setaf "$COLOUR")\]\u\[$(tput sgr0)\]@\[$(tput bold)$(tput setaf 3)\]\h\[$(tput sgr0)\]] \[$(tput bold)$(tput setaf 4)\]\w\[$(tput sgr0)\]$(__prompt_git_status "\[\033[0;35;40m\] (%s)\[\033[0;37;40m\]") ->'
PS1='[\[$(tput bold)$(tput setaf "$(__prompt_user_colour)")\]\u\[$(tput sgr0)\]@\[$(tput bold)$(tput setaf 3)\]\h\[$(tput sgr0)\]] \[$(tput bold)$(tput setaf 4)\]\w\[$(tput sgr0)\]$(__prompt_git_status "\[\033[0;35;40m\] (%s)\[\033[0;37;40m\]") ->'
else
# Set the prompts.
# Coloured username + host + directory:
# shellcheck disable=SC2154
PS1="[\[$(tput bold)$(tput setaf "$COLOUR")\]\u\[$(tput sgr0)\]@\[$(tput bold)$(tput setaf 3)\]\h\[$(tput sgr0)\]] \[$(tput bold)$(tput setaf 4)\]\$(echo \"\${PWD/#\$HOME/~}\" | awk -F/ '{if (NF>3) {printf \".../\" \$(NF-1) \"/\" \$NF} else {printf \$0}}')\[$(tput sgr0)\]$(__prompt_git_status "\[\033[0;35;40m\] (%s)\[\033[0;37;40m\]") ->"
PS1='[\[$(tput bold)$(tput setaf "$(__prompt_user_colour)")\]\u\[$(tput sgr0)\]@\[$(tput bold)$(tput setaf 3)\]\h\[$(tput sgr0)\]] \[$(tput bold)$(tput setaf 4)\]\$(echo \"\${PWD/#\$HOME/~}\" | awk -F/ '\''{if (NF>3) {printf \".../\" \$(NF-1) \"/\" \$NF} else {printf \$0}}'\'')\[$(tput sgr0)\]$(__prompt_git_status "\[\033[0;35;40m\] (%s)\[\033[0;37;40m\]") ->'
fi
unset COLOUR
# Set the debugger prompt.
# shellcheck disable=SC2155