ansi-styles 3.2.1 → 4.2.1

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.d.ts +197 -0
  2. package/index.js +67 -69
  3. package/package.json +12 -11
  4. package/readme.md +27 -16
package/index.d.ts ADDED
@@ -0,0 +1,197 @@
1
+ import * as cssColors from 'color-name';
2
+
3
+ declare namespace ansiStyles {
4
+ interface ColorConvert {
5
+ /**
6
+ The RGB color space.
7
+
8
+ @param red - (`0`-`255`)
9
+ @param green - (`0`-`255`)
10
+ @param blue - (`0`-`255`)
11
+ */
12
+ rgb(red: number, green: number, blue: number): string;
13
+
14
+ /**
15
+ The RGB HEX color space.
16
+
17
+ @param hex - A hexadecimal string containing RGB data.
18
+ */
19
+ hex(hex: string): string;
20
+
21
+ /**
22
+ @param keyword - A CSS color name.
23
+ */
24
+ keyword(keyword: keyof typeof cssColors): string;
25
+
26
+ /**
27
+ The HSL color space.
28
+
29
+ @param hue - (`0`-`360`)
30
+ @param saturation - (`0`-`100`)
31
+ @param lightness - (`0`-`100`)
32
+ */
33
+ hsl(hue: number, saturation: number, lightness: number): string;
34
+
35
+ /**
36
+ The HSV color space.
37
+
38
+ @param hue - (`0`-`360`)
39
+ @param saturation - (`0`-`100`)
40
+ @param value - (`0`-`100`)
41
+ */
42
+ hsv(hue: number, saturation: number, value: number): string;
43
+
44
+ /**
45
+ The HSV color space.
46
+
47
+ @param hue - (`0`-`360`)
48
+ @param whiteness - (`0`-`100`)
49
+ @param blackness - (`0`-`100`)
50
+ */
51
+ hwb(hue: number, whiteness: number, blackness: number): string;
52
+
53
+ /**
54
+ Use a [4-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#3/4-bit) to set text color.
55
+ */
56
+ ansi(ansi: number): string;
57
+
58
+ /**
59
+ Use an [8-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) to set text color.
60
+ */
61
+ ansi256(ansi: number): string;
62
+ }
63
+
64
+ interface CSPair {
65
+ /**
66
+ The ANSI terminal control sequence for starting this style.
67
+ */
68
+ readonly open: string;
69
+
70
+ /**
71
+ The ANSI terminal control sequence for ending this style.
72
+ */
73
+ readonly close: string;
74
+ }
75
+
76
+ interface ColorBase {
77
+ readonly ansi: ColorConvert;
78
+ readonly ansi256: ColorConvert;
79
+ readonly ansi16m: ColorConvert;
80
+
81
+ /**
82
+ The ANSI terminal control sequence for ending this color.
83
+ */
84
+ readonly close: string;
85
+ }
86
+
87
+ interface Modifier {
88
+ /**
89
+ Resets the current color chain.
90
+ */
91
+ readonly reset: CSPair;
92
+
93
+ /**
94
+ Make text bold.
95
+ */
96
+ readonly bold: CSPair;
97
+
98
+ /**
99
+ Emitting only a small amount of light.
100
+ */
101
+ readonly dim: CSPair;
102
+
103
+ /**
104
+ Make text italic. (Not widely supported)
105
+ */
106
+ readonly italic: CSPair;
107
+
108
+ /**
109
+ Make text underline. (Not widely supported)
110
+ */
111
+ readonly underline: CSPair;
112
+
113
+ /**
114
+ Inverse background and foreground colors.
115
+ */
116
+ readonly inverse: CSPair;
117
+
118
+ /**
119
+ Prints the text, but makes it invisible.
120
+ */
121
+ readonly hidden: CSPair;
122
+
123
+ /**
124
+ Puts a horizontal line through the center of the text. (Not widely supported)
125
+ */
126
+ readonly strikethrough: CSPair;
127
+ }
128
+
129
+ interface ForegroundColor {
130
+ readonly black: CSPair;
131
+ readonly red: CSPair;
132
+ readonly green: CSPair;
133
+ readonly yellow: CSPair;
134
+ readonly blue: CSPair;
135
+ readonly cyan: CSPair;
136
+ readonly magenta: CSPair;
137
+ readonly white: CSPair;
138
+
139
+ /**
140
+ Alias for `blackBright`.
141
+ */
142
+ readonly gray: CSPair;
143
+
144
+ /**
145
+ Alias for `blackBright`.
146
+ */
147
+ readonly grey: CSPair;
148
+
149
+ readonly blackBright: CSPair;
150
+ readonly redBright: CSPair;
151
+ readonly greenBright: CSPair;
152
+ readonly yellowBright: CSPair;
153
+ readonly blueBright: CSPair;
154
+ readonly cyanBright: CSPair;
155
+ readonly magentaBright: CSPair;
156
+ readonly whiteBright: CSPair;
157
+ }
158
+
159
+ interface BackgroundColor {
160
+ readonly bgBlack: CSPair;
161
+ readonly bgRed: CSPair;
162
+ readonly bgGreen: CSPair;
163
+ readonly bgYellow: CSPair;
164
+ readonly bgBlue: CSPair;
165
+ readonly bgCyan: CSPair;
166
+ readonly bgMagenta: CSPair;
167
+ readonly bgWhite: CSPair;
168
+
169
+ /**
170
+ Alias for `bgBlackBright`.
171
+ */
172
+ readonly bgGray: CSPair;
173
+
174
+ /**
175
+ Alias for `bgBlackBright`.
176
+ */
177
+ readonly bgGrey: CSPair;
178
+
179
+ readonly bgBlackBright: CSPair;
180
+ readonly bgRedBright: CSPair;
181
+ readonly bgGreenBright: CSPair;
182
+ readonly bgYellowBright: CSPair;
183
+ readonly bgBlueBright: CSPair;
184
+ readonly bgCyanBright: CSPair;
185
+ readonly bgMagentaBright: CSPair;
186
+ readonly bgWhiteBright: CSPair;
187
+ }
188
+ }
189
+
190
+ declare const ansiStyles: {
191
+ readonly modifier: ansiStyles.Modifier;
192
+ readonly color: ansiStyles.ForegroundColor & ansiStyles.ColorBase;
193
+ readonly bgColor: ansiStyles.BackgroundColor & ansiStyles.ColorBase;
194
+ readonly codes: ReadonlyMap<number, number>;
195
+ } & ansiStyles.BackgroundColor & ansiStyles.ForegroundColor & ansiStyles.Modifier;
196
+
197
+ export = ansiStyles;
package/index.js CHANGED
@@ -1,21 +1,63 @@
1
1
  'use strict';
