Comparing version 2.0.2 to 3.0.0
151
index.js
@@ -1,76 +0,107 @@ | ||
const $ = { enabled:true }; | ||
const { FORCE_COLOR, NODE_DISABLE_COLORS, TERM } = process.env; | ||
const $ = { | ||
enabled: !NODE_DISABLE_COLORS && TERM !== 'dumb' && FORCE_COLOR !== '0' | ||
}; | ||
const CODES = { | ||
// modifiers | ||
reset: fmt(0, 0), | ||
bold: fmt(1, 22), | ||
dim: fmt(2, 22), | ||
italic: fmt(3, 23), | ||
underline: fmt(4, 24), | ||
inverse: fmt(7, 27), | ||
hidden: fmt(8, 28), | ||
strikethrough: fmt(9, 29), | ||
// colors | ||
black: fmt(30, 39), | ||
red: fmt(31, 39), | ||
green: fmt(32, 39), | ||
yellow: fmt(33, 39), | ||
blue: fmt(34, 39), | ||
magenta: fmt(35, 39), | ||
cyan: fmt(36, 39), | ||
white: fmt(37, 39), | ||
gray: fmt(90, 39), | ||
// background colors | ||
bgBlack: fmt(40, 49), | ||
bgRed: fmt(41, 49), | ||
bgGreen: fmt(42, 49), | ||
bgYellow: fmt(43, 49), | ||
bgBlue: fmt(44, 49), | ||
bgMagenta: fmt(45, 49), | ||
bgCyan: fmt(46, 49), | ||
bgWhite: fmt(47, 49) | ||
// modifiers | ||
reset: code(0, 0), | ||
bold: code(1, 22), | ||
dim: code(2, 22), | ||
italic: code(3, 23), | ||
underline: code(4, 24), | ||
inverse: code(7, 27), | ||
hidden: code(8, 28), | ||
strikethrough: code(9, 29), | ||
// colors | ||
black: code(30, 39), | ||
red: code(31, 39), | ||
green: code(32, 39), | ||
yellow: code(33, 39), | ||
blue: code(34, 39), | ||
magenta: code(35, 39), | ||
cyan: code(36, 39), | ||
white: code(37, 39), | ||
gray: code(90, 39), | ||
// background colors | ||
bgBlack: code(40, 49), | ||
bgRed: code(41, 49), | ||
bgGreen: code(42, 49), | ||
bgYellow: code(43, 49), | ||
bgBlue: code(44, 49), | ||
bgMagenta: code(45, 49), | ||
bgCyan: code(46, 49), | ||
bgWhite: code(47, 49) | ||
}; | ||
function fmt(x, y) { | ||
function code(open, close) { | ||
return { | ||
open: `\x1b[${x}m`, | ||
close: `\x1b[${y}m`, | ||
rgx: new RegExp(`\\x1b\\[${y}m`, 'g') | ||
} | ||
open: `\x1b[${open}m`, | ||
close: `\x1b[${close}m`, | ||
rgx: new RegExp(`\\x1b\\[${close}m`, 'g') | ||
}; | ||
} | ||
function run(key, str) { | ||
let tmp = CODES[key]; | ||
return tmp.open + str.replace(tmp.rgx, tmp.open) + tmp.close; | ||
} | ||
function exec(key, str) { | ||
str += ''; | ||
if (!$.enabled) return str; | ||
let arr = this.keys; | ||
while (arr.length > 0) { | ||
str = run(arr.shift(), str); | ||
function run(arr, str) { | ||
let i=0, tmp={}; | ||
for (; i < arr.length;) { | ||
tmp = Reflect.get(CODES, arr[i++]); | ||
str = tmp.open + str.replace(tmp.rgx, tmp.open) + tmp.close; | ||
} | ||
this.keys.push(key); | ||
return str; | ||
} | ||
function attach(key) { | ||
let ctx = { keys:[key] }; | ||
let fn = exec.bind(ctx, key); | ||
for (let k in CODES) { | ||
Reflect.defineProperty(fn, k, { | ||
get() { | ||
ctx.keys.push(k); | ||
return fn; | ||
} | ||
}); | ||
} | ||
return fn; | ||
function chain(keys) { | ||
let ctx = { keys }; | ||
ctx.reset = $.reset.bind(ctx); | ||
ctx.bold = $.bold.bind(ctx); | ||
ctx.dim = $.dim.bind(ctx); | ||
ctx.italic = $.italic.bind(ctx); | ||
ctx.underline = $.underline.bind(ctx); | ||
ctx.inverse = $.inverse.bind(ctx); | ||
ctx.hidden = $.hidden.bind(ctx); | ||
ctx.strikethrough = $.strikethrough.bind(ctx); | ||
ctx.black = $.black.bind(ctx); | ||
ctx.red = $.red.bind(ctx); | ||
ctx.green = $.green.bind(ctx); | ||
ctx.yellow = $.yellow.bind(ctx); | ||
ctx.blue = $.blue.bind(ctx); | ||
ctx.magenta = $.magenta.bind(ctx); | ||
ctx.cyan = $.cyan.bind(ctx); | ||
ctx.white = $.white.bind(ctx); | ||
ctx.gray = $.gray.bind(ctx); | ||
ctx.bgBlack = $.bgBlack.bind(ctx); | ||
ctx.bgRed = $.bgRed.bind(ctx); | ||
ctx.bgGreen = $.bgGreen.bind(ctx); | ||
ctx.bgYellow = $.bgYellow.bind(ctx); | ||
ctx.bgBlue = $.bgBlue.bind(ctx); | ||
ctx.bgMagenta = $.bgMagenta.bind(ctx); | ||
ctx.bgCyan = $.bgCyan.bind(ctx); | ||
ctx.bgWhite = $.bgWhite.bind(ctx); | ||
return ctx; | ||
} | ||
for (let k in CODES) { | ||
$[k] = attach(k); | ||
function init(key) { | ||
return function (txt) { | ||
let isChain = !!this.keys; | ||
if (isChain) this.keys.includes(key) || this.keys.push(key); | ||
if (txt !== void 0) return $.enabled ? run(isChain ? this.keys : [key], txt+'') : txt+''; | ||
return isChain ? this : chain([key]); | ||
}; | ||
} | ||
for (let key in CODES) { | ||
Object.defineProperty($, key, { | ||
value: init(key), | ||
writable: false | ||
}); | ||
} | ||
module.exports = $; |
@@ -1,56 +0,51 @@ | ||
// TypeScript definitions for kleur | ||
// Definitions by: Madhav Varshney <https://github.com/madhavarshney> | ||
// Originally written for ansi-colors | ||
// Originally by: Rogier Schouten <https://github.com/rogierschouten> | ||
// Adapted by: Madhav Varshney <https://github.com/madhavarshney> | ||
/** | ||
* Each method can be called as a function | ||
* or return a chainable Kleur object | ||
*/ | ||
interface KleurOrFunction extends KleurInstance { | ||
(string: string): string; | ||
// Returns Instance or String (when called with input) | ||
interface Color { | ||
(x: string | number): string; | ||
(): Kleur; | ||
} | ||
interface KleurInstance { | ||
/* Colors */ | ||
black: KleurOrFunction; | ||
red: KleurOrFunction; | ||
green: KleurOrFunction; | ||
yellow: KleurOrFunction; | ||
blue: KleurOrFunction; | ||
magenta: KleurOrFunction; | ||
cyan: KleurOrFunction; | ||
white: KleurOrFunction; | ||
gray: KleurOrFunction; | ||
interface Kleur { | ||
// Colors | ||
black: Color; | ||
red: Color; | ||
green: Color; | ||
yellow: Color; | ||
blue: Color; | ||
magenta: Color; | ||
cyan: Color; | ||
white: Color; | ||
gray: Color; | ||
/* Background Colors */ | ||
bgBlack: KleurOrFunction; | ||
bgRed: KleurOrFunction; | ||
bgGreen: KleurOrFunction; | ||
bgYellow: KleurOrFunction; | ||
bgBlue: KleurOrFunction; | ||
bgMagenta: KleurOrFunction; | ||
bgCyan: KleurOrFunction; | ||
bgWhite: KleurOrFunction; | ||
// Backgrounds | ||
bgBlack: Color; | ||
bgRed: Color; | ||
bgGreen: Color; | ||
bgYellow: Color; | ||
bgBlue: Color; | ||
bgMagenta: Color; | ||
bgCyan: Color; | ||
bgWhite: Color; | ||
/* Modifiers */ | ||
reset: KleurOrFunction; | ||
bold: KleurOrFunction; | ||
dim: KleurOrFunction; | ||
italic: KleurOrFunction; | ||
underline: KleurOrFunction; | ||
inverse: KleurOrFunction; | ||
hidden: KleurOrFunction; | ||
strikethrough: KleurOrFunction; | ||
// Modifiers | ||
reset: Color; | ||
bold: Color; | ||
dim: Color; | ||
italic: Color; | ||
underline: Color; | ||
inverse: Color; | ||
hidden: Color; | ||
strikethrough: Color; | ||
} | ||
declare let kleur: KleurInstance; | ||
declare let kleur: Kleur; | ||
export default kleur; | ||
/** | ||
* Enable ANSI Colors. | ||
* Note: You cannot write to ES6 exports and therefore to `kleur.enabled` | ||
* in TypeScript with `import * as kleur from 'kleur';`. | ||
* Workaround is to use `import kleur = require('kleur');`. | ||
* Note: ES6 imports are immutable | ||
* Therefore `kleur.enabled` can only be changed via | ||
* import kleur = require('kleur'); | ||
*/ | ||
export let enabled: boolean; |
{ | ||
"name": "kleur", | ||
"version": "2.0.2", | ||
"version": "3.0.0", | ||
"repository": "lukeed/kleur", | ||
@@ -5,0 +5,0 @@ "description": "The fastest Node.js library for formatting terminal text with ANSI colors~!", |
@@ -25,3 +25,3 @@ <div align="center"> | ||
* No dependencies | ||
* Super [lightweight](##load-time) & [performant](#performance) | ||
* Super [lightweight](#load-time) & [performant](#performance) | ||
* Supports [nested](#nested-methods) & [chained](#chained-methods) colors | ||
@@ -32,5 +32,9 @@ * No `String.prototype` modifications | ||
_Originally inspired by [`ansi-colors`](https://github.com/doowb/ansi-colors). See [Credits](#credits) for more info!_ | ||
--- | ||
As of `v3.0` the Chalk-style syntax (magical getter) is no longer used.<br>If you need or require that syntax, consider using [`ansi-colors`](https://github.com/doowb/ansi-colors), which maintains `chalk` parity. | ||
--- | ||
## Install | ||
@@ -46,12 +50,12 @@ | ||
```js | ||
const kleur = require('kleur'); | ||
const { red, white, blue, bold } = require('kleur'); | ||
// basic usage | ||
kleur.red('red text'); | ||
red('red text'); | ||
// chained methods | ||
kleur.blue.bold.underline('howdy partner'); | ||
blue().bold().underline('howdy partner'); | ||
// nested methods | ||
kleur.bold(`${ kleur.bgRed.white('[ERROR]') } ${ kleur.red.italic('Something happened')}`); | ||
bold(`${ white().bgRed('[ERROR]') } ${ red().italic('Something happened')}`); | ||
``` | ||
@@ -62,6 +66,6 @@ | ||
```js | ||
console.log(kleur.bold.red('this is a bold red message')); | ||
console.log(kleur.bold.italic('this is a bold italicized message')); | ||
console.log(kleur.bold.yellow.bgRed.italic('this is a bold yellow italicized message')); | ||
console.log(kleur.green.bold.underline('this is a bold green underlined message')); | ||
console.log(bold().red('this is a bold red message')); | ||
console.log(bold().italic('this is a bold italicized message')); | ||
console.log(bold().yellow().bgRed().italic('this is a bold yellow italicized message')); | ||
console.log(green().bold().underline('this is a bold green underlined message')); | ||
``` | ||
@@ -76,4 +80,4 @@ | ||
console.log(yellow(`foo ${red.bold('red')} bar ${cyan('cyan')} baz`)); | ||
console.log(yellow('foo ' + red.bold('red') + ' bar ' + cyan('cyan') + ' baz')); | ||
console.log(yellow(`foo ${red().bold('red')} bar ${cyan('cyan')} baz`)); | ||
console.log(yellow('foo ' + red().bold('red') + ' bar ' + cyan('cyan') + ' baz')); | ||
``` | ||
@@ -86,3 +90,3 @@ | ||
Toggle color support as needed; `kleur` assumes it's always enabled. | ||
Toggle color support as needed; `kleur` includes simple auto-detection which may not cover all cases. | ||
@@ -95,3 +99,3 @@ ```js | ||
// or use a library to detect support | ||
// or use another library to detect support | ||
kleur.enabled = require('color-support').level; | ||
@@ -105,4 +109,6 @@ | ||
Any `kleur` method returns a `String` (when invoked, not chained). It's up to the developer to pass the output to destinations like `console.log`, `process.stdout.write`, etc. | ||
Any `kleur` method returns a `String` when invoked with input; otherwise chaining is expected. | ||
> It's up to the developer to pass the output to destinations like `console.log`, `process.stdout.write`, etc. | ||
The methods below are grouped by type for legibility purposes only. They each can be [chained](#chained-methods) or [nested](#nested-methods) with one another. | ||
@@ -124,3 +130,3 @@ | ||
> Using Node v8.9.0 | ||
> Using Node v10.13.0 | ||
@@ -130,6 +136,5 @@ ### Load time | ||
``` | ||
chalk: 9.372ms | ||
turbocolor: 0.526ms | ||
ansi-colors: 0.851ms | ||
kleur: 0.862ms | ||
chalk: 9.397ms | ||
kleur: 0.525ms | ||
ansi-colors: 1.200ms | ||
``` | ||
@@ -141,18 +146,15 @@ | ||
# All Colors | ||
ansi-colors x 60,485 ops/sec ±0.63% (96 runs sampled) | ||
chalk x 7,184 ops/sec ±3.77% (68 runs sampled) | ||
turbocolor x 95,468 ops/sec ±0.60% (94 runs sampled)) | ||
kleur x 151,365 ops/sec ±0.22% (95 runs sampled) | ||
ansi-colors x 210,244 ops/sec ±0.36% (94 runs sampled) | ||
chalk x 11,999 ops/sec ±2.08% (84 runs sampled) | ||
kleur x 442,028 ops/sec ±0.26% (95 runs sampled) | ||
# Stacked colors | ||
ansi-colors x 13,754 ops/sec ±0.44% (93 runs sampled) | ||
chalk x 1,732 ops/sec ±3.76% (71 runs sampled) | ||
turbocolor x 28,709 ops/sec ±1.32% (92 runs sampled) | ||
kleur x 30,837 ops/sec ±0.13% (93 runs sampled) | ||
ansi-colors x 25,117 ops/sec ±0.33% (92 runs sampled) | ||
chalk x 2,602 ops/sec ±1.87% (84 runs sampled) | ||
kleur x 40,395 ops/sec ±0.41% (95 runs sampled) | ||
# Nested colors | ||
ansi-colors x 28,898 ops/sec ±0.32% (96 runs sampled) | ||
chalk x 3,389 ops/sec ±4.03% (71 runs sampled) | ||
turbocolor x 48,034 ops/sec ±1.47% (99 runs sampled) | ||
kleur x 61,266 ops/sec ±0.33% (97 runs sampled) | ||
ansi-colors x 77,423 ops/sec ±0.26% (95 runs sampled) | ||
chalk x 5,705 ops/sec ±1.48% (87 runs sampled) | ||
kleur x 114,039 ops/sec ±0.44% (95 runs sampled) | ||
``` | ||
@@ -163,14 +165,16 @@ | ||
This project was originally inspired by [Brian Woodward](https://github.com/doowb)'s awesome [`ansi-colors`](https://github.com/doowb/ansi-colors) project. | ||
This project originally forked [Brian Woodward](https://github.com/doowb)'s awesome [`ansi-colors`](https://github.com/doowb/ansi-colors) library. | ||
Unlike v1, the latest version(s) of `kleur` no longer supports: | ||
Beginning with `kleur@3.0`, the Chalk-style syntax (magical getter) has been replaced with function calls per key: | ||
* printf-formatting | ||
* variadic function arguments | ||
* multiline text via `\n` or `\r` | ||
* `kleur.clear()` method | ||
```js | ||
// Old: | ||
c.red.bold.underline('old'); | ||
In addition, `kleur` continues to be ship without symbols and bright color variants. | ||
// New: | ||
c.red().bold().underline('new'); | ||
``` | ||
> <sup><em>As I work more with Rust, the newer syntax feels so much better & more natural!</em></sup> | ||
If you need _any_ of these features, please use `ansi-colors` instead~! | ||
If you prefer the old syntax, you may migrate to `ansi-colors`... or suffer the deprecation notice on older `kleur` versions :sweat_smile: | ||
@@ -177,0 +181,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
9880
138
0
173
1