Monday, September 29, 2014

Bash prompt with colors, and git branch

Do you work with several machines at the same time, travelling from directories to directories, and using git with multiple branches. And, like me, you can't remember where you are, which git branch you're in, because you have half a dozen or more terminals open on your screen.

Here is a little trick you can do with bash, change the prompt.
First of all, you have to download this nice little script called git-prompt.sh and save it in your home directory as .git-prompt.sh

Then, edit your .bashrc and add the line
source ~/.git-prompt.sh
wherever you want. It can be near the beginning for example. If you don't have .bashrc, search for .bash_profile instead.

Then search for the definition of the variable PS1 . And replace its line by the following:

export PS1='\h:\[\e[33m\]\W\[\e[0m\]$(__git_ps1 " (%s)")\$ '

If you can't find it, just add the line wherever you want but in any case, after the source command from above.

The variable PS1 defines your bash prompt as follow:
  • \h : print the hostname you are in
  • \[ and \] encloses ANSI escape commands. Here I use ANSI commands to change the color
  • \e is the ESCape character
  • [33m is for yellow
  • [0m is for "back to normal color"
  • \W is the basename of your working directory. Example /home/foobar/mydir will be written as mydir only
  • $(__git_ps1 " (%s)"): calls the __git_ps1 function which determines your GIT branch or nothing if you're not in a GIT-managed directory
  • \$ transforms into $ if you're a normal user or # if you're root


Wednesday, June 18, 2014

Boost Spirit and C++ 11

It's a little trick again to know about. This time it's about Boost Spirit and C++ 11. Boost Spirit is a C++ library to generate a parser, writing directly C++ code, unlike other parser generators like yacc, bison or ANTLR which have their own language. It's cool, very templated and produce fast code. And fun to use !

 If you want to compile the examples in the tutorial (like this one: http://www.boost.org/doc/libs/1_55_0/libs/spirit/example/qi/mini_xml3.cpp) with C++ 11, you will have a BIG error message, like hundreds of undecipherable lines of templates instantiation errors that are absolutely impossible to read if you're not a world expert, basically the author of the Spirit library.

First time I tried, I ended up with 100+ Kb of error messages ! I tried with different versions of my GCC compiler and clang++ from the LLVM project without any success.

After googling a lot, I finally found a nice little post describing the simple solution to make it work. At the beginning of your code, like the mini_xml3.cpp example, add this, before your #include statements:

#define BOOST_RESULT_OF_USE_DECLTYPE
#define BOOST_SPIRIT_USE_PHOENIX_V3


The first line will force the Boost libraries to use the C++11 decltype instead of the Boost result_of and the second line will ask Phoenix (another Boost library used by Spirit) to use the latest version V3 instead of the old V2 which is not C++ 11 friendly.

My compiler is gcc 4.8.2 and I use Boost 1.55. Your results might vary with different versions.

By the way, using Phoenix V3 is also needed if you intend to put lambda functions in the action part of your grammar rules (<- i=""> if you understand this function, it means you already read all the Boost Spirit tutorial
). So this trick solves 2 problems.