-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.ts
150 lines (126 loc) · 3.39 KB
/
debug.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { Element, OutOfBoundsError } from "@jhuggett/terminal/elements/element";
import { debugMode } from ".";
const LogLevels = [
{
name: "info",
color: { r: 100, g: 255, b: 100, a: 1 },
shorthand: "inf",
},
{
name: "warning",
color: { r: 255, g: 255, b: 100, a: 1 },
shorthand: "wrn",
},
{
name: "error",
color: { r: 255, g: 100, b: 100, a: 1 },
shorthand: "err",
},
{
name: "debug",
color: { r: 100, g: 100, b: 255, a: 1 },
shorthand: "dbg",
},
] as const;
type LogLevel = (typeof LogLevels)[number]["name"];
const Topics = [
{
name: "general",
color: { r: 255, g: 255, b: 100, a: 1 },
shorthand: "General",
},
] as const;
type Topic = (typeof Topics)[number]["name"];
type Log = {
topic: (typeof Topics)[number];
level: (typeof LogLevels)[number];
message: string;
timestamp: Date;
};
export class Debug {
maxLogs = 5000;
element?: Element<void>;
logs: Log[] = [];
log(topic: Topic, level: LogLevel, message: any) {
this.logs.unshift({
level: LogLevels.find((l) => l.name === level)!,
message: Bun.inspect(message).replaceAll("\t", ""),
topic: Topics.find((t) => t.name === topic)!,
timestamp: new Date(),
});
if (this.logs.length > this.maxLogs) {
this.logs.pop();
}
if (this.element) {
this.element.render();
this.element.shell.render();
}
}
debug(message: any) {
this.log("general", "debug", message);
}
info(message: any) {
this.log("general", "info", message);
}
warning(message: any) {
this.log("general", "warning", message);
}
error(message: any) {
this.log(
"general",
"error",
message instanceof Error ? message.stack : message
);
}
registerElement(element: Element<any>) {
element.renderer = ({ cursor, bounds }) => {
cursor.properties.backgroundColor = { r: 14, g: 10, b: 10, a: 1 };
cursor.properties.foregroundColor = { r: 25, g: 25, b: 25, a: 1 };
cursor.fill(".");
cursor.properties.foregroundColor = { r: 200, g: 200, b: 200, a: 1 };
let availableHeight = bounds.height - 1;
for (const log of this.logs) {
const timestamp = log.timestamp.toLocaleTimeString();
const topic = log.topic.shorthand;
const level = log.level.shorthand;
const message = log.message.replaceAll("\n", "");
const length =
timestamp.length +
1 +
topic.length +
1 +
level.length +
1 +
message.length;
availableHeight -= Math.ceil(length / bounds.width);
if (availableHeight <= 0) {
break;
}
cursor.moveTo({ x: 2, y: availableHeight });
try {
cursor.write(timestamp + " ", {
foregroundColor: { r: 100, g: 100, b: 100, a: 1 },
});
cursor.write(topic + " ", {
foregroundColor: log.topic.color,
});
cursor.write(level + " ", {
foregroundColor: log.level.color,
});
cursor.write(message, {
foregroundColor: { r: 255, g: 255, b: 255, a: 1 },
});
} catch (error) {
if (error instanceof OutOfBoundsError) {
break;
} else {
throw error;
}
}
}
};
this.element = element;
element.render();
this.element.shell.render();
}
}