source: trip-planner-front/node_modules/ansi-colors/index.js@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 5.7 KB
Line 
1'use strict';
2
3const isObject = val => val !== null && typeof val === 'object' && !Array.isArray(val);
4const identity = val => val;
5
6/* eslint-disable no-control-regex */
7// this is a modified version of https://github.com/chalk/ansi-regex (MIT License)
8const ANSI_REGEX = /[\u001b\u009b][[\]#;?()]*(?:(?:(?:[^\W_]*;?[^\W_]*)\u0007)|(?:(?:[0-9]{1,4}(;[0-9]{0,4})*)?[~0-9=<>cf-nqrtyA-PRZ]))/g;
9
10const create = () => {
11 const colors = { enabled: true, visible: true, styles: {}, keys: {} };
12
13 if ('FORCE_COLOR' in process.env) {
14 colors.enabled = process.env.FORCE_COLOR !== '0';
15 }
16
17 const ansi = style => {
18 let open = style.open = `\u001b[${style.codes[0]}m`;
19 let close = style.close = `\u001b[${style.codes[1]}m`;
20 let regex = style.regex = new RegExp(`\\u001b\\[${style.codes[1]}m`, 'g');
21 style.wrap = (input, newline) => {
22 if (input.includes(close)) input = input.replace(regex, close + open);
23 let output = open + input + close;
24 // see https://github.com/chalk/chalk/pull/92, thanks to the
25 // chalk contributors for this fix. However, we've confirmed that
26 // this issue is also present in Windows terminals
27 return newline ? output.replace(/\r*\n/g, `${close}$&${open}`) : output;
28 };
29 return style;
30 };
31
32 const wrap = (style, input, newline) => {
33 return typeof style === 'function' ? style(input) : style.wrap(input, newline);
34 };
35
36 const style = (input, stack) => {
37 if (input === '' || input == null) return '';
38 if (colors.enabled === false) return input;
39 if (colors.visible === false) return '';
40 let str = '' + input;
41 let nl = str.includes('\n');
42 let n = stack.length;
43 if (n > 0 && stack.includes('unstyle')) {
44 stack = [...new Set(['unstyle', ...stack])].reverse();
45 }
46 while (n-- > 0) str = wrap(colors.styles[stack[n]], str, nl);
47 return str;
48 };
49
50 const define = (name, codes, type) => {
51 colors.styles[name] = ansi({ name, codes });
52 let keys = colors.keys[type] || (colors.keys[type] = []);
53 keys.push(name);
54
55 Reflect.defineProperty(colors, name, {
56 configurable: true,
57 enumerable: true,
58 set(value) {
59 colors.alias(name, value);
60 },
61 get() {
62 let color = input => style(input, color.stack);
63 Reflect.setPrototypeOf(color, colors);
64 color.stack = this.stack ? this.stack.concat(name) : [name];
65 return color;
66 }
67 });
68 };
69
70 define('reset', [0, 0], 'modifier');
71 define('bold', [1, 22], 'modifier');
72 define('dim', [2, 22], 'modifier');
73 define('italic', [3, 23], 'modifier');
74 define('underline', [4, 24], 'modifier');
75 define('inverse', [7, 27], 'modifier');
76 define('hidden', [8, 28], 'modifier');
77 define('strikethrough', [9, 29], 'modifier');
78
79 define('black', [30, 39], 'color');
80 define('red', [31, 39], 'color');
81 define('green', [32, 39], 'color');
82 define('yellow', [33, 39], 'color');
83 define('blue', [34, 39], 'color');
84 define('magenta', [35, 39], 'color');
85 define('cyan', [36, 39], 'color');
86 define('white', [37, 39], 'color');
87 define('gray', [90, 39], 'color');
88 define('grey', [90, 39], 'color');
89
90 define('bgBlack', [40, 49], 'bg');
91 define('bgRed', [41, 49], 'bg');
92 define('bgGreen', [42, 49], 'bg');
93 define('bgYellow', [43, 49], 'bg');
94 define('bgBlue', [44, 49], 'bg');
95 define('bgMagenta', [45, 49], 'bg');
96 define('bgCyan', [46, 49], 'bg');
97 define('bgWhite', [47, 49], 'bg');
98
99 define('blackBright', [90, 39], 'bright');
100 define('redBright', [91, 39], 'bright');
101 define('greenBright', [92, 39], 'bright');
102 define('yellowBright', [93, 39], 'bright');
103 define('blueBright', [94, 39], 'bright');
104 define('magentaBright', [95, 39], 'bright');
105 define('cyanBright', [96, 39], 'bright');
106 define('whiteBright', [97, 39], 'bright');
107
108 define('bgBlackBright', [100, 49], 'bgBright');
109 define('bgRedBright', [101, 49], 'bgBright');
110 define('bgGreenBright', [102, 49], 'bgBright');
111 define('bgYellowBright', [103, 49], 'bgBright');
112 define('bgBlueBright', [104, 49], 'bgBright');
113 define('bgMagentaBright', [105, 49], 'bgBright');
114 define('bgCyanBright', [106, 49], 'bgBright');
115 define('bgWhiteBright', [107, 49], 'bgBright');
116
117 colors.ansiRegex = ANSI_REGEX;
118 colors.hasColor = colors.hasAnsi = str => {
119 colors.ansiRegex.lastIndex = 0;
120 return typeof str === 'string' && str !== '' && colors.ansiRegex.test(str);
121 };
122
123 colors.alias = (name, color) => {
124 let fn = typeof color === 'string' ? colors[color] : color;
125
126 if (typeof fn !== 'function') {
127 throw new TypeError('Expected alias to be the name of an existing color (string) or a function');
128 }
129
130 if (!fn.stack) {
131 Reflect.defineProperty(fn, 'name', { value: name });
132 colors.styles[name] = fn;
133 fn.stack = [name];
134 }
135
136 Reflect.defineProperty(colors, name, {
137 configurable: true,
138 enumerable: true,
139 set(value) {
140 colors.alias(name, value);
141 },
142 get() {
143 let color = input => style(input, color.stack);
144 Reflect.setPrototypeOf(color, colors);
145 color.stack = this.stack ? this.stack.concat(fn.stack) : fn.stack;
146 return color;
147 }
148 });
149 };
150
151 colors.theme = custom => {
152 if (!isObject(custom)) throw new TypeError('Expected theme to be an object');
153 for (let name of Object.keys(custom)) {
154 colors.alias(name, custom[name]);
155 }
156 return colors;
157 };
158
159 colors.alias('unstyle', str => {
160 if (typeof str === 'string' && str !== '') {
161 colors.ansiRegex.lastIndex = 0;
162 return str.replace(colors.ansiRegex, '');
163 }
164 return '';
165 });
166
167 colors.alias('noop', str => str);
168 colors.none = colors.clear = colors.noop;
169
170 colors.stripColor = colors.unstyle;
171 colors.symbols = require('./symbols');
172 colors.define = define;
173 return colors;
174};
175
176module.exports = create();
177module.exports.create = create;
Note: See TracBrowser for help on using the repository browser.