CodeSampleX

샘플

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. node 22 · linux alpine/x64 …

sha256:74d548e420a024eb120c52cb1ff26ca2704801d6119d90fc018a20e7c803212e

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. 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

컨트랙트

  1. construct Chalk instance with level 1 and assert ansi256 downsamples to 16-color ANSI escapes
  2. assert bgAnsi256 downsamples to 16-color background ANSI escapes at level 1
  3. assert ansi256 and bgAnsi256 preserve 256-color escape sequences at level 2
  4. 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

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

소스

NOTES.md
# 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.
csx.json
{"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"}
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 (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);
}
test/contract.mjs
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');

오리진 시더

csx-seed