Skip to content

Instantly share code, notes, and snippets.

@drhuffman12
Last active December 3, 2021 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drhuffman12/7cef184fc648f4785cdb1730ae463f6d to your computer and use it in GitHub Desktop.
Save drhuffman12/7cef184fc648f4785cdb1730ae463f6d to your computer and use it in GitHub Desktop.
bash prompt with username, host, time, path, git branch info
################################################################
## vvvv COLORS vvvvv ##
COLOR_PREFIX="\e[0;"
COLOR_DISABLED="\e[0m"
COLOR_ID_Black=30
COLOR_ID_Red=31
COLOR_ID_Green=32
COLOR_ID_Yellow=33
COLOR_ID_Blue=34
COLOR_ID_Magenta=35
COLOR_ID_Cyan=36
COLOR_ID_White=37
COLOR_Black="$(echo $COLOR_PREFIX)$(echo $COLOR_ID_Black)m"
COLOR_Red="$(echo $COLOR_PREFIX)$(echo $COLOR_ID_Red)m"
COLOR_Green="$(echo $COLOR_PREFIX)$(echo $COLOR_ID_Green)m"
COLOR_Yellow="$(echo $COLOR_PREFIX)$(echo $COLOR_ID_Yellow)m"
COLOR_Blue="$(echo $COLOR_PREFIX)$(echo $COLOR_ID_Blue)m"
COLOR_Magenta="$(echo $COLOR_PREFIX)$(echo $COLOR_ID_Magenta)m"
COLOR_Cyan="$(echo $COLOR_PREFIX)$(echo $COLOR_ID_Cyan)m"
COLOR_White="$(echo $COLOR_PREFIX)$(echo $COLOR_ID_White)m"
# COLOR_Reset=0m
color_list="\n${COLOR_Black} COLOR_Black ${COLOR_DISABLED}\n"
color_list+="${COLOR_Red} COLOR_Red ${COLOR_DISABLED}\n"
color_list+="${COLOR_Green} COLOR_Green ${COLOR_DISABLED}\n"
color_list+="${COLOR_Yellow} COLOR_Yellow ${COLOR_DISABLED}\n"
color_list+="${COLOR_Blue} COLOR_Blue ${COLOR_DISABLED}\n"
color_list+="${COLOR_Magenta} COLOR_Magenta ${COLOR_DISABLED}\n"
color_list+="${COLOR_Cyan} COLOR_Cyan ${COLOR_DISABLED}\n"
color_list+="${COLOR_White} COLOR_White ${COLOR_DISABLED}\n"
## ^^^^ COLORS ^^^^ ##
################################################################
## Host/UN
COLOR_UN_HOST=$COLOR_Red
if [ "$EUID" -ne 0 ]
then
# non-root
COLOR_UN_HOST=$COLOR_Green
fi
COLOR_DATE=$COLOR_Cyan
COLOR_DIR=$COLOR_Blue
COLOR_GIT=$COLOR_Yellow
################################################################
## GIT branch info
# function parse_git_branch {
# git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1) /'
# }
################################################################
## Prompt (check colors!!!)
MY_PROMPT="\n${COLOR_UN_HOST}\u@\H ${COLOR_DATE}(\$(date))\n ${COLOR_DIR}\w${COLOR_GIT}, git:\$(__git_ps1)${COLOR_DISABLED}\n\$ "
## (Un-comment if checking colors!!!)
# MY_PROMPT="${MY_PROMPT}color_list $:\n${color_list}\n\$ "
export PS1=$MY_PROMPT
# (ba)sh prompt mods
## git info
__git_ps1()
{
local branch=$(git branch 2>&1 2>/dev/null | perl -ne 'print if (s/^\* //g)')
if [ -n "$branch" ]; then
local remote=$(git config "branch.${branch}.remote")
local aheadbehind=$(git rev-list --left-right "${remote}/${branch}"...HEAD 2>/dev/null \
| perl -e '
$ahead = $behind = $lines = 0;
while(<>) {
if (/^([<>])/g) {
$lines++;
if ($1 eq "<") {
$behind--;
} else {
$ahead++;
}
}
}
print "${behind}|${ahead}" if $lines;')
local remote_changes=""
local color_yellow="\001\033[0;33m\002"
local color_red="\001\033[0;31m\002"
local color_cyan="\001\033[0;36m\002"
local color_reset="\001\033[0;00m\002"
if [ -n "$aheadbehind" ]; then
local behind=$(echo "$aheadbehind" | cut -d'|' -f1)
local ahead=$(echo "$aheadbehind" | cut -d'|' -f2)
local ahead_color=$color_cyan
local behind_color=$color_red
if [ "$ahead" -eq "0" ]; then
ahead_color=""
else
ahead="+${ahead}"
fi
if [ "$behind" -eq "0" ]; then
behind_color=""
fi
remote_changes=$(printf "[${behind_color}%s${color_reset}:${ahead_color}%s${color_reset}]" "$behind" "$ahead")
fi
printf "(${color_yellow}%s${color_reset}${remote_changes})" "$branch"
fi
}
# root
EUID_COLOR=31
if [ "$EUID" -ne 0 ]
then
# non-root
EUID_COLOR=33
fi
# prompt
# * first run: `git config --global --edit`
# export PS1="\e[0;$(EUID_COLOR)m\u@\H \e[0;34m($(date)) \e[0;32m\w\e[0;31m ($(git status -sb)) \e[0;34m\n\$ \e[0m"
export PS1="\e[0;$(echo $EUID_COLOR)m\u@\H \e[0;34m($(date)) \e[0;32m\w\e[0;31m (\$(__git_ps1)) \e[0;34m\n\$ \e[0m"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment