Ever wondered why the output of your favourite UNIX/Linux commands is still
displayed in black-and-white after all these years?
Ever had to search back through your scroll-buffer in search of gcc errors and salient information to tell
you what went wrong with your program's execution?
acoc is a regular expression based colour formatter for
programs that display output on the command-line. It works as a wrapper around
the target program, executing it and capturing the stdout stream. Optionally,
stderr can be redirected to stdout, so that it, too, can be manipulated.
acoc then applies matching rules to patterns in the output
and applies colour sets to those matches. A picture is worth a thousand words,
so look at the sample screenshots in the next section.
acoc has just one absolute prerequisite (other than Ruby
itself, of course), namely Florian Frank's Term::ANSIColor
module.
If you don't want to go to the trouble of fetching and installing Term::ANSIColor
yourself, you'll be happy to know that the acoc package
includes a copy for your convenience.
Another library you might care to install is Masahiro Tomita's Ruby/TPty
library to allocate pseudo-terminals in order to fool those programs that
behave differently if their stdout stream is not connected to a tty.
Whilst Ruby/TPty is not
mandatory (acoc will detect its absence), its installation is recommended in
order to increase the transparency of acoc's operation.
The configuration files used by the program are
/usr/local/etc/acoc.conf, /etc/acoc.conf and
~/acoc.conf. One or more of these must exist. A sample
/etc/acoc.conf is supplied with some example matching rules.
Blank lines and those that begin with a # are ignored.
A program configuration stanza is introduced as follows:
The square brackets are mandatory literal characters. Alternatively, the
@ symbol may be used, to allow [ and ] to retain
their usual semantics in program specs comprising a regular expression:
program_spec is defined as one or more instances of the following
component, separated by a comma:
invocation[/flags]
where invocation consists of the program's name (not including its
directory path component) plus any initial arguments.
Alternatively, invocation may be a regular expression, which can be
used to match multiple programs and/or command-line arguments in arbitrary
order. Regular expressions are automatically anchored to the beginning of the
command line.
flags, if present, is separated from 'invocation' by a slash and
consists of one or more of the following characters:
Some programs, such as ls(1), behave differently when their stdout
is not connected to a tty. Use of this option will fool the target program
into believing it is outputting to a tty, rather than a pipe to
acoc.
Use of this flag requires Masahiro Tomita's Ruby/TPty library to be
installed. Otherwise, the flag is silently ignored.
Note that the pseudo-terminal communication enabled by this flag is one-way
only, from the target program to acoc. It is thus not
possible to use acoc in combination with interactive
programs, such as the interactive Ruby interpreter (irb).
Here's an example of a line that introduces a configuration stanza:
which says to apply the following rules to the rpm and
rpmbuild commands, attempt to apply all matching rules, and also apply
those rules to the programs' stderr stream.
Another example:
This says to allocate a pseudo-terminal to ls(1), fooling it into
believing that its output is being sent to a regular terminal instead of a pipe
to acoc.
With this flag, the effect will be this:
Without it, ls will detect that its stdout is connected to a pipe and
behave accordingly:
A third example:
This says that the rules that follow should be applied to all invocations of
diff(1) and rcsdiff(1), as well as those invocations of
cvs(1) and p4 that are followed by the argument
diff.
Additionally, colouring should be applied even when stdout is not
connected to a tty, so that the colours still show up when the output
is displayed in a pager such as more(1) or less(1).
Yet another example:
In this example, the ps(1) command will be matched, as long as the
e and f options are both passed in either
order.
An alternative way to write the above spec is:
There are two things to note in this alternative:
Here's one more example:
If this were placed in ~/.acoc.conf, it would remove any matching
rules that had been installed for the diff command by either
/etc/acoc.conf or /usr/local/etc/acoc.conf.
After defining the program name and operational flags, matching rules can be
defined. These take the following form:
/regex/[flags] colour_spec
where regex is a Ruby-compatible regular expression. The delimiting
/ characters can be any character, as long as that character is not
present in the regular expression itself.
flags, if present, consists of one or more characters from the
following list:
Examples of a colour_group are white+bold,
black+on_white, etc. A complete colour_spec might
look like this:
red+bold,white,yellow+bold,black+on_green
Except when using the g flag, each component of the regex
that you wish to colour should be placed in parentheses. Text outside
parentheses will be used for matching, but will not be coloured.
For example, examine the following:
This will match a line that starts with more or one digits, followed by the
string foo and any amount of white space, followed by one or
more word characters. However, only the initial group of digits and
the group of word characters will be coloured. The string foo
and the white space that follows it will be used for matching, but will not be
coloured.
Separated from the regex by white space is the colour_spec.
Usually, you will include in this as many colours (separated by commas) as you
have parenthesised expressions in the regex. However, it's also
permissible to have fewer. If, for example, you have three parenthesised
expressions in the regex, but only two colours listed in the
colour_spec, then the second colour will be used for colouring both
the second and third matches.
If you have more colours listed in the colour_spec than there are
parenthesised expressions in the regex, the surplus colours are
ignored.
When using the g flag to perform a global match on the line, you may
list as many colours as you want. The same rules apply here. If there are more
matches than colours, the remaining matches will be coloured using the last
colour listed. Surplus colours are ignored.
acoc is only as good as the configuration file that it uses.
If you compose pattern-matching rules that you think would be useful to other
people, please send them to me for
inclusion in a subsequent release.
Overview and Introduction
Sample Screenshots
traceroute (green = fast, red = slow)
w
top (with root's processes shown in red)
Building an RPM with rpmbuild
Dependencies
Configuration
[program_spec]
@program_spec@
[rpm/ae,rpmbuild/ae]
[ls/p]
$ ls
file1 file2 file3 file4 file5 file6
$ ls
file1
file2
file3
file4
file5
file6
[diff/t,rcsdiff/t,cvs diff/t,p4 diff/t]
/ps -.*(e.*f|f.*e)/
@ps -.*[ef].*[ef]@
[tcpdump/r]
colour_spec is defined as a comma-separated list of one or more
colour_groups, which are defined as a plus-separated (+)
list of one or more of the following:
/^(\d+)foo\s*(\w+)/
Contributing
Files
File | Type |
---|---|
acoc-0.7.1.tar.gz | tarred and gzipped source |
acoc-0.7.1-1.noarch.rpm | Binary RPM |
acoc-0.7.1-1.src.rpm | Source RPM |
term-ansicolor-1.0.2.tgz | tarred and gzipped source of the Term::ANSIColor module |
ruby-term-ansicolor-1.0.2-1.i386.rpm | Binary RPM of the Term::ANSIColor module for Ruby 1.8 |
ruby-term-ansicolor-1.0.2-1.src.rpm | Source RPM of the Term::ANSIColor module |
tpty-0.0.1.tar.gz | tarred and gzipped source of the Ruby/TPty library |
ruby-tpty-0.0.1-2.i386.rpm | Binary RPM of the Ruby/TPty library for Ruby 1.8 |
ruby-tpty-0.0.1-2.src.rpm | Source RPM of the Ruby/TPty library |
You'll also need Ruby itself, of course.