[Pkg-javascript-commits] [node-ansi-styles] 01/05: New upstream version 3.0.0

Mathias Behrle mbehrle at moszumanska.debian.org
Thu Aug 17 13:46:30 UTC 2017


This is an automated email from the git hooks/post-receive script.

mbehrle pushed a commit to branch master
in repository node-ansi-styles.

commit 8747eab7af5722dd53668ca30b30d0b39708341c
Author: Mathias Behrle <mathiasb at m9s.biz>
Date:   Tue Jan 24 17:18:39 2017 +0100

    New upstream version 3.0.0
---
 .editorconfig  |  3 --
 .gitattributes |  1 +
 .jshintrc      | 12 --------
 .travis.yml    |  5 ++--
 index.js       | 83 +++++++++++++++++++++++++++++++++++++++++++--------
 package.json   | 18 +++++++----
 readme.md      | 50 +++++++++++++++++++++++++------
 test.js        | 94 ++++++++++++++++++++++++++++++++++++++++++----------------
 8 files changed, 195 insertions(+), 71 deletions(-)

diff --git a/.editorconfig b/.editorconfig
index 8f9d77e..98a761d 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -10,6 +10,3 @@ insert_final_newline = true
 [{package.json,*.yml}]
 indent_style = space
 indent_size = 2
-
-[*.md]
-trim_trailing_whitespace = false
diff --git a/.gitattributes b/.gitattributes
index 176a458..391f0a4 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -1 +1,2 @@
 * text=auto
+*.js text eol=lf
diff --git a/.jshintrc b/.jshintrc
deleted file mode 100644
index 9fe1be2..0000000
--- a/.jshintrc
+++ /dev/null
@@ -1,12 +0,0 @@
-{
-	"node": true,
-	"esnext": true,
-	"bitwise": true,
-	"curly": true,
-	"immed": true,
-	"newcap": true,
-	"noarg": true,
-	"undef": true,
-	"unused": "vars",
-	"strict": true
-}
diff --git a/.travis.yml b/.travis.yml
index dedfc07..b18bae5 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,6 +1,5 @@
 sudo: false
 language: node_js
 node_js:
-  - 'iojs'
-  - '0.12'
-  - '0.10'
+  - '6'
+  - '4'
diff --git a/index.js b/index.js
index 7894527..a2a7434 100644
--- a/index.js
+++ b/index.js
@@ -1,10 +1,27 @@
 '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],
 			italic: [3, 23],
 			underline: [4, 24],
@@ -12,7 +29,7 @@ function assembleStyles () {
 			hidden: [8, 28],
 			strikethrough: [9, 29]
 		},
-		colors: {
+		color: {
 			black: [30, 39],
 			red: [31, 39],
 			green: [32, 39],
@@ -23,7 +40,7 @@ function assembleStyles () {
 			white: [37, 39],
 			gray: [90, 39]
 		},
-		bgColors: {
+		bgColor: {
 			bgBlack: [40, 49],
 			bgRed: [41, 49],
 			bgGreen: [42, 49],
@@ -36,17 +53,17 @@ function assembleStyles () {
 	};
 
 	// 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`
 			};
 		});
 
@@ -56,6 +73,46 @@ function assembleStyles () {
 		});
 	});
 
+	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;
 }
 
diff --git a/package.json b/package.json
index 78c535f..6f845d8 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "ansi-styles",
-  "version": "2.2.1",
+  "version": "3.0.0",
   "description": "ANSI escape codes for styling strings in the terminal",
   "license": "MIT",
   "repository": "chalk/ansi-styles",
@@ -11,13 +11,14 @@
   },
   "maintainers": [
     "Sindre Sorhus <sindresorhus at gmail.com> (sindresorhus.com)",
-    "Joshua Appelman <jappelman at xebia.com> (jbnicolai.com)"
+    "Joshua Boy Nicolai Appelman <joshua at jbna.nl> (jbna.nl)",
+    "Josh Junon <i.am.qix at gmail.com> (github.com/qix-)"
   ],
   "engines": {
-    "node": ">=0.10.0"
+    "node": ">=4"
   },
   "scripts": {
-    "test": "mocha"
+    "test": "xo && ava"
   },
   "files": [
     "index.js"
@@ -44,7 +45,14 @@
     "command-line",
     "text"
   ],
+  "dependencies": {
+    "color-convert": "^1.0.0"
+  },
   "devDependencies": {
-    "mocha": "*"
+    "ava": "*",
+    "xo": "*"
+  },
+  "xo": {
+    "esnext": true
   }
 }
