This library lets you use (a very restricted subset of) HTML to style console output.
Say you have this string in a variable called html
:
<p><span style="color: #0f0">Hello!</span> <strong>This text should be bold.</strong></p>
<p>And then <strike>here we have struck-out text</strike>, <u>underlined text</u>, etc.</p>
Now we pass that to htmlout
:
htmlout(html);
Output:
You can even apply stylesheets. For instance, suppose you have the following CSS in a variable
called css
:
.info {
color: blue;
}
.success {
color: lime;
text-decoration: underline;
}
.warning {
color: orange;
font-weight: bold;
}
.fail {
color: red;
background-color: yellow;
font-weight: bold;
}
And then this is html
:
<p class="info">Here is some information.</p>
<p class="success">The mission was a success!</p>
<p class="warning">You are running low on fuel.</p>
<p class="fail">System failure!</p>
Then you use htmlout.withCSS
:
htmlout.withCSS(css)(html);
Output:
This project is used by console-highlight to do syntax highlighting in the console. Here's an example:
Obviously (well, at least without herculean effort), it isn't possible to support all CSS styles from a console. These are the styles that are at least partially supported:
color
background-color
font-style
(normal
or italic
on some terminals)font-weight
(normal
or bold
)text-decoration
(none
, underline
, strikethrough
on some terminals)text-transform
(none
, uppercase
, lowercase
, or capitalize
)htmlout follows a relatively simple process:
font-weight: bold;
is translated to the
escape sequence '\x1B[1m'
and '\x1B[21m'
.That's about it! If you have questions or run into issues, let me know!