Highlight with grc

  |   Source

I use my terminal emulators a lot. I just love command-line interface because I really feel much more freedom than I have in GUI applications. However the most command-line applications do not highlight their output making it hard to read. Let's fix it?

A few years ago I discovered a nice tool, Generic Colouriser (mirror at GitHub), that reads from standard input and generates colorized output according to the configured rules. The general syntax of grc is as follows:

grc -c <configuration_file> -s -e <colorized_output_command>

where the options are:

  • -c - configuration file
  • -s - colorize stdout
  • -e - colorize stderr

Since that time I made a small list of tools I work with the most. So here it is.

ant

Bash shortcut:

function ANT {
    grc -c ~/ant.grc -s -e ant "$@"
}

ant.grc:

regexp=^(\S.*):
colours=green
-

regexp=^\s*\[[a-z0-9\-]+\]
colours=yellow
-

regexp=^BUILD SUCCESSFUL
colours=bold white on_green
-

regexp=^BUILD FAILED
colours=bold white on_red

Sample output:

Buildfile: /home/lsh/tmp/build.xml

all:
     [echo] grc test

BUILD SUCCESSFUL
Total time: 3 seconds

diff (unified)

Bash shortcut:

function DIFF {
    grc -c ~/diff.grc -s -e diff "$@"
}

diff.grc:

regexp=^@@\s.*\s@@$
colours=bold black
-

regexp=^Index:\s.*$
colours=white on_blue
-

regexp=^=+$
colours=white on_blue
-

regexp=^-.*$
colours=red
-

regexp=^\+.*$
colours=green
-

regexp=^---\s.*$
colours=bold red
-

regexp=^\+\+\+\s.*$
colours=bold green

Sample output:

--- a   2016-12-03 12:20:25.928610790 +0200
+++ b   2016-12-03 12:20:43.628610011 +0200
@@ -1,4 +1,7 @@
+<p>
 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
 Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
 Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
 Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+</p>
+

Maven

Bash shortcut:

function MVN {
    grc -c ~/mvn.grc -s -e mvn "$@"
}

mvn.grc:

regexp=^\[\s*FATAL\s*\].*
colours=bold white on_red
-

regexp=^\[\s*ERROR\s*\].*
colours=red
-

regexp=^\[\s*WARN(ING)?\s*\].*
colours=yellow
-

regexp=^\[\s*INFO\s*\].*
colours=green
-

regexp=^\[\s*DEBUG\s*\].*
colours=blue
-

regexp=^\[\s*TRACE\s*\].*
colours=bold black
-

regexp=BUILD FAILURE
colours=white on_red
-

regexp=BUILD SUCCESS
colours=bold white on_green
-

regexp=^(\tat\s|Caused by: |\t\.\.\. \d+ more).*
colours=red

Sample output:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building grc test 0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ grc test ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/lsh/tmp/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ grc test ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ grc test ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/lsh/tmp/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ grc test ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ grc test ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ grc test ---
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] Building jar: /home/lsh/tmp/target/grc test-0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.626 s
[INFO] Finished at: 2016-12-03T14:33:58+02:00
[INFO] Final Memory: 11M/89M
[INFO] ------------------------------------------------------------------------

MySQL

Bash shortcut:

function MYSQL {
    mysql --pager='grcat ~/mysql.grc | less -RSFX' "$@"
}

mysql.grc:

# default word color
regexp=.+
colours=white
-

# table borders
regexp=[+\-]+[+\-]|[|]
colours=green
-

# NULLs
regexp=\bNULL\b
colours=on_blue
-

