Stream terminal output with ansi codes into nicely formatted HTML
Imagine you have terminal output, let's say from a test runner. It looks like this
You can pipe the output through term-to-html
to create equivalent HTML file. Same output (HTML page was white background)
The output could be used to do visual testing against CLI output
npm i -g term-to-html
mocha spec.js --reporter spec | term-to-html > mocha.html
Note: many applications detect non-interactive terminal and turn off colors. Usually you can enable colors using an environment variable, like FORCE_COLOR=2
in chalk library.
You can output HTML page with dark background using --theme dark
CLI argument
<my program> | term-to-html --theme dark
const termToHtml = require('term-to-html')
const html = termToHtml.strings(stringWithAnsi, termToHtml.themes.dark.name)
There are E2E tests and you can observe the output by running
FORCE_COLOR=2 npx mocha spec.js --reporter spec | ./bin/term-to-html.js
Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2020
License: MIT - do anything with the code, but don't blame me if it does not work.
Support: if you find any problems with this module, email / tweet / open issue on Github
Copyright (c) 2020 Gleb Bahmutov <gleb.bahmutov@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.