iro
Fast and clean terminal coloring and styling utility for Deno and Node.js.
Overview
- No dependencies
- Clean and easy syntax
- No polyfilling
- Lightweight
- Import only what you need
Usage
Deno
// hello.ts
import iro, { bold, red } from 'https://cdn.jsdelivr.net/npm/node-iro@1.0.1/src/iro.ts';
console.log(iro('Hello, World!', bold, red));
Node.js
Install using npm:
npm i node-iro
…or Yarn:
yarn add node-iro
Use it:
// hello.js
const iro = require('node-iro').default;
const { bold, red } = require('node-iro');
console.log(iro('Hello, World!', bold, red));
Advanced Usage
Nesting
// example taken from https://github.com/lukeed/kleur#nested-methods :)
console.log(iro(`foo <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>i</mi><mi>r</mi><mi>o</mi><msup><mo stretchy="false">(</mo><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mi>r</mi><mi>e</mi><msup><mi>d</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mo separator="true">,</mo><mi>r</mi><mi>e</mi><mi>d</mi><mo separator="true">,</mo><mi>b</mi><mi>o</mi><mi>l</mi><mi>d</mi><mo stretchy="false">)</mo></mrow><mi>b</mi><mi>a</mi><mi>r</mi></mrow><annotation encoding="application/x-tex">{iro('red', red, bold)} bar </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.0019em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord mathnormal">i</span><span class="mord mathnormal">ro</span><span class="mopen"><span class="mopen">(</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span><span class="mord mathnormal">re</span><span class="mord"><span class="mord mathnormal">d</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">re</span><span class="mord mathnormal">d</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">b</span><span class="mord mathnormal">o</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">d</span><span class="mclose">)</span></span><span class="mord mathnormal">ba</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span></span></span></span>{iro('cyan', cyan)} baz`, yellow));
console.log(iro('foo ' + iro('red', red, bold) + ' bar ' + iro('cyan', cyan) + ' baz', yellow));
Supported Formatting
Modifiers: reset, bold, dim, italic, underline, inverse, hidden, strikethrough
Colors: black, red, green, yellow, blue, magenta, cyan, white, gray, grey
Background colors: bgBlack, bgRed, bgGreen, bgYellow, bgBlue, bgMagenta, bgCyan, bgWhite
Note: italic
and strikethrough
are not widely supported.
Credits
This project is heavily inspired by kleur. It was mainly made to be used in Deno, but I’ve decided to release it for Node.js users too. The syntax is different for two reasons: personal preference and lean typings. If you use Node.js and prefer kleur’s syntax, you should definitely choose kleur over iro, it’s an awesome library 😊
Planned Features
- Check if terminal supports coloring
- Explicitly provide whether the coloring should happen
- Unit tests
License
MIT.