ansi-styles 4.2.1 → 5.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 +37 -67
  2. package/index.js +62 -61
  3. package/package.json +6 -11
  4. package/readme.md +17 -31
package/index.d.ts CHANGED
@@ -1,66 +1,4 @@
1
- import * as cssColors from 'color-name';
2
-
3
1
  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
2
  interface CSPair {
65
3
  /**
66
4
  The ANSI terminal control sequence for starting this style.
@@ -74,14 +12,14 @@ declare namespace ansiStyles {
74
12
  }
75
13
 
76
14
  interface ColorBase {
77
- readonly ansi: ColorConvert;
78
- readonly ansi256: ColorConvert;
79
- readonly ansi16m: ColorConvert;
80
-
81
15
  /**
82
16
  The ANSI terminal control sequence for ending this color.
83
17
  */
84
18
  readonly close: string;
19
+
20
+ ansi256(code: number): string;
21
+
22
+ ansi16m(red: number, green: number, blue: number): string;
85
23
  }
86
24
 
87
25
  interface Modifier {
@@ -110,6 +48,13 @@ declare namespace ansiStyles {
110
48
  */
111
49
  readonly underline: CSPair;
112
50
 
51
+ /**
52
+ Make text overline.
53
+
54
+ Supported on VTE-based terminals, the GNOME terminal, mintty, and Git Bash.
55
+ */
56
+ readonly overline: CSPair;
57
+
113
58
  /**
114
59
  Inverse background and foreground colors.
115
60
  */
@@ -185,6 +130,31 @@ declare namespace ansiStyles {
185
130
  readonly bgMagentaBright: CSPair;
186
131
  readonly bgWhiteBright: CSPair;
187
132
  }
133
+
134
+ interface ConvertColor {
135
+ /**
136
+ Convert from the RGB color space to the ANSI 256 color space.
137
+
138
+ @param red - (`0...255`)
139
+ @param green - (`0...255`)
140
+ @param blue - (`0...255`)
141
+ */
142
+ rgbToAnsi256(red: number, green: number, blue: number): number;
143
+
144
+ /**
145
+ Convert from the RGB HEX color space to the RGB color space.
146
+
147
+ @param hex - A hexadecimal string containing RGB data.
148
+ */
149
+ hexToRgb(hex: string): [red: number, green: number, blue: number];
150
+
151
+ /**
152
+ Convert from the RGB HEX color space to the ANSI 256 color space.
153
+
154
+ @param hex - A hexadecimal string containing RGB data.
155
+ */
156
+ hexToAnsi256(hex: string): number;
157
+ }
188
158
  }
189
159
 
190
160
  declare const ansiStyles: {
@@ -192,6 +162,6 @@ declare const ansiStyles: {
192
162
  readonly color: ansiStyles.ForegroundColor & ansiStyles.ColorBase;
193
163
  readonly bgColor: ansiStyles.BackgroundColor & ansiStyles.ColorBase;
194
164
  readonly codes: ReadonlyMap<number, number>;
195
- } & ansiStyles.BackgroundColor & ansiStyles.ForegroundColor & ansiStyles.Modifier;
165
+ } & ansiStyles.BackgroundColor & ansiStyles.ForegroundColor & ansiStyles.Modifier & ansiStyles.ConvertColor;
196
166
 
197
167
  export = ansiStyles;
package/index.js CHANGED
@@ -1,62 +1,10 @@
1
1
  'use strict';
2
2
 
3
- const wrapAnsi16 = (fn, offset) => (...args) => {
4
- const code = fn(...args);
5
- return `\u001B[${code + offset}m`;
6
- };
7
-
8
- const wrapAnsi256 = (fn, offset) => (...args) => {
9
- const code = fn(...args);
10
- return `\u001B[${38 + offset};5;${code}m`;
11
- };
12
-
13
- const wrapAnsi16m = (fn, offset) => (...args) => {
14
- const rgb = fn(...args);
15
- return `\u001B[${38 + offset};2;${rgb[0]};${rgb[1]};${rgb[2]}m`;
16
- };
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
- }
3
+ const ANSI_BACKGROUND_OFFSET = 10;
45
4
 
46
- const offset = isBackground ? 10 : 0;
47
- const styles = {};
5
+ const wrapAnsi256 = (offset = 0) => code => `\u001B[${38 + offset};5;${code}m`;
48
6
 
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
- };
7
+ const wrapAnsi16m = (offset = 0) => (red, green, blue) => `\u001B[${38 + offset};2;${red};${green};${blue}m`;
60
8
 
