npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

htmlout

v0.2.0

Published

HTML-styled console output

Downloads

30

Readme

htmlout

This library lets you use (a very restricted subset of) HTML to style console output.

Example

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:

Console 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:

Console output

This project is used by console-highlight to do syntax highlighting in the console. Here's an example:

Example output from console-highlight

Supported CSS Styles

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)

How it works

htmlout follows a relatively simple process:

  1. First, the HTML is parsed using jsdom, which provides a DOM and handles stylesheets.
  2. htmlout then iterates over every text node of the DOM, translating the relevant CSS style rules to terminal escape sequences. For example the CSS rule font-weight: bold; is translated to the escape sequence '\x1B[1m' and '\x1B[21m'.
  3. The console does not support just any arbitrary color. htmlout uses nearest-color to make a best effort to translate any color in CSS to a valid escape sequence. This actually yields very good results, especially on terminals that support 256 colors. (This isn't 100% implemented yet. Colors in hex or RGB format should work; but for short names like 'aqua', only the 16 basic colors are supported right now. HSL format isn't supported at all.)

That's about it! If you have questions or run into issues, let me know!