• Stars
    star
    1,099
  • Rank 42,192 (Top 0.9 %)
  • Language
    Python
  • License
    GNU General Publi...
  • Created over 12 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Color text streams with a polished command line interface

colout β€” Color Up Arbitrary Command Output

Colout logo

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

  • -h, --help: Show a help message and exit

  • -g, --groups: For color maps (like "rainbow"), iterate over matching groups in the pattern instead of over patterns.

  • -c, --colormap: Use the given list of comma-separated colors as a colormap (cycle the colors at each match).

  • -l min,max, --scale min,max: When using the 'scale' colormap, parse matches as decimal numbers (taking your locale into account) or as arithmetic expression (like "1+2/0.9*3") and apply the rainbow colormap linearly between the given min,max (0,100, by default).

  • -a, --all: Color the whole input at once instead of line per line (really useful for coloring a source code file with strings on multiple lines).

  • -t, --theme: Interpret PATTERN as a predefined theme (perm, cmake, g++, etc.).

  • -T DIR, --themes-dir DIR: Search for additional themes (colout_*.py files) in this directory.

  • -P DIR, --palettes-dir DIR: Search for additional palettes (*.gpl files) in this directory.

  • -d COLORMAP, --default COLORMAP: When using special colormaps (random, scale or hash), use this COLORMAP instead of the default one. This can be either one of the available colormaps or a comma-separated list of colors. WARNING: be sure to specify a default colormap that is compatible with the special colormap's mode, or else the colors may not appear the same. Also, external palettes are converted from RGB to 256-ANSI and will thus not work if you use them as default colormaps for a 8-colors mode special color.

  • -r [TYPE(S)], --resources [TYPE(S)]: Print the names of available resources. Use a comma-separated list of resources names (styles, colors, special, themes, palettes, colormaps or lexers), use 'all' (or no argument) to print all resources.

  • -s, --source: Interpret PATTERN as source code readable by the Pygments library. If the first letter of PATTERN is upper case, use the 256 color mode, if it is lower case, use the 8 colors mode. In 256 color mode, interpret COLOR as a Pygments style (e.g. "default").

  • -e CHAR, --sep-list CHAR: Use this character as a separator for list of colors/resources/numbers (instead of comma).

  • -E CHAR, --sep-pair CHAR: Use this character as a separator for foreground/background pairs (instead of period).

  • --debug: Debug mode: print what's going on internally, if you want to check what features are available.

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:

  • pygments for the source code syntax coloring
  • babel for a locale-aware number parsing

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

  • Color in bold red every occurrence of the word color in colout sources: cat colout.py | colout color red bold

  • Color in bold violet home directories in /etc/passwd: colout '/home/[a-z]+' 135 < /etc/passwd

  • Color in yellow user/groups id, in bold green name and in bold red home directories in /etc/passwd: colout ':x:([0-9]+:[0-9]+):([^:]+).*(/home/[a-z]+)' yellow,green,red normal,bold < /etc/passwd

  • Color in yellow file permissions with read rights for everyone: ls -l | colout '.(r.-){3}' yellow normal

  • Color in green read permission, in bold red write and execution ones: ls -l | colout '(r)(w*)(x*)' green,red normal,bold

  • Color in green comments in colout sources: colout '.*(#.*)$' green normal < colout.py

  • Color in bold green every numbers and in bold red the words error in make output: make 2>&1 | colout '[0-9]+' green normal | colout error

