Home

Awesome

colout — Color Up Arbitrary Command Output

<p align="center"> <img width="640" src="https://raw.githubusercontent.com/nojhan/colout/master/colout_logo.svg" alt="Colout logo" /> </p>

Synopsis

colout [-h] [-r [RESOURCE]]

colout [-g] [-c] [-l min,max] [-a] [-t] [-T DIR] [-P DIR] [-d COLORMAP] [-s] [-e CHAR] [-E CHAR] [--debug] PATTERN [COLOR(S) [STYLE(S)]]

Description

colout read lines of text stream on the standard input and output characters matching a given regular expression PATTERN in given COLOR and STYLE.

If groups are specified in the regular expression pattern, only them are taken into account, else the whole matching pattern is colored.

You can specify several colors or styles when using groups by separating them with commas. If you indicate more colors than groups, the last ones will be ignored. If you ask for fewer colors, the last one will be duplicated across remaining groups.

Available colors are: blue, black, yellow, cyan, green, magenta, white, red, rainbow, random, Random, Spectrum, spectrum, scale, Scale, hash, Hash, none, an RGB hexadecimal triplet (#11aaff, for example) or any number between 0 and 255.

Available styles are: normal, bold, faint, italic, underline, blink, rapid_blink, reverse, conceal or random (some styles may have no effect, depending on your terminal).

In some case, you can indicate a foreground and a background color, by indicating both colors separated by a period (for example: red.blue). You can also use this system to combine two styles (for example, for a bold style that also blinks: bold.blink).

rainbow will cycle over a the default colormap at each matching pattern. Rainbow will do the same over the default colormap for the 256-colors mode (this requires a terminal that supports the 256 color escape sequences).

Random will color each matching pattern with a random color among the default colormap (the 255 available in the ANSI table, by default). random will do the same in 8 colors mode.

spectrum and Spectrum are like rainbows, but with more colors (8 and 36 colors).

scale (8 colors) and Scale (256 colors) will parse the numbers characters in the matching text as a decimal number and apply the default colormap according to its position on the scale defined by the -l option (see below, "0,100" by default).

hash (8 colors) and Hash (256 colors) will take a fingerprint of the matching text and apply the default colormap according to it. This ensure that matching texts appearing several times will always get the same color.

Before interpreting the matched string as a number, colout will remove any character not supposed to be used to write down numbers. This permits to apply this special color on a large group, while interpreting only its numerical part.

You can use the name of a syntax-coloring "lexer" as a color (for example: "Cpp", "ruby", "xml+django", etc.).

If GIMP palettes files (*.gpl) are available, you can also use their names as a colormap (see the -P switch below).

Note that the RGB colors (either the hex triplets or the palettes's colors) will be converted to their nearest ANSI 256 color mode equivalents.

When not specified, a COLOR defaults to red and a STYLE defaults to bold.

colout comes with some predefined themes to rapidly color well-known outputs (see the -t switch below).

colout can be used as an interface to pygments (see also the --source switch below).

To have a list of all colors, styles, special colormaps, themes, palettes and lexers, use the -r switch (see below).

colout is released under the GNU Public License v3.

Installation

The recomended method is using pip to install the package for the local user:

$ pip install --user colout

Another method is using pipsi (pipsi is no longer maintained, https://github.com/mitsuhiko/pipsi/blob/db3e3fccbe4f8f9ed1104ed7293ec8fec6579efc/README.md#L3)

$ pipsi install colout

There is also a PPA for Ubuntu 16.04 (Xenial)/18.04 (Bionic) (@0.6.1-3~dist7, not actively maintained)

$ sudo add-apt-repository ppa:csaba-kertesz/random
$ sudo apt-get update
$ sudo apt-get/aptitude install colout

Options

Regular expressions

A regular expression (or regex) is a pattern that describes a set of strings that matches it.

colout understands regex as specified in the re python module. Given that colout is generally called by the command line, you may have to escape special characters that would be recognize by your shell.

Dependencies

Necessary Python modules:

Limitations

Don't use nested groups or colout will duplicate the corresponding input text with each matching colors.

Using a default colormap that is incompatible with the special colormap's mode (i.e. number of colors) will end badly.

Color pairs (foreground.background) work in 8-colors mode for simple coloring, but may fail with --colormap.

Examples

Simple

Somewhat useful

Bash alias

The following bash function color the output of any command with the cmake and g++ themes:

function cm()
{
    set -o pipefail
    $@ 2>&1  | colout -t cmake | colout -t g++
}

You then can use the cm alias as a prefix to your build command, for example: cm make test

GDB integration

You can use colout within the GNU debuger (gbd) to color its output. For example, the following script .gdbinit configuration will color the output of the backtrace command:

set confirm off

# Don't wrap line or the coloring regexp won't work.
set width 0

# Create a named pipe to get outputs from gdb
shell test -e /tmp/coloutPipe && rm /tmp/coloutPipe
shell mkfifo /tmp/coloutPipe

define logging_on
  # Instead of printing on stdout only, log everything...
  set logging redirect on
  # ... in our named pipe.
  set logging on /tmp/coloutPipe
end

define logging_off
  set logging off
  set logging redirect off
  # Because both gdb and our commands are writing on the same pipe at the same
  # time, it is more than probable that gdb will end before our (higher level)
  # commands.  The gdb prompt will thus render before the result of the command,
  # which is highly akward. To prevent this, we need to wait before displaying
  # the prompt again.  The more your commands are complex, the higher you will
  # need to set this.
  shell sleep 0.4s
end

define hook-backtrace
    # Note: match path = [path]file[.ext] = (.*/)?(?:$|(.+?)(?:(\.[^.]*)|))
    # This line color highlights:
    # – lines that link to source code,
    # – function call in green,
    # – arguments names in yellow, values in magenta,
    # — the parent directory in bold red (assuming that the debug session would be in a "project/build/" directory).
    shell cat /tmp/coloutPipe | colout "^(#)([0-9]+)\s+(0x\S+ )*(in )*(.*) (\(.*\)) (at) (.*/)?(?:$|(.+?)(?:(\.[^.]*)|)):([0-9]+)" red,red,blue,red,green,magenta,red,none,white,white,yellow normal,bold,normal,normal,normal,normal,normal,bold,bold,bold | colout "([\w\s]*?)(=)([^,]*?)([,\)])" yellow,blue,magenta,blue normal | colout "/($(basename $(dirname $(pwd))))/" red bold &
    logging_on
end
define hookpost-backtrace
    logging_off
end

# Don't forget to clean the adhoc pipe.
define hook-quit
    set confirm off
    shell rm -f /tmp/coloutPipe
end

Take a look at the example.gdbinit file distributed with colout for more gdb commands.

Themes

You can easily add your own theme to colout. A theme is basically a module with a function named theme that take the configuration context as an argument and return back the (modified) context and a list of triplets. Each triplet figures the same arguments than those of the command line interface.

def theme(context):
    return context,[ [regexp, colors, styles] ]

With the context dictionary at hand, you have access to the internal configuration of colout, you can thus change colormaps for special keywords, the scale, even the available colors, styles or themes.

See the cmake them for how to modify an existing colormap if (and only if) the user didn't ask for an alternative one. See the ninja theme for how to extend an existing theme with more regexps and a different configuration. See the gcc theme for an example of how to use the localization of existing softwares to build translated regexp.

Buffering

Note that when you use colout within real time streams (like tail -f X | grep Y | colout Z) of commands, you may observe that the lines are printed by large chunks and not one by one, in real time. This is not due to colout but to the buffering behavior of your shell.

To fix that, use stdbuf, for example: tail -f X | stdbuf -o0 grep Y | colout Z.

Authors