Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
111 lines (90 sloc) 3.04 KB
# *************************************
#
# Prompt
# -> Source: https://github.com/mathiasbynens/dotfiles
#
# *************************************
if [[ $COLORTERM = gnome-* && $TERM = xterm ]] && infocmp gnome-256color >/dev/null 2>&1; then
export TERM='gnome-256color';
elif infocmp xterm-256color >/dev/null 2>&1; then
export TERM='xterm-256color-italic';
fi;
prompt_git() {
local s='';
local branchName='';
local stat='';
local aheadN='';
local behindN='';
# Check if the current directory is in a Git repository.
if [ $(git rev-parse --is-inside-work-tree &>/dev/null; echo "${?}") == '0' ]; then
# check if the current directory is in .git before running git checks
if [ "$(git rev-parse --is-inside-git-dir 2> /dev/null)" == 'false' ]; then
# Ensure the index is up to date.
git update-index --really-refresh -q &>/dev/null;
# Check for uncommitted changes in the index.
if ! $(git diff --quiet --ignore-submodules --cached); then
s+='+';
fi;
# Check for unstaged changes.
if ! $(git diff-files --quiet --ignore-submodules --); then
s+='!';
fi;
# Check for untracked files.
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
s+='?';
fi;
# Check for stashed files.
if $(git rev-parse --verify refs/stash &>/dev/null); then
s+='$';
fi;
# Check for push and pull differences from remote.
stat+="$(git status --porcelain --branch | grep '^##' | grep -o '\[.\+\]$')";
aheadN+="$(echo $stat | grep -o 'ahead [[:digit:]]\+' | grep -o '[[:digit:]]\+')";
behindN+="$(echo $stat | grep -o 'behind [[:digit:]]\+' | grep -o '[[:digit:]]\+')";
fi;
# Get the short symbolic ref.
# If HEAD isn’t a symbolic ref, get the short SHA for the latest commit
# Otherwise, just give up.
branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
git rev-parse --short HEAD 2> /dev/null || \
echo '(unknown)')";
[ -n "${s}" ] && s=" [${s}]";
[ -n "$aheadN" ] && s+=" ⇡$aheadN"
[ -n "$behindN" ] && s+=" ⇣$behindN"
echo -e " ${magenta}(${italic}${1}${branchName}${reset}${magenta})${reset}${blue}${s}";
else
return;
fi;
}
tput sgr0; # reset colors
reset=$(tput sgr0);
bold=$(tput bold);
italic=$(tput sitm);
black=$(tput setaf 0);
red=$(tput setaf 1);
green=$(tput setaf 2);
yellow=$(tput setaf 3);
blue=$(tput setaf 4);
magenta=$(tput setaf 5);
cyan=$(tput setaf 6);
white=$(tput setaf 7);
purple=$(tput setaf 125);
# Highlight the user name when logged in as root.
if [[ "${USER}" == "root" ]]; then
userStyle="${red}";
else
userStyle="${orange}";
fi;
# Highlight the hostname when connected via SSH.
if [[ "${SSH_TTY}" ]]; then
hostStyle="${bold}${red}";
else
hostStyle="${yellow}";
fi;
# Set the terminal title to the current working directory.
PS1="\[\033]0;\w\007\]";
PS1+="\[${reset}\]\n"; # newline
PS1+="\[${green}\]\w"; # working directory
PS1+="\$(prompt_git \"${magenta}\")"; # Git repository details
PS1+="\[${reset}\]\n-> ";
export PS1;