

Colout – add colors to a text stream in your terminal - nojhan
http://nojhan.github.io/colout/

======
lobster_johnson
Not entirely sure of the feature overlap, but this seems to reinvent grcat and
its frontend, grc [1], a little bit.

Grcat lets you configure commands with colour regexps. Then you just prefix
the command with "grc", and it will colorize the output according the rules.
It's useful to alias commands this way:

    
    
        $ alias make="grc make"
    

colout seems most useful when you are want to highlight arbitrary patterns
independent of the command, eg. "tail some.log | colout ...".

[1]
[http://kassiopeia.juls.savba.sk/~garabik/software/grc/README...](http://kassiopeia.juls.savba.sk/~garabik/software/grc/README.txt)

~~~
nojhan
Indeed. Having to build configuration file for each command was a pain in the
ass. Colout is better integrated in my shell workflow and shows that config
files are not mandatory (but colout themes do just the same).

Plus, colout have a tons of additional features (256-colors mode, colormaps,
etc.).

~~~
lobster_johnson
Really? grcat/grc's configs are very simple. For example, this is the config
for colorizing diffs:

    
    
        regexp=^\+(.*$)
        colours=bold green
        count=more
        =======
        regexp=^\-(--.+$|[^\-].*$|$)
        colours=bold red
        count=more
        ========
        regexp=^\>([^\>].*|$)
        colours=bold green
        count=more
        =======
        regexp=^\<([^\<].*|$)
        colours=bold red
        count=more
        =======
        regexp=^@@ .* @@$
        colours=magenta
        count=more
    

I agree that colout works better for ad-hoc stuff.

You should consider inverting the way it's invoked, though, to be more like
grc, otherwise you can't reliably use colout in an alias (as pointed out
elsewhere in this thread).

~~~
nojhan
You can, it's just a matter of knowing how to write a function for your shell.

~~~
nillian
Exactly.

ls example in zsh:

    
    
      alias ls="ls -lph1 --color=always"
    
      function ls {
        /bin/ls $@ | colout [regex] colours
      }
    

This will pass all arguments after "ls" to the /bin/ls command. This also
means you can keep your existing aliases clean and tidy for common params.

Remember to use the full path (or /usr/bin/env maybe) so that you don't create
a recursive call to your function ;)

This doesn't deal with the situation of being unable to get the return value
of the original command, but depending on your needs, this may not be an
issue.

(edited for spacing)

------
mhw
Just to be clear, in case the post's headline misleads you, there's nothing OS
X specific about colout - it'll work just as happily on Linux. The
documentation even points to an PPA from where you can install an Ubuntu
package.

------
msluyter
Just installed it via pip and gave it a try. I like it! I think I can already
find some interesting uses for it. A couple of suggestions:

The website says:

    
    
      colout -r will give you the lists of available colors,     
      colormaps, themes and supported programming languages.
    

But I think that's a typo and should be 'colout -h'.

Also, a handy alias list of common use cases would be great.

~~~
nojhan
The `-h` switch show the help, but the `-r` show the resources loaded at
startup.

Alias for common use cases are named "themes" in the list of resources.

~~~
msluyter
Hmm... when I run colout -r, I get:

    
    
       % colout -r
       usage: colout.py [-h] [-g] [-c] [-l SCALE] [-a] [-t] [-s]
                      REGEX [COLOR] [STYLE]
       colout.py: error: too few arguments

~~~
nojhan
That's a bug I'm still trying to fix, but the argparse module made it somewhat
difficult to find an elegant solution.

An easy workaround is to indicate a fake pattern argument:

    
    
        colout -r x

~~~
msluyter

       % colout -r x
       usage: colout.py [-h] [-g] [-c] [-l SCALE] [-a] [-t] [-s]
                     REGEX [COLOR] [STYLE]
       colout.py: error: unrecognized arguments: -r
    