# data in ( ) and ' '
regexp=\([\w\d,']+\)
colours=blue
-

# numeric
regexp=\s[\d\.]+(\s|.$)
colours=yellow
-

# date or time
regexp=(\d{4}-\d{2}-\d{2}|\d{2}:\d{2}:\d{2})
colours=cyan
-

# hashes
regexp=\b\*?[0-9A-F]{6,}\b
colours=black on_yellow
-

# IP v4 address
regexp=(\d{1,3}\.){3}\d{1,3}(:\d{1,5})?
colours=cyan
-

# schema
regexp=`\w+`
colours=yellow
-

# email
regexp=[\w\.\-_]+@[\w\.\-_]+
colours=magenta
-

# row delimeter when using \G key
regexp=[*]+.+[*]+
count=stop
colours=white
-

# column names when using \G key
regexp=^\s*\w+:
colours=white

Sample output:

+-----------------------+---------------+------+-----+---------+-------+
| Field                 | Type          | Null | Key | Default | Extra |
+-----------------------+---------------+------+-----+---------+-------+
| Host                  | char(60)      | NO   | PRI |         |       |
| Db                    | char(64)      | NO   | PRI |         |       |
| User                  | char(16)      | NO   | PRI |         |       |
| Select_priv           | enum('N','Y') | NO   |     | N       |       |
| Insert_priv           | enum('N','Y') | NO   |     | N       |       |
| Update_priv           | enum('N','Y') | NO   |     | N       |       |
| Delete_priv           | enum('N','Y') | NO   |     | N       |       |
| Create_priv           | enum('N','Y') | NO   |     | N       |       |
| Drop_priv             | enum('N','Y') | NO   |     | N       |       |
| Grant_priv            | enum('N','Y') | NO   |     | N       |       |
| References_priv       | enum('N','Y') | NO   |     | N       |       |
| Index_priv            | enum('N','Y') | NO   |     | N       |       |
| Alter_priv            | enum('N','Y') | NO   |     | N       |       |
| Create_tmp_table_priv | enum('N','Y') | NO   |     | N       |       |
| Lock_tables_priv      | enum('N','Y') | NO   |     | N       |       |
| Create_view_priv      | enum('N','Y') | NO   |     | N       |       |
| Show_view_priv        | enum('N','Y') | NO   |     | N       |       |
| Create_routine_priv   | enum('N','Y') | NO   |     | N       |       |
| Alter_routine_priv    | enum('N','Y') | NO   |     | N       |       |
| Execute_priv          | enum('N','Y') | NO   |     | N       |       |
| Event_priv            | enum('N','Y') | NO   |     | N       |       |
| Trigger_priv          | enum('N','Y') | NO   |     | N       |       |
+-----------------------+---------------+------+-----+---------+-------+

Subversion

Bash shortcut (this shortcut also adds pagination and specifies different rules for different svn commands):

function SVN {
    local RULES=~/svn.grc
    local PAGED=
    case "$1" in
        "blame")
            PAGED=yes
            ;;
        "diff")
            RULES=~/diff.grc
            PAGED=yes
            ;;
        "log")
            PAGED=yes
            ;;
        "st")
            PAGED=yes
            ;;
        "status")
            PAGED=yes
            ;;
        *)
            ;;
    esac
    if [ "$PAGED" == "yes" ]; then
        grc -c $RULES -s -e svn "$@" | nl | less -rFX
    else
        grc -c $RULES -s -e svn "$@"
    fi
}

svn.grc:

##################
## FIRST COLUMN ##
##################

# ? - untracked
regexp=^\?.*
colours=bold black
-

# ! - missing
regexp=^\!.*
colours=white on_red
-

# A - added
regexp=^(A)... (.*)
colours=,bold cyan,cyan
-

# C - conflicted
regexp=^C... .*
colours=black on_yellow
-

# D - deleted
regexp=^(D)... (.*)
colours=,bold red,red
-

# G - merged (during svn update)
regexp=^(G)... (.*)
colours=,bold yellow,yellow
-

# I - ignored
regexp=^I... .*
colours=bold black
-

# M - modified
regexp=^(M)... (.*)
colours=,bold green,green
-

# R - replaced
regexp=^(R)... (.*)
colours=,bold magenta,magenta
-

# U - updated (during svn update)
regexp=^(U)... (.*)
colours=,bold green,green
-

# X - external
regexp=^X... .*
colours=magenta
-

###################
## SECOND COLUMN ##
###################

# M - modified properties
regexp=^.(M).. .
colours=,bold on_green
-

# C - conflicted properties
regexp=^.(C).. .
colours=,bold on_yellow
-

##################
## THIRD COLUMN ##
##################

# L - locked
regexp=^..(L). .
colours=bold red
-

###################
## FOURTH COLUMN ##
###################

# + - copied
regexp=^...(\+)
colours=,bold cyan
-

##################
## FIFTH COLUMN ##
##################

# S - switched
regexp=^....(S)
colours=,green
-

##################
## SIXTH COLUMN ##
##################

# K - locked
regexp=^.....(K)
colours=,red
-

# O - locked in another working copy
regexp=^.....(O)
colours=,bold red
-

# T - stolen lock
regexp=^.....(T)
colours=,bold green
-

# B - broken lock
regexp=^.....(B)
colours=,bold magenta
-

##############
## SVN INFO ##
##############

regexp=^(Path|Working Copy Root Path|URL|Relative URL|Repository Root|Repository UUID|Revision|Node Kind|Schedule|Last Changed Author|Last Changed Rev|Last Changed Date):(.*)
colours=,cyan,bold cyan
-

#############
## SVN LOG ##
#############

regexp=^-{6,}
colours=green
-

regexp=^(r\d+) \|
colours=,bold yellow
-

###############
## SVN BLAME ##
###############

regexp=^(\d+)\s+([0-9a-zA-Z\-\.]+)\s+
colours=,yellow, green

Sample output:

     1  Index: pom.xml
     2  ===================================================================
     3  --- pom.xml   (revision 1772463)
     4  +++ pom.xml   (working copy)
     5  @@ -24,7 +24,7 @@
     6     <parent>
     7       <groupId>org.apache.maven.enforcer</groupId>
     8       <artifactId>enforcer</artifactId>
     9  -    <version>1.4.1</version>
    10  +    <version>1.4.2</version>
    11     </parent>
    12
    13     org.apache.maven.plugins
     1  A       README
     2  M       pom.xml
Comments powered by Disqus