ansi-styles
Advanced tools
Comparing version 2.2.1 to 3.0.0
83
index.js
'use strict'; | ||
const colorConvert = require('color-convert'); | ||
function assembleStyles () { | ||
var styles = { | ||
modifiers: { | ||
const wrapAnsi16 = (fn, offset) => function () { | ||
const code = fn.apply(colorConvert, arguments); | ||
return `\u001B[${code + offset}m`; | ||
}; | ||
const wrapAnsi256 = (fn, offset) => function () { | ||
const code = fn.apply(colorConvert, arguments); | ||
return `\u001B[${38 + offset};5;${code}m`; | ||
}; | ||
const wrapAnsi16m = (fn, offset) => function () { | ||
const rgb = fn.apply(colorConvert, arguments); | ||
return `\u001B[${38 + offset};2;${rgb[0]};${rgb[1]};${rgb[2]}m`; | ||
}; | ||
function assembleStyles() { | ||
const styles = { | ||
modifier: { | ||
reset: [0, 0], | ||
bold: [1, 22], // 21 isn't widely supported and 22 does the same thing | ||
// 21 isn't widely supported and 22 does the same thing | ||
bold: [1, 22], | ||
dim: [2, 22], | ||
@@ -15,3 +32,3 @@ italic: [3, 23], | ||
}, | ||
colors: { | ||
color: { | ||
black: [30, 39], | ||
@@ -27,3 +44,3 @@ red: [31, 39], | ||
}, | ||
bgColors: { | ||
bgColor: { | ||
bgBlack: [40, 49], | ||
@@ -41,13 +58,13 @@ bgRed: [41, 49], | ||
// fix humans | ||
styles.colors.grey = styles.colors.gray; | ||
styles.color.grey = styles.color.gray; | ||
Object.keys(styles).forEach(function (groupName) { | ||
var group = styles[groupName]; | ||
Object.keys(styles).forEach(groupName => { | ||
const group = styles[groupName]; | ||
Object.keys(group).forEach(function (styleName) { | ||
var style = group[styleName]; | ||
Object.keys(group).forEach(styleName => { | ||
const style = group[styleName]; | ||
styles[styleName] = group[styleName] = { | ||
open: '\u001b[' + style[0] + 'm', | ||
close: '\u001b[' + style[1] + 'm' | ||
open: `\u001B[${style[0]}m`, | ||
close: `\u001B[${style[1]}m` | ||
}; | ||
@@ -62,2 +79,42 @@ }); | ||
const rgb2rgb = (r, g, b) => [r, g, b]; | ||
styles.color.close = '\u001B[39m'; | ||
styles.bgColor.close = '\u001B[49m'; | ||
styles.color.ansi = {}; | ||
styles.color.ansi256 = {}; | ||
styles.color.ansi16m = { | ||
rgb: wrapAnsi16m(rgb2rgb, 0) | ||
}; | ||
styles.bgColor.ansi = {}; | ||
styles.bgColor.ansi256 = {}; | ||
styles.bgColor.ansi16m = { | ||
rgb: wrapAnsi16m(rgb2rgb, 10) | ||
}; | ||
for (const key of Object.keys(colorConvert)) { | ||
if (typeof colorConvert[key] !== 'object') { | ||
continue; | ||
} | ||
const suite = colorConvert[key]; | ||
if ('ansi16' in suite) { | ||
styles.color.ansi[key] = wrapAnsi16(suite.ansi16, 0); | ||
styles.bgColor.ansi[key] = wrapAnsi16(suite.ansi16, 10); | ||
} | ||
if ('ansi256' in suite) { | ||
styles.color.ansi256[key] = wrapAnsi256(suite.ansi256, 0); | ||
styles.bgColor.ansi256[key] = wrapAnsi256(suite.ansi256, 10); | ||
} | ||
if ('rgb' in suite) { | ||
styles.color.ansi16m[key] = wrapAnsi16m(suite.rgb, 0); | ||
styles.bgColor.ansi16m[key] = wrapAnsi16m(suite.rgb, 10); | ||
} | ||
} | ||
return styles; | ||
@@ -64,0 +121,0 @@ } |
{ | ||
"name": "ansi-styles", | ||
"version": "2.2.1", | ||
"version": "3.0.0", | ||
"description": "ANSI escape codes for styling strings in the terminal", | ||
@@ -14,9 +14,10 @@ "license": "MIT", | ||
"Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)", | ||
"Joshua Appelman <jappelman@xebia.com> (jbnicolai.com)" | ||
"Joshua Boy Nicolai Appelman <joshua@jbna.nl> (jbna.nl)", | ||
"Josh Junon <i.am.qix@gmail.com> (github.com/qix-)" | ||
], | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "xo && ava" | ||
}, | ||
@@ -48,5 +49,12 @@ "files": [ | ||
], | ||
"dependencies": { | ||
"color-convert": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*" | ||
"ava": "*", | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
"esnext": true | ||
} | ||
} |
@@ -20,8 +20,17 @@ # ansi-styles [![Build Status](https://travis-ci.org/chalk/ansi-styles.svg?branch=master)](https://travis-ci.org/chalk/ansi-styles) | ||
```js | ||
var ansi = require('ansi-styles'); | ||
const style = require('ansi-styles'); | ||
console.log(ansi.green.open + 'Hello world!' + ansi.green.close); | ||
console.log(`${style.green.open}Hello world!${style.green.close}`); | ||
// color conversion between 16/256/truecolor | ||
// NOTE: if conversion goes to 16 colors or 256 colors, the original color | ||
// may be degraded to fit that color palette. This means terminals | ||
// that do not support 16 million colors will best-match the | ||
// original color. | ||
console.log(style.bgColor.ansi.hsl(120, 80, 72) + 'Hello world!' + style.bgColor.close); | ||
console.log(style.color.ansi256.rgb(199, 20, 250) + 'Hello world!' + style.color.close); | ||
console.log(style.color.ansi16m.hex('#ABCDEF') + 'Hello world!' + style.color.close); | ||
``` | ||
## API | ||
@@ -71,7 +80,7 @@ | ||
By default you get a map of styles, but the styles are also available as groups. They are non-enumerable so they don't show up unless you access them explicitly. This makes it easier to expose only a subset in a higher-level module. | ||
By default, you get a map of styles, but the styles are also available as groups. They are non-enumerable so they don't show up unless you access them explicitly. This makes it easier to expose only a subset in a higher-level module. | ||
- `ansi.modifiers` | ||
- `ansi.colors` | ||
- `ansi.bgColors` | ||
- `style.modifier` | ||
- `style.color` | ||
- `style.bgColor` | ||
@@ -82,8 +91,31 @@ | ||
```js | ||
console.log(ansi.colors.green.open); | ||
console.log(style.color.green.open); | ||
``` | ||
## [256 / 16 million (TrueColor) support](https://gist.github.com/XVilka/8346728) | ||
`ansi-styles` uses the [`color-convert`](https://github.com/Qix-/color-convert) package to allow for converting between various colors and ANSI escapes, with support for 256 and 16 million colors. | ||
To use these, call the associated conversion function with the intended output, for example: | ||
```js | ||
style.color.ansi.rgb(100, 200, 15); // RGB to 16 color ansi foreground code | ||
style.bgColor.ansi.rgb(100, 200, 15); // RGB to 16 color ansi background code | ||
style.color.ansi256.hsl(120, 100, 60); // HSL to 256 color ansi foreground code | ||
style.bgColor.ansi256.hsl(120, 100, 60); // HSL to 256 color ansi foreground code | ||
style.color.ansi16m.hex('#C0FFEE'); // Hex (RGB) to 16 million color foreground code | ||
style.bgColor.ansi16m.hex('#C0FFEE'); // Hex (RGB) to 16 million color background code | ||
``` | ||
## Related | ||
- [ansi-escapes](https://github.com/sindresorhus/ansi-escapes) - ANSI escape codes for manipulating the terminal | ||
## License | ||
MIT © [Sindre Sorhus](http://sindresorhus.com) | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
7881
102
119
1
2
1
+ Addedcolor-convert@^1.0.0
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)