コマンド結果などをhtmlに変換してくれるaha

色付きのコマンドの結果などから html に変換してくれる aha というコマンドを少し試してみました．

theZiz/aha: Ansi HTML Adapter

Debian では aha パッケージがあるのでパッケージから導入しました．

$ sudo apt install aha

aha の help を変換してみます．

$ aha -h | aha -b | xsel

Ansi Html Adapter Version 0.5.1
aha takes SGR-colored Input and prints W3C conform HTML-Code
use: aha <options> [-f file]
aha (--help|-h|-?)
aha reads the Input from a file or stdin and writes HTML-Code to stdout
Options:
--black, -b: Black Background and White "standard color"
--pink, -p: Pink Background
--style X, -y X: Set the style used in the element
--stylesheet, -s: Use a stylesheet instead of inline styles
--iso X, -i X: Uses ISO 8859-X instead of utf-8. X must be 1..16
--title X, -t X: Gives the html output the title "X" instead of
"stdin" or the filename
--lang X, -L X: Uses the ISO 639-1 code X for the language
--line-fix, -l: Uses a fix for inputs using control sequences to
change the cursor position like htop. It's a hot fix,
it may not work with any program like htop. Example:
echo q | htop | aha -l > htop.htm
--word-wrap, -w: Wrap long lines in the html file. This works with
CSS3 supporting browsers as well as many older ones.
--no-header, -n: Don't include header into generated HTML,
useful for inclusion in full HTML files.
--no-xml, -x: Don't use doctype xml but html (may useful for old
browsers like IE)
--css X, -c X: Add css file X to the output. In fact just adds
to the header.
--ignore-cr, -r: Ignore all carriage-returns (ASCII sign 13, \r)
which may lead to double new lines in html.
Examples:
Create an HTML file with a black background, a custom title and
a larger font-size using aha's help:

$ aha -h | aha -b -t 'aha help' -y 'font-size:1.8em' > aha-help.html

Create an HTML file with a white background using the output of diff

$ diff -u --color=always oldfile.c newfile.c | aha > diff.html

Create an HTML file with a black background from the output of htop.
You have to use option -l due the other new-line-commands htop uses:

$ echo q | htop | aha -b -l > htop.html

Create an HTML file from the output of this man page. man uses bold
and underline formatting from nroff, which ul converts to SGR:

$ MAN_KEEP_FORMATTING=1 COLUMNS=80 man aha | ul | aha > man-aha.html

Copyleft Alexander Matthes aka Ziz 2020
ziz@mailbox.org
https://github.com/theZiz/aha
This application is subject to the MPL1.1 or LGPLv2+.

$ colortest-256 | aha -b | xsel

System colors:

Color cube, 6x6x6:

Grayscale ramp:

lolcat もと思ったら，

$ cowsay hello | lolcat | aha -b -n | xsel

色が付いていません．

_______
< hello >
-------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||

パイプのところで消えちゃっています．パイプがあるとパイプの後ろのコマンドが扱えないかもしれないと気を使って色を落としています．

$ echo hello | lolcat | od -xc
0000000 6568 6c6c 0a6f
h e l l o \n
0000006

man lolcatより -f で出力出来るようです．

-f, --force
Force color even when stdout is not a tty.

出ました :)

$ echo hello | lolcat -f | od -xc
0000000 5b1b 3833 323b 313b 3032 323b 3b31 3432
033 [ 3 8 ; 2 ; 1 2 0 ; 2 1 ; 2 4
0000020 6d31 1b68 335b 6d39 5b1b 3833 323b 313b
1 m h 033 [ 3 9 m 033 [ 3 8 ; 2 ; 1
0000040 3631 323b 3b34 3432 6d33 1b65 335b 6d39
1 6 ; 2 4 ; 2 4 3 m e 033 [ 3 9 m
0000060 5b1b 3833 323b 313b 3231 323b 3b36 3432
033 [ 3 8 ; 2 ; 1 1 2 ; 2 6 ; 2 4
0000100 6d35 1b6c 335b 6d39 5b1b 3833 323b 313b
5 m l 033 [ 3 9 m 033 [ 3 8 ; 2 ; 1
0000120 3730 323b 3b39 3432 6d36 1b6c 335b 6d39
0 7 ; 2 9 ; 2 4 6 m l 033 [ 3 9 m
0000140 5b1b 3833 323b 313b 3330 333b 3b32 3432
033 [ 3 8 ; 2 ; 1 0 3 ; 3 2 ; 2 4
0000160 6d38 1b6f 335b 6d39 5b1b 3833 323b 393b
8 m o 033 [ 3 9 m 033 [ 3 8 ; 2 ; 9
0000200 3b39 3433 323b 3934 1b6d 335b 6d39 000a
9 ; 3 4 ; 2 4 9 m 033 [ 3 9 m \n
0000217

_______
< hello >
-------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||

lolcat は -f オプションで行けましたがそのようなオプションがない場合は unbuffer コマンドなどが使えます． unbuffer コマンドは Debian では expect パッケージに含まれています．

Tip

以下のようなものもあるようですが未検証．

Sasasu/ColorThis: make your pipe and logfile colorful

lilydjwg/stdoutisatty: Make programs think their stdout is a tty / terminal.

zsh: 22 Zsh Modules

$ apt moo moo | aha -b -n

(__)
_______~(..)~
,----\(oo)
/|____|,'
* /"\ /\
~ ~ ~ ~
..."Have you mooed today?"...

$ unbuffer apt moo moo | aha -b -n

(__)
_______~(..)~
,----\(oo)
/|____|,'
* /\ /\
wWwWwWwWwWwWwWwWwWwWwWwWwWwWw
..."Have you mooed today?"...

スクリーンショットの方が確実ですが，ちょっとしたことなら html の方がサイズも小さく良いかもしれません．

参考URL

ILoveCandy and lolcat? : r/archlinux

コンソールのカラー出力 – ArchWiki

環境

$ dpkg-query -W aha xsel colortest cowsay lolcat coreutils apt
aha 0.5.1-3
apt 2.9.8
colortest 20110624-9
coreutils 9.4-3.1
cowsay 3.03+dfsg2-8
lolcat 100.0.1-4
xsel 1.2.1-1
$ lsb_release -dr
Description: Debian GNU/Linux trixie/sid
Release: n/a
$ arch
x86_64