Skip to content

Instantly share code, notes, and snippets.

@dorian-davis
Created November 2, 2017 23:10
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 dorian-davis/c0aae18b4f0d8e5fbb05bf9db7c6cd65 to your computer and use it in GitHub Desktop.
Save dorian-davis/c0aae18b4f0d8e5fbb05bf9db7c6cd65 to your computer and use it in GitHub Desktop.
Colourful git branch in Mac command line prompt (add to ~/.bash_profile) and automatic `nvm use` when cd into a folder containing a .nvmrc file
# check nvm version in nvmrc file (if present) and load/use it on directory change
enter_directory(){
if [ "$PWD" != "$PREV_PWD" ]; then
PREV_PWD="$PWD";
if [ -e ".nvmrc" ]; then
nvm use;
fi
fi
}
export PROMPT_COMMAND="$PROMPT_COMMAND enter_directory;"
# colorful prompt with git branches
# parse_git_branch() {
# git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
# }
# export PS1="\u \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
# Adds the current branch to the bash prompt when the working directory is
# part of a Git repository. Includes color-coding and indicators to quickly
# indicate the status of working directory.
#
# To use: Copy into ~/.bashrc and tweak if desired.
#
# Based upon the following gists:
# <https://gist.github.com/henrik/31631>
# <https://gist.github.com/srguiwiz/de87bf6355717f0eede5>
# Modified by me, using ideas from comments on those gists.
#
# License: MIT, unless the authors of those two gists object :)
git_branch() {
# -- Finds and outputs the current branch name by parsing the list of
# all branches
# -- Current branch is identified by an asterisk at the beginning
# -- If not in a Git repository, error message goes to /dev/null and
# no output is produced
git branch --no-color 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
git_status() {
# Outputs a series of indicators based on the status of the
# working directory:
# + changes are staged and ready to commit
# ! unstaged changes are present
# ? untracked files are present
# S changes have been stashed
# P local commits need to be pushed to the remote
local status="$(git status --porcelain 2>/dev/null)"
local output=''
[[ -n $(egrep '^[MADRC]' <<<"$status") ]] && output="$output+"
[[ -n $(egrep '^.[MD]' <<<"$status") ]] && output="$output!"
[[ -n $(egrep '^\?\?' <<<"$status") ]] && output="$output?"
[[ -n $(git stash list) ]] && output="${output}S"
[[ -n $(git log --branches --not --remotes) ]] && output="${output}P"
[[ -n $output ]] && output="|$output" # separate from branch name
echo $output
}
git_color() {
# Receives output of git_status as argument; produces appropriate color
# code based on status of working directory:
# - White if everything is clean
# - Green if all changes are staged
# - Red if there are uncommitted changes with nothing staged
# - Yellow if there are both staged and unstaged changes
# - Blue if there are unpushed commits
local staged=$([[ $1 =~ \+ ]] && echo yes)
local dirty=$([[ $1 =~ [!\?] ]] && echo yes)
local needs_push=$([[ $1 =~ P ]] && echo yes)
if [[ -n $staged ]] && [[ -n $dirty ]]; then
echo -e '\033[1;33m' # bold yellow
elif [[ -n $staged ]]; then
echo -e '\033[1;32m' # bold green
elif [[ -n $dirty ]]; then
echo -e '\033[1;31m' # bold red
elif [[ -n $needs_push ]]; then
echo -e '\033[1;34m' # bold blue
else
echo -e '\033[1;37m' # bold white
fi
}
git_prompt() {
# First, get the branch name...
local branch=$(git_branch)
# Empty output? Then we're not in a Git repository, so bypass the rest
# of the function, producing no output
if [[ -n $branch ]]; then
local state=$(git_status)
local color=$(git_color $state)
# Now output the actual code to insert the branch and status
echo -e "\x01$color\x02[$branch$state]\x01\033[00m\x02 " # last bit resets color
fi
}
# Sample prompt declaration. Tweak as you see fit, or just stick
# "$(git_prompt)" into your favorite prompt.
PS1='\u: \w $(git_prompt)\[\033[00m\]\$ '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment