sirrobert-terminal-ansi

ANSI terminal codes for node

Usage no npm install needed!

<script type="module">
  import sirrobertTerminalAnsi from 'https://cdn.skypack.dev/sirrobert-terminal-ansi';
</script>

README

Overview

ANSI color codes for the terminal in node.

Usage

Installation

Install with

npm install --save sirrobert-terminal-ansi

Invocation

Invoke in the normal way:

const ANSI = require(* sirrobert-terminal-ansi* );

The exported obejct, ANSI (name it whatever you like), is a nested hash. It looks like this:

var codes = {
  "off" : '\u001b[0m',

  foreground: {
    "normal" : '\u001b[39m',
    "bold"   : "\u001b[1m",
    "faint"  : "\u001b[2m",
    // ...
  },

  background: {
    "normal" : '\u001b[49m',
    "black"  : "\u001b[40m",
    "red"    : "\u001b[41m",
    // ...
  }
};

Note that these are just strings. That means you can compound them using string concatenation:

console.log(ANSI.bg.blue, "This has a blue background.", ANSI.reset);
process.stdout.write(ANSI.bg.green, "This has a green background.", ANSI.reset);

Of course, feel free to assign them locally:

local fg = ANSI.fg;
local bg = ANSI.bg;
console.log(bg.red + fg.black + "The A-Team!" + ANSI.reset);

And you don't have to reset after each line, either.

local fg = ANSI.fg;
local bg = ANSI.bg;
console.log(bg.red + fg.black); // Prints an apparently blank line.
console.log("The A-Team Rules!");
console.log("Everone else Droolz!");
console.log("This text is also super cool!");
console.log(ANSI.reset);        // Prints an apparently blank line.

Codes

The codes are broken up into background and foreground. You can call them with .foreground and .background respectively (or .fg and .bg for short).

Obviously the actual output colors depend on the mappings of your terminal.

The available modifiers are:

  • ANSI

    • .off

    Foreground

    For example, ANSI.foreground.bold or ANSI.foreground.blue.

    • normal

    Modifiers

    • bold
    • faint
    • standout
    • underline
    • blink
    • reverse
    • hidden
    • no-standout
    • no-underline
    • no-blink
    • no-reverse

    Colors

    • black
    • red
    • green
    • yellow
    • blue
    • magenta
    • cyan
    • white

    Light Colors

    • dark_gray (or "light_black", but that's yucky.)
    • light_red
    • light_green
    • light_yellow
    • light_blue
    • light_magenta
    • light_cyan
    • light_gray

    Background

    For example, ANSI.background.red or ANSI.bg.light_blue.

    • normal

    Core Colors

    • black
    • red
    • green
    • yellow
    • blue
    • magenta
    • cyan
    • white

    Extended Colors

    • dark_gray
    • light_red
    • light_green
    • light_yellow
    • light_blue
    • light_magenta
    • light_cyan
    • light_gray

Convenience Aliases

In addition, there are some convenience aliases.

  • ANSI.foreground can be written ANSI.fg.
  • ANSI.background can be written ANSI.bg.
  • ANSI.reset is an alias for ANSI.fg.normal + ANSI.bg.normal.