:(

~~~
nojhan
You've got a too old version, update to the current one and you will see
something like:

    
    
        [:~/code/colout/colout] parse_r+* ± colout -r x
        Available resources:
        STYLES: reverse, bold, italic, normal, conceal, rapid_blink, faint, underline, blink
        COLORS: blue, none, black, yellow, cyan, green, magenta, white, red
        SPECIAL: random, Random, scale, Scale, colormap
        THEMES: g++, cmake, json, perm
        COLORMAPS: rainbow, jet72, Spectrum, spectrum, Rainbow
        LEXERS: Cucumber, abap, ada, ahk, antlr, antlr-as, antlr-cpp, antlr-csharp, antlr-java, antlr-objc, antlr-perl, antlr-python, antlr-ruby, apacheconf, applescript, as, as3, aspx-cs, aspx-vb, asy, basemake, bash, bat, bbcode, befunge, blitzmax, boo, brainfuck, c, c-objdump, cfm, cfs, cheetah, clojure, cmake, coffee-script, common-lisp, console, control, cpp, cpp-objdump, csharp, css, css+django, css+erb, css+genshitext, css+mako, css+myghty, css+php, css+smarty, cython, d, d-objdump, delphi, diff, django, dpatch, duel, dylan, erb, erl, erlang, evoque, factor, felix, fortran, gas, genshi, genshitext, glsl, gnuplot, go, gooddata-cl, groff, haml, haskell, html, html+cheetah, html+django, html+evoque, html+genshi, html+mako, html+myghty, html+php, html+smarty, html+velocity, hx, hybris, ini, io, ioke, irc, jade, java, js, js+cheetah, js+django, js+erb, js+genshitext, js+mako, js+myghty, js+php, js+smarty, jsp, lhs, lighty, llvm, logtalk, lua, make, mako, maql, mason, matlab, matlabsession, minid, modelica, modula2, moocode, mupad, mxml, myghty, mysql, nasm, newspeak, nginx, numpy, objdump, objective-c, objective-j, ocaml, ooc, perl, php, postscript, pot, pov, prolog, properties, protobuf, py3tb, pycon, pytb, python, python3, ragel, ragel-c, ragel-cpp, ragel-d, ragel-em, ragel-java, ragel-objc, ragel-ruby, raw, rb, rbcon, rconsole, rebol, redcode, rhtml, rst, sass, scala, scaml, scheme, scss, smalltalk, smarty, sourceslist, splus, sql, sqlite3, squidconf, ssp, tcl, tcsh, tex, text, trac-wiki, v, vala, vb.net, velocity, vim, xml, xml+cheetah, xml+django, xml+erb, xml+evoque, xml+mako, xml+myghty, xml+php, xml+smarty, xml+velocity, xquery, xslt, yaml

------
Fuzzwah
Love this. Thanks.

I can see a bunch of ways I'll make use of this. The first one I attempted to
work on involved trying to color scale the use% in the output of a "df -h".

Alas it didn't work as I expected it:

    
    
        $ df -h | colout --scale 0,100 "([0-9]*)%" scale
        ValueError: could not convert string to float:
    

However if I grep out just the /dev/sda line it does what I expected:

    
    
        $ df -h | grep /dev/sda | colout --scale 0,100 "([0-9]*)%" scale
        /dev/sda         20G  3.2G   16G  17% /
    

The 17% is a pretty blue.

Tips?

~~~
nojhan
You use the "zero or more" * operator, which will match empty strings in front
of the "%". But an empty string cannot be converted to a float. Just use the
"at least one" operator: +. Also, the scale is 0,100 by default, you can omit
it.

    
    
        df -h | colout "([0-9]+)%" scale

~~~
Fuzzwah
Thanks for the response. My lazy regex makes me feel bad.

------
JeffJenkins
I really like the idea of alternating the color of lines of output, although I
might use something a bit less bold. Is there an easy way to have command
piped through this in bash?

~~~
shabble
one problem of the 'alias yourcmd to yourcmd | colorize' is dealing with
preserving the return code from the actual command, rather than the last in
the pipeline.

It can be done with things liek $PIPESTATUS, but some sort of wrapper/function
to do it would be nice.

------
lysium
Why did you have to switch to python3?

[https://github.com/nojhan/colout/commit/37934e9266d05e1519ac...](https://github.com/nojhan/colout/commit/37934e9266d05e1519ac16d5269fef0b3f3b38e3)

[https://github.com/nojhan/colout/commit/64454e42f1cf5a4f8cd4...](https://github.com/nojhan/colout/commit/64454e42f1cf5a4f8cd41480adb00ae0aa0c74cb)

Update: 37934e9 still works with python 2.6

~~~
nojhan
There is some incompatibility, like the gettext module in the g++ theme, the
way themes are imported, unicode support, the print statement… But maybe it's
still possible to write a code that work on both versions?

~~~
lysium
I see. As a quick fix, I tried to call python 2.6 from the colout script, but
that failed because importlib exists only in python3. I don't know how to make
it work on both versions, I'm afraid.

------
JonnieCache
The background image of this site causes a weird strobing effect on the screen
of my MacBook Pro 6,2. _Really_ odd.

When I drag it over to the cinema display it looks fine.

Noone else here in the office can see it, both those with good vision and
those who are technically blind. Maybe I have special powers. Or a brain
tumor.

EDIT: The tool is great btw :)

~~~
sp332
Try this test page: <http://www.lagom.nl/lcd-test/inversion.php>

~~~
gknoy
This is somewhere between "oh god my eyes" and extremely interesting. Thanks
for sharing that!

------
akkartik
It doesn't do alternating colors, but this reminded me of _cw_ , which saves
you the trouble of explicitly piping the output of common commands.

<http://freecode.com/projects/cw>

------
tealtan
This might be a dumb question: are there shells that have a HTML-based front
and passes commands into the background? Seems like something like that would
be far more flexible than this – which feels like a Jenga tower of hacks.

~~~
caseyohara
TermKit[1] was built in this way a few years ago. It was novel at the time and
the demo[2] seemed promising, but looks like it hasn't been updated in ~2
years now.

[1] <https://github.com/unconed/TermKit>

[2] <http://www.youtube.com/watch?v=_6Z5dnlfcls>

------
ChikkaChiChi
I like the idea of colored permissions. Even if your aren't looking
specifically at them an ls -latest would draw your attention if something
we're set insecurely.

------
ilink
I was just thinking about how useful something like this would be. Thanks for
reading my mind! And now to see if I can get it to work under cygwin.

------
ajanuary
Is it just me or is there a bug shown in their second example? The closing
square bracket isn't coloured.

~~~
nojhan
That's because the log was already colored (the word "fail" in red). As colout
tries to color the whole line, the previous end escape character is
interpreted by the terminal before the last one :

    
    
        <red>* This is a colored line [<red>fail<end>]</end>
    

(You can use the `--debug` switch of colout to get hints about such problems).

------
iso8859-1
pygmentize replicates the syntax highlighting featured here, except it is a
bit more configurable: <http://pygments.org/docs/cmdline/>

~~~
nojhan
It's not really a replication, as colout just _USE_ the pygments library.
Colout just add the possibility to color code that is located inside a text
that should not be colored, which pygmentize cannot do:

    
    
        colout -a -s Python monokai < code.py # just like pygmentize
        colout "^(:*)'(:*)'(:*)$" blue,python,blue < code.py # just the code 'inside quotes', not the rest of the line