61
9
  function assembleStyles() {
62
10
  const codes = new Map();
@@ -68,6 +16,7 @@ function assembleStyles() {
68
16
  dim: [2, 22],
69
17
  italic: [3, 23],
70
18
  underline: [4, 24],
19
+ overline: [53, 55],
71
20
  inverse: [7, 27],
72
21
  hidden: [8, 28],
73
22
  strikethrough: [9, 29]
@@ -146,12 +95,64 @@ function assembleStyles() {
146
95
  styles.color.close = '\u001B[39m';
147
96
  styles.bgColor.close = '\u001B[49m';
148
97
 
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));
98
+ styles.color.ansi256 = wrapAnsi256();
99
+ styles.color.ansi16m = wrapAnsi16m();
100
+ styles.bgColor.ansi256 = wrapAnsi256(ANSI_BACKGROUND_OFFSET);
101
+ styles.bgColor.ansi16m = wrapAnsi16m(ANSI_BACKGROUND_OFFSET);
102
+
103
+ // From https://github.com/Qix-/color-convert/blob/3f0e0d4e92e235796ccb17f6e85c72094a651f49/conversions.js
104
+ Object.defineProperties(styles, {
105
+ rgbToAnsi256: {
106
+ value: (red, green, blue) => {
107
+ // We use the extended greyscale palette here, with the exception of
108
+ // black and white. normal palette only has 4 greyscale shades.
109
+ if (red === green && green === blue) {
110
+ if (red < 8) {
111
+ return 16;
112
+ }
113
+
114
+ if (red > 248) {
115
+ return 231;
116
+ }
117
+
118
+ return Math.round(((red - 8) / 247) * 24) + 232;
119
+ }
120
+
121
+ return 16 +
122
+ (36 * Math.round(red / 255 * 5)) +
123
+ (6 * Math.round(green / 255 * 5)) +
124
+ Math.round(blue / 255 * 5);
125
+ },
126
+ enumerable: false
127
+ },
128
+ hexToRgb: {
129
+ value: hex => {
130
+ const matches = /(?<colorString>[a-f\d]{6}|[a-f\d]{3})/i.exec(hex.toString(16));
131
+ if (!matches) {
132
+ return [0, 0, 0];
133
+ }
134
+
135
+ let {colorString} = matches.groups;
136
+
137
+ if (colorString.length === 3) {
138
+ colorString = colorString.split('').map(character => character + character).join('');
139
+ }
140
+
141
+ const integer = Number.parseInt(colorString, 16);
142
+
143
+ return [
144
+ (integer >> 16) & 0xFF,
145
+ (integer >> 8) & 0xFF,
146
+ integer & 0xFF
147
+ ];
148
+ },
149
+ enumerable: false
150
+ },
151
+ hexToAnsi256: {
152
+ value: hex => styles.rgbToAnsi256(...styles.hexToRgb(hex)),
153
+ enumerable: false
154
+ }
155
+ });
155
156
 
156
157
  return styles;
157
158
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ansi-styles",
3
- "version": "4.2.1",
3
+ "version": "5.2.0",
4
4
  "description": "ANSI escape codes for styling strings in the terminal",
5
5
  "license": "MIT",
6
6
  "repository": "chalk/ansi-styles",
@@ -8,10 +8,10 @@
8
8
  "author": {
9
9
  "name": "Sindre Sorhus",
10
10
  "email": "sindresorhus@gmail.com",
11
- "url": "sindresorhus.com"
11
+ "url": "https://sindresorhus.com"
12
12
  },
13
13
  "engines": {
14
- "node": ">=8"
14
+ "node": ">=10"
15
15
  },
16
16
  "scripts": {
17
17
  "test": "xo && ava && tsd",
@@ -43,15 +43,10 @@
43
43
  "command-line",
44
44
  "text"
45
45
  ],
46
- "dependencies": {
47
- "@types/color-name": "^1.1.1",
48
- "color-convert": "^2.0.1"
49
- },
50
46
  "devDependencies": {
51
- "@types/color-convert": "^1.9.0",
52
- "ava": "^2.3.0",
47
+ "ava": "^2.4.0",
53
48
  "svg-term-cli": "^2.1.1",
54
- "tsd": "^0.11.0",
55
- "xo": "^0.25.3"
49
+ "tsd": "^0.14.0",
50
+ "xo": "^0.37.1"
56
51
  }
57
52
  }
