GRC

19 Apr 2013 in Tech

GRC, or the "Generic Colouriser" is a utility that lets you run output through a script that adds colour via regular expressions. This doesn't sound too useful, but once you use it you'll never look back. The easiest way to show you is to add an example. Let's start with ping - here's a screenshot without GRC.

ping-normal

Here's one with GRC:

ping-grc

The different is subtle, but it makes all the difference. It helps you see the information that you're looking for more easily. There's loads of built in stylesheets, including things like diff and make.

We start off by creating an alias for grc. We use -es to make sure that error output is parsed too, then written back to stdout.

bash
alias colourify="grc -es --colour=auto"

Then, we want to make sure that all of the commands that we support are run via our new alias. Here's what I have set up:

bash
alias colourify="grc -es --colour=auto"
alias configure='colourify ./configure'
alias diff='colourify diff'
alias make='colourify make'
alias gcc='colourify gcc'
alias g++='colourify g++'
alias netstat='colourify netstat'
alias ping='colourify ping'
alias traceroute='colourify traceroute'

By default, grc comes with a set of commands that it supports out of the box. You can see the regular expressions that it matches by running:

bash
$ cat /etc/grc.conf

As well as this, I set up a few of my own regular expressions (e.g. for cURL highlighting). To do this, I add the following to ~/.grc/grc.conf.

bash
# curl command
(^|[/\w\.]+/)curl\s?
conf.curl

See how it specifies conf.curl? This is the highlighting configuration file that it will use. Let's go ahead and create that at ~/.grc/conf.curl.

bash
# HTTP header
regexp=^([A-Z].*)\:\s(.*)$
colours=default, default, cyan
count=more
=====
# HTTP success
regexp=^HTTP.*?\s([1-3]\d{2}.*)$
colours=default, green
count=more
=====
# HTTP error
regexp=^HTTP.*?\s([4|5]\d{2}.*)$
colours=default, red
count=more
=====
# JSON attribute
regexp=(\".*\")\:
colours=default, blue
count=more
=====
# String value
regexp=\:\s(\".*\"),?$
colours=default, yellow, default
count=more
=====
# Number value
regexp=\:\s(\d+),?$
colours=default, magenta, default
count=more
=====
# Boolean value
regexp=\:\s(true|false),?$
colours=default, bold green
count=more
=====
# Null value
regexp=\:\s(null),?$
colours=default, bold red
count=more

Finally, we need to add an alias for curl

bash
alias curl='colourify curl'

Then give it a go:

curl-grc

This is just the start - you can use grc to add colour to any terminal output you can think of. All you need is regular expressions and a bit of time