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

@silva97/ansi

v1.1.3

Published

Tag function to parse ANSI colors in template strings

Downloads

5

Readme

npm package Travis CI

@silva97/ansi

This package implements a tag function to parse template strings and add ANSI escape sequences to use colors and text styles on terminal. (read about template strings)

// Example
const { ansi } = require('@silva97/ansi');

console.log(ansi`%{f.green}Hello %{f.red;bold;under}World!`);

Output:
terminal-output

Installation

npm install @silva97/ansi
# Or using yarn:
yarn add @silva97/ansi

ansi tags

The ansi tags follows the format %{...}, it's similar to notation ${...} of template strings to expands the content of an expression on the string. But ansi tags is used to output ANSI escapes.

The color name is specified using the format mode.color. Example f.blue will be set the foreground color to blue.

| Mode | ANSI code | Description | | :--: | :-------: | :--------------------- | | f | 3 | Foreground color | | F | 9 | Light foreground color | | b | 4 | Background color |


| Color | ANSI code | | :------: | :-------: | | black | 0 | | red | 1 | | green | 2 | | yellow | 3 | | blue | 4 | | violet | 5 | | purple | 5 | | cyan | 6 | | white | 7 |

You can also specify styles to text. Example: %{bold;strike}Hello!.

| Style | ANSI code | Description | | :------: | :-------: | :--------------------------------------- | | normal | 0 | Resets to normal style | | bold | 1 | Bold text | | italic | 3 | Italic text | | under | 4 | underline text | | blink | 5 | Blinks the text | | invert | 7 | Inverts background and foreground colors | | strike | 9 | ~~strike text~~ |

Warning: Some styles, like blink and strike, will not work on all terminals. (i.e. VS code integrated terminal)

Note: You can also specify the style numbers instead of the names. Example: %{31;1} will be translated to \x1b[31;1m (same as %{f.red;bold}).

It's yet template strings!

Using ansi tag function you can yet use ${} expressions inside yours strings. Example:

const { ansi } = require('@silva97/ansi');

const name = 'Luiz Felipe';

console.log(ansi`Your name is %{f.green;bold}${name}%{normal}!`);

You can easy disable colors!

You can simple disable the colors of the output if you set ansi.enabled to false. For example:

const { ansi } = require('@silva97/ansi');

ansi.enabled = false;
const name = 'Luiz Felipe';

console.log(ansi`Your name is %{f.green;bold}${name}%{normal}!`);
// Output: "Your name is Luiz Felipe!"

It's will print the text without colors.

Purify text from ANSI escapes

If you previous has been escaped an text, but need the raw text back. You just need to use the purify() function and it will remove all ANSI escapes from the text.

const { ansi, purify } = require('@silva97/ansi');

const name = 'Luiz Felipe';
const welcome = ansi`Your name is %{f.green;bold}${name}%{normal}!`;

console.log(welcome);         // Colored text
console.log(purify(welcome)); // Normal text