diff --git a/readme.md b/readme.md
index 3f933f6..c40bace 100644
--- a/readme.md
+++ b/readme.md
@@ -17,11 +17,20 @@ $ npm install --save ansi-styles
 ## Usage
 
 ```js
-var ansi = require('ansi-styles');
+const style = require('ansi-styles');
+
+console.log(`${style.green.open}Hello world!${style.green.close}`);
 
-console.log(ansi.green.open + 'Hello world!' + ansi.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
 
@@ -67,20 +76,43 @@ Each style has an `open` and `close` property.
 
 ## Advanced usage
 
-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`
 
 
 ###### Example
 
 ```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)
diff --git a/test.js b/test.js
index 5e17462..fbc1098 100644
--- a/test.js
+++ b/test.js
@@ -1,42 +1,84 @@
-'use strict';
-var assert = require('assert');
-var ansi = require('./');
+import test from 'ava';
+import style from './';
 
 // generates the screenshot
-Object.keys(ansi).forEach(function (el) {
-	var style = ansi[el].open;
+for (const [key, val] of Object.entries(style)) { // eslint-disable-line no-use-extend-native/no-use-extend-native
+	let code = val.open;
 
-	if (el === 'reset' || el === 'hidden') {
-		return;
+	if (key === 'reset' || key === 'hidden' || key === 'grey') {
+		continue;
 	}
 
-	if (/^bg[^B]/.test(el)) {
-		style = ansi.black.open + style;
+	if (/^bg[^B]/.test(key)) {
+		code = style.black.open + code;
 	}
 
-	process.stdout.write(style + el + ansi.reset.open + ansi.reset.close + ' ');
-});
+	process.stdout.write(code + key + style.reset.close + ' ');
+}
 
-it('should return ANSI escape codes', function () {
-	assert.equal(ansi.green.open, '\u001b[32m');
-	assert.equal(ansi.bgGreen.open, '\u001b[42m');
-	assert.equal(ansi.green.close, '\u001b[39m');
-	assert.equal(ansi.gray.open, ansi.grey.open);
+test('return ANSI escape codes', t => {
+	t.is(style.green.open, '\u001B[32m');
+	t.is(style.bgGreen.open, '\u001B[42m');
+	t.is(style.green.close, '\u001B[39m');
+	t.is(style.gray.open, style.grey.open);
 });
 
-it('should group related codes into categories', function () {
-	assert.equal(ansi.colors.magenta, ansi.magenta);
-	assert.equal(ansi.bgColors.bgYellow, ansi.bgYellow);
-	assert.equal(ansi.modifiers.bold, ansi.bold);
+test('group related codes into categories', t => {
+	t.is(style.color.magenta, style.magenta);
+	t.is(style.bgColor.bgYellow, style.bgYellow);
+	t.is(style.modifier.bold, style.bold);
 });
 
-it('groups should not be enumerable', function () {
-	assert.equal(Object.keys(ansi).indexOf('modifiers'), -1);
+test('groups should not be enumerable', t => {
+	t.true(Object.getOwnPropertyDescriptor(style, 'modifier') !== undefined);
+	t.true(Object.keys(style).indexOf('modifier') === -1);
 });
 
-it('should not pollute other objects', function () {
-	var obj1 = require('./');
-	var obj2 = require('./');
+test('don\'t pollute other objects', t => {
+	const obj1 = require('./');
+	const obj2 = require('./');
+
 	obj1.foo = true;
-	assert.notEqual(obj1.foo, obj2.foo);
+	t.not(obj1.foo, obj2.foo);
+});
+
+test('support conversion to ansi (16 colors)', t => {
+	t.is(style.color.ansi.rgb(255, 255, 255), '\u001B[97m');
+	t.is(style.color.ansi.hsl(140, 100, 50), '\u001B[92m');
+	t.is(style.color.ansi.hex('#990099'), '\u001B[35m');
+	t.is(style.color.ansi.hex('#FF00FF'), '\u001B[95m');
+
+	t.is(style.bgColor.ansi.rgb(255, 255, 255), '\u001B[107m');
+	t.is(style.bgColor.ansi.hsl(140, 100, 50), '\u001B[102m');
+	t.is(style.bgColor.ansi.hex('#990099'), '\u001B[45m');
+	t.is(style.bgColor.ansi.hex('#FF00FF'), '\u001B[105m');
+});
+
+test('support conversion to ansi (256 colors)', t => {
+	t.is(style.color.ansi256.rgb(255, 255, 255), '\u001B[38;5;231m');
+	t.is(style.color.ansi256.hsl(140, 100, 50), '\u001B[38;5;48m');
+	t.is(style.color.ansi256.hex('#990099'), '\u001B[38;5;127m');
+	t.is(style.color.ansi256.hex('#FF00FF'), '\u001B[38;5;201m');
+
+	t.is(style.bgColor.ansi256.rgb(255, 255, 255), '\u001B[48;5;231m');
+	t.is(style.bgColor.ansi256.hsl(140, 100, 50), '\u001B[48;5;48m');
+	t.is(style.bgColor.ansi256.hex('#990099'), '\u001B[48;5;127m');
+	t.is(style.bgColor.ansi256.hex('#FF00FF'), '\u001B[48;5;201m');
+});
+
+test('support conversion to ansi (16 million colors)', t => {
+	t.is(style.color.ansi16m.rgb(255, 255, 255), '\u001B[38;2;255;255;255m');
+	t.is(style.color.ansi16m.hsl(140, 100, 50), '\u001B[38;2;0;255;85m');
+	t.is(style.color.ansi16m.hex('#990099'), '\u001B[38;2;153;0;153m');
+	t.is(style.color.ansi16m.hex('#FF00FF'), '\u001B[38;2;255;0;255m');
+
+	t.is(style.bgColor.ansi16m.rgb(255, 255, 255), '\u001B[48;2;255;255;255m');
+	t.is(style.bgColor.ansi16m.hsl(140, 100, 50), '\u001B[48;2;0;255;85m');
+	t.is(style.bgColor.ansi16m.hex('#990099'), '\u001B[48;2;153;0;153m');
+	t.is(style.bgColor.ansi16m.hex('#FF00FF'), '\u001B[48;2;255;0;255m');
+});
+
+test('16/256/16m color close escapes', t => {
+	t.is(style.color.close, '\u001B[39m');
+	t.is(style.bgColor.close, '\u001B[49m');
 });

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-ansi-styles.git



More information about the Pkg-javascript-commits mailing list