ansi-styles 2.0.1 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +133 -46
  2. package/package.json +17 -8
  3. package/readme.md +61 -12
package/index.js CHANGED
@@ -1,56 +1,143 @@
1
1
  'use strict';
2
+ const colorConvert = require('color-convert');
2
3
 
3
- var styles = module.exports = {
4
- modifiers: {
5
- reset: [0, 0],
6
- bold: [1, 22], // 21 isn't widely supported and 22 does the same thing
7
- dim: [2, 22],
8
- italic: [3, 23],
9
- underline: [4, 24],
10
- inverse: [7, 27],
11
- hidden: [8, 28],
12
- strikethrough: [9, 29]
13
- },
14
- colors: {
15
- black: [30, 39],
16
- red: [31, 39],
17
- green: [32, 39],
18
- yellow: [33, 39],
19
- blue: [34, 39],
20
- magenta: [35, 39],
21
- cyan: [36, 39],
22
- white: [37, 39],
23
- gray: [90, 39]
24
- },
25
- bgColors: {
26
- bgBlack: [40, 49],
27
- bgRed: [41, 49],
28
- bgGreen: [42, 49],
29
- bgYellow: [43, 49],
30
- bgBlue: [44, 49],
31
- bgMagenta: [45, 49],
32
- bgCyan: [46, 49],
33
- bgWhite: [47, 49]
34
- }
4
+ const wrapAnsi16 = (fn, offset) => function () {
5
+ const code = fn.apply(colorConvert, arguments);
6
+ return `\u001B[${code + offset}m`;
7
+ };
8
+
9
+ const wrapAnsi256 = (fn, offset) => function () {
10
+ const code = fn.apply(colorConvert, arguments);
11
+ return `\u001B[${38 + offset};5;${code}m`;
35
12
  };
36
13
 
37
- // fix humans
38
- styles.colors.grey = styles.colors.gray;
14
+ const wrapAnsi16m = (fn, offset) => function () {
15
+ const rgb = fn.apply(colorConvert, arguments);
16
+ return `\u001B[${38 + offset};2;${rgb[0]};${rgb[1]};${rgb[2]}m`;
17
+ };
39
18
 
40
- Object.keys(styles).forEach(function (groupName) {
41
- var group = styles[groupName];
19
+ function assembleStyles() {
20
+ const styles = {
21
+ modifier: {
22
+ reset: [0, 0],
23
+ // 21 isn't widely supported and 22 does the same thing
24
+ bold: [1, 22],
25
+ dim: [2, 22],
26
+ italic: [3, 23],
27
+ underline: [4, 24],
28
+ inverse: [7, 27],
29
+ hidden: [8, 28],
30
+ strikethrough: [9, 29]
31
+ },
32
+ color: {
33
+ black: [30, 39],
34
+ red: [31, 39],
35
+ green: [32, 39],
36
+ yellow: [33, 39],
37
+ blue: [34, 39],
38
+ magenta: [35, 39],
39
+ cyan: [36, 39],
40
+ white: [37, 39],
41
+ gray: [90, 39],
42
42
 
43
- Object.keys(group).forEach(function (styleName) {
44
- var style = group[styleName];
43
+ // Bright color
44
+ redBright: [91, 39],
45
+ greenBright: [92, 39],
46
+ yellowBright: [93, 39],
47
+ blueBright: [94, 39],
48
+ magentaBright: [95, 39],
49
+ cyanBright: [96, 39],
50
+ whiteBright: [97, 39]
51
+ },
52
+ bgColor: {
53
+ bgBlack: [40, 49],
54
+ bgRed: [41, 49],
55
+ bgGreen: [42, 49],
56
+ bgYellow: [43, 49],
57
+ bgBlue: [44, 49],
58
+ bgMagenta: [45, 49],
59
+ bgCyan: [46, 49],
60
+ bgWhite: [47, 49],
45
61
 
46
- styles[styleName] = group[styleName] = {
47
- open: '\u001b[' + style[0] + 'm',
48
- close: '\u001b[' + style[1] + 'm'
49
- };
50
- });
62
+ // Bright color
63
+ bgBlackBright: [100, 49],
64
+ bgRedBright: [101, 49],
65
+ bgGreenBright: [102, 49],
66
+ bgYellowBright: [103, 49],
67
+ bgBlueBright: [104, 49],
68
+ bgMagentaBright: [105, 49],
69
+ bgCyanBright: [106, 49],
70
+ bgWhiteBright: [107, 49]
71
+ }
72
+ };
73
+
74
+ // Fix humans
75
+ styles.color.grey = styles.color.gray;
51
76
 
52
- Object.defineProperty(styles, groupName, {
53
- value: group,
54
- enumerable: false
77
+ Object.keys(styles).forEach(groupName => {
78
+ const group = styles[groupName];
79
+
80
+ Object.keys(group).forEach(styleName => {
81
+ const style = group[styleName];
82
+
83
+ styles[styleName] = {
84
+ open: `\u001B[${style[0]}m`,
85
+ close: `\u001B[${style[1]}m`
86
+ };
87
+
88
+ group[styleName] = styles[styleName];
89
+ });
90
+
91
+ Object.defineProperty(styles, groupName, {
92
+ value: group,
93
+ enumerable: false
94
+ });
55
95
  });
96
+
97
+ const rgb2rgb = (r, g, b) => [r, g, b];
98
+
99
+ styles.color.close = '\u001B[39m';
100
+ styles.bgColor.close = '\u001B[49m';
101
+
102
+ styles.color.ansi = {};
103
+ styles.color.ansi256 = {};
104
+ styles.color.ansi16m = {
105
+ rgb: wrapAnsi16m(rgb2rgb, 0)
106
+ };
107
+
108
+ styles.bgColor.ansi = {};
109
+ styles.bgColor.ansi256 = {};
110
+ styles.bgColor.ansi16m = {
111
+ rgb: wrapAnsi16m(rgb2rgb, 10)
112
+ };
113
+
114
+ for (const key of Object.keys(colorConvert)) {
115
+ if (typeof colorConvert[key] !== 'object') {
116
+ continue;
117
+ }
118
+
119
+ const suite = colorConvert[key];
120
+
121
+ if ('ansi16' in suite) {
122
+ styles.color.ansi[key] = wrapAnsi16(suite.ansi16, 0);
123
+ styles.bgColor.ansi[key] = wrapAnsi16(suite.ansi16, 10);
124
+ }
125
+
126
+ if ('ansi256' in suite) {
127
+ styles.color.ansi256[key] = wrapAnsi256(suite.ansi256, 0);
128
+ styles.bgColor.ansi256[key] = wrapAnsi256(suite.ansi256, 10);
129
+ }
130
+
131
+ if ('rgb' in suite) {
132
+ styles.color.ansi16m[key] = wrapAnsi16m(suite.rgb, 0);
133
+ styles.bgColor.ansi16m[key] = wrapAnsi16m(suite.rgb, 10);
134
+ }
135
+ }
136
+
137
+ return styles;
138
+ }
139
+
140
+ Object.defineProperty(module, 'exports', {
141
+ enumerable: true,
142
+ get: assembleStyles
56
143
  });
package/package.json CHANGED
@@ -1,23 +1,24 @@
1
1
  {
2
2
  "name": "ansi-styles",
3
- "version": "2.0.1",
3
+ "version": "3.1.0",
4
4
  "description": "ANSI escape codes for styling strings in the terminal",
5
5
  "license": "MIT",
6
- "repository": "sindresorhus/ansi-styles",
6
+ "repository": "chalk/ansi-styles",
7
7
  "author": {
8
8
  "name": "Sindre Sorhus",
9
9
  "email": "sindresorhus@gmail.com",
10
- "url": "http://sindresorhus.com"
10
+ "url": "sindresorhus.com"
11
11
  },
12
12
  "maintainers": [
13
- "Sindre Sorhus <sindresorhus@gmail.com> (http://sindresorhus.com)",
14
- "Joshua Appelman <jappelman@xebia.com> (http://jbnicolai.com)"
13
+ "Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)",
14
+ "Joshua Boy Nicolai Appelman <joshua@jbna.nl> (jbna.nl)",
15
+ "Josh Junon <i.am.qix@gmail.com> (github.com/qix-)"
15
16
  ],
16
17
  "engines": {
17
- "node": ">=0.10.0"
18
+ "node": ">=4"
18
19
  },
19
20
  "scripts": {
20
- "test": "mocha"
21
+ "test": "xo && ava"
21
22
  },
22
23
  "files": [
23
24
  "index.js"
@@ -44,7 +45,15 @@
44
45
  "command-line",
45
46
  "text"
46
47
  ],
48
+ "dependencies": {
49
+ "color-convert": "^1.0.0"
50
+ },
47
51
  "devDependencies": {
48
- "mocha": "*"
52
+ "ava": "*",
53
+ "babel-polyfill": "^6.23.0",
54
+ "xo": "*"
55
+ },
56
+ "ava": {
57
+ "require": "babel-polyfill"
49
58
  }
50
59
  }
package/readme.md CHANGED
@@ -1,15 +1,15 @@
1
- # ansi-styles [![Build Status](https://travis-ci.org/sindresorhus/ansi-styles.svg?branch=master)](https://travis-ci.org/sindresorhus/ansi-styles)
1
+ # ansi-styles [![Build Status](https://travis-ci.org/chalk/ansi-styles.svg?branch=master)](https://travis-ci.org/chalk/ansi-styles)
2
2
 
3
3
  > [ANSI escape codes](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles) for styling strings in the terminal
4
4
 
5
- You probably want the higher-level [chalk](https://github.com/sindresorhus/chalk) module for styling your strings.
5
+ You probably want the higher-level [chalk](https://github.com/chalk/chalk) module for styling your strings.
6
6
 
7
7
  ![](screenshot.png)
8
8
 
9
9
 
10
10
  ## Install
11
11
 
12
- ```sh
12
+ ```
13
13
  $ npm install --save ansi-styles
14
14
  ```
15
15
 
@@ -17,12 +17,21 @@ $ npm install --save ansi-styles
17
17
  ## Usage
18
18
 
19
19
  ```js
20
- var ansi = require('ansi-styles');
20
+ const style = require('ansi-styles');
21
21
 
22
- console.log(ansi.green.open + 'Hello world!' + ansi.green.close);
23
- ```
22
+ console.log(`${style.green.open}Hello world!${style.green.close}`);
24
23
 
25
24
 
25
+ // color conversion between 16/256/truecolor
26
+ // NOTE: if conversion goes to 16 colors or 256 colors, the original color
27
+ // may be degraded to fit that color palette. This means terminals
28
+ // that do not support 16 million colors will best-match the
29
+ // original color.
30
+ console.log(style.bgColor.ansi.hsl(120, 80, 72) + 'Hello world!' + style.bgColor.close);
31
+ console.log(style.color.ansi256.rgb(199, 20, 250) + 'Hello world!' + style.color.close);
32
+ console.log(style.color.ansi16m.hex('#ABCDEF') + 'Hello world!' + style.color.close);
33
+ ```
34
+
26
35
  ## API
27
36
 
28
37
  Each style has an `open` and `close` property.
@@ -53,6 +62,15 @@ Each style has an `open` and `close` property.
53
62
  - `white`
54
63
  - `gray`
55
64
 
65
+ - `blackBright`
66
+ - `redBright`
67
+ - `greenBright`
68
+ - `yellowBright`
69
+ - `blueBright`
70
+ - `magentaBright`
71
+ - `cyanBright`
72
+ - `whiteBright`
73
+
56
74
  ### Background colors
57
75
 
58
76
  - `bgBlack`
@@ -64,23 +82,54 @@ Each style has an `open` and `close` property.
64
82
  - `bgCyan`
65
83
  - `bgWhite`
66
84
 
85
+ - `bgBlackBright`
86
+ - `bgRedBright`
87
+ - `bgGreenBright`
88
+ - `bgYellowBright`
89
+ - `bgBlueBright`
90
+ - `bgMagentaBright`
91
+ - `bgCyanBright`
92
+ - `bgWhiteBright`
67
93
 
68
94
  ## Advanced usage
69
95
 
70
- 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.
96
+ 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.
71
97
 
72
- - `ansi.modifiers`
73
- - `ansi.colors`
74
- - `ansi.bgColors`
98
+ - `style.modifier`
99
+ - `style.color`
100
+ - `style.bgColor`
75
101
 
76
102
 
77
103
  ###### Example
78
104
 
79
105
  ```js
80
- console.log(ansi.colors.green.open);
106
+ console.log(style.color.green.open);
81
107
  ```
82
108
 
83
109
 
110
+ ## [256 / 16 million (TrueColor) support](https://gist.github.com/XVilka/8346728)
111
+
112
+ `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.
113
+
114
+ To use these, call the associated conversion function with the intended output, for example:
115
+
116
+ ```js
117
+ style.color.ansi.rgb(100, 200, 15); // RGB to 16 color ansi foreground code
118
+ style.bgColor.ansi.rgb(100, 200, 15); // RGB to 16 color ansi background code
119
+
120
+ style.color.ansi256.hsl(120, 100, 60); // HSL to 256 color ansi foreground code
121
+ style.bgColor.ansi256.hsl(120, 100, 60); // HSL to 256 color ansi foreground code
122
+
123
+ style.color.ansi16m.hex('#C0FFEE'); // Hex (RGB) to 16 million color foreground code
124
+ style.bgColor.ansi16m.hex('#C0FFEE'); // Hex (RGB) to 16 million color background code
125
+ ```
126
+
127
+
128
+ ## Related
129
+
130
+ - [ansi-escapes](https://github.com/sindresorhus/ansi-escapes) - ANSI escape codes for manipulating the terminal
131
+
132
+
84
133
  ## License
85
134
 
86
- MIT © [Sindre Sorhus](http://sindresorhus.com)
135
+ MIT © [Sindre Sorhus](https://sindresorhus.com)