Projekt

Obecné

Profil

Stáhnout (3.49 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2
const colorConvert = require('color-convert');
3

    
4
const wrapAnsi16 = (fn, offset) => function () {
5
	const code = fn.apply(colorConvert, arguments);
6
	return `\u001B[${code + offset}m`;
7
};
8

    
9
const wrapAnsi256 = (fn, offset) => function () {
10
	const code = fn.apply(colorConvert, arguments);
11
	return `\u001B[${38 + offset};5;${code}m`;
12
};
13

    
14
const wrapAnsi16m = (fn, offset) => function () {
15
	const rgb = fn.apply(colorConvert, arguments);
16
	return `\u001B[${38 + offset};2;${rgb[0]};${rgb[1]};${rgb[2]}m`;
17
};
18

    
19
function assembleStyles() {
20
	const codes = new Map();
21
	const styles = {
22
		modifier: {
23
			reset: [0, 0],
24
			// 21 isn't widely supported and 22 does the same thing
25
			bold: [1, 22],
26
			dim: [2, 22],
27
			italic: [3, 23],
28
			underline: [4, 24],
29
			inverse: [7, 27],
30
			hidden: [8, 28],
31
			strikethrough: [9, 29]
32
		},
33
		color: {
34
			black: [30, 39],
35
			red: [31, 39],
36
			green: [32, 39],
37
			yellow: [33, 39],
38
			blue: [34, 39],
39
			magenta: [35, 39],
40
			cyan: [36, 39],
41
			white: [37, 39],
42
			gray: [90, 39],
43

    
44
			// Bright color
45
			redBright: [91, 39],
46
			greenBright: [92, 39],
47
			yellowBright: [93, 39],
48
			blueBright: [94, 39],
49
			magentaBright: [95, 39],
50
			cyanBright: [96, 39],
51
			whiteBright: [97, 39]
52
		},
53
		bgColor: {
54
			bgBlack: [40, 49],
55
			bgRed: [41, 49],
56
			bgGreen: [42, 49],
57
			bgYellow: [43, 49],
58
			bgBlue: [44, 49],
59
			bgMagenta: [45, 49],
60
			bgCyan: [46, 49],
61
			bgWhite: [47, 49],
62

    
63
			// Bright color
64
			bgBlackBright: [100, 49],
65
			bgRedBright: [101, 49],
66
			bgGreenBright: [102, 49],
67
			bgYellowBright: [103, 49],
68
			bgBlueBright: [104, 49],
69
			bgMagentaBright: [105, 49],
70
			bgCyanBright: [106, 49],
71
			bgWhiteBright: [107, 49]
72
		}
73
	};
74

    
75
	// Fix humans
76
	styles.color.grey = styles.color.gray;
77

    
78
	for (const groupName of Object.keys(styles)) {
79
		const group = styles[groupName];
80

    
81
		for (const styleName of Object.keys(group)) {
82
			const style = group[styleName];
83

    
84
			styles[styleName] = {
85
				open: `\u001B[${style[0]}m`,
86
				close: `\u001B[${style[1]}m`
87
			};
88

    
89
			group[styleName] = styles[styleName];
90

    
91
			codes.set(style[0], style[1]);
92
		}
93

    
94
		Object.defineProperty(styles, groupName, {
95
			value: group,
96
			enumerable: false
97
		});
98

    
99
		Object.defineProperty(styles, 'codes', {
100
			value: codes,
101
			enumerable: false
102
		});
103
	}
104

    
105
	const ansi2ansi = n => n;
106
	const rgb2rgb = (r, g, b) => [r, g, b];
107

    
108
	styles.color.close = '\u001B[39m';
109
	styles.bgColor.close = '\u001B[49m';
110

    
111
	styles.color.ansi = {
112
		ansi: wrapAnsi16(ansi2ansi, 0)
113
	};
114
	styles.color.ansi256 = {
115
		ansi256: wrapAnsi256(ansi2ansi, 0)
116
	};
117
	styles.color.ansi16m = {
118
		rgb: wrapAnsi16m(rgb2rgb, 0)
119
	};
120

    
121
	styles.bgColor.ansi = {
122
		ansi: wrapAnsi16(ansi2ansi, 10)
123
	};
124
	styles.bgColor.ansi256 = {
125
		ansi256: wrapAnsi256(ansi2ansi, 10)
126
	};
127
	styles.bgColor.ansi16m = {
128
		rgb: wrapAnsi16m(rgb2rgb, 10)
129
	};
130

    
131
	for (let key of Object.keys(colorConvert)) {
132
		if (typeof colorConvert[key] !== 'object') {
133
			continue;
134
		}
135

    
136
		const suite = colorConvert[key];
137

    
138
		if (key === 'ansi16') {
139
			key = 'ansi';
140
		}
141

    
142
		if ('ansi16' in suite) {
143
			styles.color.ansi[key] = wrapAnsi16(suite.ansi16, 0);
144
			styles.bgColor.ansi[key] = wrapAnsi16(suite.ansi16, 10);
145
		}
146

    
147
		if ('ansi256' in suite) {
148
			styles.color.ansi256[key] = wrapAnsi256(suite.ansi256, 0);
149
			styles.bgColor.ansi256[key] = wrapAnsi256(suite.ansi256, 10);
150
		}
151

    
152
		if ('rgb' in suite) {
153
			styles.color.ansi16m[key] = wrapAnsi16m(suite.rgb, 0);
154
			styles.bgColor.ansi16m[key] = wrapAnsi16m(suite.rgb, 10);
155
		}
156
	}
157

    
158
	return styles;
159
}
160

    
161
// Make the export immutable
162
Object.defineProperty(module, 'exports', {
163
	enumerable: true,
164
	get: assembleStyles
165
});
(1-1/4)