aha – matoken's blog https://matoken.org/blog Is there no plan B? Mon, 21 Oct 2024 23:09:16 +0000 ja hourly 1 https://wordpress.org/?v=7.1.5 https://matoken.org/blog/wp-content/uploads/2025/03/cropped-1865f695c4eecc844385acef2f078255036adccd42c254580ea3844543ab56d9-32x32.jpeg aha – matoken's blog https://matoken.org/blog 32 32 terminal-to-htmlコマンドで端末の結果をHTMLに変換する https://matoken.org/blog/2024/10/22/convert-terminal-results-to-html-with-terminal-to-html-command/ https://matoken.org/blog/2024/10/22/convert-terminal-results-to-html-with-terminal-to-html-command/#respond Mon, 21 Oct 2024 22:33:58 +0000 https://matoken.org/blog/?p=4170

先日 aha というコマンドを試しましたが,今回 terminal-to-html という似たコマンドを試してみました.

導入とhelp
$ go install github.com/buildkite/terminal-to-html/v3/cmd/terminal-to-html@latest
$ which terminal-to-html
/home/matoken/go/bin/terminal-to-html
$ terminal-to-html --help
terminal-to-html - turn ANSI in to HTML

STDIN/STDOUT USAGE:
  cat input.raw | terminal-to-html [arguments...] > out.html

WEBSERVICE USAGE:
  terminal-to-html --http :6060 &
  curl --data-binary "@input.raw" http://localhost:6060/terminal > out.html

OPTIONS:
  --http value              HTTP service mode (eg --http :6060), endpoint is /terminal
  --preview                 wrap output in HTML & CSS so it can be easily viewed directly in a browser (default: false)
  --log-stats-to-stderr     Logs a JSON object to stderr containing resource and processing statistics after successfully processing (default: false)
  --buffer-max-lines value  Sets a limit on the number of lines to hold in the screen buffer (and also limits the possible window height), allowing the renderer to operate in a streaming fashion and enabling the processing of large inputs. Setting to 0 disables the limit, causing the renderer to buffer the entire screen before producing any output (default: 300)
  --window-max-cols value   Sets an upper bound on the window width (which may change based on input). Window size mainly affects cursor movement sequences (default: 400)
  --window-cols value       Sets the initial window width. Window size mainly affects cursor movement sequences (default: 160)
  --window-lines value      Sets the initial window height. Window size mainly affects cursor movement sequences (default: 100)
  --help, -h                show help
  --version, -v             print the version

早速実行してみます.既定値ではヘッダーやフッターは付かないようです.

$ cowsay hello terminal | terminal-to-html
 ________________
< hello terminal >
 ----------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

次に --preview option を試します.HTML と CSS でラップしてくれます.aha コマンドでは表現されなかった cow のしっぽの点滅も再現できています.
しかしここに HTML を貼り付けたら WprdPress と干渉して色コードが下部のタグになったり改行が入ってしまったりとひどいことになったのでとりあえず静止画です.ちなみに Asciidoctor Reveal.js へは綺麗に貼り付けできました.

$ unbuffer apt moo moo | terminal-to-html --preview | xsel

terminal to html apt moo2

次にウェブサービスとして動かしてみます.http 経由で変換ができます.

$ terminal-to-html --http :6060 &
$ cowsay hello > input.raw
$ curl --data-binary "@input.raw" http://localhost:6060/terminal
 _______
< hello >
 -------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
$ kill %1
$ terminal-to-html --preview --http :6060 &
$ curl --data-binary "@input.raw" http://localhost:6060/terminal | head
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
1<!DOCTYPE html>
0<html>
1       <head>
5               <meta charset="UTF-8">
6               <title>terminal-to-html Preview</title>
                <style>.term-container {
   background: #171717;
1  border-radius: 5px;
0  color: white;
0 10005  100   151  6211k  95994 --:--:-- --:--:-- --:--:-- 9917k
curl: Failed writing body

aha の方が HTML 変換のオプションが多いけど,terminal-to-html は http server 昨日が合ったりとユニークな部分も.好みで使い分けると良さそうです.

環境
$ terminal-to-html --version
terminal-to-html version 3.16.3
$ dpkg-query -W golang curl
curl    8.10.1-2
golang:amd64    2:1.23~2
$ lsb_release -dr
Description:    Debian GNU/Linux trixie/sid
Release:        n/a
$ arch
x86_64
]]>
https://matoken.org/blog/2024/10/22/convert-terminal-results-to-html-with-terminal-to-html-command/feed/ 0
コマンド結果などをhtmlに変換してくれるaha https://matoken.org/blog/2024/10/18/aha-command-that-converts-command-results-to-html/ https://matoken.org/blog/2024/10/18/aha-command-that-converts-command-results-to-html/#comments Thu, 17 Oct 2024 21:46:48 +0000 https://matoken.org/blog/?p=4143

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

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 <body> 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
                         <link rel="stylesheet" href="X" /> 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 パッケージに含まれています.

$ 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 の方がサイズも小さく良いかもしれません.

環境
$ 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
]]>
https://matoken.org/blog/2024/10/18/aha-command-that-converts-command-results-to-html/feed/ 1