Skip to content

Releases: webdiscus/ansis

v4.2.0

28 Sep 19:40
21ae59d

Choose a tag to compare

Feature

Added support named truecolors via ansis.extend() for both foreground and background.

Foreground methods are created from the provided color names, and matching background methods bg* are generated automatically.

output

Open in StackBlitz

Example:

import ansis from 'ansis';
import colorNames from 'css-color-names';

const color = ansis.extend(colorNames);

console.log(color.orange('Orange foreground'));
console.log(color.bgOrange('Orange background')); // auto-generated from "orange"

Note

The css-color-names (~6 kB) package exports CSS color names with their hex values.

This release removes the last barrier for projects migrating from Chalk v4 that used named truecolors:

- chalk.keyword('orange')('text');
+ color.orange('text');

- chalk.bgKeyword('orange')('text');
+ color.bgOrange('text');

Ansis provides this feature with a simpler, more intuitive API.

Important

Fallback for named truecolors

Ansis automatically interpolates named truecolors to the highest available color level supported by the current environment.
So you can safely use named truecolors anywhere without worrying about compatibility.

v4.1.0

16 Jun 09:15
5cb30a0

Choose a tag to compare

Features

  • Added readonly level property to get the detected color support level:

    • 0 - no colors,
    • 1 - 16 colors,
    • 2 - 256 colors,
    • 3 - truecolor.

    Access to detected color level:

    console.log('Detected color level: ', ansis.level);

Release v4.0.0

08 May 20:46
fd33a4d

Choose a tag to compare

✨ Ansis v4 - Smaller package and cleaner API

Ansis v4 drops unused duplicate aliases and legacy baggage.
This release brings a stable and more compact ANSI library.
v4 is ~15.7% smaller than v3.17

Follow the migration guide to upgrade.

⚠️ BREAKING CHANGES

1) Dropped support for Deno 1.x (EOL - October 9, 2024)

This version now supports Deno 2.0 and above.

2) Removed non-standard strike alias for strikethrough style

The legacy strike alias has been removed to clean up the API and stay consistent with ANSI style conventions.

  • The strike style was rarely (if ever) used and added unnecessary redundancy.
  • No usage of ansis.strike() was found in public GitHub repositories.
  • Other ANSI libraries use the standard strikethrough name exclusively.

3) Removed redundant aliases: grey, bgGrey, blackBright and bgBlackBright - use the standard gray and bgGray instead.

Holywar: gray vs grey vs blackBright

All these color names referred the same ANSI code.
However, keeping many separate names for the same color is too much for a small library.

Why gray only, without aliases?

ANSI codes for the gray color:

  •   90 - officially "bright black" foreground (i.e., gray) in terminal specs.
  • 100 - officially "bright black" background (i.e., bgGray) in terminal specs.

Ansis prefers the more intuitive and commonly used names: gray and bgGray.

  • gray, bgGray - Standard spelling, common used, and intuitive
  • grey, bgGrey - British spelling, uncommon, rarely used, and redundant aliases for gray and bgGray
  • blackBright, bgBlackBright - Spec-style names for "bright black", less intuitive, never used, awkward for practical use

Note

Supporting both gray and grey (or even worse, verbose aliases like bgBlackBright) introduces unnecessary duplication.
Ansis v4 is focused on a clean, minimal API by intentionally avoiding redundant aliases.

4) Using 256-color functions

The following legacy method aliases have been removed:

❌ Removed Method ✅ Use Instead
ansi256(code) fg(code)
bgAnsi256(code) bg(code)

These aliases were originally added for compatibility with Chalk.
Starting with this release, Ansis focuses on a cleaner and compact API, free from duplicated methods and legacy layers.

Why fg() and bg() are better than ansi256() and bgAnsi256()

Ansis has grown beyond being a Chalk-compatible alternative - it's now a modern and compact ANSI library with its own identity.

Clean API

  • ansis.fg(code) and ansis.bg(code) are shorter more elegant than ansis.ansi256(code) and ansis.bgAnsi256(code)
  • fg and bg clearly describe their purpose: setting foreground and background colors
  • These method names align with conventions used by many other color libraries
  • Introduced on Dec 2021, fg() and bg() are already being used in GitHub projects
  • Removing duplicates makes the API cleaner and more compact

5) Removed the unused AnsiColorsExtend type.

This type was intended to support extended theme colors, but it was never used in other projects.
If you relied on it in your own code (e.g. for typing custom styles), you can easily define it yourself.

6) Improved extend() method

The extend() method has been redesigned for better TypeScript support and flexibility.

Old behavior:

extend<U extends string>(colors: Record<U, string>): asserts this is Ansis & Record<U, Ansis>;
  • Modifies the current instance in-place.
  • Returns void.
  • ✅ Worked with default instance:
    import ansis from 'ansis';
    
    ansis.extend({ pink: '#FF75D1' });
    console.log(ansis.pink('foo'));
  • Limitation - Did not work with newly created instances:
    import { Ansis } from 'ansis';
    
    const ansis = new Ansis();
    ansis.extend({ pink: '#FF75D1' }); // TS2775: Assertions require every name in the call target to be declared with an explicit type annotation.
    console.log(ansis.pink('Hello')); // TS2339: Property 'pink' does not exist

New behavior:

extend<U extends string>(colors: Record<U, string >): Ansis & Record<U, Ansis>;
  • Returns an extended instance with full type support.

  • ✅ Works with both ansis and new Ansis():

    import antis from 'ansis';
    
    const colors = ansis.extend({ pink: '#FF75D1' });
    console.log(colors.pink('foo'));
    import { Ansis } from 'ansis';
    
    const ansis = new Ansis().extend({ pink: '#FF75D1' });
    console.log(ansis.pink('foo'));

Why this change?

TypeScript cannot widen the type of an existing variable when using asserts.
This means the old approach only worked for top-level constants like ansis, not new instances.
By returning the extended instance, the new approach enables full type inference in all scenarios.

Summary:

  • asserts version removed
  • extend() now returns an instance with extended types
  • Cleaner, safer, and fully compatible with all usage patterns

✨ Features

1) Support escape sequences in tagged template literals

Ansis now treats tagged template literals the same way as normal strings, returning the same result as the standard function call.

Example with \n (newline, unescaped):

red('prev\nnext')
red`prev\nnext`

Output:

prev
next

Example with escaped backslash:

red('prev\\next')
red`prev\\next`

Output:

prev\next

2) Manually set the color level

Ansis automatically detects color support, but you can manually set the color level.
You can create a new instance of Ansis with the desired color level.

Disable colors:

import { Ansis } from 'ansis';

const ansis = new Ansis(0);
console.log(ansis.red`foo`); // Output: plain string, no ANSI codes

Use only basic 16 colors:

import { Ansis } from 'ansis';

const ansis = new Ansis(1);
console.log(ansis.hex('#FFAB40')`Orange`); // Output: fallback to yellowBright

3) Simplified CI color detection

Affects: In rare CI environments, output may fallback to 16 colors or black & white.

Ansis provides basic support for standard CI environments by checking the commonly used CI environment variable. In these environments, Ansis assumes support for at least 16 colors. If your code uses 256-color or truecolor, Ansis automatically fallback to 16 colors or to black and white if no color support is detected.

Ansis focuses on the most common scenarios, as specific CI environments are rarely used in practice. This approach keeps the package lightweight without including unnecessary detection logic.

GitHub Actions is still detected as supporting truecolor, as most Ansis users rely on GitHub CI.

In general, color output in CI environments is not critical and can gracefully fallback when needed.

The xterm-direct detection logic (introduced in v3.5.0) has been removed, as it's unnecessary for identifying truecolor-capable terminals.

Note

No terminal emulator sets TERM=xterm-direct by default.
Modern terminals, including KDE Konsole, typically use TERM=xterm-256color along with COLORTERM=truecolor
to indicate truecolor support.


🛠️ Bug Fixes

Defaults to 16 colors for unknown color support

Ansis now defaults uses 16 colors if it cannot detect support for 256 colors or truecolor.

  • Old behavior: Unknown terminal → used truecolor (could result in incorrect colors)
  • New behavior: Unknown terminal → uses only 16 colors (ensures broad compatibility)

Note

This is not a breaking change. Ansis gracefully interpolates higher color depths (truecolor and 256 colors)
down to 16 colors when using, e.g., fg(), hex() or rgb().
To explicitly enable truecolor, set the environment variable COLORTERM=24bit or FORCE_COLOR=3.


🔄 Migrating to Ansis v4

Note

There is extremely low likelihood that you'll need to migrate, as these changes are related to very very rare use cases.
But to be sure, please check your code for these changes.

1) Upgrade to Deno v2 (if used)

This version supports Deno 2.0 and newer.

2) Replace strike with strikethrough

- ansis.strike('text')
+ ansis.strikethrough('text')

3) Replace grey and blackBright with gray

- ansis.grey('text')
- ansis.blackBright('text')
+ ansis.gray('text')

4) Replace bgGrey and bgBlackBright with bgGray

- ansis.bgGrey('text')
- ansis.bgBlackBright('text')
+ ansis.bgGray('text')

5) Replace ansi256() with fg()

- ansis.ansi256(196)('Error')
+ ansis.fg(196)('Error')

6) Replace bgAnsi256() with bg()

- ansis.bgAnsi256(21)('Info')
+ ansis.bg(21)('Info')

7) Update the extend() method

The new extend() method now returns an extended instance instead of modifying the original instance in-place.
To migrate, assign the result of extend() to a new variable (avoid reassigning the original instance):

import ansis from 'ansis';

- ansis.extend({ pink: '#FF75D1' });
+ const colors = ansis.extend({ pink: '#FF75D1' });

