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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@peteanderson/ansi

v3.8.0

Published

ANSI escape sequence generation with automatic feature detection

Readme

@peteanderson/ansi

ANSI escape sequence helpers for Node.js terminal output. Automatically detects terminal capabilities and adapts output accordingly.

Installation

npm install @peteanderson/ansi

Usage

The examples below use require(), but import works identically for ESM and TypeScript consumers — just substitute import ansi from for const ansi = require, and so on.

// Use directly as a namespace
const ansi = require("@peteanderson/ansi");
console.log(ansi.fg.red("error: something went wrong"));
console.log(ansi.bg.blue.and.fg.white.and.bold("highlighted"));

// Call it to build a custom instance
const ansiNoColor = ansi(24); // force 24-bit (true color)
console.log(ansiNoColor.fg.rgb(255, 128, 64)("light red-orange"));

// Destructure named exports
const {ansi: defaultInstance, makeAnsi, fg} = require("@peteanderson/ansi");
console.log(fg.red.and.bold("Hello, world!"));
const custom = makeAnsi({colorDepth: 8, cursor: false});

Feature Detection

The module automatically detects:

  • Color depth: 1 (none), 4 (16 colors), 8 (256 colors), 24 (true color)
  • Style support: enabled if color depth > 1
  • Other features: cursor, erase, scroll — enabled if output is a TTY and TERM is not dumb

When disabled, features output empty strings. Color outputs plain text. This can be overridden:

  • Pass a boolean to force color on/off
  • Pass a number to set specific color depth (1, 3, 4, 8, or 24)
  • Pass a stream to use for detection
  • Pass a partial features object to override individual features

API

Default Export

The default export is both the pre-built default instance and a callable factory. You can use it directly as a namespace, call it to build a custom instance, or destructure properties from it:

const ansi = require("@peteanderson/ansi");

console.log(ansi.fg.red("hello"));   // use as instance
const custom = ansi(true);           // call as factory
console.log(custom.fg.red("hello")); // forced color

makeAnsi(options?)

The factory function, exported as a named export. Returns a configured instance. Equivalent to calling the default export as a function, but exported separately for consumers who want a plain function reference:

import {makeAnsi} from "@peteanderson/ansi";
const custom = makeAnsi(true);

ansi

The pre-built default instance, exported as a named export. Unlike the default export, this is not callable — use makeAnsi if you need to build a custom instance:

import {ansi, makeAnsi} from "@peteanderson/ansi";
console.log(ansi.fg.red("hello"));

fg — foreground colors

Functions that wrap text with color codes (or plain text if disabled):

  • Basic colors: black red green yellow blue magenta cyan white default
  • Bright colors: brightBlack brightRed brightGreen brightYellow brightBlue brightMagenta brightCyan brightWhite
  • fg.rgb(r, g, b)(text) - 24-bit RGB color, downscaled for lower color depths
  • fg.x256(code)(text) or fg.x256(r, g, b)(text) - 256-color palette, downscaled as needed

All return functions with open and close properties for raw sequences and an and property for fluent chaining.

bg — background colors

Functions that wrap text with color codes (or plain text if disabled):

  • Basic colors: black red green yellow blue magenta cyan white default
  • Bright colors: brightBlack brightRed brightGreen brightYellow brightBlue brightMagenta brightCyan brightWhite
  • bg.rgb(r, g, b)(text) - 24-bit RGB color, downscaled for lower color depths
  • bg.x256(code)(text) or bg.x256(r, g, b)(text) - 256-color palette, downscaled as needed

All return functions with open and close properties for raw sequences and an and property for fluent chaining.

style

Functions that wrap text with style codes (disabled if color depth = 1):

bold dim italic underline inverse hidden strikethrough doubleUnderline framed encircled overline

All return functions with open and close properties for raw sequences and an and property for fluent chaining.

Fluent Chaining with .and

Styles can be chained using the .and property for a natural, fluent interface. The style namespace is unnested in chains:

// Top-level: use style namespace
ansi.style.bold("bold text");

// In a chain: no need for style namespace
console.log(ansi.fg.white.and.bg.blue.and.bold("white on blue, bold"));
console.log(ansi.fg.red.and.underline("red underlined"));

Arbitrary Style Combination with .combine()

Arbitrary styles can be combined using the .combine() method on any SGR function. This allows you to combine any styles into a single function, which can then be applied to text:

const dim = format => format.combine(ansi.style.dim);

console.log(dim(ansi.fg.red("dim red text")));

Arbitrary Chaining with .chain()

