Googling around I have found that Perl CPAN has the HTML::FromANSI module. Also, this module installs ansi2html which accepts input from stdin.
ls --color | ansi2html -p > my_web_page.html
ls --color | ansi2html > my_snpipet_code-no_header-footer.html
But I prefer the default output from ansi2html.sh from pixelbeat
Unfortunately the ls --color get properly converted to HTML but the git one not. No matter which script I use. Could it be bacause the color is defined in the config as color.ui=auto?
git diff HEAD master -- ensembl/sql/CVS/Tag | ansi2html -p > ~/public_html/htdocs_dev/diff1.html
git diff HEAD master -- ensembl/sql/CVS/Tag | ansi2html.sh --bg=dark > ~/public_html/htdocs_dev/diff2.html
[UPDATE]
Yes! the problem would be that I have in the configuration color.ui=auto because explicitly having --color in the command make it work:
$ git diff --color HEAD master -- ensembl/sql/CVS/Tag | ansi2html.sh --bg=dark > ~/public_html/htdocs_dev/diff3.html
6 comments:
libcaca can do this too with good UTF-8 support as well.
Nice post, I'm sure this bit a lot of people too =)
The problem you had was due to git being clever and stripping colors from the output when piped to another command. This is pretty useful since some pagers (like "less") don't understand ANSI colors, so:
$ git diff --color HEAD HEAD^1 | less
becomes a huge mess to see, while:
$ git diff --color=auto HEAD HEAD^1 | less
shows up ok. Same for ls --color=auto. Even "ack" does that very same trick.
For those interested in the implementation, you just test if STDOUT (or STDERR) is opened to a tty, using the -t file test operator. Something like:
# colors only if we're not being piped
$ENV{ANSI_COLORS_DISABLED} = 1 if not -t *STDERR;
thanks @garu for your comment, you are right, exept in the 'less' bit. There are some less-terminal-setup that are broken and this does not work, but in my linux systems 'less' accepts the color with a direct pipe or reading a file that have the redirected output. In this last case 'less' ask if I want to open a binary file, I say YES and less show the text with the colors correctly.
But as you say having the color=auto is a good thing and only if I really want the redirection with color I will add the --color option.
@anonymous. Caca labs (despite its scatological naming tendency) has very nice tools like libcaca. But despite I was tempted to explore it, I had the impression that it was going to take me more time that I had for that task. So I tried only the ready made solutions. For example I used H::FA because it has the ready made script. When I have more time I will explore more possibilities and other libraries better. By the way do you have a ready made example using libcaca?
Garu, less understands ansi colours. Try "less -r" (or -R) :)
Great tool, thanks for sharing.
Post a Comment