Skip to content

Instantly share code, notes, and snippets.

@eutobias
Last active June 1, 2021 22:55
Show Gist options
  • Save eutobias/8c199f2cb1972539558e0852abd2ae90 to your computer and use it in GitHub Desktop.
Save eutobias/8c199f2cb1972539558e0852abd2ae90 to your computer and use it in GitHub Desktop.
Show git branch on terminal with colors, green if updated, yellow Changes added to be committed and red if has changes
##############
# GIT COLORS #
##############
# Based in alot of info that i found in internet, sorry if i did not credit you
# i got a lot of stuff from a alot of places
# just put everything together
# working on MacOs Sierra
# Regular
txtblk="$(tput setaf 0 2>/dev/null || echo '\e[0;30m')" # Black
txtred="$(tput setaf 1 2>/dev/null || echo '\e[0;31m')" # Red
txtgrn="$(tput setaf 2 2>/dev/null || echo '\e[0;32m')" # Green
txtylw="$(tput setaf 3 2>/dev/null || echo '\e[0;33m')" # Yellow
txtblu="$(tput setaf 4 2>/dev/null || echo '\e[0;34m')" # Blue
txtpur="$(tput setaf 5 2>/dev/null || echo '\e[0;35m')" # Purple
txtcyn="$(tput setaf 6 2>/dev/null || echo '\e[0;36m')" # Cyan
txtwht="$(tput setaf 7 2>/dev/null || echo '\e[0;37m')" # White
# Bold
bldblk="$(tput setaf 0 2>/dev/null)$(tput bold 2>/dev/null || echo '\e[1;30m')" # Black
bldred="$(tput setaf 1 2>/dev/null)$(tput bold 2>/dev/null || echo '\e[1;31m')" # Red
bldgrn="$(tput setaf 2 2>/dev/null)$(tput bold 2>/dev/null || echo '\e[1;32m')" # Green
bldylw="$(tput setaf 3 2>/dev/null)$(tput bold 2>/dev/null || echo '\e[1;33m')" # Yellow
bldblu="$(tput setaf 4 2>/dev/null)$(tput bold 2>/dev/null || echo '\e[1;34m')" # Blue
bldpur="$(tput setaf 5 2>/dev/null)$(tput bold 2>/dev/null || echo '\e[1;35m')" # Purple
bldcyn="$(tput setaf 6 2>/dev/null)$(tput bold 2>/dev/null || echo '\e[1;36m')" # Cyan
bldwht="$(tput setaf 7 2>/dev/null)$(tput bold 2>/dev/null || echo '\e[1;37m')" # White
# Underline
undblk="$(tput setaf 0 2>/dev/null)$(tput smul 2>/dev/null || echo '\e[4;30m')" # Black
undred="$(tput setaf 1 2>/dev/null)$(tput smul 2>/dev/null || echo '\e[4;31m')" # Red
undgrn="$(tput setaf 2 2>/dev/null)$(tput smul 2>/dev/null || echo '\e[4;32m')" # Green
undylw="$(tput setaf 3 2>/dev/null)$(tput smul 2>/dev/null || echo '\e[4;33m')" # Yellow
undblu="$(tput setaf 4 2>/dev/null)$(tput smul 2>/dev/null || echo '\e[4;34m')" # Blue
undpur="$(tput setaf 5 2>/dev/null)$(tput smul 2>/dev/null || echo '\e[4;35m')" # Purple
undcyn="$(tput setaf 6 2>/dev/null)$(tput smul 2>/dev/null || echo '\e[4;36m')" # Cyan
undwht="$(tput setaf 7 2>/dev/null)$(tput smul 2>/dev/null || echo '\e[4;37m')" # White
# Background
bakblk="$(tput setab 0 2>/dev/null || echo '\e[40m')" # Black
bakred="$(tput setab 1 2>/dev/null || echo '\e[41m')" # Red
bakgrn="$(tput setab 2 2>/dev/null || echo '\e[42m')" # Green
bakylw="$(tput setab 3 2>/dev/null || echo '\e[43m')" # Yellow
bakblu="$(tput setab 4 2>/dev/null || echo '\e[44m')" # Blue
bakpur="$(tput setab 5 2>/dev/null || echo '\e[45m')" # Purple
bakcyn="$(tput setab 6 2>/dev/null || echo '\e[46m')" # Cyan
bakwht="$(tput setab 7 2>/dev/null || echo '\e[47m')" # White
# Reset
txtrst="$(tput sgr 0 2>/dev/null || echo '\e[0m')" # Text Reset
# Determine the branch/state information for this git repository.
function set_git_branch {
# Capture the output of the "git status" command.
git_status="$(git status 2> /dev/null)"
# Set color based on clean/staged/dirty.
if [[ ${git_status} =~ "working tree clean" ]]; then
state="${bldgrn}"
elif [[ ${git_status} =~ "Changes to be committed" ]]; then
state="${bldylw}"
else
state="${bldred}"
fi
# Set arrow icon based on status against remote.
remote_pattern="# Your branch is (.*) of"
if [[ ${git_status} =~ ${remote_pattern} ]]; then
if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
remote="↑"
else
remote="↓"
fi
else
remote=""
fi
diverge_pattern="# Your branch and (.*) have diverged"
if [[ ${git_status} =~ ${diverge_pattern} ]]; then
remote="↕"
fi
# # Get the name of the branch.
# branch_pattern="^# On branch ([^${IFS}]*)"
# if [[ ${git_status} =~ ${branch_pattern} ]]; then
# branch=${BASH_REMATCH[1]}
# fi
branch="$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/')"
# Set the final branch string.
BRANCH="${state}${branch}${remote}"
}
PROMPT_COMMAND="set_git_branch; $PROMPT_COMMAND"
# Default Git enabled prompt with dirty state
PS1=""
# \t 24-hour HH:MM:SS format
# \T 12-hour HH:MM:SS format.
PS1="$PS1\[$txtwht\][\t] " #date 24h format
PS1="$PS1\[$txtcyn\]\u" #username
PS1="$PS1\[$txtrst\] at \[$txtgrn\]\h" #host
PS1="$PS1\[$txtrst\] in \[$bldylw\]\w" #path
PS1="$PS1\[$txtrst\]\$BRANCH\[$txtrst\]\[$txtrst\]" #colored branch
PS1="$PS1\n\$ " #new line
export PS1
@eutobias
Copy link
Author

eutobias commented Jun 1, 2021

Updating part of PS1 write to insert date time

# Default Git enabled prompt with dirty state
PS1=""
# \t 24-hour HH:MM:SS format
# \T 12-hour HH:MM:SS format. 
PS1="$PS1\[$txtwht\][\t] " #date 24h format
PS1="$PS1\[$txtcyn\]\u" #username
PS1="$PS1\[$txtrst\] at \[$txtgrn\]\h" #host
PS1="$PS1\[$txtrst\] in \[$bldylw\]\w" #path
PS1="$PS1\[$txtrst\]\$BRANCH\[$txtrst\]\[$txtrst\]" #colored branch
PS1="$PS1\n\$ " #new line

export PS1
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment