rulururu

post Colors for gcc in shell

January 11th, 2008

Filed under: Linux, Python — Kai @ 3:16 pm

I’ve always been annoyed by not having clearly marked errors (besides the word “error”) when using gcc/g++ from shell.

Now a simple python script helps me to notice every appearing error out of lots of unimportant lines of stuff (warnings e.g.).

highlight.py:

1
2
3
4
5
6
7
import sys
while 1:
input = sys.stdin.readline()
if len(input.lower().split("error")) > 1:
print chr(27) + '[91;1m' + input.strip() + chr(27) + '[0;0m'
else:
print input.strip()

Additionally I created a function and an alias in .bashrc which determines the behavior of the shell:

alias gcc_real=$(which gcc)
alias gcc='gcc_highlight'
function gcc_highlight()
{
gcc_real $@ 2>&1 | python ~/highlight.py
}
alias gpp_real=$(which g++)
#...and the same for g++
}

The usage of gcc_real is very important, otherwise gcc would call gcc and so on…perfect recursion. ;)
gcc errors are not reported over stdout but over stderr which means i had to use 2>&1 to forward them.

1 Comment »

  1. always i used to read smaller articles that also clear their motive, and that is also happening with this paragraph which
    I am reading now.

    Comment by ausmalbilder gepard — April 18, 2012 @ 2:41 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment

ruldrurd
Powered by WordPress, Content and Design by Kai Bellmann
Entries (RSS) and Comments (RSS)