3 const wrapAnsi16 = (fn, offset) => (...args) => {
4 const code = fn(...args);
5 return `\u001B[${code + offset}m`;
8 const wrapAnsi256 = (fn, offset) => (...args) => {
9 const code = fn(...args);
10 return `\u001B[${38 + offset};5;${code}m`;
13 const wrapAnsi16m = (fn, offset) => (...args) => {
14 const rgb = fn(...args);
15 return `\u001B[${38 + offset};2;${rgb[0]};${rgb[1]};${rgb[2]}m`;
18 const ansi2ansi = n => n;
19 const rgb2rgb = (r, g, b) => [r, g, b];
21 const setLazyProperty = (object, property, get) => {
22 Object.defineProperty(object, property, {
26 Object.defineProperty(object, property, {
39 /** @type {typeof import('color-convert')} */
41 const makeDynamicStyles = (wrap, targetSpace, identity, isBackground) => {
42 if (colorConvert === undefined) {
43 colorConvert = require('color-convert');
46 const offset = isBackground ? 10 : 0;
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);
61 function assembleStyles() {
62 const codes = new Map();
66 // 21 isn't widely supported and 22 does the same thing
73 strikethrough: [9, 29]
86 blackBright: [90, 39],
88 greenBright: [92, 39],
89 yellowBright: [93, 39],
91 magentaBright: [95, 39],
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]
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;
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`
130 group[styleName] = styles[styleName];
132 codes.set(style[0], style[1]);
135 Object.defineProperty(styles, groupName, {
141 Object.defineProperty(styles, 'codes', {
146 styles.color.close = '\u001B[39m';
147 styles.bgColor.close = '\u001B[49m';
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));
159 // Make the export immutable
160 Object.defineProperty(module, 'exports', {