Skip to content

Instantly share code, notes, and snippets.

@iphysresearch
Created November 24, 2023 07:57
Show Gist options
  • Save iphysresearch/9069d95a4f2baa77c37f00dcd408f0ed to your computer and use it in GitHub Desktop.
Save iphysresearch/9069d95a4f2baa77c37f00dcd408f0ed to your computer and use it in GitHub Desktop.
Git prompt with conda and conda-auto-env
# REF: https://albertotb.com/Git-prompt-with-conda-and-conda-auto-env/
# add the following to the end of your ~/.bashrc file:
GREEN="\[\033[38;5;155m\]"
DARK_GREEN="\[\033[00;32m\]"
GRAY="\[\033[38;5;8m\]"
ORANGE="\[\033[38;5;220m\]"
BLUE="\[\033[38;5;117m\]"
WHITE="\[\033[38;5;15m\]"
YELLOW="\[\033[01;33m\]"
LIGHT_GRAY="\[\033[0;37m\]"
CYAN="\[\033[0;36m\]"
RED="\[\033[0;31m\]"
VIOLET="\[\033[01;35m\]"
MAGENTA="\[\033[0;35m\]".
RESET="\[$(tput sgr0)\]"
function git_and_conda_prompt {
local __git_branch_color="$DARK_GREEN"
local __git_branch=$(__git_ps1 ' [%s]');
# colour branch name depending on state
if [[ "${__git_branch}" =~ "*" ]]; then # if repository is dirty
__git_branch_color="$RED"
elif [[ "${__git_branch}" =~ "$" ]]; then # if there is something stashed
__git_branch_color="$YELLOW"
elif [[ "${__git_branch}" =~ "%" ]]; then # if there are only untracked files
__git_branch_color="$LIGHT_GRAY"
elif [[ "${__git_branch}" =~ "+" ]]; then # if there are staged files
__git_branch_color="$CYAN"
fi
PS1="${CONDA_PROMPT_MODIFIER}${GREEN}\u${RESET}${GRAY}@${RESET}${ORANGE}\h${RESET}${GRAY}:${RESET}${BLUE}\w${RESET}$__git_branch_color$__git_branch${GRAY}\$${RESET}${WHITE}${RESET} "
}
export PROMPT_COMMAND="conda_auto_env;git_and_conda_prompt"
if [ -f ~/conda_auto_env.sh ]; then
source ~/conda_auto_env.sh
fi
# if .git-prompt.sh exists, set options and execute it
if [ -f ~/git-prompt.sh ]; then
GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWSTASHSTATE=true
GIT_PS1_SHOWUNTRACKEDFILES=true
GIT_PS1_SHOWUPSTREAM="auto"
GIT_PS1_HIDE_IF_PWD_IGNORED=true
GIT_PS1_SHOWCOLORHINTS=true
source ~/git-prompt.sh
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment