[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#605380: ITP: aha -- ANSI HTML Adapter



On Tue, Nov 30, 2010 at 12:20:52AM +0100, Axel Beckert wrote:
> Adam Borowski wrote:
> > If there indeed is none, you can use my implementation:
> > 
> > http://angband.pl/svn/kbtin/trunk/ansi2html.c
> > 
> > It can handle all standard SGR codes.  There's no support for xterm-256
> > colors, but I can add that if it would be useful for someone.
> 
> JFTR: "echo q | env TERM=xterm htop | ansi2html" doesn't catch all
> codes either.

htop is a full-screen rather than a line based program.  Instead of a body
of text with color tags, it produces codes for a terminal with an
addressable cursor: instead of lines separated with \n, it says "go to
position 0,15; write 'foo' there".

This can be decoded only by simulating a terminal and then taking a snapshot
at the end.  There are libraries which can do this -- heck, I've written one
myself, yet this is something pretty costly and limited.  You'd have to
assume an arbitrary fixed number of columns, keep all the text in memory
while processing, and can't do this piecemeal -- so a pipe which does the
equivalent of "tail -f" would be impossible.

And to make it funnier, htop's output ends with a screen clear, so you'd end
up with an empty HTML -- so you need a frame other than the final one.  With
such a scope creep, this functionality would rather belong in one of
ttyrec-handling tools (http://en.wikipedia.org/wiki/ttyrec has a list).


If you're interesting in coloring syslogs/IRC logs/MUD logs/build logs[1]/
colordiffs/etc, which can often take tens of megabytes, you'd want to limit
yourself to plain ANSI text.  Ie, anything that "less -R" can show.

> But "screen -c /dev/null -L ccal ; cat screenlog.0 | ./ansi2html > ccal.html"
> works fine with your implementation. "screen -c /dev/null -L htop ; cat
> screenlog.0 | ./ansi2html > htop2.html" and quickly pressing works only
> slightly better.

I see the problem with ccal is that it disables all coloring if stdout is
not a terminal (isatty() in C, [ -t 1 ] in bash).  Most programs like ls,
git or colorgcc do this as well but have an argument like --color=always.

There's a number of programs which fake a tty.  It might be clearer to use a
dedicated one, but that would add a dependency, abusing "script" (priority:
Required) doesn't:

script -q /dev/null -c "ccal"

> P.S.: Looking at http://angband.pl/svn/kbtin/trunk/COPYING I'd expect
> ansi2html is GPL'ed code, right?

Yes, but only because the program it's bundled with is a code base starting
in the 80s; ansi2html can be of any license if you wish so.


[1]. I've seen ANSI color codes in build logs quite a few times.  It might
be a good idea to process them with ansi2html or another such program.  But
then, this may unleash a wave of colorgcc use on us :p

-- 
1KB		// Microsoft corollary to Hanlon's razor:
		//	Never attribute to stupidity what can be
		//	adequately explained by malice.

Attachment: signature.asc
Description: Digital signature


Reply to: