Skip to content

Commit

Permalink
feat: add support for chromium-based browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
webdiscus committed Feb 12, 2025
1 parent b02ede6 commit d3cd44d
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 24 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## 3.14.0 (2025-02-13)

- feat: add support for chromium-based browsers.\
Now you can use truecolor in the consoles of Chrome, Edge, Brave, and other Chromium-based browsers.
Browsers that do not support ANSI codes will display black and white text.
- refactor: slight reduce the package size by ~40 bytes

## 3.13.0 (2025-02-13)

Skip this version number.

## 3.12.0 (2025-02-11)

- feat: add support for `\n` as a newline in template literals\
Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
[![npm bundle size](https://img.shields.io/bundlephobia/minzip/ansis)](https://bundlephobia.com/package/ansis)


A Node.js library for applying ANSI colors in terminal output.\
A library for applying ANSI colors in terminal or Chromium-based browser console.\
**Ansis** is focused on [small size](#compare-size) and [speed](#benchmark) while providing rich [functionality](#compare) and handling [edge cases](#handling-input-arguments).

### 🚀 [Install and Quick Start](#install)[Why use Ansis](#why-ansis)
Expand All @@ -41,7 +41,7 @@ hex('#E0115F').bold.underline('Truecolor!')

## ⚖️ Similar libraries

The most popular Node.js libraries for styling terminal output using ANSI colors, similar to **Ansis**:
The most popular libraries for styling terminal output using ANSI colors, similar to **Ansis**:

[chalk][chalk], [picocolors][picocolors], [colorette][colorette], [kleur][kleur], [ansi-colors][ansi-colors], [kolorist][kolorist], [cli-color][cli-color], [colors-cli][colors-cli], [colors.js][colors.js]

Expand All @@ -59,6 +59,7 @@ The most popular Node.js libraries for styling terminal output using ANSI colors

- Supports **ESM**, **CommonJS**, **TypeScript**
- Supports **Bun**, **Deno**, **Next.JS** runtimes
- Supports Chromium-based **browsers** such as **Chrome**, **Edge**, **Opera**, **Brave**, **Vivaldi** and others.
- Drop-in [replacement](#why-ansis) for [`chalk`](#replacing-chalk) [`colorette`](#replacing-colorette) [`picocolors`](#replacing-picocolors) [`ansi-colors`](#replacing-ansi-colors)
- Default and [named import](#named-import) `import ansis, { red, bold, ansi256, hex } from 'ansis'`
- [Chained syntax](#chained-syntax) `red.bold.underline('text')`
Expand All @@ -75,13 +76,16 @@ The most popular Node.js libraries for styling terminal output using ANSI colors
- Supports [environment variables](#cli-vars) [`NO_COLOR`](using-env-no-color), [`FORCE_COLOR`](#using-env-force-color) and [flags](#cli-flags) `--no-color` `--color`
- Supports [`COLORTERM`](#using-env-colorterm) variable to test applications with 16, 256, or true-color
- [Correct style break](#new-line) at the `end of line` when used `\n` in string `'A\nB'` or template literals ``` `A\nB` ```
- Doesn't extend `String.prototype`
- Zero dependencies
- Test coverage 100%. Long term support.

<!-- - Chromium-based browsers can display truecolor text in console.
- Browsers that do not support ANSI codes will display black/white text in console. -->


## 🌍 Used by

[NestJS](https://github.com/nestjs/nest), [Facebook/StyleX](https://github.com/facebook/stylex), [Sequelize](https://github.com/sequelize/sequelize), [Salesforce](https://github.com/salesforcecli/cli), [Oclif](https://github.com/oclif/core), [WebpackBar](https://github.com/unjs/webpackbar)
[NestJS](https://github.com/nestjs/nest), [Facebook/StyleX](https://github.com/facebook/stylex), [Sequelize](https://github.com/sequelize/sequelize), [Salesforce](https://github.com/salesforcecli/cli), [Oclif](https://github.com/oclif/core), [unjs/Webpackbar](https://github.com/unjs/webpackbar), [unjs/Unplugin](https://github.com/unjs/unplugin)


<!--
Expand Down
2 changes: 1 addition & 1 deletion README.npm-src.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Ansis

Enable ANSI colors in terminal output. [Docs on GitHub](https://github.com/webdiscus/ansis).
Enable ANSI colors in terminal. [Docs on GitHub](https://github.com/webdiscus/ansis).

#

Expand Down
2 changes: 1 addition & 1 deletion README.npm.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Ansis

Enable ANSI colors in terminal output. [Docs on GitHub](https://github.com/webdiscus/ansis).
Enable ANSI colors in terminal. [Docs on GitHub](https://github.com/webdiscus/ansis).

#

Expand Down
2 changes: 1 addition & 1 deletion package-test-node16.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ansis",
"version": "3.12.0",
"version": "3.14.0",
"description": "Special version of package.json for testing in Node.js v14 - v16",
"keywords": [],
"license": "ISC",
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"name": "ansis",
"version": "3.12.0",
"description": "A small and fast Node.js library for applying ANSI colors and styles in terminal output",
"version": "3.14.0",
"description": "A small and fast library for applying ANSI colors in terminal or browser console",
"keywords": [
"ansi",
"colour",
"color",
"colors",
"styles",
Expand Down
4 changes: 2 additions & 2 deletions package.npm.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name":"ansis",
"version":"3.12.0",
"description":"ANSI colors in terminal output",
"version":"3.14.0",
"description":"ANSI colors in terminal",
"keywords":["ansi","color","style","terminal","cli"],
"license":"ISC",
"author":"webdiscus",
Expand Down
61 changes: 49 additions & 12 deletions src/color-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ let detectColorSpace = (env, isTTY, isWin) => {
// or do not set COLORTERM to `truecolor`
// therefore they can be detected by specific EVN variables

// 1) Detect color support in COLORTERM

// Common COLORTERM Values: `truecolor` or `24bit`, `ansi256`, `ansi`
// terminals, that support truecolor, e.g., iTerm, VSCode
if (colorterm === 'truecolor' || colorterm === '24bit') return SPACE_TRUECOLOR;
if (colorterm === 'ansi256') return SPACE_256COLORS;
if (colorterm === 'ansi') return SPACE_16COLORS;

// note: first, CI environments must be detected since they are not TTY and often advertise themselves as `dumb` terminals
// 2) Detect color support in CI,
// since in CI environments are not TTY and often advertise themselves as `dumb` terminals

// Azure DevOps CI
// https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml
Expand All @@ -53,28 +56,61 @@ let detectColorSpace = (env, isTTY, isWin) => {
return SPACE_16COLORS;
}

// unknown output or colors are not supported
// 3) Detect unknown output or colors are not supported
if (!isTTY || /-mono|dumb/i.test(term)) return SPACE_BW;

// truecolor support starts from Windows 10 build 14931 (2016-09-21), in 2024 we assume modern Windows is used
// 4) Truecolor support starts from Windows 10 build 14931 (2016-09-21), in 2025 we assume modern Windows is used
if (isWin) return SPACE_TRUECOLOR;

// kitty or KDE terminal emulator indicates truecolor support
if (/^xterm-(kitty|direct)$/i.test(term)) return SPACE_TRUECOLOR;
// 5) Detect terminal emulator with truecolor support

// kitty or KDE terminal
// - xterm-kitty
// - xterm-direct
if (/term-(kit|dir)/.test(term)) return SPACE_TRUECOLOR;

// JetBrains IDEA: JetBrains-JediTerm
// TODO: enable truecolor output in IDEA (defaults output 256 colors) if anybody need it
//if (env.TERMINAL_EMULATOR?.include('JediTerm')) return SPACE_TRUECOLOR;

// 6) Detect terminal emulator with 256 color support

// note: check for 256 colors after ENV variables such as TERM, COLORTERM, TERMINAL_EMULATOR etc.
// terminals, that support 256 colors, e.g., native macOS terminal
if (/-256(colou?r)?$/i.test(term)) return SPACE_256COLORS;

// known terminals supporting 16 colors
if (/^(screen|tmux|xterm|vt[1-5][0-9]([0-9])?|ansi)|color|cygwin|linux|mintty|rxvt/i.test(term)) return SPACE_16COLORS;
// - screen-256color
// - xterm-256color
// - rxvt-256color
// - putty-256color
// - mintty-256color
// - linux-256color
// - tmux-256color
// - ansi-256color
if (/-256/.test(term)) return SPACE_256COLORS;

// 7) Detect terminal emulator with 16 color support

// Use `echo $TERM` command to display terminal name in `env.TERM`.
// TODO: drop detection of very rare and exotic terminals such as:
// - /vt[1-5][0-9]([0-9])?/ - vt100,vt102,vt110,vt220,vt240,vt320,vt420,vt520 - names historically used with Unix
// - /rxvt/ - terminal emulator for X Window System
// - /tmux/ - terminal tmux installed on macOS has `tmux-256color` name

// Known terminals supporting 16 colors.
// - screen-color
// - xterm-color
// - ansi, ansi-x3.64, ansi.sysk
// - linux - Linux virtual console (tty1, tty2, SSH, etc.)
// - tmux - Terminal emulator
// - cygwin - Cygwin terminal
// - mintty - Default terminal emulator for Cygwin
// - putty-color
// - rxvt-color
// - vt100
if (/scr|xterm|tty|ansi|color|[nm]ux|vt|cyg/.test(term)) return SPACE_16COLORS;

// 8) For unknown terminals we allow truecolor output,
// since all terminals supporting only 16 or 256 colors have already been detected above

// note: for unknown terminals we allow truecolor output,
// because all terminals supporting only 16 or 256 colors have already been detected above
return SPACE_TRUECOLOR;
};

Expand Down Expand Up @@ -156,5 +192,6 @@ export const getColorSpace = (mockThis) => {
|| hasFlag(/^-{1,2}(no-color|color=(false|never))$/)) return SPACE_BW;

// optimisation: `!colorSpace` is equivalent to `colorSpace === SPACE_BW`
return isForceEnabled && !colorSpace ? SPACE_TRUECOLOR : colorSpace;
// detect browser support: !!_this.window?.chrome
return (isForceEnabled && !colorSpace) || !!_this.window?.chrome ? SPACE_TRUECOLOR : colorSpace;
};
24 changes: 24 additions & 0 deletions test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,30 @@ describe('support colors in terminals', () => {
expect(received).toEqual(expected);
});

test(`Browser chromium`, () => {
const received = colorSpace({
window: {
chrome: {},
},
});
const expected = SPACE_TRUECOLOR;
expect(received).toEqual(expected);
});

test(`Browser others`, () => {
const received = colorSpace({
window: {},
});
const expected = SPACE_BW
expect(received).toEqual(expected);
});

test(`Unknown system`, () => {
const received = colorSpace({});
const expected = SPACE_BW
expect(received).toEqual(expected);
});

});

// Node.JS
Expand Down

0 comments on commit d3cd44d

Please sign in to comment.