Somewhat useful

  • Use a different color for each line of the auth log grep user /var/log/auth.log | colout "^.*$" rainbow

  • Color each line of a file with a different color among a 256 color gradient from cyan to green: head /var/log/auth.log | colout -c "^.*$" 39,38,37,36,35,34

  • Color permissions with a predefined template: ls -l | colout -t perm

  • Color in light green comments in non-empty colout sources, with the sharp in bold green: grep -v '^\s*$' colout.py | colout '.*(#)(.*)$' green,119 bold,normal

  • Color a make output, line numbers in yellow, errors in bold red, warning in magenta, pragma in green and C++ file base names in cyan: make 2>&1 | colout ':([0-9]+):[0-9]*' yellow normal | colout error | colout warning magenta | colout pragma green normal | colout '/(\w+)*\.(h|cpp)' cyan normal Or using themes: make 2>&1 | colout -t cmake | colout -t g++

  • Color each word in the head of auth.log with a rainbow color map, starting a new colormap at each new line (the beginning of the command is just bash magic to repeat the string "(\w+)\W+": L=$(seq 10) ; P=${L//??/(\\w+)\\W+} ; head /var/log/auth.log | colout -g "^${P}(.*)$" rainbow

  • Color source code in 8 colors mode, without seeing comments: cat colout.py | grep -v "#" | colout -s python

  • Color source code in 256 color mode: cat colout.py | colout -s Python monokai

  • Color a JSON stream: echo '{"foo": "lorem", "bar":"ipsum"}' | python -mjson.tool | colout -t json

  • Color a source code substring: echo "There is an error in 'static void Functor::operator()( EOT& indiv ) { return indiv; }' you should fix it" | colout "'(.*)'" Cpp monokai

  • Color the percent of progress part of a CMake's makefile output, with a color related to the value of the progress (from 0%=blue to 100%=red): cmake .. && make | colout "^(\[\s*[0-9]+%\])" Scale

  • Color hosts and users in auth.log, with consistent colors: cat /var/log/auth.log | colout "^(\S+\s+){3}(\S+)\s(\S+\s+){3}(\S+)\s+(\S+\s+){2}(\S+)\s*" none,hash,none,hash,none,hash

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

More Repositories

1

liquidprompt

A full-featured & carefully designed adaptive prompt for Bash & Zsh
Shell
4,305
star
2

paradiseo

An evolutionary computation framework to (automatically) build fast parallel stochastic optimization solvers
C++
82
star
3

dotfiles

My config files
Vim Script
41
star
4

geometromap

Geographically accurate Paris metro map
Shell
40
star
5

tunnelmon

An (auto)ssh tunnel monitor. Gives an interactive CLI to monitor existing SSH tunnels, and tunnels that are managed with autossh.
Python
28
star
6

ubergeekism

An attempt at using as many as possible COOL computer science stuff to produce a single image (Lindenmayer system, Penrose tiling, Travelling Salesman Problem, Ant Colony Optimization, A*, etc.).
Python
15
star
7

clutchlog

C++ "spatial" logging system which targets versatile, (de)clutchable, _debugging_, in a single header.
C++
13
star
8

exceptions

C++ exception class and macros with easy nested declaration and localisation data
C++
11
star
9

moderncv-math

TeX
9
star
10

hanafuda

Set of Hanafuda cards in the form of 12 pictures.
Shell
9
star
11

kalolo

A dark 256-colors scheme for the kakoune editor, designed for usability and consistency.
KakouneScript
9
star
12

eodev

NOTE: now superseeded by the paradiseo repository.
C++
8
star
13

crtp_functor_ttp

Some examples to compare the performances of several implementations of the Functor pattern (CRTP and Inheritance)
C++
7
star
14

legood

Base SVG objects for drawing diagrams with Lego bricks.
6
star
15

descarwin

The DAE and YAHSP temporal planning solvers
TeX
6
star
16

algopattern

Educational demo of design patterns which are useful for algorithmics
Java
4
star
17

aapssfc

Apprendre Γ  programmer sans se faire chier
JavaScript
4
star
18

lp-dotmatrix

A carefully designed theme for Liquid Prompt, that will change the way you think about your prompt.
3
star
19

clockical

An SVG+javascript clock
JavaScript
3
star
20

named-pipes-services

Example of how to build services that use named pipes FIFO as I/O
C++
3
star
21

paternoster

A command line tool that call the given command for each input line matching the given regular expression pattern.
Python
3
star
22

weboob-devel

a collection of applications able to interact with websites, without requiring the user to open them in a browser
Python
3
star
23

flicksave

Automagically backup a time stamped snapshot of a file that you edit with any program, each time you hit "save".
Python
2
star
24

conky-jinx

A minimal Conky curses theme, to display a simple system monitor on a phone-size terminal.
2
star
25

github-stars-stats

Python
2
star
26

cholesky

Several C++ implementations of the Cholesky factorization
2
star
27

forthlift

A command line app to post sequences of text lines on social media: prepare your sequence of status in your text editor, then post everything.
Python
2
star
28

arshipery

Simulator of playing battleships with archery, to find the best ships placement strategy.
Python
2
star
29

coloridadd

A script to generate shapes representing colors, using the ColorAdd code developped by Miguel Neiva: http://coloradd.net The ColorAdd code is a monochromatic Graphical Code allowing colorblind to recognize colors.
Python
2
star
30

lp-pureship

Themes for Liquid Prompt, emulating Spaceship/Starship and Pure prompts.
1
star
31

illuia

Python
1
star
32

stripit

StripIt is a SVG webcomic gallery manager. It permits to display images along with metadata extracted from a SVG source file.
PHP
1
star
33

lp-neon

A theme for Liquid Prompt, featuring a "techno-ascii-art" style on three lines.
1
star