- console.log(ansis.pink.bold('foo'));
+ console.log(colors.pink.bold('foo'));

Alternatively:

- import ansis from 'ansis';
+ import { Ansis } from 'ansis';

- ansis.extend({ pink: '#FF75D1' });
+ const ansis = new Ansis().extend({ pink: '#FF75D1' });

console.log(ansis.pink.bold('foo'));

...

Read more

v3.18.0-beta.0

25 Apr 20:45
4fdcab5

Choose a tag to compare

BREAKING CHANGES

  • Dropped support for Deno 1.x (end-of-life on October 9, 2024)

Features

  • Added support for Deno 2.0 and newer

Note

The switch to Deno 2.0+ support is a breaking change and will be officially included in the next major release.
For v3.x users, it's available as an unofficial v3.18.0-beta.0 and will not be released as v3.18.0.

v4.0.0-beta

20 Apr 14:49
d171043

Choose a tag to compare

v4.0.0-beta Pre-release
Pre-release

✨ Ansis v4 - Smaller package and cleaner API

Ansis v4 drops legacy baggage and unused artifacts.
This release brings a stable and more compact ANSI library.

See notes by release v4.0.0.

v3.17.0

02 Mar 20:51
4994864

Choose a tag to compare

v3.17.0

Features

Added support for older typescript versions (< 5.6) to fix TS2526 error:

A 'this' type is available only in a non-static member of a class or interface.

Note

If you are already using TypeScript >= 5.6, this update is not required.

v3.16.0

21 Feb 19:53
f14481a

Choose a tag to compare

v3.16.0

Chore

  • Reverted the full text of ISC license.
  • Refactored code to reduce the package size.
  • Micro optimisations for named exports.

Test

  • Added tests for tsup bundler.

v3.14.0

14 Feb 21:43
d3cd44d

Choose a tag to compare

v3.14.0

Cumulative Release v3.6.0 - v3.14.0

Features

  • Added support for chromium-based browsers.
    Now you can use truecolor in the consoles of Chrome, Edge, Opera, Brave, Vivaldi and other Chromium-based browsers.
    Browsers that do not support ANSI codes will display black and white text.

  • Added support for \n as a newline in template literals, e.g.: green`Hello\nWorld` renders:

    Hello
    World
    
  • Function ansis.reset() returns the reset escape code \e[0m.

  • Enforce a specific color support by a FORCE_COLOR value:

    • false - Disables colors
    • 0 - Disables colors
    • true (or unset) - Auto detects the supported colors (if no color detected, enforce truecolor)
    • 1 - Enables 16 colors
    • 2 - Enables 256 colors
    • 3 - Enables truecolor
  • Removed undocumented pointless dummy function ansis(any).

Warning

This is not a BREAKING CHANGE because it was never officially documented!

import ansis from 'ansis';
ansis('text'); // <= now will occur the ERROR TS2349: This expression is not callable.

This warning applies only to projects where Chalk was replaced with Ansis and something like chalk('text') was used.

Just replace ansis('text') with 'text'.

The ansis('text') function was a dummy and did nothing except return the same input string.

  • Added support for legacy Node.js v14 (in package.json for npm was changed the engines to "node": ">=14").
  • Micro optimisations for slight performance improvements.

Bug Fixes

  • If the function argument is an empty string should be returned an empty string w/o escape codes:
    ansis.red('') => '', // returns empty string w/o escape codes
  • Cast falsy values false and NaN to a string.
    In previous versions, the empty string '' was returned for falsy values.
  • Functions with argument 0 , e.g. ansis.red(0), returning empty string '', now return colored value '0'

v3.5.2

02 Jan 16:09
9c3555f

Choose a tag to compare

v3.5.2

Features

  • Optimised the npm package size from 10.3 kB to 7.0 kB.

    Now, Ansis is the smallest yet most powerful library with rich functionality.
    A slightly smaller is picocolors (6.4 kB), but it offers only basic functionality.

  • Added detection of the xterm-direct terminal as supportingtruecolor.
  • Added support for the COLORTERM environment variable with the following values: truecolor, 24bit, ansi256, ansi (16 colors).

Bug Fixes (TypeScript)

  • Fixed default import in TypeScript compiled with tsc:
    import ansis from 'ansis' now works so well as import * as ansis from 'ansis'.
  • Fixed the TS2339: Property 'strip' does not exist on type when the tsc option module is node16.

v3.4.0

23 Dec 00:05
88a067c

Choose a tag to compare

v3.4.0

Cumulative Release v3.3.2 - v3.4.0

Bug Fixes

  • Correct detect TTY on Windows platform.

Chore

  • Optimised the npm package to reduce size by ~1.1 kB, from 11.4 kB to 10.3 kB.
  • Added benchmarks for kolorist package.
  • Added new representative benchmarks by using combination of 1, 2, 3 and 4 styles.
  • Refactor: invisible code optimisations.