index.js
  1  'use strict';
  2  
  3  const { FORCE_COLOR, NODE_DISABLE_COLORS, TERM } = process.env;
  4  
  5  const $ = {
  6  	enabled: !NODE_DISABLE_COLORS && TERM !== 'dumb' && FORCE_COLOR !== '0',
  7  
  8  	// modifiers
  9  	reset: init(0, 0),
 10  	bold: init(1, 22),
 11  	dim: init(2, 22),
 12  	italic: init(3, 23),
 13  	underline: init(4, 24),
 14  	inverse: init(7, 27),
 15  	hidden: init(8, 28),
 16  	strikethrough: init(9, 29),
 17  
 18  	// colors
 19  	black: init(30, 39),
 20  	red: init(31, 39),
 21  	green: init(32, 39),
 22  	yellow: init(33, 39),
 23  	blue: init(34, 39),
 24  	magenta: init(35, 39),
 25  	cyan: init(36, 39),
 26  	white: init(37, 39),
 27  	gray: init(90, 39),
 28  	grey: init(90, 39),
 29  
 30  	// background colors
 31  	bgBlack: init(40, 49),
 32  	bgRed: init(41, 49),
 33  	bgGreen: init(42, 49),
 34  	bgYellow: init(43, 49),
 35  	bgBlue: init(44, 49),
 36  	bgMagenta: init(45, 49),
 37  	bgCyan: init(46, 49),
 38  	bgWhite: init(47, 49)
 39  };
 40  
 41  function run(arr, str) {
 42  	let i=0, tmp, beg='', end='';
 43  	for (; i < arr.length; i++) {
 44  		tmp = arr[i];
 45  		beg += tmp.open;
 46  		end += tmp.close;
 47  		if (str.includes(tmp.close)) {
 48  			str = str.replace(tmp.rgx, tmp.close + tmp.open);
 49  		}
 50  	}
 51  	return beg + str + end;
 52  }
 53  
 54  function chain(has, keys) {
 55  	let ctx = { has, keys };
 56  
 57  	ctx.reset = $.reset.bind(ctx);
 58  	ctx.bold = $.bold.bind(ctx);
 59  	ctx.dim = $.dim.bind(ctx);
 60  	ctx.italic = $.italic.bind(ctx);
 61  	ctx.underline = $.underline.bind(ctx);
 62  	ctx.inverse = $.inverse.bind(ctx);
 63  	ctx.hidden = $.hidden.bind(ctx);
 64  	ctx.strikethrough = $.strikethrough.bind(ctx);
 65  
 66  	ctx.black = $.black.bind(ctx);
 67  	ctx.red = $.red.bind(ctx);
 68  	ctx.green = $.green.bind(ctx);
 69  	ctx.yellow = $.yellow.bind(ctx);
 70  	ctx.blue = $.blue.bind(ctx);
 71  	ctx.magenta = $.magenta.bind(ctx);
 72  	ctx.cyan = $.cyan.bind(ctx);
 73  	ctx.white = $.white.bind(ctx);
 74  	ctx.gray = $.gray.bind(ctx);
 75  	ctx.grey = $.grey.bind(ctx);
 76  
 77  	ctx.bgBlack = $.bgBlack.bind(ctx);
 78  	ctx.bgRed = $.bgRed.bind(ctx);
 79  	ctx.bgGreen = $.bgGreen.bind(ctx);
 80  	ctx.bgYellow = $.bgYellow.bind(ctx);
 81  	ctx.bgBlue = $.bgBlue.bind(ctx);
 82  	ctx.bgMagenta = $.bgMagenta.bind(ctx);
 83  	ctx.bgCyan = $.bgCyan.bind(ctx);
 84  	ctx.bgWhite = $.bgWhite.bind(ctx);
 85  
 86  	return ctx;
 87  }
 88  
 89  function init(open, close) {
 90  	let blk = {
 91  		open: `\x1b[${open}m`,
 92  		close: `\x1b[${close}m`,
 93  		rgx: new RegExp(`\\x1b\\[${close}m`, 'g')
 94  	};
 95  	return function (txt) {
 96  		if (this !== void 0 && this.has !== void 0) {
 97  			this.has.includes(open) || (this.has.push(open),this.keys.push(blk));
 98  			return txt === void 0 ? this : $.enabled ? run(this.keys, txt+'') : txt+'';
 99  		}
100  		return txt === void 0 ? chain([open], [blk]) : $.enabled ? run([blk], txt+'') : txt+'';
101  	};
102  }
103  
104  module.exports = $;