Skip to content

Commit 3d21f22

Browse files
committed
feat: add readonly level property to get the detected color support level
1 parent 197fbaa commit 3d21f22

18 files changed

+186
-113
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# Changelog
22

3+
## 4.1.0 (2025-05-28)
4+
5+
- feat: add readonly `level` property to get the detected color support level:
6+
- 0 - no colors,
7+
- 1 - 16 colors,
8+
- 2 - 256 colors,
9+
- 3 - truecolor.
10+
11+
## 4.0.0 (2025-05-08)
12+
13+
Release v4.
14+
315
## Pre release note: ✨ Ansis v4 - Smaller package, and cleaner API
416

517
Ansis v4 drops legacy baggage and unused artifacts.

README.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -878,26 +878,34 @@ Execute the script in a terminal:
878878

879879
Ansis automatically detects the supported color level:
880880

881-
- Truecolor
882-
- ANSI 256 colors
883-
- ANSI 16 colors
884-
- black & white (no colors)
881+
- `0` – No color (black & white)
882+
- `1` – Basic ANSI (16 colors)
883+
- `2` – Extended ANSI (256 colors)
884+
- `3` – Truecolor (24-bit RGB)
885885

886-
Ansis has the method `isSupported()` that returns a `boolean` value whether the output supports ANSI color and styles.
886+
You can access the detected color level via the readonly `level` property:
887887

888888
```js
889889
import ansis from 'ansis';
890890

891-
console.log('Color output: ', ansis.isSupported());
891+
console.log('Detected color level: ', ansis.level);
892892
```
893893

894-
There is no standard way to detect terminal color support.
895-
The most common method is to check the `TERM` and `COLORTERM` environment variables, which often indicate the supported color level.
894+
To check if ANSI color output is supported, use the `isSupported()` method:
895+
896+
```js
897+
import ansis from 'ansis';
898+
899+
console.log('Color output supported:', ansis.isSupported());
900+
```
901+
902+
> [!NOTE]
903+
> There is no standard way to detect terminal color support.
904+
> The most common method is to check the `TERM` and `COLORTERM` environment variables, which often indicate the supported color level.
896905
897906
Most standard CI systems can be identified by the presence of the `CI` environment variable.
898907
While some CI uses their own specific environment variables, they are inconsistent and not widely adopted.
899908

