ansi-styles 4.0.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.
Files changed (3) hide show
  1. package/index.js +48 -48
  2. package/package.json +3 -3
  3. package/readme.md +2 -2
package/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  'use strict';
2
- const colorConvert = require('color-convert');
3
2
 
4
3
  const wrapAnsi16 = (fn, offset) => (...args) => {
5
4
  const code = fn(...args);
@@ -16,6 +15,48 @@ const wrapAnsi16m = (fn, offset) => (...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() {
20
61
  const codes = new Map();
21
62
  const styles = {
@@ -101,56 +142,15 @@ function assembleStyles() {
101
142
  });
102
143
  }
103
144
 
104
- const ansi2ansi = n => n;
105
- const rgb2rgb = (r, g, b) => [r, g, b];
106
-
107
145
  styles.color.close = '\u001B[39m';
108
146
  styles.bgColor.close = '\u001B[49m';
109
147
 
110
- styles.color.ansi = {
111
- ansi: wrapAnsi16(ansi2ansi, 0)
112
- };
113
- styles.color.ansi256 = {
114
- ansi256: wrapAnsi256(ansi2ansi, 0)
115
- };
116
- styles.color.ansi16m = {
117
- rgb: wrapAnsi16m(rgb2rgb, 0)
118
- };
119
-
120
- styles.bgColor.ansi = {
121
- ansi: wrapAnsi16(ansi2ansi, 10)
122
- };
123
- styles.bgColor.ansi256 = {
124
- ansi256: wrapAnsi256(ansi2ansi, 10)
125
- };
126
- styles.bgColor.ansi16m = {
127
- rgb: wrapAnsi16m(rgb2rgb, 10)
128
- };
129
-
130
- for (let [key, suite] of Object.entries(colorConvert)) {
131
- if (typeof suite !== 'object') {
132
- continue;
133
- }
134
-
135
- if (key === 'ansi16') {
136
- key = 'ansi';
137
- }
138
-
139
- if ('ansi16' in suite) {
140
- styles.color.ansi[key] = wrapAnsi16(suite.ansi16, 0);
141
- styles.bgColor.ansi[key] = wrapAnsi16(suite.ansi16, 10);
142
- }
143
-
144
- if ('ansi256' in suite) {
145
- styles.color.ansi256[key] = wrapAnsi256(suite.ansi256, 0);
146
- styles.bgColor.ansi256[key] = wrapAnsi256(suite.ansi256, 10);
147
- }
148
-
149
- if ('rgb' in suite) {
150
- styles.color.ansi16m[key] = wrapAnsi16m(suite.rgb, 0);
151
- styles.bgColor.ansi16m[key] = wrapAnsi16m(suite.rgb, 10);
152
- }
153
- }
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));
154
154
 
155
155
  return styles;
156
156
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ansi-styles",
3
- "version": "4.0.0",
3
+ "version": "4.1.0",
4
4
  "description": "ANSI escape codes for styling strings in the terminal",
5
5
  "license": "MIT",
6
6
  "repository": "chalk/ansi-styles",
@@ -42,10 +42,10 @@
42
42
  "text"
43
43
  ],
44
44
  "dependencies": {
45
- "color-convert": "^2.0.0"
45
+ "color-convert": "^2.0.1"
46
46
  },
47
47
  "devDependencies": {
48
- "ava": "^1.4.1",
48
+ "ava": "^2.3.0",
49
49
  "svg-term-cli": "^2.1.1",
50
50
  "xo": "^0.24.0"
51
51
  }
package/readme.md CHANGED
@@ -1,6 +1,6 @@
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
 
@@ -29,7 +29,7 @@ console.log(`${style.green.open}Hello world!${style.green.close}`);
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('#ABCDEF') + 'Hello world!' + style.color.close);
32
+ console.log(style.color.ansi16m.hex('#abcdef') + 'Hello world!' + style.color.close);
33
33
  ```
34
34
 
35
35
  ## API