2
- const colorConvert = require('color-convert');
3
2
 
4
- const wrapAnsi16 = (fn, offset) => function () {
5
- const code = fn.apply(colorConvert, arguments);
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) => function () {
10
- const code = fn.apply(colorConvert, arguments);
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) => function () {
15
- const rgb = fn.apply(colorConvert, arguments);
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
+ /** @type {typeof import('color-convert')} */
40
+ let colorConvert;
41
+ const makeDynamicStyles = (wrap, targetSpace, identity, isBackground) => {
42
+ if (colorConvert === undefined) {
43
+ colorConvert = require('color-convert');
44
+ }
45
+
46
+ const offset = isBackground ? 10 : 0;
47
+ const styles = {};
48
+
49
+ for (const [sourceSpace, suite] of Object.entries(colorConvert)) {
50
+ const name = sourceSpace === 'ansi16' ? 'ansi' : sourceSpace;
51
+ if (sourceSpace === targetSpace) {
52
+ styles[name] = wrap(identity, offset);
53
+ } else if (typeof suite === 'object') {
54
+ styles[name] = wrap(suite[targetSpace], offset);
55
+ }
56
+ }
57
+
58
+ return styles;
59
+ };
60
+
19
61
  function assembleStyles() {
20
62
  const codes = new Map();
21
63
  const styles = {
@@ -39,9 +81,9 @@ function assembleStyles() {
39
81
  magenta: [35, 39],
40
82
  cyan: [36, 39],
41
83
  white: [37, 39],
42
- gray: [90, 39],
43
84
 
44
85
  // Bright color
86
+ blackBright: [90, 39],
45
87
  redBright: [91, 39],
46
88
  greenBright: [92, 39],
47
89
  yellowBright: [93, 39],
@@ -72,15 +114,14 @@ function assembleStyles() {
72
114
  }
73
115
  };
74
116
 
75
- // Fix humans
76
- styles.color.grey = styles.color.gray;
77
-
78
- for (const groupName of Object.keys(styles)) {
79
- const group = styles[groupName];
80
-
81
- for (const styleName of Object.keys(group)) {
82
- const style = group[styleName];
117
+ // Alias bright black as gray (and grey)
118
+ styles.color.gray = styles.color.blackBright;
119
+ styles.bgColor.bgGray = styles.bgColor.bgBlackBright;
120
+ styles.color.grey = styles.color.blackBright;
121
+ styles.bgColor.bgGrey = styles.bgColor.bgBlackBright;
83
122
 
123
+ for (const [groupName, group] of Object.entries(styles)) {
124
+ for (const [styleName, style] of Object.entries(group)) {
84
125
  styles[styleName] = {
85
126
  open: `\u001B[${style[0]}m`,
86
127
  close: `\u001B[${style[1]}m`
@@ -95,65 +136,22 @@ function assembleStyles() {
95
136
  value: group,
96
137
  enumerable: false
97
138
  });
98
-
99
- Object.defineProperty(styles, 'codes', {
100
- value: codes,
101
- enumerable: false
102
- });
103
139
  }
104
140
 
105
- const ansi2ansi = n => n;
106
- const rgb2rgb = (r, g, b) => [r, g, b];
141
+ Object.defineProperty(styles, 'codes', {
142
+ value: codes,
143
+ enumerable: false
144
+ });
107
145
 
108
146
  styles.color.close = '\u001B[39m';
109
147
  styles.bgColor.close = '\u001B[49m';
110
148
 
111
- styles.color.ansi = {
112
- ansi: wrapAnsi16(ansi2ansi, 0)
113
- };
114
- styles.color.ansi256 = {
115
- ansi256: wrapAnsi256(ansi2ansi, 0)
116
- };
117
- styles.color.ansi16m = {
118
- rgb: wrapAnsi16m(rgb2rgb, 0)
119
- };
120
-
121
- styles.bgColor.ansi = {
122
- ansi: wrapAnsi16(ansi2ansi, 10)
123
- };
124
- styles.bgColor.ansi256 = {
125
- ansi256: wrapAnsi256(ansi2ansi, 10)
126
- };
127
- styles.bgColor.ansi16m = {
128
- rgb: wrapAnsi16m(rgb2rgb, 10)
129
- };
130
-
131
- for (let key of Object.keys(colorConvert)) {
132
- if (typeof colorConvert[key] !== 'object') {
133
- continue;
134
- }
135
-
136
- const suite = colorConvert[key];
137
-
138
- if (key === 'ansi16') {
139
- key = 'ansi';
140
- }
141
-
142
- if ('ansi16' in suite) {
143
- styles.color.ansi[key] = wrapAnsi16(suite.ansi16, 0);
144
- styles.bgColor.ansi[key] = wrapAnsi16(suite.ansi16, 10);
145
- }
146
-
147
- if ('ansi256' in suite) {
148
- styles.color.ansi256[key] = wrapAnsi256(suite.ansi256, 0);
149
- styles.bgColor.ansi256[key] = wrapAnsi256(suite.ansi256, 10);
150
- }
151
-
152
- if ('rgb' in suite) {
153
- styles.color.ansi16m[key] = wrapAnsi16m(suite.rgb, 0);
154
- styles.bgColor.ansi16m[key] = wrapAnsi16m(suite.rgb, 10);
155
- }
156
- }
149
+ setLazyProperty(styles.color, 'ansi', () => makeDynamicStyles(wrapAnsi16, 'ansi16', ansi2ansi, false));
150
+ setLazyProperty(styles.color, 'ansi256', () => makeDynamicStyles(wrapAnsi256, 'ansi256', ansi2ansi, false));
151
+ setLazyProperty(styles.color, 'ansi16m', () => makeDynamicStyles(wrapAnsi16m, 'rgb', rgb2rgb, false));
152
+ setLazyProperty(styles.bgColor, 'ansi', () => makeDynamicStyles(wrapAnsi16, 'ansi16', ansi2ansi, true));
153
+ setLazyProperty(styles.bgColor, 'ansi256', () => makeDynamicStyles(wrapAnsi256, 'ansi256', ansi2ansi, true));
154
+ setLazyProperty(styles.bgColor, 'ansi16m', () => makeDynamicStyles(wrapAnsi16m, 'rgb', rgb2rgb, true));
157
155
 
158
156
  return styles;
159
157
  }
package/package.json CHANGED
@@ -1,23 +1,25 @@
1
1
  {
2
2
  "name": "ansi-styles",
3
- "version": "3.2.1",
3
+ "version": "4.2.1",
4
4
  "description": "ANSI escape codes for styling strings in the terminal",
5
5
  "license": "MIT",
6
6
  "repository": "chalk/ansi-styles",
7
+ "funding": "https://github.com/chalk/ansi-styles?sponsor=1",
7
8
  "author": {
8
9
  "name": "Sindre Sorhus",
9
10
  "email": "sindresorhus@gmail.com",
10
11
  "url": "sindresorhus.com"
11
12
  },
12
13
  "engines": {
13
- "node": ">=4"
14
+ "node": ">=8"
14
15
  },
15
16
  "scripts": {
16
- "test": "xo && ava",
17
+ "test": "xo && ava && tsd",
17
18
  "screenshot": "svg-term --command='node screenshot' --out=screenshot.svg --padding=3 --width=55 --height=3 --at=1000 --no-cursor"
18
19
  },
19
20
  "files": [
20
- "index.js"
21
+ "index.js",
22
+ "index.d.ts"
21
23
  ],
22
24
  "keywords": [
23
25
  "ansi",
@@ -42,15 +44,14 @@
42
44
  "text"
43
45
  ],
44
46
  "dependencies": {
45
- "color-convert": "^1.9.0"
47
+ "@types/color-name": "^1.1.1",
48
+ "color-convert": "^2.0.1"
46
49
  },
47
50
  "devDependencies": {
48
- "ava": "*",
49
- "babel-polyfill": "^6.23.0",
51
+ "@types/color-convert": "^1.9.0",
52
+ "ava": "^2.3.0",
50
53
  "svg-term-cli": "^2.1.1",
51
- "xo": "*"
52
- },
53
- "ava": {
54
- "require": "babel-polyfill"
54
+ "tsd": "^0.11.0",
55
+ "xo": "^0.25.3"
55
56
  }
56
57
  }
package/readme.md CHANGED
@@ -1,11 +1,10 @@
1
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
- > [ANSI escape codes](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles) for styling strings in the terminal
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
- <img src="https://cdn.rawgit.com/chalk/ansi-styles/8261697c95bf34b6c7767e2cbe9941a851d59385/screenshot.svg" width="900">
8
-
7
+ <img src="screenshot.svg" width="900">
9
8
 
10
9
  ## Install
11
10
 
@@ -13,7 +12,6 @@ You probably want the higher-level [chalk](https://github.com/chalk/chalk) modul
13
12
  $ npm install ansi-styles
14
13
  ```
15
14
 
16
-
17
15
  ## Usage
18
16
 
19
17
  ```js
@@ -29,14 +27,13 @@ console.log(`${style.green.open}Hello world!${style.green.close}`);
29
27
  // original color.
30
28
  console.log(style.bgColor.ansi.hsl(120, 80, 72) + 'Hello world!' + style.bgColor.close);
31
29
  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);
30
+ console.log(style.color.ansi16m.hex('#abcdef') + 'Hello world!' + style.color.close);
33
31
  ```
34
32
 
35
33
  ## API
36
34
 
37
35
  Each style has an `open` and `close` property.
38
36
 
39
-
40
37
  ## Styles
41
38
 
42
39
  ### Modifiers
@@ -60,7 +57,7 @@ Each style has an `open` and `close` property.
60
57
  - `magenta`
61
58
  - `cyan`
62
59
  - `white`
63
- - `gray` ("bright black")
60
+ - `blackBright` (alias: `gray`, `grey`)
64
61
  - `redBright`
65
62
  - `greenBright`
66
63
  - `yellowBright`
@@ -79,7 +76,7 @@ Each style has an `open` and `close` property.
79
76
  - `bgMagenta`
80
77
  - `bgCyan`
81
78
  - `bgWhite`
82
- - `bgBlackBright`
79
+ - `bgBlackBright` (alias: `bgGray`, `bgGrey`)
83
80
  - `bgRedBright`
84
81
  - `bgGreenBright`
85
82
  - `bgYellowBright`
@@ -88,7 +85,6 @@ Each style has an `open` and `close` property.
88
85
  - `bgCyanBright`
89
86
  - `bgWhiteBright`
90
87
 
91
-
92
88
  ## Advanced usage
93
89
 
94
90
  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.
@@ -112,11 +108,21 @@ console.log(style.codes.get(36));
112
108
  //=> 39
113
109
  ```
114
110
 
115
-
116
111
  ## [256 / 16 million (TrueColor) support](https://gist.github.com/XVilka/8346728)
117
112
 
118
113
  `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
114
 
115
+ The following color spaces from `color-convert` are supported:
116
+
117
+ - `rgb`
118
+ - `hex`
119
+ - `keyword`
120
+ - `hsl`
121
+ - `hsv`
122
+ - `hwb`
123
+ - `ansi`
124
+ - `ansi256`
125
+
120
126
  To use these, call the associated conversion function with the intended output, for example:
121
127
 
122
128
  ```js
@@ -130,18 +136,23 @@ style.color.ansi16m.hex('#C0FFEE'); // Hex (RGB) to 16 million color foreground
130
136
  style.bgColor.ansi16m.hex('#C0FFEE'); // Hex (RGB) to 16 million color background code
131
137
  ```
132
138
 
133
-
134
139
  ## Related
135
140
 
136
141
  - [ansi-escapes](https://github.com/sindresorhus/ansi-escapes) - ANSI escape codes for manipulating the terminal
137
142
 
138
-
139
143
  ## Maintainers
140
144
 
141
145
  - [Sindre Sorhus](https://github.com/sindresorhus)
142
146
  - [Josh Junon](https://github.com/qix-)
143
147
 
144
-
145
- ## License
146
-
147
- MIT
148
+ ---
149
+
150
+ <div align="center">
151
+ <b>
152
+ <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>
153
+ </b>
154
+ <br>
155
+ <sub>
156
+ Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
157
+ </sub>
158
+ </div>