900-
901909
Ansis provides basic support for standard CI environments by checking the commonly used `CI` environment variable.
902910
In such cases, Ansis assumes support for at least 16 colors.
903911
If your code uses 256-color or truecolor, Ansis automatically [fallback](#fallback) to 16 colors, or to black and white if no color support is detected.

README.npm-node10.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Ansis
22

3-
ANSI color library – special version for Node.js 10+.
3+
ANSI color library – special build for Node.js 10+.
44

55
[Documentation](../../)
66

bench/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,20 @@ import picocolors from 'picocolors';
4040
import { Ansis, cyan, green, red, yellow, hex, rgb } from 'ansis';
4141

4242
import spectrum from '../examples/fixtures/spectrum.js';
43-
import { getLevel } from '../src/color-support.js';
4443
import { colorLevels, LEVEL_TRUECOLOR } from '../src/color-levels.js';
4544

4645
import packages from './packages.js';
4746

4847
// create a new instance of Ansis for correct measure in benchmark
4948
const ansis = new Ansis();
50-
const colorSpace = getLevel();
49+
const colorLevel = ansis.level;
5150

5251
const log = console.log;
5352

5453
log();
55-
log(cyan.inverse` Colors `, `Your terminal supports ${cyan(colorLevels[colorSpace])}.`);
54+
log(cyan.inverse` Colors `, `Your terminal supports ${cyan(colorLevels[colorLevel])}.`);
5655

57-
if (colorSpace < LEVEL_TRUECOLOR) {
56+
if (colorLevel < LEVEL_TRUECOLOR) {
5857
log(red.inverse` WARNING `, yellow`Your terminal doesn't support Truecolor!`);
5958
log('The result of some tests can be NOT correct!\nChoose a modern terminal, e.g. iTerm.\n');
6059
}

bench/lib/bench.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Benchmark from 'benchmark';
2-
import { Ansis } from '../../src/index.js';
2+
import { Ansis } from 'ansis';
33

44
const benchStyle = new Ansis();
55

bench/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"dependencies": {
1212
"@colors/colors": "1.6.0",
1313
"ansi-colors": "4.1.3",
14-
"ansis": "file:../dist",
14+
"ansis": "file:../dist/node14",
1515
"benchmark": "2.1.4",
1616
"chalk": "5.4.1",
1717
"cli-color": "2.0.4",
@@ -21,6 +21,6 @@
2121
"kleur": "4.1.5",
2222
"kolorist": "1.8.0",
2323
"picocolors": "1.1.1",
24-
"vitest": "^3.0.8"
24+
"vitest": "^3.1.4"
2525
}
2626
}

bench/truecolor.bench.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@ import Bench from './lib/bench.js';
55
import chalk from 'chalk';
66
import { Ansis, cyan, green, red, yellow, hex } from 'ansis';
77

8-
import { getLevel } from '../src/color-support.js';
98
import { colorLevels, LEVEL_TRUECOLOR } from '../src/color-levels.js';
109

1110
const log = console.log;
1211

1312
// create a new instance of Ansis for correct measure in benchmark
14-
const truecolorBench = new Ansis();
15-
const colorSpace = getLevel();
13+
const ansis = new Ansis();
14+
const colorLevel = ansis.level;
1615

1716
log();
18-
log(cyan.inverse` Colors `, `Your terminal supports ${cyan(colorLevels[colorSpace])}.`);
17+
log(cyan.inverse` Colors `, `Your terminal supports ${cyan(colorLevels[colorLevel])}.`);
1918

20-
if (colorSpace < LEVEL_TRUECOLOR) {
19+
if (colorLevel < LEVEL_TRUECOLOR) {
2120
log(red.inverse` WARNING `, yellow`Your terminal doesn't support Truecolor!`);
2221
log('The result of some tests can be NOT correct!\nChoose a modern terminal, e.g. iTerm.\n');
2322
}
@@ -38,12 +37,12 @@ log(hex('#F88').inverse.bold` -= Benchmark =- `);
3837

3938
bench('RGB colors').
4039
add('chalk', () => chalk.rgb(99, 150, 200)('foo')).
41-
add('ansis', () => truecolorBench.rgb(99, 150, 200)('foo')).
40+
add('ansis', () => ansis.rgb(99, 150, 200)('foo')).
4241
run();
4342

4443
bench('HEX colors').
4544
add('chalk #FBA', () => chalk.hex('#FBA')('foo')).
46-
add('ansis #FBA', () => truecolorBench.hex('#FBA')('foo')).
45+
add('ansis #FBA', () => ansis.hex('#FBA')('foo')).
4746
add('chalk #FBA123', () => chalk.hex('#FBA123')('foo')).
48-
add('ansis #FBA123', () => truecolorBench.hex('#FBA123')('foo')).
47+
add('ansis #FBA123', () => ansis.hex('#FBA123')('foo')).
4948
run();

compare/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"dependencies": {
1010
"@colors/colors": "1.6.0",
1111
"ansi-colors": "4.1.3",
12-
"ansis": "file:../dist",
12+
"ansis": "file:../dist/node14",
1313
"chalk": "5.3.0",
1414
"cli-color": "2.0.4",
1515
"colors": "1.4.0",
@@ -23,4 +23,4 @@
2323
"webpack": "^5.98.0",
2424
"webpack-cli": "^6.0.1"
2525
}
26-
}
26+
}

examples/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
"spectrum": "node ./spectrum.js",
88
"stylex": "node ./StyleX.js"
99
}
10-
}
10+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ansis",
3-
"version": "4.0.0",
3+
"version": "4.1.0",
44
"description": "A small and fast ANSI color library",
55
"keywords": [
66
"ansi",

package.npm-node10.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name":"ansis",
3-
"version":"4.0.0-node10",
3+
"version":"4.1.0-node10",
44
"description":"ANSI color lib",
55
"keywords":["ansi","colors","cli"],
66
"license":"ISC",

package.npm-node14.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name":"ansis",
3-
"version":"4.0.0",
3+
"version":"4.1.0",
44
"description":"ANSI color lib",
55
"keywords":["ansi","colors","cli"],
66
"license":"ISC",

0 commit comments

Comments
 (0)