-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodifiers.ts
73 lines (66 loc) · 1.81 KB
/
modifiers.ts
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
import { type Level } from "./levels.ts";
import { type Rubiks } from "./rubiks.ts";
export type Modifier = (self: Rubiks) => Level;
interface NerdIconsModifierOptions {
error?: boolean;
warn?: boolean;
success?: boolean;
info?: boolean;
fatal?: boolean;
}
/**
* Function that returns a modifier that adds nerd icons to the default logging levels.
* @param [options] - Set what levels have a nerd icon.
* @returns {Modifier} The modifier function.
*/
export function nerdIcons(
options: NerdIconsModifierOptions = {
error: true,
warn: true,
success: true,
info: true,
fatal: true,
},
): Modifier {
return (_) => (self, _) => {
const nc = self.noColor;
if (options.error) {
self.prefixes.error = `${nc ? "" : "\x1b[31m"}\x1b[0m `;
}
if (options.fatal) {
self.prefixes.fatal = `${nc ? "" : "\x1b[31m"}\x1b[0m `;
}
if (options.warn) {
self.prefixes.warn = `${nc ? "" : "\x1b[33m"}\x1b[0m `;
}
if (options.success) {
self.prefixes.success = `${nc ? "" : "\x1b[32m"}\x1b[0m `;
}
if (options.info) {
self.prefixes.info = `${nc ? "" : "\x1b[34m"}\x1b[0m `;
}
};
}
/**
* Modifier that adds a date to the start of every log.
* @param self - The instance
* @returns The level that modifies the dates
*/
export function withDates(self: Rubiks): Level {
const c = self.prefixes.all || "";
return (self: Rubiks, _) => {
const d = new Date();
self.prefixes.all = `${c}${
self.noColor ? "" : "\x1b[90m"
}[${d.toLocaleDateString()} ${d.toLocaleTimeString()}]\x1b[0m `;
};
}
/**
* Modifier that sets noColor to true
* @param self - The instance
* @returns A fuction to be executed in every log or null
*/
export function noColor(self: Rubiks): null {
self.noColor = true;
return null;
}