|
| 1 | +'use strict'; |
| 2 | +const colorConvert = require('color-convert'); |
| 3 | + |
| 4 | +const wrapAnsi16 = (fn, offset) => function () { |
| 5 | + const code = fn.apply(colorConvert, arguments); |
| 6 | + return `\u001B[${code + offset}m`; |
| 7 | +}; |
| 8 | + |
| 9 | +const wrapAnsi256 = (fn, offset) => function () { |
| 10 | + const code = fn.apply(colorConvert, arguments); |
| 11 | + return `\u001B[${38 + offset};5;${code}m`; |
| 12 | +}; |
| 13 | + |
| 14 | +const wrapAnsi16m = (fn, offset) => function () { |
| 15 | + const rgb = fn.apply(colorConvert, arguments); |
| 16 | + return `\u001B[${38 + offset};2;${rgb[0]};${rgb[1]};${rgb[2]}m`; |
| 17 | +}; |
| 18 | + |
| 19 | +function assembleStyles() { |
| 20 | + const styles = { |
| 21 | + modifier: { |
| 22 | + reset: [0, 0], |
| 23 | + // 21 isn't widely supported and 22 does the same thing |
| 24 | + bold: [1, 22], |
| 25 | + dim: [2, 22], |
| 26 | + italic: [3, 23], |
| 27 | + underline: [4, 24], |
| 28 | + inverse: [7, 27], |
| 29 | + hidden: [8, 28], |
| 30 | + strikethrough: [9, 29] |
| 31 | + }, |
| 32 | + color: { |
| 33 | + black: [30, 39], |
| 34 | + red: [31, 39], |
| 35 | + green: [32, 39], |
| 36 | + yellow: [33, 39], |
| 37 | + blue: [34, 39], |
| 38 | + magenta: [35, 39], |
| 39 | + cyan: [36, 39], |
| 40 | + white: [37, 39], |
| 41 | + gray: [90, 39], |
| 42 | + |
| 43 | + // Bright color |
| 44 | + redBright: [91, 39], |
| 45 | + greenBright: [92, 39], |
| 46 | + yellowBright: [93, 39], |
| 47 | + blueBright: [94, 39], |
| 48 | + magentaBright: [95, 39], |
| 49 | + cyanBright: [96, 39], |
| 50 | + whiteBright: [97, 39] |
| 51 | + }, |
| 52 | + bgColor: { |
| 53 | + bgBlack: [40, 49], |
| 54 | + bgRed: [41, 49], |
| 55 | + bgGreen: [42, 49], |
| 56 | + bgYellow: [43, 49], |
| 57 | + bgBlue: [44, 49], |
| 58 | + bgMagenta: [45, 49], |
| 59 | + bgCyan: [46, 49], |
| 60 | + bgWhite: [47, 49], |
| 61 | + |
| 62 | + // Bright color |
| 63 | + bgBlackBright: [100, 49], |
| 64 | + bgRedBright: [101, 49], |
| 65 | + bgGreenBright: [102, 49], |
| 66 | + bgYellowBright: [103, 49], |
| 67 | + bgBlueBright: [104, 49], |
| 68 | + bgMagentaBright: [105, 49], |
| 69 | + bgCyanBright: [106, 49], |
| 70 | + bgWhiteBright: [107, 49] |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + // Fix humans |
| 75 | + styles.color.grey = styles.color.gray; |
| 76 | + |
| 77 | + Object.keys(styles).forEach(groupName => { |
| 78 | + const group = styles[groupName]; |
| 79 | + |
| 80 | + Object.keys(group).forEach(styleName => { |
| 81 | + const style = group[styleName]; |
| 82 | + |
| 83 | + styles[styleName] = { |
| 84 | + open: `\u001B[${style[0]}m`, |
| 85 | + close: `\u001B[${style[1]}m` |
| 86 | + }; |
| 87 | + |
| 88 | + group[styleName] = styles[styleName]; |
| 89 | + }); |
| 90 | + |
| 91 | + Object.defineProperty(styles, groupName, { |
| 92 | + value: group, |
| 93 | + enumerable: false |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + const rgb2rgb = (r, g, b) => [r, g, b]; |
| 98 | + |
| 99 | + styles.color.close = '\u001B[39m'; |
| 100 | + styles.bgColor.close = '\u001B[49m'; |
| 101 | + |
| 102 | + styles.color.ansi = {}; |
| 103 | + styles.color.ansi256 = {}; |
| 104 | + styles.color.ansi16m = { |
| 105 | + rgb: wrapAnsi16m(rgb2rgb, 0) |
| 106 | + }; |
| 107 | + |
| 108 | + styles.bgColor.ansi = {}; |
| 109 | + styles.bgColor.ansi256 = {}; |
| 110 | + styles.bgColor.ansi16m = { |
| 111 | + rgb: wrapAnsi16m(rgb2rgb, 10) |
| 112 | + }; |
| 113 | + |
| 114 | + for (const key of Object.keys(colorConvert)) { |
| 115 | + if (typeof colorConvert[key] !== 'object') { |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + const suite = colorConvert[key]; |
| 120 | + |
| 121 | + if ('ansi16' in suite) { |
| 122 | + styles.color.ansi[key] = wrapAnsi16(suite.ansi16, 0); |
| 123 | + styles.bgColor.ansi[key] = wrapAnsi16(suite.ansi16, 10); |
| 124 | + } |
| 125 | + |
| 126 | + if ('ansi256' in suite) { |
| 127 | + styles.color.ansi256[key] = wrapAnsi256(suite.ansi256, 0); |
| 128 | + styles.bgColor.ansi256[key] = wrapAnsi256(suite.ansi256, 10); |
| 129 | + } |
| 130 | + |
| 131 | + if ('rgb' in suite) { |
| 132 | + styles.color.ansi16m[key] = wrapAnsi16m(suite.rgb, 0); |
| 133 | + styles.bgColor.ansi16m[key] = wrapAnsi16m(suite.rgb, 10); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return styles; |
| 138 | +} |
| 139 | + |
| 140 | +//Object.defineProperty(module, 'exports', { |
| 141 | +// enumerable: true, |
| 142 | +// get: assembleStyles |
| 143 | +//}); |
| 144 | +module.exports = assembleStyles(); |
0 commit comments