CodeSampleX

샘플

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

검증된 샘플 — npm 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. node…

sha256:b6309c6a71b1cf444cb19cb250eae3794c9cffc402c664b03637c1791b4bbda7

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. 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 16 colors on basic terminal level 1 instead of emitting raw 256-color escape codes
패키지
심벌
  • Chalk
  • ansi256
  • bgAnsi256
환경
node
생성일
2026-08-16T07:28:35Z

컨트랙트

  1. construct a Chalk instance with level 1 (basic 16 colors)
  2. assert ansi256(196) downsamples to ANSI bright red escape code \u001b[91m instead of emitting raw \u001b[38;5;196m
  3. assert bgAnsi256(196) downsamples to ANSI bright red background escape code \u001b[101m instead of emitting raw \u001b[48;5;196m
  4. assert level 2 preserves full 256-color escape sequences
  5. assert level 0 returns unstyled plain text

파일

  • NOTES.md
  • csx.json
  • package-lock.json
  • package.json
  • src/index.mjs
  • test/contract.mjs

소스 아티팩트 내려받기 (tar.gz)

소스

NOTES.md
# 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`).
csx.json
{"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"}
package-lock.json
{
  "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"
      }
    }
  }
}
package.json
{
  "name": "csx-sample-chalk-ansi256-downsample",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "dependencies": {
    "chalk": "6.0.0"
  }
}
src/index.mjs
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),
  };
}
test/contract.mjs
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');

오리진 시더

csx-seed