-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsole.js
118 lines (98 loc) · 2.49 KB
/
Console.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import moment from 'moment';
export default class Logger {
constructor() {
const dark_colours = [
"Black",
"Red",
"Green",
"Yellow",
"Blue",
"Magenta",
"Cyan",
"White",
];
const light_colours = [
"Grey",
"LightRed",
"LightGreen",
"LightYellow",
"LightBlue",
"LightMagenta",
"LightCyan",
"LightWhite"
]
this.format = this.createEnum([
"Reset",
"Bold",
null,
null,
"Underscore",
null,
null,
null,
null,
"Strikethrough"
])
this.foreground = this.createEnum([
...new Array(30).fill(null),
...dark_colours,
...new Array(52).fill(null),
...light_colours
]);
this.background = this.createEnum([
...new Array(40).fill(null),
...dark_colours,
...new Array(53).fill(null),
...light_colours
]);
console.inline = (...args) => {
process.stdout.write(this.prefix + " " + this.custom(...args));
}
console.input = (...args) => {
return new Promise((resolve) => {
console.inline([this.custom(this.unicode(this.background.Cyan), this.unicode(this.format.Bold), "INPUT"), ...args].join(' '))
process.stdin.once('data', (data) => {
resolve(data.toString().trim());
});
})
}
const log = console.log.bind(console);
console.log = (...args) => {
log(this.prefix, ...args);
};
const warn = console.warn.bind(console);
console.warn = (...args) => {
warn(this.prefix, this.custom(this.unicode(this.background.Yellow), this.unicode(this.format.Bold), "WARN"), ...args);
};
const error = console.error.bind(console);
console.error = (...args) => {
error(this.prefix, this.custom(this.unicode(this.background.Red), this.unicode(this.format.Bold), "ERROR"), ...args);
};
const debug = console.debug.bind(console);
console.debug = (...args) => {
debug(this.prefix, this.custom(this.unicode(this.background.Blue), this.unicode(this.format.Bold), "DEBUG"), ...args);
};
const success = console.info.bind(console);
console.success = (...args) => {
success(this.prefix, this.custom(this.unicode(this.background.Green), this.unicode(this.format.Bold), "SUCCESS"), ...args);
};
}
get prefix() {
return moment().format('(kk:mm:ss.SSS)');
}
custom(...content) {
return content.join("") + this.unicode(this.format.Reset);
}
unicode(id) {
return `\x1b[${id}m`
}
createEnum(keys) {
const obj = {};
for (const [index, key] of keys.entries()) {
if (key === null) continue;
obj[key] = index;
obj[index] = key;
}
return Object.freeze(obj);
}
};