About Peter Daum

Peter is senior Java developer in the telecommunication industry. He works in the area of business and operations support systems and is always happy to share his knowledge and experience. He is interested in everything related to Java and software craftsmanship.

GIT & ZShell : Colorize your shell depending on your branch

As announced in my last post I asked my colleague Tillman if I’m allowed to post his nice extension for the zshell (as you may have concluded he gives his OK! ).

And here it is:
 
 
 
 
 
 
 

# Required to have the colour names
autoload colors zsh/terminfo
if [[ "$terminfo[colors]" -ge 8 ]]; then
  colors
fi
for color in RED GREEN YELLOW BLUE MAGENTA CYAN WHITE; do
  eval PR_$color='%{$terminfo[bold]$fg[${(L)color}]%}'
  eval PR_LIGHT_$color='%{$fg[${(L)color}]%}'
  (( count = $count + 1 ))
done
PR_NO_COLOUR="%{$terminfo[sgr0]%}"
 
# Thanks Tillman
function get_color(){
BRANCH=`git rev-parse --abbrev-ref HEAD`
case "$BRANCH" in
*master*) COLOR=${PR_RED} ;;
*hotfix*) COLOR=${PR_MAGENTA} ;;
*release-candidate*) COLOR=${PR_YELLOW} ;;
*release*) COLOR=${PR_YELLOW} ;;
*bugfix*) COLOR=${PR_CYAN} ;;
*develop*) COLOR=${PR_BLUE} ;;
*) COLOR=${PR_GREEN} ;;
esac
echo $COLOR
}

The color mapping is the same as in these graphic

branch-to-profile-mapping

Branch to Environment / Profile mapping

Applying can be done, for example, when adding this git status helper and refine it with the get_color function.

function git_status () {
  local output
  gs=$(git status 2> /dev/null)
  if [[ $? == 128 ]]; then
    return
  fi
  ref=$(git symbolic-ref HEAD 2> /dev/null) || return
  output=${$(get_color)}" "${ref#refs/heads/}
  if echo "$gs" | grep -q "Changes to be committed"; then
    output=${output}${PR_GREEN}+
  fi
  if echo "$gs" | grep -Eq "(Changes not staged for commit|Changed but not updated)"; then
    output=${output}${PR_RED}!
  fi
  if echo "$gs" | grep -q "Untracked files"; then
    output=${output}${PR_RED}?
  fi  
  echo "${output}${PR_NO_COLOUR}"
}
 
PROMPT=$'%(?,${PR_GREEN}+,${PR_RED}-)${PR_NO_COLOUR} %(4c,./%1~,%~) $(git_status) %(!.${PR_RED}#${PR_NO_COLOUR} .%% )'

At least some examples:

gitColorizedBranches
 

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


seven × 1 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close