pax_global_header00006660000000000000000000000064141314442240014511gustar00rootroot0000000000000052 comment=228cea3fa726857785b8c069cccc58e9743cd71d picocolors-1.0.0/000077500000000000000000000000001413144422400136635ustar00rootroot00000000000000picocolors-1.0.0/.editorconfig000066400000000000000000000004211413144422400163350ustar00rootroot00000000000000# EditorConfig is awesome: https://EditorConfig.org root = true [*] end_of_line = lf insert_final_newline = true [*.{js,d.ts}] charset = utf-8 indent_style = tab indent_size = 2 trim_trailing_whitespace = true [package.json,*.yaml] indent_style = space indent_size = 2 picocolors-1.0.0/.github/000077500000000000000000000000001413144422400152235ustar00rootroot00000000000000picocolors-1.0.0/.github/workflows/000077500000000000000000000000001413144422400172605ustar00rootroot00000000000000picocolors-1.0.0/.github/workflows/benchmarks.yaml000066400000000000000000000013171413144422400222630ustar00rootroot00000000000000name: Benchmarks on: push: branches: [ main ] pull_request: branches: [ main ] env: FORCE_COLOR: 3 jobs: benchmarks: name: Benchmarks runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: 16 - run: npm install - name: Ensure color support detection run: node tests/environments.js - name: Simple API calls run: node benchmarks/simple.js - name: Complex formatting expression run: node benchmarks/complex.js - name: Library module's init time run: node benchmarks/loading.js - name: NPM package size run: node benchmarks/size.js picocolors-1.0.0/.github/workflows/testing.yaml000066400000000000000000000015331413144422400216230ustar00rootroot00000000000000name: Testing on: push: branches: [ main ] pull_request: branches: [ main ] jobs: modern: name: Node v${{ matrix.node-version }} runs-on: ubuntu-latest strategy: matrix: node-version: - 16 - 14 - 12 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install - run: node tests/test.js legacy: name: Node v${{ matrix.node-version }} (Legacy) runs-on: ubuntu-latest strategy: matrix: node-version: - 10 - 8 - 6 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install - run: node tests/test.js picocolors-1.0.0/.prettierignore000066400000000000000000000000251413144422400167230ustar00rootroot00000000000000picocolors.browser.* picocolors-1.0.0/CHANGELOG.md000066400000000000000000000027461413144422400155050ustar00rootroot00000000000000# Changelog ## [v1.0.0](https://github.com/alexeyraspopov/picocolors/releases/tag/v1.0.0) - Removed several code elements to reduce the package size ([#31](https://github.com/alexeyraspopov/picocolors/pull/31)) - Fixed optional flag for `createColors()` in TypeScript typings ([#34](https://github.com/alexeyraspopov/picocolors/pull/34)) ## [v0.2.1](https://github.com/alexeyraspopov/picocolors/releases/tag/v0.2.1) - Removed semicolons to reduce the package size ([#28](https://github.com/alexeyraspopov/picocolors/pull/28)) - Fixed type definitions ([#29](https://github.com/alexeyraspopov/picocolors/pull/29)) - Made `createColors()` use `isColorSupported` if no flag was provided ([`aaf57e1`](https://github.com/alexeyraspopov/picocolors/commit/aaf57e14b250112c6ad4fbeff08ad78cafc6c887)) ## [v0.2.0](https://github.com/alexeyraspopov/picocolors/releases/tag/v0.2.0) - Removed ESM Module to fix the rest of compatibility issues and reduce package size ([#26](https://github.com/alexeyraspopov/picocolors/pull/26)) - Added support for non-string inputs ([`3276400`](https://github.com/alexeyraspopov/picocolors/commit/3276400d5046c93ae56648e3db137a20b1f420b4)) ## [v0.1.0](https://github.com/alexeyraspopov/picocolors/releases/tag/v0.1.0) - Added CommonJS support ([#7](https://github.com/alexeyraspopov/picocolors/pull/7)) - Ensured Node.js 6+ support ([#8](https://github.com/alexeyraspopov/picocolors/pull/8)) - Added Browsers support ([#10](https://github.com/alexeyraspopov/picocolors/pull/10)) picocolors-1.0.0/LICENSE000066400000000000000000000014151413144422400146710ustar00rootroot00000000000000ISC License Copyright (c) 2021 Alexey Raspopov, Kostiantyn Denysov, Anton Verinov Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. picocolors-1.0.0/README.md000066400000000000000000000111371413144422400151450ustar00rootroot00000000000000# picocolors The tiniest and the fastest library for terminal output formatting with ANSI colors. ```javascript import pc from "picocolors" console.log( pc.green(`How are ${pc.italic(`you`)} doing?`) ) ``` - **No dependencies.** - **14 times** smaller and **2 times** faster than chalk. - Used by popular tools like PostCSS, SVGO, Stylelint, and Browserslist. - Node.js v6+ & browsers support. Support for both CJS and ESM projects. - TypeScript type declarations included. - [`NO_COLOR`](https://no-color.org/) friendly. ## Motivation With `picocolors` we are trying to draw attention to the `node_modules` size problem and promote performance-first culture. ## Prior Art Credits go to the following projects: - [Nanocolors](https://github.com/ai/nanocolors) by [@ai](https://github.com/ai) - [Colorette](https://github.com/jorgebucaran/colorette) by [@jorgebucaran](https://github.com/jorgebucaran) - [Kleur](https://github.com/lukeed/kleur) by [@lukeed](https://github.com/lukeed) - [Colors.js](https://github.com/Marak/colors.js) by [@Marak](https://github.com/Marak) - [Chalk](https://github.com/chalk/chalk) by [@sindresorhus](https://github.com/sindresorhus) ## Benchmarks The space in node_modules including sub-dependencies: ```diff $ node ./benchmarks/size.js Data from packagephobia.com chalk 101 kB cli-color 1249 kB ansi-colors 25 kB kleur 21 kB colorette 17 kB nanocolors 16 kB + picocolors 7 kB ``` Library loading time: ```diff $ node ./benchmarks/loading.js chalk 6.760 ms cli-color 37.949 ms ansi-colors 1.790 ms kleur 2.278 ms kleur/colors 0.858 ms colorette 2.724 ms nanocolors 0.885 ms + picocolors 0.470 ms ``` Benchmark for simple use case: ```diff $ node ./benchmarks/simple.js chalk 17,544,513 ops/sec cli-color 808,186 ops/sec ansi-colors 3,798,290 ops/sec kleur 17,209,612 ops/sec kleur/colors 29,425,416 ops/sec colorette 30,236,826 ops/sec nanocolors 29,618,223 ops/sec + picocolors 29,514,102 ops/sec ``` Benchmark for complex use cases: ```diff $ node ./benchmarks/complex.js chalk 856,888 ops/sec cli-color 109,678 ops/sec ansi-colors 306,752 ops/sec kleur 568,643 ops/sec kleur/colors 984,151 ops/sec colorette 1,337,991 ops/sec nanocolors 1,088,193 ops/sec + picocolors 1,772,265 ops/sec ``` ## Usage Picocolors provides an object which includes a variety of text coloring and formatting functions ```javascript import pc from "picocolors" ``` The object includes following coloring functions: `black`, `red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, `white`, `gray`. ```javascript console.log(`I see a ${pc.red("red door")} and I want it painted ${pc.black("black")}`) ``` The object also includes following background color modifier functions: `bgBlack`, `bgRed`, `bgGreen`, `bgYellow`, `bgBlue`, `bgMagenta`, `bgCyan`, `bgWhite`. ```javascript console.log( pc.bgBlack( pc.white(`Tom appeared on the sidewalk with a bucket of whitewash and a long-handled brush.`) ) ) ``` Besides colors, the object includes following formatting functions: `dim`, `bold`, `hidden`, `italic`, `underline`, `strikethrough`, `reset`, `inverse`. ```javascript for (let task of tasks) { console.log(`${pc.bold(task.name)} ${pc.dim(task.durationMs + "ms")}`) } ``` The library provides additional utilities to ensure the best results for the task: - `isColorSupported` — boolean, explicitly tells whether or not the colors or formatting appear on the screen ```javascript import pc from "picocolors" if (pc.isColorSupported) { console.log("Yay! This script can use colors and formatters") } ``` - `createColors(enabled)` — a function that returns a new API object with manually defined color support configuration ```javascript import pc from "picocolors" let { red, bgWhite } = pc.createColors(options.enableColors) ``` ## Replacing `chalk` 1. Replace package name in import: ```diff - import chalk from 'chalk' + import pico from 'picocolors' ``` 2. Replace variable: ```diff - chalk.red(text) + pico.red(text) ``` 3. Replace chains to nested calls: ```diff - chalk.red.bold(text) + pico.red(pico.bold(text)) ``` 4. You can use [`colorize-template`](https://github.com/usmanyunusov/colorize-template) to replace chalk’s tagged template literal. ```diff + import { createColorize } from 'colorize-template' + let colorize = createColorize(pico) - chalk.red.bold`full {yellow ${"text"}}` + colorize`{red.bold full {yellow ${"text"}}}` ``` picocolors-1.0.0/benchmarks/000077500000000000000000000000001413144422400160005ustar00rootroot00000000000000picocolors-1.0.0/benchmarks/complex.js000077500000000000000000000066051413144422400200170ustar00rootroot00000000000000#!/usr/bin/env node // Benchmark results are unstable. To have more stable results: // 1. Restart OS. Do not run any applications. Put power cable to laptop. // 2. Run tests 5 times. // 3. Took the best result for each candidate. let benchmark = require("benchmark") let colorette = require("colorette") let kleur = require("kleur") let kleurColors = require("kleur/colors") let chalk = require("chalk") let ansi = require("ansi-colors") let cliColor = require("cli-color") let picocolors = require("../picocolors.js") let nanocolors = require("nanocolors") function formatNumber(number) { return String(number) .replace(/\d{3}$/, ",$&") .replace(/^(\d|\d\d)(\d{3},)/, "$1,$2") } let suite = new benchmark.Suite() let out let index = 1e8 suite .add("chalk", () => { out = chalk.red('.') + chalk.yellow('.') + chalk.green('.') + chalk.bgRed.black(" ERROR ") + chalk.red( " Add plugin " + chalk.yellow("name") + " to use time limit with " + chalk.yellow(++index) ) }) .add("cli-color", () => { out = cliColor.red('.') + cliColor.yellow('.') + cliColor.green('.') + cliColor.bgRed.black(" ERROR ") + cliColor.red( " Add plugin " + cliColor.yellow("name") + " to use time limit with " + cliColor.yellow(++index) ) }) .add("ansi-colors", () => { out = ansi.red('.') + ansi.yellow('.') + ansi.green('.') + ansi.bgRed.black(" ERROR ") + ansi.red( " Add plugin " + ansi.yellow("name") + " to use time limit with " + ansi.yellow(++index) ) }) .add("kleur", () => { out = kleur.red('.') + kleur.yellow('.') + kleur.green('.') + kleur.bgRed().black(" ERROR ") + kleur.red( " Add plugin " + kleur.yellow("name") + " to use time limit with " + kleur.yellow(++index) ) }) .add("kleur/colors", () => { out = kleurColors.red('.') + kleurColors.yellow('.') + kleurColors.green('.') + kleurColors.bgRed(kleurColors.black(" ERROR ")) + kleurColors.red( " Add plugin " + kleurColors.yellow("name") + " to use time limit with " + kleurColors.yellow(++index) ) }) .add("colorette", () => { out = colorette.red('.') + colorette.yellow('.') + colorette.green('.') + colorette.bgRed(colorette.black(" ERROR ")) + colorette.red( " Add plugin " + colorette.yellow("name") + " to use time limit with " + colorette.yellow(++index) ) }) .add("nanocolors", () => { out = nanocolors.red('.') + nanocolors.yellow('.') + nanocolors.green('.') + nanocolors.bgRed(nanocolors.black(" ERROR ")) + nanocolors.red( " Add plugin " + nanocolors.yellow("name") + " to use time limit with " + nanocolors.yellow(++index) ) }) .add("picocolors", () => { out = picocolors.red('.') + picocolors.yellow('.') + picocolors.green('.') + picocolors.bgRed(picocolors.black(" ERROR ")) + picocolors.red( " Add plugin " + picocolors.yellow("name") + " to use time limit with " + picocolors.yellow(`${++index}`) ) }) .on("cycle", event => { let prefix = event.target.name === "picocolors" ? "+ " : " " let name = event.target.name.padEnd("kleur/colors ".length) let hz = formatNumber(event.target.hz.toFixed(0)).padStart(10) process.stdout.write(`${prefix}${name}${picocolors.bold(hz)} ops/sec\n`) }) .on("error", event => { process.stderr.write(picocolors.red(event.target.error.toString()) + "\n") process.exit(1) }) .run() picocolors-1.0.0/benchmarks/loading-runner.js000066400000000000000000000015431413144422400212650ustar00rootroot00000000000000const { performance } = require("perf_hooks") let before function showTime(name) { let after = performance.now() process.stdout.write(name + " " + (after - before) + "\n") } before = performance.now() let chalk = require("chalk") showTime("chalk") before = performance.now() let cliColor = require("cli-color") showTime("cli-color") before = performance.now() let ansi = require("ansi-colors") showTime("ansi-colors") before = performance.now() let kleur = require("kleur") showTime("kleur") before = performance.now() let kleurColors = require("kleur/colors") showTime("kleur/colors") before = performance.now() let colorette = require("colorette") showTime("colorette") before = performance.now() let nanocolors = require("nanocolors") showTime("nanocolors") before = performance.now() let picocolors = require("../picocolors.js") showTime("picocolors") picocolors-1.0.0/benchmarks/loading.js000077500000000000000000000013171413144422400177600ustar00rootroot00000000000000#!/usr/bin/env node let { execSync } = require("child_process") let RUNS = 50 let results = {} for (let i = 0; i < RUNS; i++) { let output = execSync("node ./benchmarks/loading-runner.js").toString() output .trim() .split("\n") .forEach(line => { let [name, result] = line.split(" ") results[name] = (results[name] || 0) + parseFloat(result) }) } for (let name in results) { let prefix = name === "picocolors" ? "+ " : " " let title = name.padEnd("kleur/colors ".length) let time = (Math.round((1000 * results[name]) / RUNS) / 1000) .toString() .replace(/\.\d$/, "$&00") .replace(/\.\d\d$/, "$&0") process.stdout.write(prefix + title + "\x1B[1m" + time.padStart(6) + "\x1B[22m ms\n") } picocolors-1.0.0/benchmarks/simple.js000077500000000000000000000040721413144422400176350ustar00rootroot00000000000000#!/usr/bin/env node // Benchmark results are unstable. To have more stable results: // 1. Restart OS. Do not run any applications. Put power cable to laptop. // 2. Run tests 5 times. // 3. Took the best result for each candidate. let benchmark = require("benchmark") let colorette = require("colorette") let kleur = require("kleur") let kleurColors = require("kleur/colors") let chalk = require("chalk") let ansi = require("ansi-colors") let cliColor = require("cli-color") let picocolors = require("../picocolors.js") let nanocolors = require("nanocolors") function formatNumber(number) { return String(number) .replace(/\d{3}$/, ",$&") .replace(/^(\d|\d\d)(\d{3},)/, "$1,$2") } console.log(colorette.green("colorette")) console.log(kleur.green("kleur")) console.log(chalk.green("chalk")) console.log(ansi.green("ansi")) console.log(cliColor.green("cliColor")) console.log(picocolors.green("picocolors")) console.log(nanocolors.green("nanocolors")) let suite = new benchmark.Suite() let out suite .add("chalk", () => { out = chalk.red("Add plugin to use time limit") }) .add("cli-color", () => { out = cliColor.red("Add plugin to use time limit") }) .add("ansi-colors", () => { out = ansi.red("Add plugin to use time limit") }) .add("kleur", () => { out = kleur.red("Add plugin to use time limit") }) .add("kleur/colors", () => { out = kleurColors.red("Add plugin to use time limit") }) .add("colorette", () => { out = colorette.red("Add plugin to use time limit") }) .add("nanocolors", () => { out = nanocolors.red("Add plugin to use time limit") }) .add("picocolors", () => { out = picocolors.red("Add plugin to use time limit") }) .on("cycle", event => { let prefix = event.target.name === "picocolors" ? "+ " : " " let name = event.target.name.padEnd("kleur/colors ".length) let hz = formatNumber(event.target.hz.toFixed(0)).padStart(10) process.stdout.write(`${prefix}${name}${picocolors.bold(hz)} ops/sec\n`) }) .on("error", event => { process.stderr.write(picocolors.red(event.target.error.toString()) + "\n") process.exit(1) }) .run() picocolors-1.0.0/benchmarks/size.js000077500000000000000000000016571413144422400173240ustar00rootroot00000000000000#!/usr/bin/env node let { get } = require("https") let { bold, gray } = require("../picocolors.js") async function getJSON(url) { return new Promise(resolve => { get(url, res => { let text = "" res.on("data", chunk => { text += chunk }) res.on("end", () => { resolve(JSON.parse(text)) }) }) }) } async function benchmark(lib) { let data = await getJSON(`https://packagephobia.com/v2/api.json?p=${lib}`) let size = data.install.bytes process.stdout.write( lib.padEnd("ansi-colors ".length) + bold( Math.round(size / 1024) .toString() .padStart(4) ) + " kB\n" ) } async function start() { process.stdout.write(gray("Data from packagephobia.com\n")) await benchmark(" chalk") await benchmark(" cli-color") await benchmark(" ansi-colors") await benchmark(" kleur") await benchmark(" colorette") await benchmark(" nanocolors") await benchmark("+ picocolors") } start() picocolors-1.0.0/package.json000066400000000000000000000020331413144422400161470ustar00rootroot00000000000000{ "name": "picocolors", "version": "1.0.0", "main": "./picocolors.js", "types": "./picocolors.d.ts", "browser": { "./picocolors.js": "./picocolors.browser.js" }, "sideEffects": false, "description": "The tiniest and the fastest library for terminal output formatting with ANSI colors", "scripts": { "test": "node tests/test.js" }, "files": [ "picocolors.*", "types.ts" ], "keywords": [ "terminal", "colors", "formatting", "cli", "console" ], "author": "Alexey Raspopov", "repository": "alexeyraspopov/picocolors", "license": "ISC", "devDependencies": { "ansi-colors": "^4.1.1", "benchmark": "^2.1.4", "chalk": "^4.1.2", "clean-publish": "^3.0.3", "cli-color": "^2.0.0", "colorette": "^2.0.12", "kleur": "^4.1.4", "nanocolors": "^0.2.12", "prettier": "^2.4.1" }, "prettier": { "printWidth": 100, "useTabs": true, "tabWidth": 2, "semi": false, "arrowParens": "avoid" }, "clean-publish": { "cleanDocs": true } } picocolors-1.0.0/picocolors.browser.js000066400000000000000000000005501413144422400200570ustar00rootroot00000000000000var x=String; var create=function() {return {isColorSupported:false,reset:x,bold:x,dim:x,italic:x,underline:x,inverse:x,hidden:x,strikethrough:x,black:x,red:x,green:x,yellow:x,blue:x,magenta:x,cyan:x,white:x,gray:x,bgBlack:x,bgRed:x,bgGreen:x,bgYellow:x,bgBlue:x,bgMagenta:x,bgCyan:x,bgWhite:x}}; module.exports=create(); module.exports.createColors = create; picocolors-1.0.0/picocolors.d.ts000066400000000000000000000002121413144422400166240ustar00rootroot00000000000000import { Colors } from "./types" declare const picocolors: Colors & { createColors: (enabled?: boolean) => Colors } export = picocolors picocolors-1.0.0/picocolors.js000066400000000000000000000050421413144422400163760ustar00rootroot00000000000000let tty = require("tty") let isColorSupported = !("NO_COLOR" in process.env || process.argv.includes("--no-color")) && ("FORCE_COLOR" in process.env || process.argv.includes("--color") || process.platform === "win32" || (tty.isatty(1) && process.env.TERM !== "dumb") || "CI" in process.env) let formatter = (open, close, replace = open) => input => { let string = "" + input let index = string.indexOf(close, open.length) return ~index ? open + replaceClose(string, close, replace, index) + close : open + string + close } let replaceClose = (string, close, replace, index) => { let start = string.substring(0, index) + replace let end = string.substring(index + close.length) let nextIndex = end.indexOf(close) return ~nextIndex ? start + replaceClose(end, close, replace, nextIndex) : start + end } let createColors = (enabled = isColorSupported) => ({ isColorSupported: enabled, reset: enabled ? s => `\x1b[0m${s}\x1b[0m` : String, bold: enabled ? formatter("\x1b[1m", "\x1b[22m", "\x1b[22m\x1b[1m") : String, dim: enabled ? formatter("\x1b[2m", "\x1b[22m", "\x1b[22m\x1b[2m") : String, italic: enabled ? formatter("\x1b[3m", "\x1b[23m") : String, underline: enabled ? formatter("\x1b[4m", "\x1b[24m") : String, inverse: enabled ? formatter("\x1b[7m", "\x1b[27m") : String, hidden: enabled ? formatter("\x1b[8m", "\x1b[28m") : String, strikethrough: enabled ? formatter("\x1b[9m", "\x1b[29m") : String, black: enabled ? formatter("\x1b[30m", "\x1b[39m") : String, red: enabled ? formatter("\x1b[31m", "\x1b[39m") : String, green: enabled ? formatter("\x1b[32m", "\x1b[39m") : String, yellow: enabled ? formatter("\x1b[33m", "\x1b[39m") : String, blue: enabled ? formatter("\x1b[34m", "\x1b[39m") : String, magenta: enabled ? formatter("\x1b[35m", "\x1b[39m") : String, cyan: enabled ? formatter("\x1b[36m", "\x1b[39m") : String, white: enabled ? formatter("\x1b[37m", "\x1b[39m") : String, gray: enabled ? formatter("\x1b[90m", "\x1b[39m") : String, bgBlack: enabled ? formatter("\x1b[40m", "\x1b[49m") : String, bgRed: enabled ? formatter("\x1b[41m", "\x1b[49m") : String, bgGreen: enabled ? formatter("\x1b[42m", "\x1b[49m") : String, bgYellow: enabled ? formatter("\x1b[43m", "\x1b[49m") : String, bgBlue: enabled ? formatter("\x1b[44m", "\x1b[49m") : String, bgMagenta: enabled ? formatter("\x1b[45m", "\x1b[49m") : String, bgCyan: enabled ? formatter("\x1b[46m", "\x1b[49m") : String, bgWhite: enabled ? formatter("\x1b[47m", "\x1b[49m") : String, }) module.exports = createColors() module.exports.createColors = createColors picocolors-1.0.0/tests/000077500000000000000000000000001413144422400150255ustar00rootroot00000000000000picocolors-1.0.0/tests/environments.js000066400000000000000000000040151413144422400201120ustar00rootroot00000000000000let vm = require("vm") let fs = require("fs") let pc = require("../picocolors.js") let assert = require("assert") let source = fs.readFileSync(__dirname + "/../picocolors.js", "utf-8") test("ci server", () => { let pc = initModuleEnv({ env: { TERM: "dumb", CI: "1" } }) assert.equal(pc.isColorSupported, true) assert.equal(pc.red("text"), pc.createColors(true).red("text")) }) test("arg --color", () => { let pc = initModuleEnv({ env: { TERM: "dumb" }, argv: ["--color"] }) assert.equal(pc.isColorSupported, true) assert.equal(pc.red("text"), pc.createColors(true).red("text")) }) test("env NO_COLOR", () => { let pc = initModuleEnv({ env: { FORCE_COLOR: "1", NO_COLOR: "1" } }) assert.equal(pc.isColorSupported, false) assert.equal(pc.red("text"), pc.createColors(false).red("text")) }) test("env FORCE_COLOR", () => { let pc = initModuleEnv({ env: { TERM: "dumb", FORCE_COLOR: "1" } }) assert.equal(pc.isColorSupported, true) assert.equal(pc.red("text"), pc.createColors(true).red("text")) }) test("arg --no-color", () => { let pc = initModuleEnv({ env: { FORCE_COLOR: "1" }, argv: ["--no-color"] }) assert.equal(pc.isColorSupported, false) assert.equal(pc.red("text"), pc.createColors(false).red("text")) }) test("no term", () => { let pc = initModuleEnv({ env: { TERM: "dumb" } }) assert.equal(pc.isColorSupported, false) assert.equal(pc.red("text"), pc.createColors(false).red("text")) }) test("windows", () => { let pc = initModuleEnv({ env: { TERM: "dumb" }, platform: "win32" }) assert.equal(pc.isColorSupported, true) assert.equal(pc.red("text"), pc.createColors(true).red("text")) }) function test(name, fn) { try { fn() console.log(pc.green("✓ " + name)) } catch (error) { console.log(pc.red("✗ " + name)) throw error } } function initModuleEnv({ env, argv = [], platform = "darwin" }) { let process = { env, argv, platform } let context = vm.createContext({ require, process, module: { exports: {} } }) let script = new vm.Script(source) script.runInContext(context) return context.module.exports } picocolors-1.0.0/tests/test.js000066400000000000000000000065741413144422400163560ustar00rootroot00000000000000let pc = require("../picocolors.js") let assert = require("assert") const FMT = { reset: ["\x1b[0m", "\x1b[0m"], bold: ["\x1b[1m", "\x1b[22m"], dim: ["\x1b[2m", "\x1b[22m"], italic: ["\x1b[3m", "\x1b[23m"], underline: ["\x1b[4m", "\x1b[24m"], inverse: ["\x1b[7m", "\x1b[27m"], hidden: ["\x1b[8m", "\x1b[28m"], strikethrough: ["\x1b[9m", "\x1b[29m"], black: ["\x1b[30m", "\x1b[39m"], red: ["\x1b[31m", "\x1b[39m"], green: ["\x1b[32m", "\x1b[39m"], yellow: ["\x1b[33m", "\x1b[39m"], blue: ["\x1b[34m", "\x1b[39m"], magenta: ["\x1b[35m", "\x1b[39m"], cyan: ["\x1b[36m", "\x1b[39m"], white: ["\x1b[37m", "\x1b[39m"], gray: ["\x1b[90m", "\x1b[39m"], bgBlack: ["\x1b[40m", "\x1b[49m"], bgRed: ["\x1b[41m", "\x1b[49m"], bgGreen: ["\x1b[42m", "\x1b[49m"], bgYellow: ["\x1b[43m", "\x1b[49m"], bgBlue: ["\x1b[44m", "\x1b[49m"], bgMagenta: ["\x1b[45m", "\x1b[49m"], bgCyan: ["\x1b[46m", "\x1b[49m"], bgWhite: ["\x1b[47m", "\x1b[49m"], } test("color matching", () => { for (let format in FMT) { assert.equal(pc[format]("string"), FMT[format][0] + "string" + FMT[format][1]) console.log(pc[format]("testing: " + format)) } }) test("format/color nesting", () => { assert.equal( pc.bold(`BOLD ${pc.red(`RED ${pc.dim("DIM")} RED`)} BOLD`), FMT.bold[0] + "BOLD " + FMT.red[0] + "RED " + FMT.dim[0] + "DIM" + FMT.dim[1] + FMT.bold[0] + " RED" + FMT.red[1] + " BOLD" + FMT.bold[1] ) }) test("proper wrapping", () => { assert.equal( pc.red(pc.bold("==TEST==")), FMT.red[0] + FMT.bold[0] + "==TEST==" + FMT.bold[1] + FMT.red[1] ) }) test("complex case of wrapping", () => { assert.equal( pc.bold(pc.yellow(pc.bgRed(pc.italic("==TEST==")))), FMT.bold[0] + FMT.yellow[0] + FMT.bgRed[0] + FMT.italic[0] + "==TEST==" + FMT.italic[1] + FMT.bgRed[1] + FMT.yellow[1] + FMT.bold[1] ) assert.equal( pc.cyan(pc.bold(pc.underline("==TEST=="))), FMT.cyan[0] + FMT.bold[0] + FMT.underline[0] + "==TEST==" + FMT.underline[1] + FMT.bold[1] + FMT.cyan[1] ) }) test("close sequence replacement", () => { assert.equal( pc.red(`foo ${pc.yellow("bar")} baz`), FMT.red[0] + "foo " + FMT.yellow[0] + "bar" + FMT.red[0] + " baz" + FMT.red[1] ) assert.equal( pc.bold(`foo ${pc.red(pc.dim("bar"))} baz`), FMT.bold[0] + "foo " + FMT.red[0] + FMT.dim[0] + "bar" + FMT.dim[1] + FMT.bold[0] + FMT.red[1] + " baz" + FMT.bold[1] ) assert.equal( pc.yellow(`foo ${pc.red(pc.bold("red"))} bar ${pc.cyan("cyan")} baz`), FMT.yellow[0] + "foo " + FMT.red[0] + FMT.bold[0] + "red" + FMT.bold[1] + FMT.yellow[0] + " bar " + FMT.cyan[0] + "cyan" + FMT.yellow[0] + " baz" + FMT.yellow[1] ) }) test("non-string input", () => { assert.equal(pc.red(), FMT.red[0] + "undefined" + FMT.red[1]) assert.equal(pc.red(undefined), FMT.red[0] + "undefined" + FMT.red[1]) assert.equal(pc.red(0), FMT.red[0] + "0" + FMT.red[1]) assert.equal(pc.red(NaN), FMT.red[0] + "NaN" + FMT.red[1]) assert.equal(pc.red(null), FMT.red[0] + "null" + FMT.red[1]) assert.equal(pc.red(true), FMT.red[0] + "true" + FMT.red[1]) assert.equal(pc.red(false), FMT.red[0] + "false" + FMT.red[1]) assert.equal(pc.red(Infinity), FMT.red[0] + "Infinity" + FMT.red[1]) }) function test(name, fn) { try { fn() console.log(pc.green("✓ " + name)) } catch (error) { console.log(pc.red("✗ " + name)) throw error } } picocolors-1.0.0/types.ts000066400000000000000000000011421413144422400153750ustar00rootroot00000000000000export type Formatter = (input: string | number | null | undefined) => string export interface Colors { isColorSupported: boolean reset: Formatter bold: Formatter dim: Formatter italic: Formatter underline: Formatter inverse: Formatter hidden: Formatter strikethrough: Formatter black: Formatter red: Formatter green: Formatter yellow: Formatter blue: Formatter magenta: Formatter cyan: Formatter white: Formatter gray: Formatter bgBlack: Formatter bgRed: Formatter bgGreen: Formatter bgYellow: Formatter bgBlue: Formatter bgMagenta: Formatter bgCyan: Formatter bgWhite: Formatter }