Skip to content

Instantly share code, notes, and snippets.

@ceruleanotter
Last active September 3, 2020 20:10
Show Gist options
  • Save ceruleanotter/cb4536d48e4d4cb675357c4c534fdfd6 to your computer and use it in GitHub Desktop.
Save ceruleanotter/cb4536d48e4d4cb675357c4c534fdfd6 to your computer and use it in GitHub Desktop.
github bash profile enhancements
### GIT ENHANCEMENT START ###
# Git enhancement settings
unset GIT_PS1_SHOWSTASHSTATE
# Git enhancements
source ~/.git-completion.sh
source ~/.git-prompt.sh
# colors!
cyan=$(tput setaf 33)
gray=$(tput setaf 242)
blue=$(tput setaf 27)
green=$(tput setaf 28)
yellow=$(tput setaf 221)
orange=$(tput setaf 202)
gold=$(tput setaf 100)
red=$(tput setaf 124)
purple="$(tput setaf 99)"
reset=$(tput sgr0)
# Returns the name of the git repo we're currently in, if any.
# Colored based on whether the repo is currently dirty or not.
function git_repo_colored {
# indicators for when there is something stashed/staged
git_stash=""
git_stage=""
git_status="$(git status 2>&1)"
if git rev-parse --verify refs/stash >/dev/null 2>&1; then
git_stash="[stashed]"
fi
if git rev-parse --quiet --verify HEAD >/dev/null 2>&1; then
git diff-index --cached --quiet HEAD -- || git_stage="[staged]"
fi
# If we're in a git repo
if [[ "$git_status" != *"Not a git repository"* ]]; then
if [[ "$git_status" = *"nothing to commit"* ]]; then
# Clean repository - nothing to commit
git_repo_status_color=$green
elif [[ "$git_status" = *"Changes not staged for commit"* ]]; then
# unstaged local changes
git_repo_status_color=$yellow
elif [[ "$git_status" = *"nmerged"* ]]; then
# Mid conflicted merge
git_repo_status_color=$red
else
# ??? profit!
git_repo_status_color=$green
fi
curr_git_repo="$(__git_ps1 " (%s)")"
else
# Prompt when not in GIT repo
curr_git_repo=''
fi
}
# Nicer cwd printing
function smart_pwd {
local pwdmaxlen=25
local trunc_symbol=".."
local dir=${PWD##*/}
local tmp=""
pwdmaxlen=$(( ( pwdmaxlen < ${#dir} ) ? ${#dir} : pwdmaxlen ))
new_pwd=${PWD/#$HOME/\~}
local pwdoffset=$(( ${#new_pwd} - pwdmaxlen ))
if [ ${pwdoffset} -gt "0" ]
then
tmp=${new_pwd:$pwdoffset:$pwdmaxlen}
tmp=${trunc_symbol}/${tmp#*/}
if [ "${#tmp}" -lt "${#new_pwd}" ]; then
new_pwd=$tmp
fi
fi
}
PROMPT_COMMAND="git_repo_colored; smart_pwd"
PS1='\[$gray\]\u\[$git_repo_status_color\]$curr_git_repo\[$purple\]$git_stash\[$cyan\]$git_stage\[$blue\] $new_pwd\[$orange\] - \[$reset\]'
### GIT ENHANCEMENT END ###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment