CodeSampleX

サンプル

ansi-styles 6.2.3: color

検証済みサンプル — npm ansi-styles 6.2.3: color. node 22 · linux alpine/x64 · docker で contract を実行し、成功しました: ansiStyles.color provides standard and bright foreground…

sha256:c05c54127e5e0f2b6635793265b6378702311eee3e8b55d7e41bda59aace4596

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
2
ビルドした署名鍵
1
宣言された環境 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

検証実行環境

環境 コントラクト ステージ 実行日
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-23
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-07

ケース

HOW
ゴール
verify ansi-styles.color in pkg:npm/ansi-styles@6.2.3
パッケージ
シンボル
  • ansi-styles.color
環境
node 22.23.2
作成日
2026-08-23T05:32:32Z

コントラクト

  1. ansiStyles.color provides standard and bright foreground colors with matching open and close ANSI escape sequences
  2. ansiStyles.color.close defines the default foreground reset escape sequence
  3. ansiStyles.color.ansi converts standard ANSI color codes to foreground escape sequences
  4. ansiStyles.color.ansi256 generates 256-color palette foreground ANSI escape codes
  5. ansiStyles.color.ansi16m converts RGB values into 24-bit truecolor foreground ANSI escape codes
  6. applyForegroundColor applies foreground color styling including standard names, 256-color palette, and truecolor RGB to text

ファイル

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • test/contract.js

ソースアーティファクトをダウンロード (tar.gz)