package/readme.md CHANGED
@@ -1,4 +1,4 @@
1
- # ansi-styles [![Build Status](https://travis-ci.org/chalk/ansi-styles.svg?branch=master)](https://travis-ci.org/chalk/ansi-styles)
1
+ # ansi-styles
2
2
 
3
3
  > [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles) for styling strings in the terminal
4
4
 
@@ -20,14 +20,13 @@ const style = require('ansi-styles');
20
20
  console.log(`${style.green.open}Hello world!${style.green.close}`);
21
21
 
22
22
 
23
- // Color conversion between 16/256/truecolor
24
- // NOTE: If conversion goes to 16 colors or 256 colors, the original color
25
- // may be degraded to fit that color palette. This means terminals
23
+ // Color conversion between 256/truecolor
24
+ // NOTE: When converting from truecolor to 256 colors, the original color
25
+ // may be degraded to fit the new color palette. This means terminals
26
26
  // that do not support 16 million colors will best-match the
27
27
  // original color.
28
- console.log(style.bgColor.ansi.hsl(120, 80, 72) + 'Hello world!' + style.bgColor.close);
29
- console.log(style.color.ansi256.rgb(199, 20, 250) + 'Hello world!' + style.color.close);
30
- console.log(style.color.ansi16m.hex('#abcdef') + 'Hello world!' + style.color.close);
28
+ console.log(`${style.color.ansi256(style.rgbToAnsi256(199, 20, 250))}Hello World${style.color.close}`)
29
+ console.log(`${style.color.ansi16m(...style.hexToRgb('#abcdef'))}Hello World${style.color.close}`)
31
30
  ```
32
31
 
33
32
  ## API
@@ -43,6 +42,7 @@ Each style has an `open` and `close` property.
43
42
  - `dim`
44
43
  - `italic` *(Not widely supported)*
45
44
  - `underline`
45
+ - `overline` *Supported on VTE-based terminals, the GNOME terminal, mintty, and Git Bash.*
46
46
  - `inverse`
47
47
  - `hidden`
48
48
  - `strikethrough` *(Not widely supported)*
@@ -110,30 +110,22 @@ console.log(style.codes.get(36));
110
110
 
111
111
  ## [256 / 16 million (TrueColor) support](https://gist.github.com/XVilka/8346728)
112
112
 
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.
113
+ `ansi-styles` allows converting between various color formats and ANSI escapes, with support for 256 and 16 million colors.
114
114
 
115
115
  The following color spaces from `color-convert` are supported:
116
116
 
117
117
  - `rgb`
118
118
  - `hex`
119
- - `keyword`
120
- - `hsl`
121
- - `hsv`
122
- - `hwb`
123
- - `ansi`
124
119
  - `ansi256`
125
120
 
126
121
  To use these, call the associated conversion function with the intended output, for example:
127
122
 
128
123
  ```js
129
- style.color.ansi.rgb(100, 200, 15); // RGB to 16 color ansi foreground code
130
- style.bgColor.ansi.rgb(100, 200, 15); // RGB to 16 color ansi background code
124
+ style.color.ansi256(style.rgbToAnsi256(100, 200, 15)); // RGB to 256 color ansi foreground code
125
+ style.bgColor.ansi256(style.hexToAnsi256('#C0FFEE')); // HEX to 256 color ansi foreground code
131
126
 
132
- style.color.ansi256.hsl(120, 100, 60); // HSL to 256 color ansi foreground code
133
- style.bgColor.ansi256.hsl(120, 100, 60); // HSL to 256 color ansi foreground code
134
-
135
- style.color.ansi16m.hex('#C0FFEE'); // Hex (RGB) to 16 million color foreground code
136
- style.bgColor.ansi16m.hex('#C0FFEE'); // Hex (RGB) to 16 million color background code
127
+ style.color.ansi16m(100, 200, 15); // RGB to 16 million color foreground code
128
+ style.bgColor.ansi16m(...style.hexToRgb('#C0FFEE')); // Hex (RGB) to 16 million color foreground code
137
129
  ```
138
130
 
139
131
  ## Related
@@ -145,14 +137,8 @@ style.bgColor.ansi16m.hex('#C0FFEE'); // Hex (RGB) to 16 million color backgroun
145
137
  - [Sindre Sorhus](https://github.com/sindresorhus)
146
138
  - [Josh Junon](https://github.com/qix-)
147
139
 
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>
140
+ ## For enterprise
141
+
142
+ Available as part of the Tidelift Subscription.
143
+
144
+ The maintainers of `ansi-styles` and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-ansi-styles?utm_source=npm-ansi-styles&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)