ansi-styles 6.1.0 → 6.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.d.ts +47 -1
  2. package/index.js +85 -78
  3. package/package.json +3 -3
  4. package/readme.md +21 -3
package/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export interface CSPair {
1
+ export interface CSPair { // eslint-disable-line @typescript-eslint/naming-convention
2
2
  /**
3
3
  The ANSI terminal control sequence for starting this style.
4
4
  */
@@ -180,6 +180,52 @@ export interface ConvertColor {
180
180
  hexToAnsi(hex: string): number;
181
181
  }
182
182
 
183
+ /**
184
+ Basic modifier names.
185
+ */
186
+ export type ModifierName = keyof Modifier;
187
+
188
+ /**
189
+ Basic foreground color names.
190
+
191
+ [More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
192
+ */
193
+ export type ForegroundColorName = keyof ForegroundColor;
194
+
195
+ /**
196
+ Basic background color names.
197
+
198
+ [More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
199
+ */
200
+ export type BackgroundColorName = keyof BackgroundColor;
201
+
202
+ /**
203
+ Basic color names. The combination of foreground and background color names.
204
+
205
+ [More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
206
+ */
207
+ export type ColorName = ForegroundColorName | BackgroundColorName;
208
+
209
+ /**
210
+ Basic modifier names.
211
+ */
212
+ export const modifierNames: readonly ModifierName[];
213
+
214
+ /**
215
+ Basic foreground color names.
216
+ */
217
+ export const foregroundColorNames: readonly ForegroundColorName[];
218
+
219
+ /**
220
+ Basic background color names.
221
+ */
222
+ export const backgroundColorNames: readonly BackgroundColorName[];
223
+
224
+ /*
225
+ Basic color names. The combination of foreground and background color names.
226
+ */
227
+ export const colorNames: readonly ColorName[];
228
+
183
229
  declare const ansiStyles: {
184
230
  readonly modifier: Modifier;
185
231
  readonly color: ColorBase & ForegroundColor;
package/index.js CHANGED
@@ -6,74 +6,73 @@ const wrapAnsi256 = (offset = 0) => code => `\u001B[${38 + offset};5;${code}m`;
6
6
 
7
7
  const wrapAnsi16m = (offset = 0) => (red, green, blue) => `\u001B[${38 + offset};2;${red};${green};${blue}m`;
8
8
 
9
+ const styles = {
10
+ modifier: {
11
+ reset: [0, 0],
12
+ // 21 isn't widely supported and 22 does the same thing
13
+ bold: [1, 22],
14
+ dim: [2, 22],
15
+ italic: [3, 23],
16
+ underline: [4, 24],
17
+ overline: [53, 55],
18
+ inverse: [7, 27],
19
+ hidden: [8, 28],
20
+ strikethrough: [9, 29],
21
+ },
22
+ color: {
23
+ black: [30, 39],
24
+ red: [31, 39],
25
+ green: [32, 39],
26
+ yellow: [33, 39],
27
+ blue: [34, 39],
28
+ magenta: [35, 39],
29
+ cyan: [36, 39],
30
+ white: [37, 39],
31
+
32
+ // Bright color
33
+ blackBright: [90, 39],
34
+ gray: [90, 39], // Alias of `blackBright`
35
+ grey: [90, 39], // Alias of `blackBright`
36
+ redBright: [91, 39],
37
+ greenBright: [92, 39],
38
+ yellowBright: [93, 39],
39
+ blueBright: [94, 39],
40
+ magentaBright: [95, 39],
41
+ cyanBright: [96, 39],
42
+ whiteBright: [97, 39],
43
+ },
44
+ bgColor: {
45
+ bgBlack: [40, 49],
46
+ bgRed: [41, 49],
47
+ bgGreen: [42, 49],
48
+ bgYellow: [43, 49],
49
+ bgBlue: [44, 49],
50
+ bgMagenta: [45, 49],
51
+ bgCyan: [46, 49],
52
+ bgWhite: [47, 49],
53
+
54
+ // Bright color
55
+ bgBlackBright: [100, 49],
56
+ bgGray: [100, 49], // Alias of `bgBlackBright`
57
+ bgGrey: [100, 49], // Alias of `bgBlackBright`
58
+ bgRedBright: [101, 49],
59
+ bgGreenBright: [102, 49],
60
+ bgYellowBright: [103, 49],
61
+ bgBlueBright: [104, 49],
62
+ bgMagentaBright: [105, 49],
63
+ bgCyanBright: [106, 49],
64
+ bgWhiteBright: [107, 49],
65
+ },
66
+ };
67
+
9
68
  function assembleStyles() {
10
69
  const codes = new Map();
11
- const styles = {
12
- modifier: {
13
- reset: [0, 0],
14
- // 21 isn't widely supported and 22 does the same thing
15
- bold: [1, 22],
16
- dim: [2, 22],
17
- italic: [3, 23],
18
- underline: [4, 24],
19
- overline: [53, 55],
20
- inverse: [7, 27],
21
- hidden: [8, 28],
22
- strikethrough: [9, 29]
23
- },
24
- color: {
25
- black: [30, 39],
26
- red: [31, 39],
27
- green: [32, 39],
28
- yellow: [33, 39],
29
- blue: [34, 39],
30
- magenta: [35, 39],
31
- cyan: [36, 39],
32
- white: [37, 39],
33
-
34
- // Bright color
35
- blackBright: [90, 39],
36
- redBright: [91, 39],
37
- greenBright: [92, 39],
38
- yellowBright: [93, 39],
39
- blueBright: [94, 39],
40
- magentaBright: [95, 39],
41
- cyanBright: [96, 39],
42
- whiteBright: [97, 39]
43
- },
44
- bgColor: {
45
- bgBlack: [40, 49],
46
- bgRed: [41, 49],
47
- bgGreen: [42, 49],
48
- bgYellow: [43, 49],
49
- bgBlue: [44, 49],
50
- bgMagenta: [45, 49],
51
- bgCyan: [46, 49],
52
- bgWhite: [47, 49],
53
-
54
- // Bright color
55
- bgBlackBright: [100, 49],
56
- bgRedBright: [101, 49],
57
- bgGreenBright: [102, 49],
58
- bgYellowBright: [103, 49],
59
- bgBlueBright: [104, 49],
60
- bgMagentaBright: [105, 49],
61
- bgCyanBright: [106, 49],
62
- bgWhiteBright: [107, 49]
63
- }
64
- };
65
-
66
- // Alias bright black as gray (and grey)
67
- styles.color.gray = styles.color.blackBright;
68
- styles.bgColor.bgGray = styles.bgColor.bgBlackBright;
69
- styles.color.grey = styles.color.blackBright;
70
- styles.bgColor.bgGrey = styles.bgColor.bgBlackBright;
71
70
 
72
71
  for (const [groupName, group] of Object.entries(styles)) {
73
72
  for (const [styleName, style] of Object.entries(group)) {
74
73
  styles[styleName] = {
75
74
  open: `\u001B[${style[0]}m`,
76
- close: `\u001B[${style[1]}m`
75
+ close: `\u001B[${style[1]}m`,
77
76
  };
78
77
 
79
78
  group[styleName] = styles[styleName];
@@ -83,13 +82,13 @@ function assembleStyles() {
83
82
 
84
83
  Object.defineProperty(styles, groupName, {
85
84
  value: group,
86
- enumerable: false
85
+ enumerable: false,
87
86
  });
88
87
  }
89
88
 
90
89
  Object.defineProperty(styles, 'codes', {
91
90
  value: codes,
92
- enumerable: false
91
+ enumerable: false,
93
92
  });
94
93
 
95
94
  styles.color.close = '\u001B[39m';
@@ -120,39 +119,41 @@ function assembleStyles() {
120
119
  return Math.round(((red - 8) / 247) * 24) + 232;
121
120
  }
122
121
 
123
- return 16 +
124
- (36 * Math.round(red / 255 * 5)) +
125
- (6 * Math.round(green / 255 * 5)) +
126
- Math.round(blue / 255 * 5);
122
+ return 16
123
+ + (36 * Math.round(red / 255 * 5))
124
+ + (6 * Math.round(green / 255 * 5))
125
+ + Math.round(blue / 255 * 5);
127
126
  },
128
- enumerable: false
127
+ enumerable: false,
129
128
  },
130
129
  hexToRgb: {
131
130
  value: hex => {
132
- const matches = /(?<colorString>[a-f\d]{6}|[a-f\d]{3})/i.exec(hex.toString(16));
131
+ const matches = /[a-f\d]{6}|[a-f\d]{3}/i.exec(hex.toString(16));
133
132
  if (!matches) {
134
133
  return [0, 0, 0];
135
134
  }
136
135
 
137
- let {colorString} = matches.groups;
136
+ let [colorString] = matches;
138
137
 
139
138
  if (colorString.length === 3) {
140
- colorString = colorString.split('').map(character => character + character).join('');
139
+ colorString = [...colorString].map(character => character + character).join('');
141
140
  }
142
141
 
143
142
  const integer = Number.parseInt(colorString, 16);
144
143
 
145
144
  return [
145
+ /* eslint-disable no-bitwise */
146
146
  (integer >> 16) & 0xFF,
147
147
  (integer >> 8) & 0xFF,
148
- integer & 0xFF
148
+ integer & 0xFF,
149
+ /* eslint-enable no-bitwise */
149
150
  ];
150
151
  },
151
- enumerable: false
152
+ enumerable: false,
152
153
  },
153
154
  hexToAnsi256: {
154
155
  value: hex => styles.rgbToAnsi256(...styles.hexToRgb(hex)),
155
- enumerable: false
156
+ enumerable: false,
156
157
  },
157
158
  ansi256ToAnsi: {
158
159
  value: code => {
@@ -188,6 +189,7 @@ function assembleStyles() {
188
189
  return 30;
189
190
  }
190
191
 
192
+ // eslint-disable-next-line no-bitwise
191
193
  let result = 30 + ((Math.round(blue) << 2) | (Math.round(green) << 1) | Math.round(red));
192
194
 
193
195
  if (value === 2) {
@@ -196,16 +198,16 @@ function assembleStyles() {
196
198
 
197
199
  return result;
198
200
  },
199
- enumerable: false
201
+ enumerable: false,
200
202
  },
201
203
  rgbToAnsi: {
202
204
  value: (red, green, blue) => styles.ansi256ToAnsi(styles.rgbToAnsi256(red, green, blue)),
203
- enumerable: false
205
+ enumerable: false,
204
206
  },
205
207
  hexToAnsi: {
206
208
  value: hex => styles.ansi256ToAnsi(styles.hexToAnsi256(hex)),
207
- enumerable: false
208
- }
209
+ enumerable: false,
210
+ },
209
211
  });
210
212
 
211
213
  return styles;
@@ -214,3 +216,8 @@ function assembleStyles() {
214
216
  const ansiStyles = assembleStyles();
215
217
 
216
218
  export default ansiStyles;
219
+
220
+ export const modifierNames = Object.keys(styles.modifier);
221
+ export const foregroundColorNames = Object.keys(styles.color);
222
+ export const backgroundColorNames = Object.keys(styles.bgColor);
223
+ export const colorNames = [...foregroundColorNames, ...backgroundColorNames];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ansi-styles",
3
- "version": "6.1.0",
3
+ "version": "6.2.0",
4
4
  "description": "ANSI escape codes for styling strings in the terminal",
5
5
  "license": "MIT",
6
6
  "repository": "chalk/ansi-styles",
@@ -48,7 +48,7 @@
48
48
  "devDependencies": {
49
49
  "ava": "^3.15.0",
50
50
  "svg-term-cli": "^2.1.1",
51
- "tsd": "^0.14.0",
52
- "xo": "^0.38.2"
51
+ "tsd": "^0.19.0",
52
+ "xo": "^0.47.0"
53
53
  }
54
54
  }
package/readme.md CHANGED
@@ -4,12 +4,12 @@
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="screenshot.svg" width="900">
7
+ ![](screenshot.png)
8
8
 
9
9
  ## Install
10
10
 
11
- ```
12
- $ npm install ansi-styles
11
+ ```sh
12
+ npm install ansi-styles
13
13
  ```
14
14
 
15
15
  ## Usage
@@ -32,8 +32,26 @@ console.log(`${styles.color.ansi16m(...styles.hexToRgb('#abcdef'))}Hello World${
32
32
 
33
33
  ## API
34
34
 
35
+ ### `open` and `close`
36
+
35
37
  Each style has an `open` and `close` property.
36
38
 
39
+ ### `modifierNames`, `foregroundColorNames`, `backgroundColorNames`, and `colorNames`
40
+
41
+ All supported style strings are exposed as an array of strings for convenience. `colorNames` is the combination of `foregroundColorNames` and `backgroundColorNames`.
42
+
43
+ This can be useful if you need to validate input:
44
+
45
+ ```js
46
+ import {modifierNames, foregroundColorNames} from 'ansi-styles';
47
+
48
+ console.log(modifierNames.includes('bold'));
49
+ //=> true
50
+
51
+ console.log(foregroundColorNames.includes('pink'));
52
+ //=> false
53
+ ```
54
+
37
55
  ## Styles
38
56
 
39
57
  ### Modifiers