The fluent interface is usually more convenient than .combine(), but it has one limitation: once an attribute has been set, it cannot be overridden via .and. For example, ansi.fg.red.and.fg.blue("text") will error both at compile time and at runtime, because the fg property isn't available in the chain, since it was already used.

This can be inconvenient when you want to override default styles. Take this example:

import ansi from "@peteanderson/ansi";

function formatText(text: string, format?: (defaultStyle: Format) => Format): string {
    let style = ansi.fg.green;
    if (format)
        style = format(style);
    return style(text);
}

formatText("Some error message", style => style.and.fg.red);
// ❌ TypeError: Cannot read properties of undefined (reading 'red')

This will throw an error at runtime since fg won't be available in the chain. However, with chain(), you can create a new formatter that allows overriding styles that have already been used:

import ansi from "./src";

function formatText(text: string, format?: (defaultStyle: Format) => Format): string {
    let style = ansi.fg.green;
    if (format)
        style = format(ansi.chain(style));
    return style(text);
}

formatText("Some error message", style => style.and.fg.red);
// ✅ "Some error message" in red

reset

The SGR reset sequence. Empty string if color is disabled.

plain

An identity formatter that returns its input string unchanged. Useful for conditionally disabling formatting or for testing:

function formatText(text, useColor) {
    const format = useColor ? ansi.fg.green : ansi.plain;
    return format.and.bold(text);
}

cursor

Available if feature is enabled:

  • cursor.show / cursor.hide - show or hide the cursor
  • cursor.position.get - get the current cursor position (terminal sends position to stdin)
  • cursor.position.set(row, col) - set cursor position (1-based indexing)
  • cursor.shape.block / cursor.shape.underline / cursor.shape.bar - change cursor shape
  • cursor.up(n) / cursor.down(n) / cursor.forward(n) / cursor.backward(n) - move cursor; negative values move in the opposite direction
  • cursor.nextLine(n) / cursor.prevLine(n) - move cursor to next/previous line; negative values move in the opposite direction
  • cursor.x(col) - move cursor to column (1-based)
  • cursor.save / cursor.restore - save/restore cursor position (VT100)

terminal

Terminal control features (available if feature is enabled):

  • terminal.focusReporting.enable / terminal.focusReporting.disable - enable/disable focus reporting
  • terminal.alternateBuffer.on / terminal.alternateBuffer.off - switch to/from alternate buffer
  • terminal.alternateBuffer.legacy.on / terminal.alternateBuffer.legacy.off - use legacy alternate buffer sequences
  • terminal.bracketedPasteMode.enable / terminal.bracketedPasteMode.disable - enable/disable bracketed paste mode

insert

Available if feature is enabled:

  • insert.char(count) - insert characters (default 1); negative values delete characters
  • insert.line(count) - insert lines (default 1); negative values delete lines

delete

Available if feature is enabled:

  • delete.char(count) - delete characters (default 1); negative values insert characters
  • delete.line(count) - delete lines (default 1); negative values insert lines

erase

Available if feature is enabled:

  • erase.char(count) - erase characters (default 1)
  • erase.line.toStart / erase.line.toEnd / erase.line.full - erase line
  • erase.screen.toStart / erase.screen.toEnd / erase.screen.full - erase screen
  • erase.screen.scrollback - erase screen and scrollback buffer

scroll

Available if feature is enabled:

  • scroll.up(lines) - scroll up (default 1 line); negative values scroll down
  • scroll.down(lines) - scroll down (default 1 line); negative values scroll up
  • scroll.setRegion(top, bottom) - set scroll region; omit top/bottom to use defaults (top/bottom of viewport)

strip(text)

Removes all ANSI CSI sequences from a string (always available).

slice(text, start, end)

Slices a string by visible characters, ignoring ANSI sequences (always available).

splitAt(text, visibleIndex)

Splits a string into two parts at the given visible index, ignoring ANSI sequences (always available).

sanitize(text)

Removes "unsafe" CSI sequences, leaving only color and style codes (always available).

simplify(text)

Simplifies ANSI SGR sequences by combining adjacent sequences and removing redundant codes (always available).

visibleLength(text)

Returns the visible length of a string with all ANSI sequences removed (always available).

padStart(text, targetLength) and padEnd(text, targetLength)

Pads a string to a target visible length using spaces. Accounts for ANSI sequences when calculating length.

features

Object containing the detected feature configuration:

{
    colorDepth : 1 | 3 | 4 | 8 | 24;
    style      : boolean;
    cursor     : boolean;
    erase      : boolean;
    scroll     : boolean;
    terminal   : boolean;
}

disabled

A pre-built Ansi instance with all features disabled. Useful for testing or for disabling output in certain environments.

License

MIT