Skip to content

Instantly share code, notes, and snippets.

@jkuester
Created October 10, 2024 14:12
Show Gist options
  • Save jkuester/dcd3e906dd7a2ea26b96779c6bf24f46 to your computer and use it in GitHub Desktop.
Save jkuester/dcd3e906dd7a2ea26b96779c6bf24f46 to your computer and use it in GitHub Desktop.
Bash Prompt
# .bashrc
######################################################################
# Setup bash prompt with the following features:
#
# container_id:short/directory (git_branch)
# ❱
#
# - Set the color of the prompt prefix "❱" based on the success/failure of the last command
# - Display git repo details (when you are in a directory that is a git repo)
# - You need to download the .git-prompt.sh script into your home directory
# - https://raw.githubusercontent.com/git/git/refs/heads/master/contrib/completion/git-prompt.sh
# - Display CONTAINER_ID value when set (e.g. when you have bashed into a container)
# - This is most useful when you are using something like https://distrobox.it/ which will automatically map your host user/home/.bashrc/etc into the container
# - Set the terminal title to be `container_id:short/directory (git_branch)`
# - When running in Zellij, this will actually be the title of the Zellij pane
# - When running in Zellij, do not include the `container_id:short/directory (git_branch)` line in the prompt (since that data is already present in the pane title)
#
# See https://bash-prompt-generator.org/ for more ideas/options!
######################################################################
. ~/.git-prompt.sh
function my_prompt_command () {
LAST_RESULT=$?
COLOR_RED="\[\e[31;1m\]"
COLOR_BLUE="\[\e[38;5;38m\]"
COLOR_GREEN="\[\e[32;1m\]"
COLOR_NONE="\[\e[0m\]"
if [[ $LAST_RESULT == 0 ]]; then
COLOR_PROMPT=${COLOR_GREEN}
else
COLOR_PROMPT=${COLOR_RED}
fi
GIT_PS1_SHOWDIRTYSTATE="yes"
GIT_PS1_SHOWUNTRACKEDFILES="yes"
GIT_PS1_SHOWCONFLICTSTATE="yes"
GIT_PS1_SHOWCOLORHINTS=""
GIT_STATUS=$(__git_ps1 " (%s)")
IN_CONTAINER_ID="${COLOR_BLUE}${CONTAINER_ID:+$CONTAINER_ID:}${COLOR_NONE}"
PROMPT="${COLOR_PROMPT}❱${COLOR_NONE}"
# This the actual prompt text
if [[ -n $ZELLIJ ]]; then
PS1="${PROMPT} "
else
PS1="${IN_CONTAINER_ID}\w${GIT_STATUS}\n${PROMPT} "
fi
if [[ $PWD == $HOME* ]]; then
SHORT_PWD="~${PWD#$HOME}"
else
SHORT_PWD=$PWD
fi
# This magially sets the terminal title (cannot use colors)
echo -ne "\033]0;${CONTAINER_ID:+$CONTAINER_ID:}${SHORT_PWD}${GIT_STATUS}\007"
}
# This gets executed each time the prompt gets regenerated
PROMPT_COMMAND=my_prompt_command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment