Hacker News new | past | comments | ask | show | jobs | submit login
Colout – add colors to a text stream in your terminal (nojhan.github.io)
116 points by nojhan on June 6, 2013 | hide | past | web | favorite | 40 comments



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...


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.).


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).


Having just seen both utilities, colout looks very useful while grc looks like a PITA. I don't want to write configs for every adhoc command.


Yeah, grc is not for ad hoc utilities. It's meant to work with tools you use frequently, like make, diff, gcc, ping etc. where the syntax is known.


It should not be too difficult to use the same config files in colout, btw.


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


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)


I was referring to the exit code problem. Is it solvable?


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.


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.


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.


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


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


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


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


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?


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


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


Also, the install docs mention python2.7 but after a bit of head to brickwall I realised that this is trying to use python3.

To get it working I needed to do:

    sudo python3 setup.py install
And /usr/local/bin/colout was created for me (unlike what it mentions in the readme atm).


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?


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.



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?


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.


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 :)



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


Thanks. As always HN delivers the goods.


Does the same on my Thinkpad W520 - thought it was something to do with my panel.


Same to my 2012 MBP.


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.


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


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


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.


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.


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


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).


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


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




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: