terminal-to-htmlコマンドで端末の結果をHTMLに変換する

先日 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

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)