ansi-styles 2.1.0 → 3.2.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 (4) hide show
  1. package/index.js +106 -19
  2. package/license +4 -16
  3. package/package.json +52 -48
  4. package/readme.md +74 -13
package/index.js CHANGED
@@ -1,10 +1,28 @@
1
1
  'use strict';
2
+ const colorConvert = require('color-convert');
2
3
 
3
- function assembleStyles () {
4
- var styles = {
5
- modifiers: {
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`;
12
+ };
13
+
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
+ };
18
+
19
+ function assembleStyles() {
20
+ const codes = new Map();
21
+ const styles = {
22
+ modifier: {
6
23
  reset: [0, 0],
7
- bold: [1, 22], // 21 isn't widely supported and 22 does the same thing
24
+ // 21 isn't widely supported and 22 does the same thing
25
+ bold: [1, 22],
8
26
  dim: [2, 22],
9
27
  italic: [3, 23],
10
28
  underline: [4, 24],
@@ -12,7 +30,7 @@ function assembleStyles () {
12
30
  hidden: [8, 28],
13
31
  strikethrough: [9, 29]
14
32
  },
15
- colors: {
33
+ color: {
16
34
  black: [30, 39],
17
35
  red: [31, 39],
18
36
  green: [32, 39],
@@ -21,9 +39,18 @@ function assembleStyles () {
21
39
  magenta: [35, 39],
22
40
  cyan: [36, 39],
23
41
  white: [37, 39],
24
- gray: [90, 39]
42
+ gray: [90, 39],
43
+
44
+ // Bright color
45
+ redBright: [91, 39],
46
+ greenBright: [92, 39],
47
+ yellowBright: [93, 39],
48
+ blueBright: [94, 39],
49
+ magentaBright: [95, 39],
50
+ cyanBright: [96, 39],
51
+ whiteBright: [97, 39]
25
52
  },
26
- bgColors: {
53
+ bgColor: {
27
54
  bgBlack: [40, 49],
28
55
  bgRed: [41, 49],
29
56
  bgGreen: [42, 49],
@@ -31,34 +58,94 @@ function assembleStyles () {
31
58
  bgBlue: [44, 49],
32
59
  bgMagenta: [45, 49],
33
60
  bgCyan: [46, 49],
34
- bgWhite: [47, 49]
61
+ bgWhite: [47, 49],
62
+
63
+ // Bright color
64
+ bgBlackBright: [100, 49],
65
+ bgRedBright: [101, 49],
66
+ bgGreenBright: [102, 49],
67
+ bgYellowBright: [103, 49],
68
+ bgBlueBright: [104, 49],
69
+ bgMagentaBright: [105, 49],
70
+ bgCyanBright: [106, 49],
71
+ bgWhiteBright: [107, 49]
35
72
  }
36
73
  };
37
74
 
38
- // fix humans
39
- styles.colors.grey = styles.colors.gray;
75
+ // Fix humans
76
+ styles.color.grey = styles.color.gray;
40
77
 
41
- Object.keys(styles).forEach(function (groupName) {
42
- var group = styles[groupName];
78
+ for (const groupName of Object.keys(styles)) {
79
+ const group = styles[groupName];
43
80
 
44
- Object.keys(group).forEach(function (styleName) {
45
- var style = group[styleName];
81
+ for (const styleName of Object.keys(group)) {
82
+ const style = group[styleName];
46
83
 
47
- styles[styleName] = group[styleName] = {
48
- open: '\u001b[' + style[0] + 'm',
49
- close: '\u001b[' + style[1] + 'm'
84
+ styles[styleName] = {
85
+ open: `\u001B[${style[0]}m`,
86
+ close: `\u001B[${style[1]}m`
50
87
  };
51
- });
88
+
89
+ group[styleName] = styles[styleName];
90
+
91
+ codes.set(style[0], style[1]);
92
+ }
52
93
 
53
94
  Object.defineProperty(styles, groupName, {
54
95
  value: group,
55
96
  enumerable: false
56
97
  });
57
- });
98
+
99
+ Object.defineProperty(styles, 'codes', {
100
+ value: codes,
101
+ enumerable: false
102
+ });
103
+ }
104
+
105
+ const rgb2rgb = (r, g, b) => [r, g, b];
106
+
107
+ styles.color.close = '\u001B[39m';
108
+ styles.bgColor.close = '\u001B[49m';
109
+
110
+ styles.color.ansi = {};
111
+ styles.color.ansi256 = {};
112
+ styles.color.ansi16m = {
113
+ rgb: wrapAnsi16m(rgb2rgb, 0)
114
+ };
115
+
116
+ styles.bgColor.ansi = {};
117
+ styles.bgColor.ansi256 = {};
118
+ styles.bgColor.ansi16m = {
119
+ rgb: wrapAnsi16m(rgb2rgb, 10)
120
+ };
121
+
122
+ for (const key of Object.keys(colorConvert)) {
123
+ if (typeof colorConvert[key] !== 'object') {
124
+ continue;
125
+ }
126
+
127
+ const suite = colorConvert[key];
128
+
129
+ if ('ansi16' in suite) {
130
+ styles.color.ansi[key] = wrapAnsi16(suite.ansi16, 0);
131
+ styles.bgColor.ansi[key] = wrapAnsi16(suite.ansi16, 10);
132
+ }
133
+
134
+ if ('ansi256' in suite) {
135
+ styles.color.ansi256[key] = wrapAnsi256(suite.ansi256, 0);
136
+ styles.bgColor.ansi256[key] = wrapAnsi256(suite.ansi256, 10);
137
+ }
138
+
139
+ if ('rgb' in suite) {
140
+ styles.color.ansi16m[key] = wrapAnsi16m(suite.rgb, 0);
141
+ styles.bgColor.ansi16m[key] = wrapAnsi16m(suite.rgb, 10);
142
+ }
143
+ }
58
144
 
59
145
  return styles;
60
146
  }
61
147
 
148
+ // Make the export immutable
62
149
  Object.defineProperty(module, 'exports', {
63
150
  enumerable: true,
64
151
  get: assembleStyles
package/license CHANGED
@@ -1,21 +1,9 @@
1
- The MIT License (MIT)
1
+ MIT License
2
2
 
3
3
  Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
4
4
 
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
11
6
 
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14
8
 
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/package.json CHANGED
@@ -1,50 +1,54 @@
1
1
  {
2
- "name": "ansi-styles",
3
- "version": "2.1.0",
4
- "description": "ANSI escape codes for styling strings in the terminal",
5
- "license": "MIT",
6
- "repository": "chalk/ansi-styles",
7
- "author": {
8
- "name": "Sindre Sorhus",
9
- "email": "sindresorhus@gmail.com",
10
- "url": "sindresorhus.com"
11
- },
12
- "maintainers": [
13
- "Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)",
14
- "Joshua Appelman <jappelman@xebia.com> (jbnicolai.com)"
15
- ],
16
- "engines": {
17
- "node": ">=0.10.0"
18
- },
19
- "scripts": {
20
- "test": "mocha"
21
- },
22
- "files": [
23
- "index.js"
24
- ],
25
- "keywords": [
26
- "ansi",
27
- "styles",
28
- "color",
29
- "colour",
30
- "colors",
31
- "terminal",
32
- "console",
33
- "cli",
34
- "string",
35
- "tty",
36
- "escape",
37
- "formatting",
38
- "rgb",
39
- "256",
40
- "shell",
41
- "xterm",
42
- "log",
43
- "logging",
44
- "command-line",
45
- "text"
46
- ],
47
- "devDependencies": {
48
- "mocha": "*"
49
- }
2
+ "name": "ansi-styles",
3
+ "version": "3.2.0",
4
+ "description": "ANSI escape codes for styling strings in the terminal",
5
+ "license": "MIT",
6
+ "repository": "chalk/ansi-styles",
7
+ "author": {
8
+ "name": "Sindre Sorhus",
9
+ "email": "sindresorhus@gmail.com",
10
+ "url": "sindresorhus.com"
11
+ },
12
+ "engines": {
13
+ "node": ">=4"
14
+ },
15
+ "scripts": {
16
+ "test": "xo && ava"
17
+ },
18
+ "files": [
19
+ "index.js"
20
+ ],
21
+ "keywords": [
22
+ "ansi",
23
+ "styles",
24
+ "color",
25
+ "colour",
26
+ "colors",
27
+ "terminal",
28
+ "console",
29
+ "cli",
30
+ "string",
31
+ "tty",
32
+ "escape",
33
+ "formatting",
34
+ "rgb",
35
+ "256",
36
+ "shell",
37
+ "xterm",
38
+ "log",
39
+ "logging",
40
+ "command-line",
41
+ "text"
42
+ ],
43
+ "dependencies": {
44
+ "color-convert": "^1.9.0"
45
+ },
46
+ "devDependencies": {
47
+ "ava": "*",
48
+ "babel-polyfill": "^6.23.0",
49
+ "xo": "*"
50
+ },
51
+ "ava": {
52
+ "require": "babel-polyfill"
53
+ }
50
54
  }
package/readme.md CHANGED
@@ -10,18 +10,27 @@ You probably want the higher-level [chalk](https://github.com/chalk/chalk) modul
10
10
  ## Install
11
11
 
12
12
  ```
13
- $ npm install --save ansi-styles
13
+ $ npm install ansi-styles
14
14
  ```
15
15
 
16
16
 
17
17
  ## Usage
18
18
 
19
19
  ```js
20
- var ansi = require('ansi-styles');
20
+ const style = require('ansi-styles');
21
+
22
+ console.log(`${style.green.open}Hello world!${style.green.close}`);
21
23
 
22
- console.log(ansi.green.open + 'Hello world!' + ansi.green.close);
23
- ```
24
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
+ ```
25
34
 
26
35
  ## API
27
36
 
@@ -35,11 +44,11 @@ Each style has an `open` and `close` property.
35
44
  - `reset`
36
45
  - `bold`
37
46
  - `dim`
38
- - `italic` *(not widely supported)*
47
+ - `italic` *(Not widely supported)*
39
48
  - `underline`
40
49
  - `inverse`
41
50
  - `hidden`
42
- - `strikethrough` *(not widely supported)*
51
+ - `strikethrough` *(Not widely supported)*
43
52
 
44
53
  ### Colors
45
54
 
@@ -51,7 +60,14 @@ Each style has an `open` and `close` property.
51
60
  - `magenta`
52
61
  - `cyan`
53
62
  - `white`
54
- - `gray`
63
+ - `gray` ("bright black")
64
+ - `redBright`
65
+ - `greenBright`
66
+ - `yellowBright`
67
+ - `blueBright`
68
+ - `magentaBright`
69
+ - `cyanBright`
70
+ - `whiteBright`
55
71
 
56
72
  ### Background colors
57
73
 
@@ -63,24 +79,69 @@ Each style has an `open` and `close` property.
63
79
  - `bgMagenta`
64
80
  - `bgCyan`
65
81
  - `bgWhite`
82
+ - `bgBlackBright`
83
+ - `bgRedBright`
84
+ - `bgGreenBright`
85
+ - `bgYellowBright`
86
+ - `bgBlueBright`
87
+ - `bgMagentaBright`
88
+ - `bgCyanBright`
89
+ - `bgWhiteBright`
66
90
 
67
91
 
68
92
  ## Advanced usage
69
93
 
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.
94
+ 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.
95
+
96
+ - `style.modifier`
97
+ - `style.color`
98
+ - `style.bgColor`
99
+
100
+ ###### Example
71
101
 
72
- - `ansi.modifiers`
73
- - `ansi.colors`
74
- - `ansi.bgColors`
102
+ ```js
103
+ console.log(style.color.green.open);
104
+ ```
75
105
 
106
+ Raw escape codes (i.e. without the CSI escape prefix `\u001B[` and render mode postfix `m`) are available under `style.codes`, which returns a `Map` with the open codes as keys and close codes as values.
76
107
 
77
108
  ###### Example
78
109
 
79
110
  ```js
80
- console.log(ansi.colors.green.open);
111
+ console.log(style.codes.get(36));
112
+ //=> 39
81
113
  ```
82
114
 
83
115
 
116
+ ## [256 / 16 million (TrueColor) support](https://gist.github.com/XVilka/8346728)
117
+
118
+ `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.
119
+
120
+ To use these, call the associated conversion function with the intended output, for example:
121
+
122
+ ```js
123
+ style.color.ansi.rgb(100, 200, 15); // RGB to 16 color ansi foreground code
124
+ style.bgColor.ansi.rgb(100, 200, 15); // RGB to 16 color ansi background code
125
+
126
+ style.color.ansi256.hsl(120, 100, 60); // HSL to 256 color ansi foreground code
127
+ style.bgColor.ansi256.hsl(120, 100, 60); // HSL to 256 color ansi foreground code
128
+
129
+ style.color.ansi16m.hex('#C0FFEE'); // Hex (RGB) to 16 million color foreground code
130
+ style.bgColor.ansi16m.hex('#C0FFEE'); // Hex (RGB) to 16 million color background code
131
+ ```
132
+
133
+
134
+ ## Related
135
+
136
+ - [ansi-escapes](https://github.com/sindresorhus/ansi-escapes) - ANSI escape codes for manipulating the terminal
137
+
138
+
139
+ ## Maintainers
140
+
141
+ - [Sindre Sorhus](https://github.com/sindresorhus)
142
+ - [Josh Junon](https://github.com/qix-)
143
+
144
+
84
145
  ## License
85
146
 
86
- MIT © [Sindre Sorhus](http://sindresorhus.com)
147
+ MIT