source: trip-planner-front/node_modules/wrap-ansi/node_modules/ansi-styles/index.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.0 KB
Line 
1'use strict';
2
3const wrapAnsi16 = (fn, offset) => (...args) => {
4 const code = fn(...args);
5 return `\u001B[${code + offset}m`;
6};
7
8const wrapAnsi256 = (fn, offset) => (...args) => {
9 const code = fn(...args);
10 return `\u001B[${38 + offset};5;${code}m`;
11};
12
13const wrapAnsi16m = (fn, offset) => (...args) => {
14 const rgb = fn(...args);
15 return `\u001B[${38 + offset};2;${rgb[0]};${rgb[1]};${rgb[2]}m`;
16};
17
18const ansi2ansi = n => n;
19const rgb2rgb = (r, g, b) => [r, g, b];
20
21const 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')} */
40let colorConvert;
41const 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
61function assembleStyles() {
62 const codes = new Map();
63 const styles = {
64 modifier: {
65 reset: [0, 0],
66 // 21 isn't widely supported and 22 does the same thing
67 bold: [1, 22],
68 dim: [2, 22],
69 italic: [3, 23],
70 underline: [4, 24],
71 inverse: [7, 27],
72 hidden: [8, 28],
73 strikethrough: [9, 29]
74 },
75 color: {
76 black: [30, 39],
77 red: [31, 39],
78 green: [32, 39],
79 yellow: [33, 39],
80 blue: [34, 39],
81 magenta: [35, 39],
82 cyan: [36, 39],
83 white: [37, 39],
84
85 // Bright color
86 blackBright: [90, 39],
87 redBright: [91, 39],
88 greenBright: [92, 39],
89 yellowBright: [93, 39],
90 blueBright: [94, 39],
91 magentaBright: [95, 39],
92 cyanBright: [96, 39],
93 whiteBright: [97, 39]
94 },
95 bgColor: {
96 bgBlack: [40, 49],
97 bgRed: [41, 49],
98 bgGreen: [42, 49],
99 bgYellow: [43, 49],
100 bgBlue: [44, 49],
101 bgMagenta: [45, 49],
102 bgCyan: [46, 49],
103 bgWhite: [47, 49],
104
105 // Bright color
106 bgBlackBright: [100, 49],
107 bgRedBright: [101, 49],
108 bgGreenBright: [102, 49],
109 bgYellowBright: [103, 49],
110 bgBlueBright: [104, 49],
111 bgMagentaBright: [105, 49],
112 bgCyanBright: [106, 49],
113 bgWhiteBright: [107, 49]
114 }
115 };
116
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;
122
123 for (const [groupName, group] of Object.entries(styles)) {
124 for (const [styleName, style] of Object.entries(group)) {
125 styles[styleName] = {
126 open: `\u001B[${style[0]}m`,
127 close: `\u001B[${style[1]}m`
128 };
129
130 group[styleName] = styles[styleName];
131
132 codes.set(style[0], style[1]);
133 }
134
135 Object.defineProperty(styles, groupName, {
136 value: group,
137 enumerable: false
138 });
139 }
140
141 Object.defineProperty(styles, 'codes', {
142 value: codes,
143 enumerable: false
144 });
145
146 styles.color.close = '\u001B[39m';
147 styles.bgColor.close = '\u001B[49m';
148
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));
155
156 return styles;
157}
158
159// Make the export immutable
160Object.defineProperty(module, 'exports', {
161 enumerable: true,
162 get: assembleStyles
163});
Note: See TracBrowser for help on using the repository browser.