Path: blob/master/node_modules/ansi-styles/index.js
2591 views
'use strict';12const wrapAnsi16 = (fn, offset) => (...args) => {3const code = fn(...args);4return `\u001B[${code + offset}m`;5};67const wrapAnsi256 = (fn, offset) => (...args) => {8const code = fn(...args);9return `\u001B[${38 + offset};5;${code}m`;10};1112const wrapAnsi16m = (fn, offset) => (...args) => {13const rgb = fn(...args);14return `\u001B[${38 + offset};2;${rgb[0]};${rgb[1]};${rgb[2]}m`;15};1617const ansi2ansi = n => n;18const rgb2rgb = (r, g, b) => [r, g, b];1920const setLazyProperty = (object, property, get) => {21Object.defineProperty(object, property, {22get: () => {23const value = get();2425Object.defineProperty(object, property, {26value,27enumerable: true,28configurable: true29});3031return value;32},33enumerable: true,34configurable: true35});36};3738/** @type {typeof import('color-convert')} */39let colorConvert;40const makeDynamicStyles = (wrap, targetSpace, identity, isBackground) => {41if (colorConvert === undefined) {42colorConvert = require('color-convert');43}4445const offset = isBackground ? 10 : 0;46const styles = {};4748for (const [sourceSpace, suite] of Object.entries(colorConvert)) {49const name = sourceSpace === 'ansi16' ? 'ansi' : sourceSpace;50if (sourceSpace === targetSpace) {51styles[name] = wrap(identity, offset);52} else if (typeof suite === 'object') {53styles[name] = wrap(suite[targetSpace], offset);54}55}5657return styles;58};5960function assembleStyles() {61const codes = new Map();62const styles = {63modifier: {64reset: [0, 0],65// 21 isn't widely supported and 22 does the same thing66bold: [1, 22],67dim: [2, 22],68italic: [3, 23],69underline: [4, 24],70inverse: [7, 27],71hidden: [8, 28],72strikethrough: [9, 29]73},74color: {75black: [30, 39],76red: [31, 39],77green: [32, 39],78yellow: [33, 39],79blue: [34, 39],80magenta: [35, 39],81cyan: [36, 39],82white: [37, 39],8384// Bright color85blackBright: [90, 39],86redBright: [91, 39],87greenBright: [92, 39],88yellowBright: [93, 39],89blueBright: [94, 39],90magentaBright: [95, 39],91cyanBright: [96, 39],92whiteBright: [97, 39]93},94bgColor: {95bgBlack: [40, 49],96bgRed: [41, 49],97bgGreen: [42, 49],98bgYellow: [43, 49],99bgBlue: [44, 49],100bgMagenta: [45, 49],101bgCyan: [46, 49],102bgWhite: [47, 49],103104// Bright color105bgBlackBright: [100, 49],106bgRedBright: [101, 49],107bgGreenBright: [102, 49],108bgYellowBright: [103, 49],109bgBlueBright: [104, 49],110bgMagentaBright: [105, 49],111bgCyanBright: [106, 49],112bgWhiteBright: [107, 49]113}114};115116// Alias bright black as gray (and grey)117styles.color.gray = styles.color.blackBright;118styles.bgColor.bgGray = styles.bgColor.bgBlackBright;119styles.color.grey = styles.color.blackBright;120styles.bgColor.bgGrey = styles.bgColor.bgBlackBright;121122for (const [groupName, group] of Object.entries(styles)) {123for (const [styleName, style] of Object.entries(group)) {124styles[styleName] = {125open: `\u001B[${style[0]}m`,126close: `\u001B[${style[1]}m`127};128129group[styleName] = styles[styleName];130131codes.set(style[0], style[1]);132}133134Object.defineProperty(styles, groupName, {135value: group,136enumerable: false137});138}139140Object.defineProperty(styles, 'codes', {141value: codes,142enumerable: false143});144145styles.color.close = '\u001B[39m';146styles.bgColor.close = '\u001B[49m';147148setLazyProperty(styles.color, 'ansi', () => makeDynamicStyles(wrapAnsi16, 'ansi16', ansi2ansi, false));149setLazyProperty(styles.color, 'ansi256', () => makeDynamicStyles(wrapAnsi256, 'ansi256', ansi2ansi, false));150setLazyProperty(styles.color, 'ansi16m', () => makeDynamicStyles(wrapAnsi16m, 'rgb', rgb2rgb, false));151setLazyProperty(styles.bgColor, 'ansi', () => makeDynamicStyles(wrapAnsi16, 'ansi16', ansi2ansi, true));152setLazyProperty(styles.bgColor, 'ansi256', () => makeDynamicStyles(wrapAnsi256, 'ansi256', ansi2ansi, true));153setLazyProperty(styles.bgColor, 'ansi16m', () => makeDynamicStyles(wrapAnsi16m, 'rgb', rgb2rgb, true));154155return styles;156}157158// Make the export immutable159Object.defineProperty(module, 'exports', {160enumerable: true,161get: assembleStyles162});163164165