ansi-styles 4.2.0 → 5.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.
Files changed (4) hide show
  1. package/index.d.ts +37 -67
  2. package/index.js +53 -61
  3. package/package.json +7 -11
  4. package/readme.md +12 -34
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
- }
45
-
46
- const offset = isBackground ? 10 : 0;
47
- const styles = {};
3
+ const ANSI_BACKGROUND_OFFSET = 10;
48
4
 
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
- }
5
+ const wrapAnsi256 = (offset = 0) => code => `\u001B[${38 + offset};5;${code}m`;
57
6
 
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,55 @@ 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
+ styles.rgbToAnsi256 = (red, green, blue) => {
105
+ // We use the extended greyscale palette here, with the exception of
106
+ // black and white. normal palette only has 4 greyscale shades.
107
+ if (red === green && green === blue) {
108
+ if (red < 8) {
109
+ return 16;
110
+ }
111
+
112
+ if (red > 248) {
113
+ return 231;
114
+ }
115
+
116
+ return Math.round(((red - 8) / 247) * 24) + 232;
117
+ }
118
+
119
+ return 16 +
120
+ (36 * Math.round(red / 255 * 5)) +
121
+ (6 * Math.round(green / 255 * 5)) +
122
+ Math.round(blue / 255 * 5);
123
+ };
124
+
125
+ styles.hexToRgb = hex => {
126
+ const matches = /(?<colorString>[a-f\d]{6}|[a-f\d]{3})/i.exec(hex.toString(16));
127
+ if (!matches) {
128
+ return [0, 0, 0];
129
+ }
130
+
131
+ let {colorString} = matches.groups;
132
+
133
+ if (colorString.length === 3) {
134
+ colorString = colorString.split('').map(character => character + character).join('');
135
+ }
136
+
137
+ const integer = Number.parseInt(colorString, 16);
138
+
139
+ return [
140
+ (integer >> 16) & 0xFF,
141
+ (integer >> 8) & 0xFF,
142
+ integer & 0xFF
143
+ ];
144
+ };
145
+
146
+ styles.hexToAnsi256 = hex => styles.rgbToAnsi256(...styles.hexToRgb(hex));
155
147
 
156
148
  return styles;
157
149
  }
package/package.json CHANGED
@@ -1,16 +1,17 @@
1
1
  {
2
2
  "name": "ansi-styles",
3
- "version": "4.2.0",
3
+ "version": "5.1.0",
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
- "url": "sindresorhus.com"
11
+ "url": "https://sindresorhus.com"
11
12
  },
12
13
  "engines": {
13
- "node": ">=8"
14
+ "node": ">=10"
14
15
  },
15
16
  "scripts": {
16
17
  "test": "xo && ava && tsd",
@@ -42,15 +43,10 @@
42
43
  "command-line",
43
44
  "text"
44
45
  ],
45
- "dependencies": {
46
- "@types/color-name": "^1.1.1",
47
- "color-convert": "^2.0.1"
48
- },
49
46
  "devDependencies": {
50
- "@types/color-convert": "^1.9.0",
51
- "ava": "^2.3.0",
47
+ "ava": "^2.4.0",
52
48
  "svg-term-cli": "^2.1.1",
53
- "tsd": "^0.10.0",
54
- "xo": "^0.25.3"
49
+ "tsd": "^0.14.0",
50
+ "xo": "^0.37.1"
55
51
  }
56
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
 
@@ -6,14 +6,12 @@ You probably want the higher-level [chalk](https://github.com/chalk/chalk) modul
6
6
 
7
7
  <img src="screenshot.svg" width="900">
8
8
 
9
-
10
9
  ## Install
11
10
 
12
11
  ```
13
12
  $ npm install ansi-styles
14
13
  ```
15
14
 
16
-
17
15
  ## Usage
18
16
 
19
17
  ```js
@@ -27,16 +25,14 @@ console.log(`${style.green.open}Hello world!${style.green.close}`);
27
25
  // may be degraded to fit that color palette. This means terminals
28
26
  // that do not support 16 million colors will best-match the
29
27
  // 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);
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}`)
33
30
  ```
34
31
 
35
32
  ## API
36
33
 
37
34
  Each style has an `open` and `close` property.
38
35
 
39
-
40
36
  ## Styles
41
37
 
42
38
  ### Modifiers
@@ -46,6 +42,7 @@ Each style has an `open` and `close` property.
46
42
  - `dim`
47
43
  - `italic` *(Not widely supported)*
48
44
  - `underline`
45
+ - `overline` *Supported on VTE-based terminals, the GNOME terminal, mintty, and Git Bash.*
49
46
  - `inverse`
50
47
  - `hidden`
51
48
  - `strikethrough` *(Not widely supported)*
@@ -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,55 +108,37 @@ 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
- `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.
119
114
 
120
115
  The following color spaces from `color-convert` are supported:
121
116
 
122
117
  - `rgb`
123
118
  - `hex`
124
- - `keyword`
125
- - `hsl`
126
- - `hsv`
127
- - `hwb`
128
- - `ansi`
129
119
  - `ansi256`
130
120
 
131
121
  To use these, call the associated conversion function with the intended output, for example:
132
122
 
133
123
  ```js
134
- style.color.ansi.rgb(100, 200, 15); // RGB to 16 color ansi foreground code
135
- style.bgColor.ansi.rgb(100, 200, 15); // RGB to 16 color ansi background code
136
-
137
- style.color.ansi256.hsl(120, 100, 60); // HSL to 256 color ansi foreground code
138
- style.bgColor.ansi256.hsl(120, 100, 60); // HSL to 256 color ansi foreground 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
139
126
 
140
- style.color.ansi16m.hex('#C0FFEE'); // Hex (RGB) to 16 million color foreground code
141
- 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
142
129
  ```
143
130
 
144
-
145
131
  ## Related
146
132
 
147
133
  - [ansi-escapes](https://github.com/sindresorhus/ansi-escapes) - ANSI escape codes for manipulating the terminal
148
134
 
149
-
150
135
  ## Maintainers
151
136
 
152
137
  - [Sindre Sorhus](https://github.com/sindresorhus)
153
138
  - [Josh Junon](https://github.com/qix-)
154
139
 
140
+ ## For enterprise
155
141
 
156
- ---
142
+ Available as part of the Tidelift Subscription.
157
143
 
158
- <div align="center">
159
- <b>
160
- <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>
161
- </b>
162
- <br>
163
- <sub>
164
- Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
165
- </sub>
166
- </div>
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)