示例
chalk 6.0.0: Downsample ansi256 and bgAnsi256 colors to standard 16-color ANSI escapes when running at level 1
已验证示例 — npm chalk 6.0.0: Downsample ansi256 and bgAnsi256 colors to standard 16-color ANSI escapes when running at level 1. contract 在 node 22 · linux…
sha256:74d548e420a024eb120c52cb1ff26ca2704801d6119d90fc018a20e7c803212e
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。
MIT-0
执行证据
声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。
- 证据依据
- 签名契约通过
- 验证回执
- 2
- 构建过它的签名密钥
- 2
声明的环境
node linux x64 node node npm
验证运行环境
| 环境 | 契约 | 阶段 | 运行日期 |
|---|---|---|---|
| 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 |
案例
HOW- 目标
- Downsample ansi256 and bgAnsi256 colors to standard 16-color ANSI escapes when running at level 1
- 符号
-
- Chalk.prototype.ansi256
- Chalk.prototype.bgAnsi256
- 环境
- node
- 创建时间
- 2026-08-16T07:28:32Z
契约
- construct Chalk instance with level 1 and assert ansi256 downsamples to 16-color ANSI escapes
- assert bgAnsi256 downsamples to 16-color background ANSI escapes at level 1
- assert ansi256 and bgAnsi256 preserve 256-color escape sequences at level 2
- assert ansi256 and bgAnsi256 return plain unstyled text when level is 0
文件
- NOTES.md
- csx.json
- package-lock.json
- package.json
- src/index.mjs
- test/contract.mjs
源代码
# Notes on chalk ansi256 level-1 downsampling
## search_known_solution response
`search_known_solution` returned existing published sample `sha256:5ac46730b5bb5d118cfacbf37a585770c5984e5dd37bc9ffbde2f967ada3a06c` ("Make chalk emit colour codes when output is piped instead of silently producing plain text"). That sample addresses TTY non-interactive pipe detection; this sample proves the silent major version boundary change in Chalk 6.0.0 (PR #687) where `ansi256()` and `bgAnsi256()` downsample to standard 16-color ANSI escapes when running at `level: 1`.
## What a naive model would write instead
A model assumes `chalk.ansi256(196)` and `chalk.bgAnsi256(21)` unconditionally produce 256-color escape sequences (`\u001b[38;5;196m` and `\u001b[48;5;21m`) even when restricted to a 16-color (`level: 1`) environment.
## How the wrong version fails
In Chalk <= 5.x, `ansi256()` silently bypassed `level: 1` color downsampling and emitted raw 256-color escapes that render corrupted or uncolored text on 16-color terminals while build suites pass silently green.
{"case":{"caseId":"case:sha256:caa59cc897be79333bbf8c5a991ea37ac120ad82079196fd2e555c2d67712973","contract":["construct Chalk instance with level 1 and assert ansi256 downsamples to 16-color ANSI escapes","assert bgAnsi256 downsamples to 16-color background ANSI escapes at level 1","assert ansi256 and bgAnsi256 preserve 256-color escape sequences at level 2","assert ansi256 and bgAnsi256 return plain unstyled text when level is 0"],"goal":"Downsample ansi256 and bgAnsi256 colors to standard 16-color ANSI escapes when running at level 1","kind":"HOW","packages":["pkg:npm/chalk@6.0.0"],"schemaVersion":1,"symbols":["Chalk.prototype.ansi256","Chalk.prototype.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.prototype.ansi256","Chalk.prototype.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 (PR #687), ansi256() and bgAnsi256() are downsampled to standard 16-color ANSI escapes
// when running at level 1 (basic 16-color ANSI support).
// In Chalk <= 5.x, ansi256() bypassed level 1 downsampling and emitted 256-color escape sequences (\u001b[38;5;...m)
// which silently produced broken escapes or uncolored output on basic 16-color terminal emulators.
export function formatBasic(text, fgCode, bgCode) {
let customChalk = new Chalk({ level: 1 });
if (fgCode !== undefined) {
customChalk = customChalk.ansi256(fgCode);
}
if (bgCode !== undefined) {
customChalk = customChalk.bgAnsi256(bgCode);
}
return customChalk(text);
}
export function formatExtended(text, fgCode, bgCode) {
let customChalk = new Chalk({ level: 2 });
if (fgCode !== undefined) {
customChalk = customChalk.ansi256(fgCode);
}
if (bgCode !== undefined) {
customChalk = customChalk.bgAnsi256(bgCode);
}
return customChalk(text);
}
export function formatDisabled(text, fgCode, bgCode) {
let customChalk = new Chalk({ level: 0 });
if (fgCode !== undefined) {
customChalk = customChalk.ansi256(fgCode);
}
if (bgCode !== undefined) {
customChalk = customChalk.bgAnsi256(bgCode);
}
return customChalk(text);
}
import { strict as assert } from 'node:assert';
import { formatBasic, formatExtended, formatDisabled } from '../src/index.mjs';
// --- Level 1: 16-color ANSI palette downsampling (Chalk 6 behavior) ---
// In Chalk 6.0.0, ansi256(196) (bright red in 256 palette) is downsampled to ANSI bright red (\u001b[91m),
// and bgAnsi256(21) (bright blue in 256 palette) is downsampled to ANSI bright blue bg (\u001b[104m).
// A naive model expecting \u001b[38;5;196m (the Chalk <= 5 behavior) fails here.
const basicRed = formatBasic('alert', 196);
assert.equal(basicRed, '\u001b[91malert\u001b[39m');
assert.ok(!basicRed.includes('\u001b[38;5;'), 'Level 1 must downsample away 256-color escape sequences');
const basicBgBlue = formatBasic('status', undefined, 21);
assert.equal(basicBgBlue, '\u001b[104mstatus\u001b[49m');
assert.ok(!basicBgBlue.includes('\u001b[48;5;'), 'Level 1 must downsample away 256-color background escape sequences');
// Combined foreground and background downsampled at level 1:
const basicCombined = formatBasic('notice', 196, 21);
assert.equal(basicCombined, '\u001b[91m\u001b[104mnotice\u001b[49m\u001b[39m');
// --- Level 2: 256-color palette (extended escape sequences preserved) ---
const extendedRed = formatExtended('alert', 196);
assert.equal(extendedRed, '\u001b[38;5;196malert\u001b[39m');
const extendedBgBlue = formatExtended('status', undefined, 21);
assert.equal(extendedBgBlue, '\u001b[48;5;21mstatus\u001b[49m');
// --- Level 0: Colors disabled (raw plain text returned) ---
assert.equal(formatDisabled('alert', 196), 'alert');
assert.equal(formatDisabled('status', undefined, 21), 'status');
console.log('CONTRACT PASS: chalk 6 downsamples ansi256 and bgAnsi256 to 16 colors at level 1');