ansi-styles 3.1.0 → 4.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.
- package/index.js +72 -53
- package/license +4 -16
- package/package.json +50 -57
- package/readme.md +36 -16
package/index.js
CHANGED
@@ -1,22 +1,64 @@
|
|
1
1
|
'use strict';
|
2
|
-
const colorConvert = require('color-convert');
|
3
2
|
|
4
|
-
const wrapAnsi16 = (fn, offset) =>
|
5
|
-
const code = fn
|
3
|
+
const wrapAnsi16 = (fn, offset) => (...args) => {
|
4
|
+
const code = fn(...args);
|
6
5
|
return `\u001B[${code + offset}m`;
|
7
6
|
};
|
8
7
|
|
9
|
-
const wrapAnsi256 = (fn, offset) =>
|
10
|
-
const code = fn
|
8
|
+
const wrapAnsi256 = (fn, offset) => (...args) => {
|
9
|
+
const code = fn(...args);
|
11
10
|
return `\u001B[${38 + offset};5;${code}m`;
|
12
11
|
};
|
13
12
|
|
14
|
-
const wrapAnsi16m = (fn, offset) =>
|
15
|
-
const rgb = fn
|
13
|
+
const wrapAnsi16m = (fn, offset) => (...args) => {
|
14
|
+
const rgb = fn(...args);
|
16
15
|
return `\u001B[${38 + offset};2;${rgb[0]};${rgb[1]};${rgb[2]}m`;
|
17
16
|
};
|
18
17
|
|
18
|
+
const ansi2ansi = n => n;
|
19
|
+
const rgb2rgb = (r, g, b) => [r, g, b];
|
20
|
+
|
21
|
+
const setLazyProperty = (object, property, get) => {
|
22
|
+
Object.defineProperty(object, property, {
|
23
|
+
get: () => {
|
24
|
+
const value = get();
|
25
|
+
|
26
|
+
Object.defineProperty(object, property, {
|
27
|
+
value,
|
28
|
+
enumerable: true,
|
29
|
+
configurable: true
|
30
|
+
});
|
31
|
+
|
32
|
+
return value;
|
33
|
+
},
|
34
|
+
enumerable: true,
|
35
|
+
configurable: true
|
36
|
+
});
|
37
|
+
};
|
38
|
+
|
39
|
+
let colorConvert;
|
40
|
+
const makeDynamicStyles = (wrap, targetSpace, identity, isBackground) => {
|
41
|
+
if (colorConvert === undefined) {
|
42
|
+
colorConvert = require('color-convert');
|
43
|
+
}
|
44
|
+
|
45
|
+
const offset = isBackground ? 10 : 0;
|
46
|
+
const styles = {};
|
47
|
+
|
48
|
+
for (const [sourceSpace, suite] of Object.entries(colorConvert)) {
|
49
|
+
const name = sourceSpace === 'ansi16' ? 'ansi' : sourceSpace;
|
50
|
+
if (sourceSpace === targetSpace) {
|
51
|
+
styles[name] = wrap(identity, offset);
|
52
|
+
} else if (typeof suite === 'object') {
|
53
|
+
styles[name] = wrap(suite[targetSpace], offset);
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
57
|
+
return styles;
|
58
|
+
};
|
59
|
+
|
19
60
|
function assembleStyles() {
|
61
|
+
const codes = new Map();
|
20
62
|
const styles = {
|
21
63
|
modifier: {
|
22
64
|
reset: [0, 0],
|
@@ -38,9 +80,9 @@ function assembleStyles() {
|
|
38
80
|
magenta: [35, 39],
|
39
81
|
cyan: [36, 39],
|
40
82
|
white: [37, 39],
|
41
|
-
gray: [90, 39],
|
42
83
|
|
43
84
|
// Bright color
|
85
|
+
blackBright: [90, 39],
|
44
86
|
redBright: [91, 39],
|
45
87
|
greenBright: [92, 39],
|
46
88
|
yellowBright: [93, 39],
|
@@ -71,72 +113,49 @@ function assembleStyles() {
|
|
71
113
|
}
|
72
114
|
};
|
73
115
|
|
74
|
-
//
|
75
|
-
styles.color.
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
Object.keys(group).forEach(styleName => {
|
81
|
-
const style = group[styleName];
|
116
|
+
// Alias bright black as gray (and grey)
|
117
|
+
styles.color.gray = styles.color.blackBright;
|
118
|
+
styles.bgColor.bgGray = styles.bgColor.bgBlackBright;
|
119
|
+
styles.color.grey = styles.color.blackBright;
|
120
|
+
styles.bgColor.bgGrey = styles.bgColor.bgBlackBright;
|
82
121
|
|
122
|
+
for (const [groupName, group] of Object.entries(styles)) {
|
123
|
+
for (const [styleName, style] of Object.entries(group)) {
|
83
124
|
styles[styleName] = {
|
84
125
|
open: `\u001B[${style[0]}m`,
|
85
126
|
close: `\u001B[${style[1]}m`
|
86
127
|
};
|
87
128
|
|
88
129
|
group[styleName] = styles[styleName];
|
89
|
-
|
130
|
+
|
131
|
+
codes.set(style[0], style[1]);
|
132
|
+
}
|
90
133
|
|
91
134
|
Object.defineProperty(styles, groupName, {
|
92
135
|
value: group,
|
93
136
|
enumerable: false
|
94
137
|
});
|
95
|
-
});
|
96
138
|
|
97
|
-
|
139
|
+
Object.defineProperty(styles, 'codes', {
|
140
|
+
value: codes,
|
141
|
+
enumerable: false
|
142
|
+
});
|
143
|
+
}
|
98
144
|
|
99
145
|
styles.color.close = '\u001B[39m';
|
100
146
|
styles.bgColor.close = '\u001B[49m';
|
101
147
|
|
102
|
-
styles.color
|
103
|
-
styles.color
|
104
|
-
styles.color
|
105
|
-
|
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
|
-
}
|
148
|
+
setLazyProperty(styles.color, 'ansi', () => makeDynamicStyles(wrapAnsi16, 'ansi16', ansi2ansi, false));
|
149
|
+
setLazyProperty(styles.color, 'ansi256', () => makeDynamicStyles(wrapAnsi256, 'ansi256', ansi2ansi, false));
|
150
|
+
setLazyProperty(styles.color, 'ansi16m', () => makeDynamicStyles(wrapAnsi16m, 'rgb', rgb2rgb, false));
|
151
|
+
setLazyProperty(styles.bgColor, 'ansi', () => makeDynamicStyles(wrapAnsi16, 'ansi16', ansi2ansi, true));
|
152
|
+
setLazyProperty(styles.bgColor, 'ansi256', () => makeDynamicStyles(wrapAnsi256, 'ansi256', ansi2ansi, true));
|
153
|
+
setLazyProperty(styles.bgColor, 'ansi16m', () => makeDynamicStyles(wrapAnsi16m, 'rgb', rgb2rgb, true));
|
136
154
|
|
137
155
|
return styles;
|
138
156
|
}
|
139
157
|
|
158
|
+
// Make the export immutable
|
140
159
|
Object.defineProperty(module, 'exports', {
|
141
160
|
enumerable: true,
|
142
161
|
get: assembleStyles
|
package/license
CHANGED
@@ -1,21 +1,9 @@
|
|
1
|
-
|
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,59 +1,52 @@
|
|
1
1
|
{
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
"ava": "*",
|
53
|
-
"babel-polyfill": "^6.23.0",
|
54
|
-
"xo": "*"
|
55
|
-
},
|
56
|
-
"ava": {
|
57
|
-
"require": "babel-polyfill"
|
58
|
-
}
|
2
|
+
"name": "ansi-styles",
|
3
|
+
"version": "4.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
|
+
"engines": {
|
13
|
+
"node": ">=8"
|
14
|
+
},
|
15
|
+
"scripts": {
|
16
|
+
"test": "xo && ava",
|
17
|
+
"screenshot": "svg-term --command='node screenshot' --out=screenshot.svg --padding=3 --width=55 --height=3 --at=1000 --no-cursor"
|
18
|
+
},
|
19
|
+
"files": [
|
20
|
+
"index.js"
|
21
|
+
],
|
22
|
+
"keywords": [
|
23
|
+
"ansi",
|
24
|
+
"styles",
|
25
|
+
"color",
|
26
|
+
"colour",
|
27
|
+
"colors",
|
28
|
+
"terminal",
|
29
|
+
"console",
|
30
|
+
"cli",
|
31
|
+
"string",
|
32
|
+
"tty",
|
33
|
+
"escape",
|
34
|
+
"formatting",
|
35
|
+
"rgb",
|
36
|
+
"256",
|
37
|
+
"shell",
|
38
|
+
"xterm",
|
39
|
+
"log",
|
40
|
+
"logging",
|
41
|
+
"command-line",
|
42
|
+
"text"
|
43
|
+
],
|
44
|
+
"dependencies": {
|
45
|
+
"color-convert": "^2.0.1"
|
46
|
+
},
|
47
|
+
"devDependencies": {
|
48
|
+
"ava": "^2.3.0",
|
49
|
+
"svg-term-cli": "^2.1.1",
|
50
|
+
"xo": "^0.24.0"
|
51
|
+
}
|
59
52
|
}
|
package/readme.md
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
# ansi-styles [](https://travis-ci.org/chalk/ansi-styles)
|
2
2
|
|
3
|
-
> [ANSI escape codes](
|
3
|
+
> [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles) for styling strings in the terminal
|
4
4
|
|
5
5
|
You probably want the higher-level [chalk](https://github.com/chalk/chalk) module for styling your strings.
|
6
6
|
|
7
|
-
|
7
|
+
<img src="screenshot.svg" width="900">
|
8
8
|
|
9
9
|
|
10
10
|
## Install
|
11
11
|
|
12
12
|
```
|
13
|
-
$ npm install
|
13
|
+
$ npm install ansi-styles
|
14
14
|
```
|
15
15
|
|
16
16
|
|
@@ -22,14 +22,14 @@ const style = require('ansi-styles');
|
|
22
22
|
console.log(`${style.green.open}Hello world!${style.green.close}`);
|
23
23
|
|
24
24
|
|
25
|
-
//
|
26
|
-
// NOTE:
|
25
|
+
// Color conversion between 16/256/truecolor
|
26
|
+
// NOTE: If conversion goes to 16 colors or 256 colors, the original color
|
27
27
|
// may be degraded to fit that color palette. This means terminals
|
28
28
|
// that do not support 16 million colors will best-match the
|
29
29
|
// original color.
|
30
30
|
console.log(style.bgColor.ansi.hsl(120, 80, 72) + 'Hello world!' + style.bgColor.close);
|
31
31
|
console.log(style.color.ansi256.rgb(199, 20, 250) + 'Hello world!' + style.color.close);
|
32
|
-
console.log(style.color.ansi16m.hex('#
|
32
|
+
console.log(style.color.ansi16m.hex('#abcdef') + 'Hello world!' + style.color.close);
|
33
33
|
```
|
34
34
|
|
35
35
|
## API
|
@@ -44,11 +44,11 @@ Each style has an `open` and `close` property.
|
|
44
44
|
- `reset`
|
45
45
|
- `bold`
|
46
46
|
- `dim`
|
47
|
-
- `italic` *(
|
47
|
+
- `italic` *(Not widely supported)*
|
48
48
|
- `underline`
|
49
49
|
- `inverse`
|
50
50
|
- `hidden`
|
51
|
-
- `strikethrough` *(
|
51
|
+
- `strikethrough` *(Not widely supported)*
|
52
52
|
|
53
53
|
### Colors
|
54
54
|
|
@@ -60,9 +60,7 @@ Each style has an `open` and `close` property.
|
|
60
60
|
- `magenta`
|
61
61
|
- `cyan`
|
62
62
|
- `white`
|
63
|
-
- `gray`
|
64
|
-
|
65
|
-
- `blackBright`
|
63
|
+
- `blackBright` (alias: `gray`, `grey`)
|
66
64
|
- `redBright`
|
67
65
|
- `greenBright`
|
68
66
|
- `yellowBright`
|
@@ -81,8 +79,7 @@ Each style has an `open` and `close` property.
|
|
81
79
|
- `bgMagenta`
|
82
80
|
- `bgCyan`
|
83
81
|
- `bgWhite`
|
84
|
-
|
85
|
-
- `bgBlackBright`
|
82
|
+
- `bgBlackBright` (alias: `bgGray`, `bgGrey`)
|
86
83
|
- `bgRedBright`
|
87
84
|
- `bgGreenBright`
|
88
85
|
- `bgYellowBright`
|
@@ -91,6 +88,7 @@ Each style has an `open` and `close` property.
|
|
91
88
|
- `bgCyanBright`
|
92
89
|
- `bgWhiteBright`
|
93
90
|
|
91
|
+
|
94
92
|
## Advanced usage
|
95
93
|
|
96
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.
|
@@ -99,13 +97,21 @@ By default, you get a map of styles, but the styles are also available as groups
|
|
99
97
|
- `style.color`
|
100
98
|
- `style.bgColor`
|
101
99
|
|
102
|
-
|
103
100
|
###### Example
|
104
101
|
|
105
102
|
```js
|
106
103
|
console.log(style.color.green.open);
|
107
104
|
```
|
108
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.
|
107
|
+
|
108
|
+
###### Example
|
109
|
+
|
110
|
+
```js
|
111
|
+
console.log(style.codes.get(36));
|
112
|
+
//=> 39
|
113
|
+
```
|
114
|
+
|
109
115
|
|
110
116
|
## [256 / 16 million (TrueColor) support](https://gist.github.com/XVilka/8346728)
|
111
117
|
|
@@ -130,6 +136,20 @@ style.bgColor.ansi16m.hex('#C0FFEE'); // Hex (RGB) to 16 million color backgroun
|
|
130
136
|
- [ansi-escapes](https://github.com/sindresorhus/ansi-escapes) - ANSI escape codes for manipulating the terminal
|
131
137
|
|
132
138
|
|
133
|
-
##
|
139
|
+
## Maintainers
|
140
|
+
|
141
|
+
- [Sindre Sorhus](https://github.com/sindresorhus)
|
142
|
+
- [Josh Junon](https://github.com/qix-)
|
143
|
+
|
144
|
+
|
145
|
+
---
|
134
146
|
|
135
|
-
|
147
|
+
<div align="center">
|
148
|
+
<b>
|
149
|
+
<a href="https://tidelift.com/subscription/pkg/npm-ansi-styles?utm_source=npm-ansi-styles&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
|
150
|
+
</b>
|
151
|
+
<br>
|
152
|
+
<sub>
|
153
|
+
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
|
154
|
+
</sub>
|
155
|
+
</div>
|