Last active
July 13, 2024 15:04
-
-
Save HaleTom/f9a3d75b9b0fb320e5b28e096067a57d to your computer and use it in GitHub Desktop.
Coloured bash prompt including git status and previous exit code - http://codereview.stackexchange.com/questions/137799/coloured-bash-prompt-including-git-status-and-previous-exit-code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# NO LONGER UPDATED | |
# Merged into: https://gist.github.com/HaleTom/631efef6fb6ae86618128647dc887aee | |
################## | |
# Set the prompt # | |
################## | |
# Select git info displayed, see /usr/lib/git-core/git-sh-prompt for more | |
export GIT_PS1_SHOWDIRTYSTATE=1 # '*'=unstaged, '+'=staged | |
export GIT_PS1_SHOWSTASHSTATE=1 # '$'=stashed | |
export GIT_PS1_SHOWUNTRACKEDFILES=1 # '%'=untracked | |
export GIT_PS1_SHOWUPSTREAM="verbose" # 'u='=no difference, 'u+1'=ahead by 1 commit | |
export GIT_PS1_STATESEPARATOR='' # No space between branch and index status | |
export GIT_PS1_DESCRIBE_STYLE="describe" # detached HEAD style: | |
# contains relative to newer annotated tag (v1.6.3.2~35) | |
# branch relative to newer tag or branch (master~4) | |
# describe relative to older annotated tag (v1.6.3.1-13-gdd42c2f) | |
# default exactly eatching tag | |
# Check if we support colours | |
__colour_enabled() { | |
local -i colors=$(tput colors 2>/dev/null) | |
[[ $? -eq 0 ]] && [[ $colors -gt 2 ]] | |
} | |
# Sets prompt like: ravi@boxy:~/prj/sample_app | |
__set_bash_prompt() { | |
local exit="$?" # Save the exit status of the last command | |
# PS1 is made from $PreGitPS1 + <git-status> + $PostGitPS1 | |
local PreGitPS1="${debian_chroot:+($debian_chroot)}" | |
local PostGitPS1="" | |
if __colour_enabled; then | |
export GIT_PS1_SHOWCOLORHINTS=1 | |
# Wrap the colour codes between \[ and \], so that | |
# bash counts the correct number of characters for line wrapping: | |
local Red='\[\e[0;31m\]'; local BRed='\[\e[1;31m\]' | |
local Gre='\[\e[0;32m\]'; local BGre='\[\e[1;32m\]' | |
local Yel='\[\e[0;33m\]'; local BYel='\[\e[1;33m\]' | |
local Blu='\[\e[0;34m\]'; local BBlu='\[\e[1;34m\]' | |
local Mag='\[\e[0;35m\]'; local BMag='\[\e[1;35m\]' | |
local Cya='\[\e[0;36m\]'; local BCya='\[\e[1;36m\]' | |
local Whi='\[\e[0;37m\]'; local BWhi='\[\e[1;37m\]' | |
local None='\[\e[0m\]' # Return to default colour | |
else # No colour | |
unset GIT_PS1_SHOWCOLORHINTS | |
local Red BRed Gre BGre Yel BYel Blu BBlu Mag BMag Cya BCya Whi BWhi None | |
fi | |
# No username and bright colour if root | |
if [[ ${EUID} = 0 ]]; then | |
PreGitPS1+="$BRed\h " | |
else | |
PreGitPS1+="$Red\u@\h$None:" | |
fi | |
PreGitPS1+="$Blu\w$None" | |
# Now build the part after git's status | |
# Highlight non-standard exit codes | |
if [[ $exit != 0 ]]; then | |
PostGitPS1="$Red[$exit]" | |
fi | |
# Change colour of prompt if root | |
if [[ ${EUID} == 0 ]]; then | |
PostGitPS1+="$BRed"'\$ '"$None" | |
else | |
PostGitPS1+="$Mag"'\$ '"$None" | |
fi | |
# Set PS1 from $PreGitPS1 + <git-status> + $PostGitPS1 | |
__git_ps1 "$PreGitPS1" "$PostGitPS1" '(%s)' | |
# echo '$PS1='"$PS1" | |
# defaut user prompt: | |
# PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] \w\[\033[00m\] $(__git_ps1 "(%s)") \$ ' | |
} | |
# This tells bash to reinterpret PS1 after every command, which we | |
# need because __git_ps1 will return different text and colors | |
PROMPT_COMMAND=__set_bash_prompt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment