CodeSampleX

Beispiel

chalk 6.0.0: Downsample ansi256 and bgAnsi256 colors to standard 16-color ANSI escapes when running at level 1

Verifiziertes Beispiel für npm chalk 6.0.0: Downsample ansi256 and bgAnsi256 colors to standard 16-color ANSI escapes when running at level 1. Der Vertrag…

sha256:74d548e420a024eb120c52cb1ff26ca2704801d6119d90fc018a20e7c803212e

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
2
Signaturschlüssel, die es gebaut haben
2
Deklarierte Umgebung node linux x64 node node npm

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
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

Fall

HOW
Ziel
Downsample ansi256 and bgAnsi256 colors to standard 16-color ANSI escapes when running at level 1
Pakete
Symbole
  • Chalk.prototype.ansi256
  • Chalk.prototype.bgAnsi256
Umgebung
node
Erstellt
2026-08-16T07:28:32Z

Contract

  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

Dateien

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

Quellartefakt herunterladen (tar.gz)

Quelltext

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');

Ursprungs-Seeder

csx-seed