Live data from Hacker News

Colout – add colors to a text stream in your terminal

nojhan.github.io

31–40 of 41 posts

Re: Colout – add colors to a text stream in your terminal

#31
post #30

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,…

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

Re: Colout – add colors to a text stream in your terminal

#32

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 

Re: Colout – add colors to a text stream in your terminal

#33
post #30

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,…

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

Re: Colout – add colors to a text stream in your terminal

#34
post #17

Earlier quoted context omitted.

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

Re: Colout – add colors to a text stream in your terminal

#35
post #27
post #26

Why did you have to switch to python3? https://github.com/nojhan/colout/commit/37934e9266d05e1519ac... https://github.com/nojhan/colout/commit/64454e42f1cf5a4f8cd4... Update: 37934e9 still works with python 2.6

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.

Re: Colout – add colors to a text stream in your terminal

#36
post #34

Earlier quoted context omitted.

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

Re: Colout – add colors to a text stream in your terminal

#37
post #25

Earlier quoted context omitted.

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=^\ 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,…

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)

Re: Colout – add colors to a text stream in your terminal

#38
post #25

Earlier quoted context omitted.

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=^\ 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,…

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

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

Re: Colout – add colors to a text stream in your terminal

#39
post #33
post #30

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,…

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.

Re: Colout – add colors to a text stream in your terminal

#40
post #34

Earlier quoted context omitted.

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
Post reply on HN