ansi-colors
Advanced tools
Comparing version 2.0.2 to 2.0.3
63
index.js
'use strict'; | ||
const util = require('util'); | ||
const { format } = require('util'); | ||
const ansiRegex = /\u001b\[[0-9]+m/ig; | ||
@@ -80,36 +80,25 @@ const colors = { enabled: true, visible: true, ansiRegex }; | ||
const isString = val => val && typeof val === 'string'; | ||
const unstyle = val => isString(val) ? val.replace(ansiRegex, '') : val; | ||
const hasOpen = (input, open) => input.slice(0, open.length) === open; | ||
const hasClose = (input, close) => input.slice(-close.length) === close; | ||
const color = (str, style, hasNewline) => { | ||
const { open, close } = style; | ||
if (!(hasOpen(str, open) && hasClose(str, close))) { | ||
str = style.open + str.replace(style.closeRe, style.open) + style.close; | ||
function wrap() { | ||
if (!colors.visible) return ''; | ||
let out = format.apply(null, arguments); | ||
if (!colors.enabled || out.trim().length === 0) return out; | ||
let tmp, isMulti = out.includes('\n'); | ||
for (let i = 0; i < this.stack.length; i++) { | ||
tmp = styles[this.stack[i]]; // { x1, x2, rgx } | ||
out = tmp.open + out.replace(tmp.closeRe, tmp.open) + tmp.close; | ||
if (isMulti) { | ||
out = out.replace(/(\r?\n)/g, `${tmp.close}$1${tmp.open}`); | ||
} | ||
} | ||
if (hasNewline) { | ||
return str.replace(/(\r?\n)/g, `${style.close}$1${style.open}`); | ||
} | ||
return str; | ||
}; | ||
function wrap(...args) { | ||
if (colors.visible === false) return ''; | ||
let str = util.format(...args); | ||
if (colors.enabled === false) return str; | ||
if (str.trim() === '') return str; | ||
const newline = str.includes('\n'); | ||
for (const key of this.stack) { | ||
str = color(str, styles[key], newline); | ||
} | ||
return str; | ||
return out; | ||
} | ||
function style(stack) { | ||
const create = (...args) => wrap.call(create, ...args); | ||
create.stack = stack; | ||
Reflect.setPrototypeOf(create, colors); | ||
return create; | ||
let ctx = {}; | ||
let fn = wrap.bind(ctx); | ||
ctx.stack = fn.stack = stack; | ||
Reflect.setPrototypeOf(fn, colors); | ||
return fn; | ||
} | ||
@@ -121,10 +110,13 @@ | ||
style.closeRe = new RegExp(`\\u001b\\[${style[1]}m`, 'g'); | ||
return style; | ||
} | ||
for (const key of Object.keys(styles)) { | ||
for (let key in styles) { | ||
decorate(styles[key]); | ||
Reflect.defineProperty(colors, key, { | ||
get() { | ||
return style(this.stack ? this.stack.concat(key) : [key]); | ||
if (this.stack === void 0) { | ||
return style([key]); | ||
} | ||
this.stack.push(key); | ||
return this; | ||
} | ||
@@ -134,3 +126,6 @@ }); | ||
colors.stripColor = colors.strip = colors.unstyle = unstyle; | ||
colors.stripColor = colors.strip = colors.unstyle = str => { | ||
return str && typeof str === 'string' ? str.replace(ansiRegex, '') : str; | ||
} | ||
colors.styles = styles; | ||
@@ -137,0 +132,0 @@ colors.symbols = symbols; |
{ | ||
"name": "ansi-colors", | ||
"description": "Collection of ansi colors and styles.", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"homepage": "https://github.com/doowb/ansi-colors", | ||
@@ -9,3 +9,6 @@ "author": "Brian Woodward (https://github.com/doowb)", | ||
"Brian Woodward (https://twitter.com/doowb)", | ||
"Jon Schlinkert (http://twitter.com/jonschlinkert)" | ||
"Jason Schilling (https://sourecode.de)", | ||
"Jon Schlinkert (http://twitter.com/jonschlinkert)", | ||
"Jordan (https://github.com/Silic0nS0ldier)", | ||
"Luke Edwards (https://lukeed.com)" | ||
], | ||
@@ -12,0 +15,0 @@ "repository": "doowb/ansi-colors", |
@@ -222,3 +222,3 @@ # ansi-colors [![NPM version](https://img.shields.io/npm/v/ansi-colors.svg?style=flat)](https://www.npmjs.com/package/ansi-colors) [![NPM monthly downloads](https://img.shields.io/npm/dm/ansi-colors.svg?style=flat)](https://npmjs.org/package/ansi-colors) [![NPM total downloads](https://img.shields.io/npm/dt/ansi-colors.svg?style=flat)](https://npmjs.org/package/ansi-colors) [![Linux Build Status](https://img.shields.io/travis/doowb/ansi-colors.svg?style=flat&label=Travis)](https://travis-ci.org/doowb/ansi-colors) [![Windows Build Status](https://img.shields.io/appveyor/ci/doowb/ansi-colors.svg?style=flat&label=AppVeyor)](https://ci.appveyor.com/project/doowb/ansi-colors) | ||
MacBook Pro, Intel Core i7, 2.5 GHz, 16 GB. | ||
MacBook Pro, Intel Core i7, 2.3 GHz, 16 GB. | ||
@@ -230,5 +230,5 @@ ### Load time | ||
``` | ||
ansi-colors: 2.057ms | ||
chalk: 9.063ms | ||
clorox: 1.701ms | ||
chalk: 11.795ms | ||
clorox: 1.019ms | ||
ansi-colors: 0.867ms | ||
``` | ||
@@ -240,15 +240,15 @@ | ||
# All Colors | ||
ansi-colors x 93,508 ops/sec ±1.19% (88 runs sampled) | ||
chalk x 8,871 ops/sec ±2.33% (81 runs sampled) | ||
clorox x 1,401 ops/sec ±2.62% (77 runs sampled) | ||
ansi-colors x 104,559 ops/sec ±0.80% (91 runs sampled) | ||
chalk x 8,869 ops/sec ±2.09% (82 runs sampled) | ||
clorox x 1,315 ops/sec ±2.19% (76 runs sampled) | ||
# Stacked colors | ||
ansi-colors x 14,413 ops/sec ±1.35% (88 runs sampled) | ||
chalk x 1,824 ops/sec ±2.46% (79 runs sampled) | ||
clorox x 563 ops/sec ±2.83% (75 runs sampled) | ||
ansi-colors x 36,208 ops/sec ±0.65% (88 runs sampled) | ||
chalk x 1,789 ops/sec ±1.77% (80 runs sampled) | ||
clorox x 528 ops/sec ±2.00% (78 runs sampled) | ||
# Nested colors | ||
ansi-colors x 37,897 ops/sec ±1.02% (92 runs sampled) | ||
chalk x 4,196 ops/sec ±1.88% (81 runs sampled) | ||
clorox x 676 ops/sec ±2.70% (69 runs sampled) | ||
ansi-colors x 42,828 ops/sec ±0.36% (90 runs sampled) | ||
chalk x 4,082 ops/sec ±1.93% (83 runs sampled) | ||
clorox x 628 ops/sec ±2.10% (60 runs sampled) | ||
``` | ||
@@ -258,3 +258,3 @@ | ||
| **Feature** | **ansi-colors** | **chalk** | **clorox** | **colors** | | ||
| **Feature** | **ansi-colors** | **chalk** | **clorox** | **colors** | | ||
| --- | --- | --- | --- | --- | | ||
@@ -269,23 +269,23 @@ | Nested colors | yes | yes | no | yes | | ||
<details> | ||
<summary><strong>Contributing</strong></summary> | ||
### Related projects | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). | ||
* [ansi-wrap](https://www.npmjs.com/package/ansi-wrap): Create ansi colors by passing the open and close codes. | [homepage](https://github.com/jonschlinkert/ansi-wrap "Create ansi colors by passing the open and close codes.") | ||
* [strip-color](https://www.npmjs.com/package/strip-color): Strip ANSI color codes from a string. No dependencies. | [homepage](https://github.com/jonschlinkert/strip-color "Strip ANSI color codes from a string. No dependencies.") | ||
</details> | ||
### Contributing | ||
<details> | ||
<summary><strong>Running Tests</strong></summary> | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). | ||
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: | ||
### Contributors | ||
```sh | ||
$ npm install && npm test | ||
``` | ||
| **Commits** | **Contributor** | | ||
| --- | --- | | ||
| 21 | [doowb](https://github.com/doowb) | | ||
| 10 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 6 | [lukeed](https://github.com/lukeed) | | ||
| 2 | [Silic0nS0ldier](https://github.com/Silic0nS0ldier) | | ||
| 1 | [chapterjason](https://github.com/chapterjason) | | ||
</details> | ||
### Building docs | ||
<details> | ||
<summary><strong>Building docs</strong></summary> | ||
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ | ||
@@ -299,19 +299,10 @@ | ||
</details> | ||
### Running tests | ||
### Related projects | ||
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: | ||
You might also be interested in these projects: | ||
```sh | ||
$ npm install && npm test | ||
``` | ||
* [ansi-wrap](https://www.npmjs.com/package/ansi-wrap): Create ansi colors by passing the open and close codes. | [homepage](https://github.com/jonschlinkert/ansi-wrap "Create ansi colors by passing the open and close codes.") | ||
* [strip-color](https://www.npmjs.com/package/strip-color): Strip ANSI color codes from a string. No dependencies. | [homepage](https://github.com/jonschlinkert/strip-color "Strip ANSI color codes from a string. No dependencies.") | ||
### Contributors | ||
| **Commits** | **Contributor** | | ||
| --- | --- | | ||
| 18 | [doowb](https://github.com/doowb) | | ||
| 7 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 2 | [Silic0nS0ldier](https://github.com/Silic0nS0ldier) | | ||
### Author | ||
@@ -321,5 +312,4 @@ | ||
* [LinkedIn Profile](https://linkedin.com/in/woodwardbrian) | ||
* [GitHub Profile](https://github.com/doowb) | ||
* [Twitter Profile](https://twitter.com/doowb) | ||
* [github/doowb](https://github.com/doowb) | ||
* [twitter/doowb](https://twitter.com/doowb) | ||
@@ -333,2 +323,2 @@ ### License | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on May 04, 2018._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.7.0, on July 16, 2018._ |
Sorry, the diff of this file is not supported yet
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
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
18317
260
316