| | |
| | | 'use strict'; |
| | | const colorConvert = require('color-convert'); |
| | | |
| | | const wrapAnsi16 = (fn, offset) => function () { |
| | | const code = fn.apply(colorConvert, arguments); |
| | | const wrapAnsi16 = (fn, offset) => (...args) => { |
| | | const code = fn(...args); |
| | | return `\u001B[${code + offset}m`; |
| | | }; |
| | | |
| | | const wrapAnsi256 = (fn, offset) => function () { |
| | | const code = fn.apply(colorConvert, arguments); |
| | | const wrapAnsi256 = (fn, offset) => (...args) => { |
| | | const code = fn(...args); |
| | | return `\u001B[${38 + offset};5;${code}m`; |
| | | }; |
| | | |
| | | const wrapAnsi16m = (fn, offset) => function () { |
| | | const rgb = fn.apply(colorConvert, arguments); |
| | | const wrapAnsi16m = (fn, offset) => (...args) => { |
| | | const rgb = fn(...args); |
| | | return `\u001B[${38 + offset};2;${rgb[0]};${rgb[1]};${rgb[2]}m`; |
| | | }; |
| | | |
| | | const ansi2ansi = n => n; |
| | | const rgb2rgb = (r, g, b) => [r, g, b]; |
| | | |
| | | const setLazyProperty = (object, property, get) => { |
| | | Object.defineProperty(object, property, { |
| | | get: () => { |
| | | const value = get(); |
| | | |
| | | Object.defineProperty(object, property, { |
| | | value, |
| | | enumerable: true, |
| | | configurable: true |
| | | }); |
| | | |
| | | return value; |
| | | }, |
| | | enumerable: true, |
| | | configurable: true |
| | | }); |
| | | }; |
| | | |
| | | /** @type {typeof import('color-convert')} */ |
| | | let colorConvert; |
| | | const makeDynamicStyles = (wrap, targetSpace, identity, isBackground) => { |
| | | if (colorConvert === undefined) { |
| | | colorConvert = require('color-convert'); |
| | | } |
| | | |
| | | const offset = isBackground ? 10 : 0; |
| | | const styles = {}; |
| | | |
| | | for (const [sourceSpace, suite] of Object.entries(colorConvert)) { |
| | | const name = sourceSpace === 'ansi16' ? 'ansi' : sourceSpace; |
| | | if (sourceSpace === targetSpace) { |
| | | styles[name] = wrap(identity, offset); |
| | | } else if (typeof suite === 'object') { |
| | | styles[name] = wrap(suite[targetSpace], offset); |
| | | } |
| | | } |
| | | |
| | | return styles; |
| | | }; |
| | | |
| | | function assembleStyles() { |
| | |
| | | magenta: [35, 39], |
| | | cyan: [36, 39], |
| | | white: [37, 39], |
| | | gray: [90, 39], |
| | | |
| | | // Bright color |
| | | blackBright: [90, 39], |
| | | redBright: [91, 39], |
| | | greenBright: [92, 39], |
| | | yellowBright: [93, 39], |
| | |
| | | } |
| | | }; |
| | | |
| | | // Fix humans |
| | | styles.color.grey = styles.color.gray; |
| | | // Alias bright black as gray (and grey) |
| | | styles.color.gray = styles.color.blackBright; |
| | | styles.bgColor.bgGray = styles.bgColor.bgBlackBright; |
| | | styles.color.grey = styles.color.blackBright; |
| | | styles.bgColor.bgGrey = styles.bgColor.bgBlackBright; |
| | | |
| | | for (const groupName of Object.keys(styles)) { |
| | | const group = styles[groupName]; |
| | | |
| | | for (const styleName of Object.keys(group)) { |
| | | const style = group[styleName]; |
| | | |
| | | for (const [groupName, group] of Object.entries(styles)) { |
| | | for (const [styleName, style] of Object.entries(group)) { |
| | | styles[styleName] = { |
| | | open: `\u001B[${style[0]}m`, |
| | | close: `\u001B[${style[1]}m` |
| | |
| | | value: group, |
| | | enumerable: false |
| | | }); |
| | | |
| | | Object.defineProperty(styles, 'codes', { |
| | | value: codes, |
| | | enumerable: false |
| | | }); |
| | | } |
| | | |
| | | const ansi2ansi = n => n; |
| | | const rgb2rgb = (r, g, b) => [r, g, b]; |
| | | Object.defineProperty(styles, 'codes', { |
| | | value: codes, |
| | | enumerable: false |
| | | }); |
| | | |
| | | styles.color.close = '\u001B[39m'; |
| | | styles.bgColor.close = '\u001B[49m'; |
| | | |
| | | styles.color.ansi = { |
| | | ansi: wrapAnsi16(ansi2ansi, 0) |
| | | }; |
| | | styles.color.ansi256 = { |
| | | ansi256: wrapAnsi256(ansi2ansi, 0) |
| | | }; |
| | | styles.color.ansi16m = { |
| | | rgb: wrapAnsi16m(rgb2rgb, 0) |
| | | }; |
| | | |
| | | styles.bgColor.ansi = { |
| | | ansi: wrapAnsi16(ansi2ansi, 10) |
| | | }; |
| | | styles.bgColor.ansi256 = { |
| | | ansi256: wrapAnsi256(ansi2ansi, 10) |
| | | }; |
| | | styles.bgColor.ansi16m = { |
| | | rgb: wrapAnsi16m(rgb2rgb, 10) |
| | | }; |
| | | |
| | | for (let key of Object.keys(colorConvert)) { |
| | | if (typeof colorConvert[key] !== 'object') { |
| | | continue; |
| | | } |
| | | |
| | | const suite = colorConvert[key]; |
| | | |
| | | if (key === 'ansi16') { |
| | | key = 'ansi'; |
| | | } |
| | | |
| | | if ('ansi16' in suite) { |
| | | styles.color.ansi[key] = wrapAnsi16(suite.ansi16, 0); |
| | | styles.bgColor.ansi[key] = wrapAnsi16(suite.ansi16, 10); |
| | | } |
| | | |
| | | if ('ansi256' in suite) { |
| | | styles.color.ansi256[key] = wrapAnsi256(suite.ansi256, 0); |
| | | styles.bgColor.ansi256[key] = wrapAnsi256(suite.ansi256, 10); |
| | | } |
| | | |
| | | if ('rgb' in suite) { |
| | | styles.color.ansi16m[key] = wrapAnsi16m(suite.rgb, 0); |
| | | styles.bgColor.ansi16m[key] = wrapAnsi16m(suite.rgb, 10); |
| | | } |
| | | } |
| | | setLazyProperty(styles.color, 'ansi', () => makeDynamicStyles(wrapAnsi16, 'ansi16', ansi2ansi, false)); |
| | | setLazyProperty(styles.color, 'ansi256', () => makeDynamicStyles(wrapAnsi256, 'ansi256', ansi2ansi, false)); |
| | | setLazyProperty(styles.color, 'ansi16m', () => makeDynamicStyles(wrapAnsi16m, 'rgb', rgb2rgb, false)); |
| | | setLazyProperty(styles.bgColor, 'ansi', () => makeDynamicStyles(wrapAnsi16, 'ansi16', ansi2ansi, true)); |
| | | setLazyProperty(styles.bgColor, 'ansi256', () => makeDynamicStyles(wrapAnsi256, 'ansi256', ansi2ansi, true)); |
| | | setLazyProperty(styles.bgColor, 'ansi16m', () => makeDynamicStyles(wrapAnsi16m, 'rgb', rgb2rgb, true)); |
| | | |
| | | return styles; |
| | | } |