-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlog.js
More file actions
93 lines (82 loc) · 2.42 KB
/
log.js
File metadata and controls
93 lines (82 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const { splitError } = require('./errors');
const { getArg } = require('./utils');
const LOG_COLORS = {
INFO: {
ansi: 6,
named: 39,
rgb: [0, 192, 255]
},
OK: {
ansi: 2,
named: 35,
rgb: [0, 192, 64]
},
WARN: {
ansi: 3,
named: 202,
rgb: [255, 112, 0]
},
ERR: {
ansi: 1,
named: 160,
rgb: [224, 0, 0]
}
};
const FG = 30;
const BG = 40;
const RESET = '\x1B[0m';
const BOLD = '\x1B[1m';
const currentTheme = {};
function buildTheme (pallette) {
let parser;
switch (pallette) {
case 'rgb':
parser = (color, offset) => `\x1B[${offset + 8};2;${color[0]};${color[1]};${color[2]}m`;
break;
case 'named':
parser = (color, offset) => `\x1B[${offset + 8};5;${color}m`;
break;
case 'ansi':
parser = (color, offset = FG) => `\x1B[${color + offset}m`;
break;
default:
process.stdout.write(`Unknown color pallette "${pallette}"\n`);
process.exit(-1);
}
for (const type in LOG_COLORS) {
currentTheme[type] = parser(LOG_COLORS[type][pallette], BG);
}
currentTheme.ERR_LINE = parser(LOG_COLORS.ERR[pallette], FG);
currentTheme.TEXT = parser(({ ansi: 7, named: 255, rgb: [255, 255, 255] })[pallette], FG);
}
// todo! doc changes
let pallette = getArg('--colors');
if (!pallette) {
if (process.env.COLORTERM) {
if (process.env.COLORTERM == 'truecolor' || process.env.COLORTERM == 'x24') {
pallette = 'rgb';
} else if (~process.env.COLORTERM.indexOf('256color')) {
pallette = 'named';
}
} else {
pallette = 'ansi';
}
}
buildTheme(pallette);
const print = (type, text) => {
process.stdout.write(`${currentTheme[type]}${currentTheme.TEXT}${BOLD} ${type} ${RESET} ${text}${RESET}\n`);
}
exports.text = text => process.stdout.write(text + '\n'),
exports.info = text => print('INFO', text),
exports.success = text => print('OK', text),
exports.warn = text => print('WARN', text),
exports.error = (text, error) => {
print('ERR', text);
if (error) {
if (!(error instanceof Array)) error = splitError(error);
for (const line of error) {
process.stdout.write(` ${currentTheme.ERR_LINE}│${RESET} ${line}\n`);
}
process.stdout.write(` ${currentTheme.ERR_LINE}└─${RESET}\n`);
}
}