ansi-styles
Advanced tools
Comparing version 4.0.0 to 4.1.0
96
index.js
'use strict'; | ||
const colorConvert = require('color-convert'); | ||
@@ -19,2 +18,44 @@ const wrapAnsi16 = (fn, offset) => (...args) => { | ||
const ansi2ansi = n => n; | ||
const rgb2rgb = (r, g, b) => [r, g, b]; | ||
const setLazyProperty = (object, property, get) => { | ||
Object.defineProperty(object, property, { | ||
get: () => { | ||
const value = get(); | ||
Object.defineProperty(object, property, { | ||
value, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
return value; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
}; | ||
let colorConvert; | ||
const makeDynamicStyles = (wrap, targetSpace, identity, isBackground) => { | ||
if (colorConvert === undefined) { | ||
colorConvert = require('color-convert'); | ||
} | ||
const offset = isBackground ? 10 : 0; | ||
const styles = {}; | ||
for (const [sourceSpace, suite] of Object.entries(colorConvert)) { | ||
const name = sourceSpace === 'ansi16' ? 'ansi' : sourceSpace; | ||
if (sourceSpace === targetSpace) { | ||
styles[name] = wrap(identity, offset); | ||
} else if (typeof suite === 'object') { | ||
styles[name] = wrap(suite[targetSpace], offset); | ||
} | ||
} | ||
return styles; | ||
}; | ||
function assembleStyles() { | ||
@@ -105,53 +146,12 @@ const codes = new Map(); | ||
const ansi2ansi = n => n; | ||
const rgb2rgb = (r, g, b) => [r, g, b]; | ||
styles.color.close = '\u001B[39m'; | ||
styles.bgColor.close = '\u001B[49m'; | ||
styles.color.ansi = { | ||
ansi: wrapAnsi16(ansi2ansi, 0) | ||
}; | ||
styles.color.ansi256 = { | ||
ansi256: wrapAnsi256(ansi2ansi, 0) | ||
}; | ||
styles.color.ansi16m = { | ||
rgb: wrapAnsi16m(rgb2rgb, 0) | ||
}; | ||
setLazyProperty(styles.color, 'ansi', () => makeDynamicStyles(wrapAnsi16, 'ansi16', ansi2ansi, false)); | ||
setLazyProperty(styles.color, 'ansi256', () => makeDynamicStyles(wrapAnsi256, 'ansi256', ansi2ansi, false)); | ||
setLazyProperty(styles.color, 'ansi16m', () => makeDynamicStyles(wrapAnsi16m, 'rgb', rgb2rgb, false)); | ||
setLazyProperty(styles.bgColor, 'ansi', () => makeDynamicStyles(wrapAnsi16, 'ansi16', ansi2ansi, true)); | ||
setLazyProperty(styles.bgColor, 'ansi256', () => makeDynamicStyles(wrapAnsi256, 'ansi256', ansi2ansi, true)); | ||
setLazyProperty(styles.bgColor, 'ansi16m', () => makeDynamicStyles(wrapAnsi16m, 'rgb', rgb2rgb, true)); | ||
styles.bgColor.ansi = { | ||
ansi: wrapAnsi16(ansi2ansi, 10) | ||
}; | ||
styles.bgColor.ansi256 = { | ||
ansi256: wrapAnsi256(ansi2ansi, 10) | ||
}; | ||
styles.bgColor.ansi16m = { | ||
rgb: wrapAnsi16m(rgb2rgb, 10) | ||
}; | ||
for (let [key, suite] of Object.entries(colorConvert)) { | ||
if (typeof suite !== 'object') { | ||
continue; | ||
} | ||
if (key === 'ansi16') { | ||
key = 'ansi'; | ||
} | ||
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; | ||
@@ -158,0 +158,0 @@ } |
{ | ||
"name": "ansi-styles", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "ANSI escape codes for styling strings in the terminal", | ||
@@ -45,6 +45,6 @@ "license": "MIT", | ||
"dependencies": { | ||
"color-convert": "^2.0.0" | ||
"color-convert": "^2.0.1" | ||
}, | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"ava": "^2.3.0", | ||
"svg-term-cli": "^2.1.1", | ||
@@ -51,0 +51,0 @@ "xo": "^0.24.0" |
# ansi-styles [![Build Status](https://travis-ci.org/chalk/ansi-styles.svg?branch=master)](https://travis-ci.org/chalk/ansi-styles) | ||
> [ANSI escape codes](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles) for styling strings in the terminal | ||
> [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles) for styling strings in the terminal | ||
@@ -32,3 +32,3 @@ You probably want the higher-level [chalk](https://github.com/chalk/chalk) module for styling your strings. | ||
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); | ||
console.log(style.color.ansi16m.hex('#abcdef') + 'Hello world!' + style.color.close); | ||
``` | ||
@@ -35,0 +35,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
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
10214
138
Updatedcolor-convert@^2.0.1