Skip to content

Instantly share code, notes, and snippets.

@gibizer
Last active September 11, 2022 13:03
Show Gist options
  • Save gibizer/d94457f84b5bb967f8555ed81cc5f269 to your computer and use it in GitHub Desktop.
Save gibizer/d94457f84b5bb967f8555ed81cc5f269 to your computer and use it in GitHub Desktop.
# --- prompt ---
# based on https://gist.github.com/mkottman/1936195
RESET="\[\033[0m\]"
RED="\[\033[0;31m\]"
GREEN="\[\033[01;32m\]"
DARK_GREEN="\[\033[38;5;70m\]"
BLUE="\[\033[01;34m\]"
YELLOW="\[\033[0;33m\]"
WHITE="\[\033[0;37m\]"
MAGENTA="\[\033[0;35m\]"
PS_LINE=`printf -- ' %.0s' {1..200}`
function get_git_status {
PS_FILL=${PS_LINE:0:$COLUMNS}
branch=""
branch=$(git branch --show-current 2> /dev/null)
if [ "$branch" == "" ]; then
branch=$(git log --oneline 2> /dev/null | head -1)
fi
if [ "$branch" == "" ]; then
# we are not in a git repo
branch=""
else
branch="${YELLOW}[$branch]${RESET}"
if [[ -n $(git status -s) ]]; then
branch="${RED}D${RESET}$branch"
fi
git_status=$(git status)
if [[ -n $(echo $git_status | grep 'fix conflicts') ]]; then
branch="${RED}C${RESET}$branch"
fi
if [[ -n $(echo $git_status | grep 'currently editing a commit') ]]; then
branch="${YELLOW}E${RESET}$branch"
fi
if [[ -n $(echo $git_status | grep 'currently cherry-picking') ]]; then
branch="c-p|$branch"
fi
if [[ -n $(echo $git_status | grep -E 'currently rebasing|while rebasing') ]]; then
cmd=$(cat .git/rebase-merge/git-rebase-todo | wc -l)
branch="reb$cmd|$branch"
fi
if [[ -n $(echo $git_status | grep 'You have unmerged paths') ]]; then
branch="mrg|$branch"
fi
fi
PS_GIT_STATUS="$branch"
}
function generate_prompt
{
exit_code=$?
if [[ "$exit_code" != "0" ]]; then
EXIT_CODE="[${RED}$(printf '%03d' $exit_code)${RESET}]"
else
EXIT_CODE="[$(printf '%03d' $exit_code)]"
fi
get_git_status
if [[ -n "$VIRTUAL_ENV" ]]; then
# Strip out the path and just leave the env name
venv="(${VIRTUAL_ENV##*/})"
else
# In case you don't have one activated
venv=''
fi
PS_VENV="$GREEN$venv$RESET"
PS_INFO="$DARK_GREEN\u${RESET}@${DARK_GREEN}\h$RESET:$BLUE\w${RESET}"
PS_GIT="$PS_GIT_STATUS"
PS_TIME="\[\033[\$((COLUMNS-16))G\] ${EXIT_CODE} ${WHITE}[\t]${RESET}"
TERM_TITLE="\033]0;\u@\h:\w$\007"
if [[ ${SHLVL} -eq 1 ]]; then
PROMPT="${DARK_GREEN}\$${RESET} "
else
PROMPT="${MAGENTA}\$${RESET} "
fi
export PS1="\${PS_FILL}\[\033[0G\]${PS_INFO} ${PS_VENV}${PS_GIT}${PS_TIME}\n${RESET}${PROMPT}"
# make the terminal title dynamic as well
export PS1="$TERM_TITLE$PS1"
}
PROMPT_COMMAND=generate_prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment