Projekt

Obecné

Profil

Stáhnout (3.86 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2

    
3
const colors = { enabled: true, visible: true, styles: {}, keys: {} };
4

    
5
if ('FORCE_COLOR' in process.env) {
6
  colors.enabled = process.env.FORCE_COLOR !== '0';
7
}
8

    
9
const ansi = style => {
10
  style.open = `\u001b[${style.codes[0]}m`;
11
  style.close = `\u001b[${style.codes[1]}m`;
12
  style.regex = new RegExp(`\\u001b\\[${style.codes[1]}m`, 'g');
13
  return style;
14
};
15

    
16
const wrap = (style, str, nl) => {
17
  let { open, close, regex } = style;
18
  str = open + (str.includes(close) ? str.replace(regex, close + open) : str) + close;
19
  // see https://github.com/chalk/chalk/pull/92, thanks to the
20
  // chalk contributors for this fix. However, we've confirmed that
21
  // this issue is also present in Windows terminals
22
  return nl ? str.replace(/\r?\n/g, `${close}$&${open}`) : str;
23
};
24

    
25
const style = (input, stack) => {
26
  if (input === '' || input == null) return '';
27
  if (colors.enabled === false) return input;
28
  if (colors.visible === false) return '';
29
  let str = '' + input;
30
  let nl = str.includes('\n');
31
  let n = stack.length;
32
  while (n-- > 0) str = wrap(colors.styles[stack[n]], str, nl);
33
  return str;
34
};
35

    
36
const define = (name, codes, type) => {
37
  colors.styles[name] = ansi({ name, codes });
38
  let t = colors.keys[type] || (colors.keys[type] = []);
39
  t.push(name);
40

    
41
  Reflect.defineProperty(colors, name, {
42
    get() {
43
      let color = input => style(input, color.stack);
44
      Reflect.setPrototypeOf(color, colors);
45
      color.stack = this.stack ? this.stack.concat(name) : [name];
46
      return color;
47
    }
48
  });
49
};
50

    
51
define('reset', [0, 0], 'modifier');
52
define('bold', [1, 22], 'modifier');
53
define('dim', [2, 22], 'modifier');
54
define('italic', [3, 23], 'modifier');
55
define('underline', [4, 24], 'modifier');
56
define('inverse', [7, 27], 'modifier');
57
define('hidden', [8, 28], 'modifier');
58
define('strikethrough', [9, 29], 'modifier');
59

    
60
define('black', [30, 39], 'color');
61
define('red', [31, 39], 'color');
62
define('green', [32, 39], 'color');
63
define('yellow', [33, 39], 'color');
64
define('blue', [34, 39], 'color');
65
define('magenta', [35, 39], 'color');
66
define('cyan', [36, 39], 'color');
67
define('white', [37, 39], 'color');
68
define('gray', [90, 39], 'color');
69
define('grey', [90, 39], 'color');
70

    
71
define('bgBlack', [40, 49], 'bg');
72
define('bgRed', [41, 49], 'bg');
73
define('bgGreen', [42, 49], 'bg');
74
define('bgYellow', [43, 49], 'bg');
75
define('bgBlue', [44, 49], 'bg');
76
define('bgMagenta', [45, 49], 'bg');
77
define('bgCyan', [46, 49], 'bg');
78
define('bgWhite', [47, 49], 'bg');
79

    
80
define('blackBright', [90, 39], 'bright');
81
define('redBright', [91, 39], 'bright');
82
define('greenBright', [92, 39], 'bright');
83
define('yellowBright', [93, 39], 'bright');
84
define('blueBright', [94, 39], 'bright');
85
define('magentaBright', [95, 39], 'bright');
86
define('cyanBright', [96, 39], 'bright');
87
define('whiteBright', [97, 39], 'bright');
88

    
89
define('bgBlackBright', [100, 49], 'bgBright');
90
define('bgRedBright', [101, 49], 'bgBright');
91
define('bgGreenBright', [102, 49], 'bgBright');
92
define('bgYellowBright', [103, 49], 'bgBright');
93
define('bgBlueBright', [104, 49], 'bgBright');
94
define('bgMagentaBright', [105, 49], 'bgBright');
95
define('bgCyanBright', [106, 49], 'bgBright');
96
define('bgWhiteBright', [107, 49], 'bgBright');
97

    
98
/* eslint-disable no-control-regex */
99
// this is a modified, optimized version of
100
// https://github.com/chalk/ansi-regex (MIT License)
101
const re = colors.ansiRegex = /[\u001b\u009b][[\]#;?()]*(?:(?:(?:[^\W_]*;?[^\W_]*)\u0007)|(?:(?:[0-9]{1,4}(;[0-9]{0,4})*)?[~0-9=<>cf-nqrtyA-PRZ]))/g;
102

    
103
colors.hasColor = colors.hasAnsi = str => {
104
  re.lastIndex = 0;
105
  return !!str && typeof str === 'string' && re.test(str);
106
};
107

    
108
colors.unstyle = str => {
109
  re.lastIndex = 0;
110
  return typeof str === 'string' ? str.replace(re, '') : str;
111
};
112

    
113
colors.none = colors.clear = colors.noop = str => str; // no-op, for programmatic usage
114
colors.stripColor = colors.unstyle;
115
colors.symbols = require('./symbols');
116
colors.define = define;
117
module.exports = colors;
(3-3/5)