Exemplo
chalk 6.0.0: Downsample ansi256 and bgAnsi256 colors to 16 colors on basic terminal level 1 instead of emitting raw 256-color escape codes
Amostra verificada para npm chalk 6.0.0: Downsample ansi256 and bgAnsi256 colors to 16 colors on basic terminal level 1 instead of emitting raw 256-color…
sha256:b6309c6a71b1cf444cb19cb250eae3794c9cffc402c664b03637c1791b4bbda7
Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu.
Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas.
MIT-0
Evidência de execução
O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.
- Base da evidência
- Contrato assinado aprovado
- Recibos de verificação
- 2
- Chaves de assinatura que o compilaram
- 2
Ambiente declarado
node linux x64 node node npm
Ambientes das execuções de verificação
| Ambiente | Contrato | Etapas | Execução |
|---|---|---|---|
| node 22 · linux alpine/x64 · docker ed25519:d91480838ac982c9 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · node-typescript@1 |
2026-08-16 |
| node 22 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · node-typescript@1 |
2026-08-18 |
Caso
HOW- Objetivo
- Downsample ansi256 and bgAnsi256 colors to 16 colors on basic terminal level 1 instead of emitting raw 256-color escape codes
- Pacotes
- Símbolos
-
- Chalk
- ansi256
- bgAnsi256
- Ambiente
- node
- Criado
- 2026-08-16T07:28:35Z
Contrato
- construct a Chalk instance with level 1 (basic 16 colors)
- assert ansi256(196) downsamples to ANSI bright red escape code \u001b[91m instead of emitting raw \u001b[38;5;196m
- assert bgAnsi256(196) downsamples to ANSI bright red background escape code \u001b[101m instead of emitting raw \u001b[48;5;196m
- assert level 2 preserves full 256-color escape sequences
- assert level 0 returns unstyled plain text
Arquivos
- NOTES.md
- csx.json
- package-lock.json
- package.json
- src/index.mjs
- test/contract.mjs
Código-fonte
# Notes
## Search Result
`search_known_solution` returned COMPATIBLE matches, including existing published sample `sha256:5ac46730b5bb5d118cfacbf37a585770c5984e5dd37bc9ffbde2f967ada3a06c` ("Make chalk emit colour codes when output is piped instead of silently producing plain text"). To avoid duplicating known behavior, this sample targets the PR #687 behavioral fix in Chalk v6.0.0.
## What a Model Would Have Written Instead
A model would assume that `chalk.ansi256(n)` and `chalk.bgAnsi256(n)` in Chalk v5 already downsampled to basic 16-color ANSI escape sequences when color support was set to `level: 1` (just as `chalk.rgb()` and `chalk.hex()` did), writing assertions expecting 16-color escapes on level 1 instances.
## How the Wrong Version Fails
On `chalk@5.6.2` (and earlier), the contract fails loudly with an `AssertionError` because `ansi256()` and `bgAnsi256()` bypass downsampling at `level: 1` and emit raw 256-color escape sequences (`\u001b[38;5;196m` / `\u001b[48;5;196m`) that 16-color terminals cannot render, instead of downsampling to basic 16-color codes (`\u001b[91m` / `\u001b[101m`).
{"case":{"caseId":"case:sha256:3a7f0dbc7c3e12274380ce84bf693fd893292ba8aabbe924bb350802e9a33764","contract":["construct a Chalk instance with level 1 (basic 16 colors)","assert ansi256(196) downsamples to ANSI bright red escape code \\u001b[91m instead of emitting raw \\u001b[38;5;196m","assert bgAnsi256(196) downsamples to ANSI bright red background escape code \\u001b[101m instead of emitting raw \\u001b[48;5;196m","assert level 2 preserves full 256-color escape sequences","assert level 0 returns unstyled plain text"],"goal":"Downsample ansi256 and bgAnsi256 colors to 16 colors on basic terminal level 1 instead of emitting raw 256-color escape codes","kind":"HOW","packages":["pkg:npm/chalk@6.0.0"],"schemaVersion":1,"symbols":["Chalk","ansi256","bgAnsi256"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","ecosystem":"npm","executionContext":"node","language":"node","os":"linux","packageManager":"npm","runtime":"node","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/chalk@6.0.0"],"schemaVersion":1,"symbols":["Chalk","ansi256","bgAnsi256"],"verifierAdapter":"node-typescript@1"}
{
"name": "csx-sample-chalk-ansi256-downsample",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "csx-sample-chalk-ansi256-downsample",
"version": "1.0.0",
"license": "MIT-0",
"dependencies": {
"chalk": "6.0.0"
}
},
"node_modules/chalk": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-6.0.0.tgz",
"integrity": "sha512-2uNTXIuTTxk7ciZgAU1BQcgnchcG0xXnrs6jzkQfj9SsRa9M2s5zE8WT96hS6KmG4MzWHSrvH43DF1m4XRkrFg==",
"license": "MIT",
"engines": {
"node": ">=22"
},
"funding": {
"url": "https://github.com/chalk/chalk?sponsor=1"
}
}
}
}
{
"name": "csx-sample-chalk-ansi256-downsample",
"version": "1.0.0",
"private": true,
"type": "module",
"license": "MIT-0",
"dependencies": {
"chalk": "6.0.0"
}
}
import { Chalk } from 'chalk';
// In chalk < 6.0.0, rgb() and hex() downsampled to 16 colors when level was 1,
// but ansi256() and bgAnsi256() emitted raw 256-color ANSI escapes (\u001b[38;5;...m).
// Chalk 6.0.0 downsamples ansi256 and bgAnsi256 to 16 colors when level is 1.
export function formatWithLevel1(code, text) {
const customChalk = new Chalk({ level: 1 });
return {
fg: customChalk.ansi256(code)(text),
bg: customChalk.bgAnsi256(code)(text),
};
}
export function formatWithLevel2(code, text) {
const customChalk = new Chalk({ level: 2 });
return {
fg: customChalk.ansi256(code)(text),
bg: customChalk.bgAnsi256(code)(text),
};
}
export function formatWithLevel0(code, text) {
const customChalk = new Chalk({ level: 0 });
return {
fg: customChalk.ansi256(code)(text),
bg: customChalk.bgAnsi256(code)(text),
};
}
import { strict as assert } from 'node:assert';
import { formatWithLevel1, formatWithLevel2, formatWithLevel0 } from '../src/index.mjs';
// At color level 1 (basic 16-color ANSI support), ansi256 and bgAnsi256 must downsample
// to the closest 16-color ANSI escape code.
// Color 196 (#ff0000) downsamples to ANSI bright red (foreground 91, background 101).
const level1Result = formatWithLevel1(196, 'alert');
assert.equal(
level1Result.fg,
'\u001b[91malert\u001b[39m',
'ansi256(196) must be downsampled to ANSI bright red foreground (\u001b[91m) at level 1'
);
assert.equal(
level1Result.bg,
'\u001b[101malert\u001b[49m',
'bgAnsi256(196) must be downsampled to ANSI bright red background (\u001b[101m) at level 1'
);
// At color level 2 (256-color support), 256-color escape sequences are preserved
const level2Result = formatWithLevel2(196, 'alert');
assert.equal(
level2Result.fg,
'\u001b[38;5;196malert\u001b[39m',
'ansi256(196) must emit exact 256-color escape sequence (\u001b[38;5;196m) at level 2'
);
assert.equal(
level2Result.bg,
'\u001b[48;5;196malert\u001b[49m',
'bgAnsi256(196) must emit exact 256-color bg escape sequence (\u001b[48;5;196m) at level 2'
);
// At color level 0 (colors disabled), strings are returned without ANSI escape sequences
const level0Result = formatWithLevel0(196, 'alert');
assert.equal(
level0Result.fg,
'alert',
'ansi256(196) must return plain text when colors are disabled (level 0)'
);
assert.equal(
level0Result.bg,
'alert',
'bgAnsi256(196) must return plain text when colors are disabled (level 0)'
);
console.log('CONTRACT PASS: chalk downsamples ansi256 and bgAnsi256 to 16 colors at level 1');