ソース

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify ansi-styles.color in pkg:npm/ansi-styles@6.2.3
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/ansi-styles@6.2.3
Demonstrate these symbols/APIs:
  - ansi-styles.color
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:b86dcca21c13fd772c479cdfaf1130a766591ea711a4e582933b9985079b6a7b","constraints":{"executionContext":"node"},"contract":["ansiStyles.color provides standard and bright foreground colors with matching open and close ANSI escape sequences","ansiStyles.color.close defines the default foreground reset escape sequence","ansiStyles.color.ansi converts standard ANSI color codes to foreground escape sequences","ansiStyles.color.ansi256 generates 256-color palette foreground ANSI escape codes","ansiStyles.color.ansi16m converts RGB values into 24-bit truecolor foreground ANSI escape codes","applyForegroundColor applies foreground color styling including standard names, 256-color palette, and truecolor RGB to text"],"goal":"verify ansi-styles.color in pkg:npm/ansi-styles@6.2.3","kind":"HOW","packages":["pkg:npm/ansi-styles@6.2.3"],"schemaVersion":1,"symbols":["ansi-styles.color"]},"contractCommand":["node","test/contract.js"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/ansi-styles@6.2.3"],"schemaVersion":1,"subject":"pkg:npm/ansi-styles@6.2.3","symbols":["ansi-styles.color"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-ansi-styles-color",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-ansi-styles-color",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "ansi-styles": "6.2.3"
      }
    },
    "node_modules/ansi-styles": {
      "version": "6.2.3",
      "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-6.2.3.tgz",
      "integrity": "sha512-4Dj6M28JB+oAH8kFkTLUo+a2jwOFkuqb3yucU0CANcRRUbxS0cP0nZYCGjcc3BNXwRIsUVmDGgzawme7zvJHvg==",
      "license": "MIT",
      "engines": {
        "node": ">=12"
      },
      "funding": {
        "url": "https://github.com/chalk/ansi-styles?sponsor=1"
      }
    }
  }
}
package.json
{
  "name": "sample-ansi-styles-color",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room verification for ansi-styles color",
  "main": "src/index.js",
  "scripts": {
    "test": "node test/contract.js"
  },
  "dependencies": {
    "ansi-styles": "6.2.3"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify ansi-styles.color in pkg:npm/ansi-styles@6.2.3",
  "kind": "HOW",
  "packages": [
    "pkg:npm/ansi-styles@6.2.3"
  ],
  "symbols": [
    "ansi-styles.color"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/index.js
const ansiStylesModule = require('ansi-styles');

const ansiStyles = ansiStylesModule.default || ansiStylesModule;
const { color } = ansiStyles;

/**
 * Returns the color object from ansi-styles.
 * @returns {object}
 */
function getColor() {
  return color;
}

/**
 * Retrieves standard or bright foreground color style pair.
 * @param {string} name
 * @returns {{ open: string, close: string }}
 */
function getNamedColor(name) {
  const style = color[name];
  if (!style) {
    throw new Error(`Unknown foreground color: ${name}`);
  }
  return {
    open: style.open,
    close: style.close,
  };
}

/**
 * Formats a foreground color escape sequence using a 256-color palette index.
 * @param {number} code
 * @returns {{ open: string, close: string }}
 */
function getAnsi256Color(code) {
  if (typeof code !== 'number' || code < 0 || code > 255) {
    throw new RangeError(`Color code must be between 0 and 255: ${code}`);
  }
  return {
    open: color.ansi256(code),
    close: color.close,
  };
}

/**
 * Formats a foreground color escape sequence using 24-bit truecolor RGB values.
 * @param {number} red
 * @param {number} green
 * @param {number} blue
 * @returns {{ open: string, close: string }}
 */
function getAnsi16mColor(red, green, blue) {
  return {
    open: color.ansi16m(red, green, blue),
    close: color.close,
  };
}

/**
 * Applies foreground styling to the provided text.
 * @param {string} text
 * @param {{ name?: string, ansi256?: number, rgb?: [number, number, number] }} options
 * @returns {string}
 */
function applyForegroundColor(text, options = {}) {
  let openSequence = '';
  const closeSequence = color.close;

  if (options.name) {
    openSequence = getNamedColor(options.name).open;
  } else if (typeof options.ansi256 === 'number') {
    openSequence = getAnsi256Color(options.ansi256).open;
  } else if (Array.isArray(options.rgb) && options.rgb.length === 3) {
    const [r, g, b] = options.rgb;
    openSequence = getAnsi16mColor(r, g, b).open;
  } else {
    throw new Error('Must specify one of name, ansi256, or rgb options');
  }

  return `${openSequence}${text}${closeSequence}`;
}

module.exports = {
  ansiStyles,
  color,
  getColor,
  getNamedColor,
  getAnsi256Color,
  getAnsi16mColor,
  applyForegroundColor,
};
test/contract.js
const assert = require('node:assert');
const {
  ansiStyles,
  color,
  getColor,
  getNamedColor,
  getAnsi256Color,
  getAnsi16mColor,
  applyForegroundColor,
} = require('../src/index.js');

// 1. ansiStyles.color provides standard and bright foreground colors with matching open and close ANSI escape sequences
assert(color && typeof color === 'object', 'color must be an object');
assert.strictEqual(getColor(), color);

const standardColors = {
  black: '\u001B[30m',
  red: '\u001B[31m',
  green: '\u001B[32m',
  yellow: '\u001B[33m',
  blue: '\u001B[34m',
  magenta: '\u001B[35m',
  cyan: '\u001B[36m',
  white: '\u001B[37m',
};

for (const [name, expectedOpen] of Object.entries(standardColors)) {
  const style = color[name];
  assert(style, `Missing standard style ${name}`);
  assert.strictEqual(style.open, expectedOpen, `${name} open code mismatch`);
  assert.strictEqual(style.close, '\u001B[39m', `${name} close code mismatch`);

  const named = getNamedColor(name);
  assert.strictEqual(named.open, expectedOpen);
  assert.strictEqual(named.close, '\u001B[39m');
}

const brightColors = {
  blackBright: '\u001B[90m',
  redBright: '\u001B[91m',
  greenBright: '\u001B[92m',
  yellowBright: '\u001B[93m',
  blueBright: '\u001B[94m',
  magentaBright: '\u001B[95m',
  cyanBright: '\u001B[96m',
  whiteBright: '\u001B[97m',
};

for (const [name, expectedOpen] of Object.entries(brightColors)) {
  const style = color[name];
  assert(style, `Missing bright style ${name}`);
  assert.strictEqual(style.open, expectedOpen, `${name} open code mismatch`);
  assert.strictEqual(style.close, '\u001B[39m', `${name} close code mismatch`);
}

// Aliases
assert.deepStrictEqual(color.gray, color.blackBright);
assert.deepStrictEqual(color.grey, color.blackBright);

// 2. ansiStyles.color.close defines the default foreground reset escape sequence
assert.strictEqual(color.close, '\u001B[39m');

// 3. ansiStyles.color.ansi converts standard ANSI color codes to foreground escape sequences
assert(typeof color.ansi === 'function', 'color.ansi must be a function');
assert.strictEqual(color.ansi(30), '\u001B[30m');
assert.strictEqual(color.ansi(31), '\u001B[31m');
assert.strictEqual(color.ansi(32), '\u001B[32m');
assert.strictEqual(color.ansi(37), '\u001B[37m');

// 4. ansiStyles.color.ansi256 generates 256-color palette foreground ANSI escape codes
assert(typeof color.ansi256 === 'function', 'color.ansi256 must be a function');
assert.strictEqual(color.ansi256(0), '\u001B[38;5;0m');
assert.strictEqual(color.ansi256(100), '\u001B[38;5;100m');
assert.strictEqual(color.ansi256(255), '\u001B[38;5;255m');

const fg256 = getAnsi256Color(200);
assert.strictEqual(fg256.open, '\u001B[38;5;200m');
assert.strictEqual(fg256.close, '\u001B[39m');

assert.throws(() => getAnsi256Color(-1), RangeError);
assert.throws(() => getAnsi256Color(256), RangeError);

// 5. ansiStyles.color.ansi16m converts RGB values into 24-bit truecolor foreground ANSI escape codes
assert(typeof color.ansi16m === 'function', 'color.ansi16m must be a function');
assert.strictEqual(color.ansi16m(255, 128, 0), '\u001B[38;2;255;128;0m');
assert.strictEqual(color.ansi16m(0, 0, 0), '\u001B[38;2;0;0;0m');
assert.strictEqual(color.ansi16m(12, 34, 56), '\u001B[38;2;12;34;56m');

const fg16m = getAnsi16mColor(10, 20, 30);
assert.strictEqual(fg16m.open, '\u001B[38;2;10;20;30m');
assert.strictEqual(fg16m.close, '\u001B[39m');

// 6. applyForegroundColor applies foreground color styling including standard names, 256-color palette, and truecolor RGB to text
const message = 'Sample Text';
assert.strictEqual(applyForegroundColor(message, { name: 'cyan' }), `\u001B[36m${message}\u001B[39m`);
assert.strictEqual(applyForegroundColor(message, { name: 'redBright' }), `\u001B[91m${message}\u001B[39m`);
assert.strictEqual(applyForegroundColor(message, { ansi256: 45 }), `\u001B[38;5;45m${message}\u001B[39m`);
assert.strictEqual(applyForegroundColor(message, { rgb: [255, 100, 50] }), `\u001B[38;2;255;100;50m${message}\u001B[39m`);

assert.throws(() => applyForegroundColor(message, { name: 'invalidColor' }), /Unknown foreground color: invalidColor/);
assert.throws(() => applyForegroundColor(message, {}), /Must specify one of name, ansi256, or rgb options/);

console.log('All contract assertions passed successfully.');

オリジンシーダー

匿名