vortez 4.1.1 → 5.0.0-dev.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitignore +4 -12
- package/README.md +235 -289
- package/build/Template.d.ts +16 -2
- package/build/Template.js +20 -8
- package/build/Template.js.map +1 -1
- package/build/Vortez.d.ts +8 -15
- package/build/Vortez.js +8 -16
- package/build/Vortez.js.map +1 -1
- package/build/beta/Beta.d.ts +8 -0
- package/build/beta/Beta.js +9 -0
- package/build/beta/Beta.js.map +1 -0
- package/build/{Beta → beta}/JwtManager.js +1 -1
- package/build/{Beta → beta}/JwtManager.js.map +1 -1
- package/build/{Beta → beta}/Mail.d.ts +1 -1
- package/build/{Beta → beta}/Mail.js +1 -1
- package/build/beta/Mail.js.map +1 -0
- package/build/logger/Debug.d.ts +174 -0
- package/build/logger/Debug.js +295 -0
- package/build/logger/Debug.js.map +1 -0
- package/build/logger/Logger.d.ts +38 -0
- package/build/logger/Logger.js +48 -0
- package/build/logger/Logger.js.map +1 -0
- package/build/{Server → server}/BodyParser.js.map +1 -1
- package/build/{Server → server}/Cookie.js.map +1 -1
- package/build/server/LoggerManager.d.ts +21 -0
- package/build/server/LoggerManager.js +30 -0
- package/build/server/LoggerManager.js.map +1 -0
- package/build/{Server → server}/Request.d.ts +4 -2
- package/build/{Server → server}/Request.js +2 -1
- package/build/server/Request.js.map +1 -0
- package/build/{Server → server}/Response.d.ts +4 -1
- package/build/{Server → server}/Response.js +24 -8
- package/build/server/Response.js.map +1 -0
- package/build/{Server → server}/Server.d.ts +20 -67
- package/build/server/Server.js +211 -0
- package/build/server/Server.js.map +1 -0
- package/build/server/ServerDebug.d.ts +17 -0
- package/build/server/ServerDebug.js +67 -0
- package/build/server/ServerDebug.js.map +1 -0
- package/build/server/ServerError.d.ts +18 -0
- package/build/server/ServerError.js +15 -0
- package/build/server/ServerError.js.map +1 -0
- package/build/{Server → server}/Session.d.ts +9 -13
- package/build/{Server → server}/Session.js +34 -35
- package/build/server/Session.js.map +1 -0
- package/build/{Config.d.ts → server/config/Config.d.ts} +19 -3
- package/build/server/config/Config.js +60 -0
- package/build/server/config/Config.js.map +1 -0
- package/build/server/config/ConfigLoader.d.ts +24 -0
- package/build/server/config/ConfigLoader.js +53 -0
- package/build/server/config/ConfigLoader.js.map +1 -0
- package/build/server/config/ConfigValidator.d.ts +71 -0
- package/build/server/config/ConfigValidator.js +131 -0
- package/build/server/config/ConfigValidator.js.map +1 -0
- package/build/server/router/HttpRule.d.ts +69 -0
- package/build/server/router/HttpRule.js +96 -0
- package/build/server/router/HttpRule.js.map +1 -0
- package/build/server/router/Router.d.ts +127 -0
- package/build/server/router/Router.js +211 -0
- package/build/server/router/Router.js.map +1 -0
- package/build/server/router/Rule.d.ts +71 -0
- package/build/server/router/Rule.js +113 -0
- package/build/server/router/Rule.js.map +1 -0
- package/build/server/router/WsRule.d.ts +25 -0
- package/build/server/router/WsRule.js +34 -0
- package/build/server/router/WsRule.js.map +1 -0
- package/build/server/router/middleware/HttpMiddleware.d.ts +35 -0
- package/build/server/router/middleware/HttpMiddleware.js +90 -0
- package/build/server/router/middleware/HttpMiddleware.js.map +1 -0
- package/build/server/router/middleware/Middleware.d.ts +77 -0
- package/build/server/router/middleware/Middleware.js +69 -0
- package/build/server/router/middleware/Middleware.js.map +1 -0
- package/build/server/router/middleware/WsMiddleware.d.ts +24 -0
- package/build/server/router/middleware/WsMiddleware.js +90 -0
- package/build/server/router/middleware/WsMiddleware.js.map +1 -0
- package/build/{Server/WebSocket → server/websocket}/Chunk.js.map +1 -1
- package/build/server/websocket/Websocket.d.ts +88 -0
- package/build/{Server/WebSocket/WebSocket.js → server/websocket/Websocket.js} +91 -53
- package/build/server/websocket/Websocket.js.map +1 -0
- package/build/utilities/ConsoleUI.d.ts +88 -0
- package/build/utilities/ConsoleUI.js +141 -0
- package/build/utilities/ConsoleUI.js.map +1 -0
- package/build/utilities/DebugUI.d.ts +66 -0
- package/build/utilities/DebugUI.js +98 -0
- package/build/utilities/DebugUI.js.map +1 -0
- package/build/utilities/Env.d.ts +81 -0
- package/build/{Utilities → utilities}/Env.js +47 -35
- package/build/utilities/Env.js.map +1 -0
- package/build/utilities/Path.d.ts +24 -0
- package/build/utilities/Path.js +36 -0
- package/build/{Utilities → utilities}/Path.js.map +1 -1
- package/build/{Utilities → utilities}/Utilities.d.ts +68 -55
- package/build/{Utilities → utilities}/Utilities.js +36 -19
- package/build/utilities/Utilities.js.map +1 -0
- package/changes.md +122 -88
- package/package.json +10 -13
- package/tests/debug.js +9 -3
- package/tests/test.js +50 -48
- package/tests/utilities.js +1 -1
- package/Notes.md +0 -12
- package/build/Beta/Mail.js.map +0 -1
- package/build/Config.js +0 -33
- package/build/Config.js.map +0 -1
- package/build/ConsoleUI.d.ts +0 -57
- package/build/ConsoleUI.js +0 -110
- package/build/ConsoleUI.js.map +0 -1
- package/build/Debug.d.ts +0 -154
- package/build/Debug.js +0 -256
- package/build/Debug.js.map +0 -1
- package/build/LoggerManager/Logger.d.ts +0 -23
- package/build/LoggerManager/Logger.js +0 -23
- package/build/LoggerManager/Logger.js.map +0 -1
- package/build/LoggerManager/LoggerManager.d.ts +0 -18
- package/build/LoggerManager/LoggerManager.js +0 -30
- package/build/LoggerManager/LoggerManager.js.map +0 -1
- package/build/Server/Request.js.map +0 -1
- package/build/Server/Response.js.map +0 -1
- package/build/Server/Rule.d.ts +0 -81
- package/build/Server/Rule.js +0 -146
- package/build/Server/Rule.js.map +0 -1
- package/build/Server/Server.js +0 -330
- package/build/Server/Server.js.map +0 -1
- package/build/Server/Session.js.map +0 -1
- package/build/Server/WebSocket/WebSocket.d.ts +0 -70
- package/build/Server/WebSocket/WebSocket.js.map +0 -1
- package/build/Utilities/Env.d.ts +0 -75
- package/build/Utilities/Env.js.map +0 -1
- package/build/Utilities/Path.d.ts +0 -18
- package/build/Utilities/Path.js +0 -27
- package/build/Utilities/Utilities.js.map +0 -1
- /package/build/{Beta → beta}/JwtManager.d.ts +0 -0
- /package/build/{Server → server}/BodyParser.d.ts +0 -0
- /package/build/{Server → server}/BodyParser.js +0 -0
- /package/build/{Server → server}/Cookie.d.ts +0 -0
- /package/build/{Server → server}/Cookie.js +0 -0
- /package/build/{Server/WebSocket → server/websocket}/Chunk.d.ts +0 -0
- /package/build/{Server/WebSocket → server/websocket}/Chunk.js +0 -0
|
@@ -1,70 +1,104 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @author NetFeez <codefeez.dev@gmail.com>
|
|
3
|
-
* @description
|
|
3
|
+
* @description adds websocket functionality to Vortez
|
|
4
4
|
* @license Apache-2.0
|
|
5
5
|
*/
|
|
6
6
|
import EVENTS from 'events';
|
|
7
7
|
import CRYPTO from 'crypto';
|
|
8
8
|
import Chunk from './Chunk.js';
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
import LoggerManager from '../LoggerManager.js';
|
|
10
|
+
const logger = LoggerManager.getInstance().webSocket;
|
|
11
|
+
export { Chunk } from './Chunk.js';
|
|
12
|
+
export class Websocket extends EVENTS {
|
|
13
|
+
request;
|
|
11
14
|
connection;
|
|
15
|
+
static WEBSOCKET_GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
|
|
16
|
+
_status = 'pending';
|
|
12
17
|
/**
|
|
13
|
-
*
|
|
14
|
-
* @param client
|
|
18
|
+
* Creates a new websocket.
|
|
19
|
+
* @param client The connection with the client.
|
|
15
20
|
*/
|
|
16
|
-
constructor(
|
|
21
|
+
constructor(request, connection) {
|
|
17
22
|
super();
|
|
18
|
-
this.
|
|
23
|
+
this.request = request;
|
|
24
|
+
this.connection = connection;
|
|
19
25
|
this.initEvents();
|
|
20
26
|
}
|
|
27
|
+
/** Whether the connection is closed. */
|
|
28
|
+
get isClosed() { return this.connection.readableEnded; }
|
|
29
|
+
get status() { return this._status; }
|
|
21
30
|
/**
|
|
22
|
-
*
|
|
23
|
-
* @param acceptKey La llave de conexión `sec-websocket-key`.
|
|
31
|
+
* Accepts the connection with the client.
|
|
24
32
|
*/
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
const headers = [
|
|
33
|
+
accept() {
|
|
34
|
+
this._status = 'accepted';
|
|
35
|
+
const key = this.request.headers['sec-websocket-key']?.trim();
|
|
36
|
+
const acceptToken = CRYPTO.createHash('SHA1').update(key + Websocket.WEBSOCKET_GUID).digest('base64');
|
|
37
|
+
const headers = this.buildHeaders([
|
|
31
38
|
'HTTP/1.1 101 Switching Protocols',
|
|
32
39
|
'Upgrade: websocket',
|
|
33
40
|
'Connection: Upgrade',
|
|
34
|
-
`Sec-WebSocket-Accept: ${acceptToken}
|
|
35
|
-
|
|
36
|
-
|
|
41
|
+
`Sec-WebSocket-Accept: ${acceptToken}`
|
|
42
|
+
], this.request.cookies);
|
|
43
|
+
this.connection.write(headers);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Rejects the connection with the client.
|
|
47
|
+
* @param code The code that will be sent to the client.
|
|
48
|
+
* @param reason The reason that will be sent to the client.
|
|
49
|
+
*/
|
|
50
|
+
reject(code, reason) {
|
|
51
|
+
this._status = 'rejected';
|
|
52
|
+
const body = JSON.stringify({ code, reason }, null, 4);
|
|
53
|
+
const headers = this.buildHeaders([
|
|
54
|
+
`HTTP/1.1 ${code} ${reason}`,
|
|
55
|
+
'Content-Type: application/json',
|
|
56
|
+
`Content-Length: ${Buffer.byteLength(body)}`,
|
|
57
|
+
'Connection: close'
|
|
58
|
+
], this.request.cookies);
|
|
37
59
|
this.connection.write(headers);
|
|
60
|
+
this.connection.end(body);
|
|
38
61
|
}
|
|
39
|
-
/**
|
|
62
|
+
/** Finishes the connection. */
|
|
40
63
|
end() { this.connection.end(); }
|
|
41
|
-
/**
|
|
64
|
+
/** Destroys the connection. */
|
|
42
65
|
destroy() { this.connection.destroy(); }
|
|
43
66
|
/**
|
|
44
|
-
*
|
|
45
|
-
* @param data
|
|
67
|
+
* Send data to the client.
|
|
68
|
+
* @param data The data that will be sent.
|
|
46
69
|
*/
|
|
47
70
|
send(data) {
|
|
71
|
+
if (this.status == 'rejected')
|
|
72
|
+
return void logger.warn('You cannot send data to a rejected websocket connection');
|
|
73
|
+
if (this.status == 'pending')
|
|
74
|
+
return void logger.warn('You cannot send data to a pending websocket connection');
|
|
75
|
+
this.write(data);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Send data to the client in JSON format.
|
|
79
|
+
* @param data The data that will be sent.
|
|
80
|
+
*/
|
|
81
|
+
sendJson(data) {
|
|
82
|
+
const message = JSON.stringify(data);
|
|
83
|
+
this.send(message);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Writes data to the client socket.
|
|
87
|
+
* @param data The data that will be sent.
|
|
88
|
+
*/
|
|
89
|
+
write(data) {
|
|
48
90
|
const [buffer, isText] = typeof data == 'string'
|
|
49
91
|
? [this.stringToBuffer(data), true]
|
|
50
92
|
: data instanceof Buffer
|
|
51
93
|
? [data, false]
|
|
52
|
-
: [this.stringToBuffer('[Error]:
|
|
94
|
+
: [this.stringToBuffer('[Error]: unsupported data type MyNetFeez-Labs.Vortez.Websocket.send'), true];
|
|
53
95
|
const message = this.encode(buffer, isText);
|
|
54
96
|
this.connection.write(message);
|
|
55
97
|
}
|
|
56
98
|
/**
|
|
57
|
-
*
|
|
58
|
-
* @param data
|
|
59
|
-
|
|
60
|
-
SendJson(data) {
|
|
61
|
-
const message = JSON.stringify(data);
|
|
62
|
-
this.send(message);
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Codifica los datos para enviar por el WebSocket.
|
|
66
|
-
* @param data Los datos que se van a codificar.
|
|
67
|
-
* @param Text indica si el contenido es texto o no.
|
|
99
|
+
* Encodes the data to be sent to the client.
|
|
100
|
+
* @param data The data that will be encoded.
|
|
101
|
+
* @param isText Whether the data is a text or a binary.
|
|
68
102
|
*/
|
|
69
103
|
encode(data, isText = false) {
|
|
70
104
|
const encoded = isText ? [129] : [130];
|
|
@@ -87,20 +121,17 @@ export class WebSocket extends EVENTS {
|
|
|
87
121
|
encoded[8] = (data.length >> 8) & 255;
|
|
88
122
|
encoded[9] = (data.length) & 255;
|
|
89
123
|
}
|
|
90
|
-
/* for (let index = 0; index < data.length; index ++) {
|
|
91
|
-
encoded.push(data[index]);
|
|
92
|
-
} */
|
|
93
124
|
encoded.push(...data);
|
|
94
125
|
return Buffer.from(encoded);
|
|
95
126
|
}
|
|
96
127
|
/**
|
|
97
|
-
*
|
|
98
|
-
* @param message
|
|
128
|
+
* Converts a string to a buffer.
|
|
129
|
+
* @param message The string that will be converted.
|
|
99
130
|
*/
|
|
100
131
|
stringToBuffer(message) {
|
|
101
132
|
return Buffer.from(message, 'utf-8');
|
|
102
133
|
}
|
|
103
|
-
/**
|
|
134
|
+
/** Initializes the events. */
|
|
104
135
|
initEvents() {
|
|
105
136
|
let surplus = Buffer.alloc(0);
|
|
106
137
|
let currentChunk = null;
|
|
@@ -138,17 +169,23 @@ export class WebSocket extends EVENTS {
|
|
|
138
169
|
off(event, listener) {
|
|
139
170
|
return super.off(event, listener);
|
|
140
171
|
}
|
|
172
|
+
buildHeaders(lines, cookies) {
|
|
173
|
+
const cookieSetters = cookies
|
|
174
|
+
? cookies.getSetters().map((setter) => `Set-Cookie: ${setter}`)
|
|
175
|
+
: [];
|
|
176
|
+
return [...lines, ...cookieSetters, '\r\n'].join('\r\n');
|
|
177
|
+
}
|
|
141
178
|
}
|
|
142
|
-
export default
|
|
143
|
-
/*
|
|
179
|
+
export default Websocket;
|
|
180
|
+
/* To accept a connection
|
|
144
181
|
*
|
|
145
182
|
* HTTP/1.1 101 Switching Protocols
|
|
146
183
|
* Upgrade: websocket
|
|
147
184
|
* Connection: Upgrade
|
|
148
185
|
* Sec-WebSocket-Accept: Sec-Websocket-key + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
|
|
149
|
-
* en HASH SHA-1
|
|
186
|
+
* en HASH SHA-1 encoded in Base64
|
|
150
187
|
*
|
|
151
|
-
**
|
|
188
|
+
** Format to data exchange
|
|
152
189
|
* 0 1 2 3
|
|
153
190
|
* 0 1 2 3
|
|
154
191
|
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
|
@@ -170,15 +207,16 @@ export default WebSocket;
|
|
|
170
207
|
* +---------------------------------------------------------------+
|
|
171
208
|
*
|
|
172
209
|
*
|
|
173
|
-
* FIN
|
|
174
|
-
*
|
|
210
|
+
* FIN and OPCODE works together to deliver messages with a separate frame
|
|
211
|
+
* Only available in OPCODE 0x0 to 0x2
|
|
212
|
+
*
|
|
175
213
|
*
|
|
176
214
|
* OPCODES:
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
215
|
+
* 0x0: continuation
|
|
216
|
+
* 0x1: text
|
|
217
|
+
* 0x2: binary
|
|
218
|
+
* 0x8: close
|
|
219
|
+
* 0x9: ping
|
|
220
|
+
* 0xA: pong
|
|
183
221
|
*/
|
|
184
|
-
//# sourceMappingURL=
|
|
222
|
+
//# sourceMappingURL=Websocket.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Websocket.js","sourceRoot":"","sources":["../../../src/server/websocket/Websocket.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,OAAO,KAAK,MAAM,YAAY,CAAC;AAG/B,OAAO,aAAa,MAAM,qBAAqB,CAAC;AAEhD,MAAM,MAAM,GAAG,aAAa,CAAC,WAAW,EAAE,CAAC,SAAS,CAAC;AAErD,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAGnC,MAAM,OAAO,SAAU,SAAQ,MAAM;IAQb;IACA;IARb,MAAM,CAAU,cAAc,GAAG,sCAAsC,CAAC;IACvE,OAAO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CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;IACtC,CAAC;IACO,YAAY,CAAC,KAAe,EAAE,OAAgB;QAClD,MAAM,aAAa,GAAG,OAAO;YACzB,CAAC,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,eAAe,MAAM,EAAE,CAAC;YAC/D,CAAC,CAAC,EAAE,CAAC;QACT,OAAO,CAAC,GAAG,KAAK,EAAE,GAAG,aAAa,EAAE,MAAM,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC7D,CAAC;;AAiBL,eAAe,SAAS,CAAC;AAEzB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author NetFeez <codefeez.dev@gmail.com>
|
|
3
|
+
* @description Provides utilities for console interaction, including text formatting and coloring.
|
|
4
|
+
* @license Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
export declare class ConsoleUI {
|
|
7
|
+
private static formatString;
|
|
8
|
+
private static rgbString;
|
|
9
|
+
private static readonly formats;
|
|
10
|
+
/**
|
|
11
|
+
* Delete text formats and colors.
|
|
12
|
+
* Removes all custom formatting and color codes from a string,
|
|
13
|
+
* returning a plain text string.
|
|
14
|
+
* @param text - The input string potentially containing formatting codes.
|
|
15
|
+
* @param prefix - The custom prefix used for formatting codes (default: '&').
|
|
16
|
+
*/
|
|
17
|
+
static cleanFormat(text: string, prefix?: string): string;
|
|
18
|
+
/**
|
|
19
|
+
* Applies formatting and colors to text for console output using specific codes.
|
|
20
|
+
* This method processes a string, replacing custom formatting and color codes with
|
|
21
|
+
* ANSI escape codes to enable rich text output in terminals that support them.
|
|
22
|
+
*
|
|
23
|
+
* @param text - The input string containing formatting codes.
|
|
24
|
+
* @param prefix - The character used to denote a formatting or color code.
|
|
25
|
+
* Defaults to `&`.
|
|
26
|
+
*
|
|
27
|
+
* @returns The formatted string with ANSI escape codes.
|
|
28
|
+
*
|
|
29
|
+
* ---
|
|
30
|
+
*
|
|
31
|
+
* The formatting and coloring system works by identifying sequences starting with the
|
|
32
|
+
* defined `prefix` followed by a specific code. These codes are then replaced with
|
|
33
|
+
* the corresponding ANSI escape codes.
|
|
34
|
+
*
|
|
35
|
+
* **Available Codes:**
|
|
36
|
+
*
|
|
37
|
+
* | Code | Description | Example |
|
|
38
|
+
* | ---------------- | ----------------------------------------------- | -------------- |
|
|
39
|
+
* | `%prefix%N` | **Bold** | `&NBold Text&R`|
|
|
40
|
+
* | `%prefix%S` | Underline | `&SUnderlined&R`|
|
|
41
|
+
* | `%prefix%P` | Blink (Support varies) | `&PBlinking&R` |
|
|
42
|
+
* | `%prefix%I` | Invert (Swap foreground/background) | `&IInverted&R` |
|
|
43
|
+
* | `%prefix%R` | Reset (All formatting) | `&RNormal&R` |
|
|
44
|
+
*
|
|
45
|
+
* | Standard Colors | Description | Foreground Code | Background Code | Example |
|
|
46
|
+
* | --------------- | ----------------- | --------------- | --------------- | ------------- |
|
|
47
|
+
* | `0` | Black | `%prefix%C0` | `%prefix%B0` | `&C0Black&R` |
|
|
48
|
+
* | `1` | Red | `%prefix%C1` | `%prefix%B1` | `&B1Red BG&R` |
|
|
49
|
+
* | `2` | Green | `%prefix%C2` | `%prefix%B2` | `&C2Green&R`|
|
|
50
|
+
* | `3` | Yellow | `%prefix%C3` | `%prefix%B3` | `&B3Yellow BG&R`|
|
|
51
|
+
* | `4` | Blue | `%prefix%C4` | `%prefix%B4` | `&C4Blue&R` |
|
|
52
|
+
* | `5` | Magenta | `%prefix%C5` | `%prefix%B5` | `&B5Magenta BG&R`|
|
|
53
|
+
* | `6` | Cyan | `%prefix%C6` | `%prefix%B6` | `&C6Cyan&R` |
|
|
54
|
+
* | `7` | White | `%prefix%C7` | `%prefix%B7` | `&B7White BG&R`|
|
|
55
|
+
*
|
|
56
|
+
* | RGB Colors | Description | Example (Text) | Example (BG) |
|
|
57
|
+
* | ------------------- | --------------------------------------------- | -------------- | ------------ |
|
|
58
|
+
* | `%prefix%C(R,G,B)` | Foreground color using RGB (0-255 for R,G,B) | `&C(255,0,0)Red` | N/A |
|
|
59
|
+
* | `%prefix%B(R,G,B)` | Background color using RGB (0-255 for R,G,B) | N/A | `&B(0,0,255)Blue BG` |
|
|
60
|
+
*
|
|
61
|
+
* **Notes:**
|
|
62
|
+
*
|
|
63
|
+
* - Do not include spaces after the commas within the parentheses for RGB codes.
|
|
64
|
+
* - Multiple codes can be combined, e.g., `&N&C1Bold Red Text&R`.
|
|
65
|
+
* - The `%prefix%R` code is automatically appended to the end of the output string
|
|
66
|
+
* to ensure subsequent console output is not affected by the formatting.
|
|
67
|
+
* - Terminal support for colors and formatting may vary.
|
|
68
|
+
* ---
|
|
69
|
+
*
|
|
70
|
+
* **Examples:**
|
|
71
|
+
* - `&N&C4Bold Blue Text&R`: Displays "Bold Blue Text" in bold and blue, then resets formatting.
|
|
72
|
+
* - `&B2Green Background&R`: Displays "Green Background" with a green background and default text color, then resets.
|
|
73
|
+
* - `&C(255,165,0)&SOrange Underlined Text&R`: Displays "Orange Underlined Text" in orange and underlined, then resets.
|
|
74
|
+
* - `&IInverted Colors&R`: Displays "Inverted Colors" with foreground and background colors swapped, then resets.
|
|
75
|
+
* - `&N&C1Error: &R&C7File not found.` : Displays "Error:" in bold red and "File not found." in white, then resets.
|
|
76
|
+
*/
|
|
77
|
+
static formatText(text: string, prefix?: string): string;
|
|
78
|
+
/**
|
|
79
|
+
* Sends a message to the user through the console.
|
|
80
|
+
* @param message - The message(s) you want to send to the user.
|
|
81
|
+
* @param newLine - Whether there is a line break or not.
|
|
82
|
+
*/
|
|
83
|
+
static send(message: string | string[], newLine?: boolean): void;
|
|
84
|
+
}
|
|
85
|
+
export declare namespace ConsoleUI {
|
|
86
|
+
type formatKey = ('N' | 'S' | 'P' | 'I' | 'R' | 'C0' | 'C1' | 'C2' | 'C3' | 'C4' | 'C5' | 'C6' | 'C7' | 'C' | 'B0' | 'B1' | 'B2' | 'B3' | 'B4' | 'B5' | 'B6' | 'B7' | 'B');
|
|
87
|
+
}
|
|
88
|
+
export default ConsoleUI;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author NetFeez <codefeez.dev@gmail.com>
|
|
3
|
+
* @description Provides utilities for console interaction, including text formatting and coloring.
|
|
4
|
+
* @license Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
export class ConsoleUI {
|
|
7
|
+
static formatString = '%prefix%((?:(?:[BC])[0-7])|[NSPIR])';
|
|
8
|
+
static rgbString = '%prefix%(?:([BC])\\((?:([0-2]?[0-9]{1,2}), ?([0-2]?[0-9]{1,2}), ?([0-2]?[0-9]{1,2}))\\))';
|
|
9
|
+
static formats = {
|
|
10
|
+
// Var: Code // Text color
|
|
11
|
+
C0: '\x1B[30m', // Black
|
|
12
|
+
C1: '\x1B[31m', // Red
|
|
13
|
+
C2: '\x1B[32m', // Green
|
|
14
|
+
C3: '\x1B[33m', // Yellow
|
|
15
|
+
C4: '\x1B[34m', // Blue
|
|
16
|
+
C5: '\x1B[35m', // Magenta
|
|
17
|
+
C6: '\x1B[36m', // Cyan
|
|
18
|
+
C7: '\x1B[37m', // White
|
|
19
|
+
C: '\x1B[38;2;R;G;Bm', // (R,G,B)
|
|
20
|
+
// Var: Code // Background color
|
|
21
|
+
B0: '\x1B[40m', // Black background
|
|
22
|
+
B1: '\x1B[41m', // Red background
|
|
23
|
+
B2: '\x1B[42m', // Green background
|
|
24
|
+
B3: '\x1B[43m', // Yellow background
|
|
25
|
+
B4: '\x1B[44m', // Blue background
|
|
26
|
+
B5: '\x1B[45m', // Magenta background
|
|
27
|
+
B6: '\x1B[46m', // Cyan background
|
|
28
|
+
B7: '\x1B[47m', // White background
|
|
29
|
+
B: '\x1B[48;2;R;G;Bm', // (R,G,B)
|
|
30
|
+
// Var: Code // Text format
|
|
31
|
+
N: '\x1B[1m', // Bold
|
|
32
|
+
S: '\x1B[4m', // Underline
|
|
33
|
+
P: '\x1B[5m', // Blink
|
|
34
|
+
I: '\x1B[7m', // Invert
|
|
35
|
+
R: '\x1B[0m', // Reset
|
|
36
|
+
none: ''
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Delete text formats and colors.
|
|
40
|
+
* Removes all custom formatting and color codes from a string,
|
|
41
|
+
* returning a plain text string.
|
|
42
|
+
* @param text - The input string potentially containing formatting codes.
|
|
43
|
+
* @param prefix - The custom prefix used for formatting codes (default: '&').
|
|
44
|
+
*/
|
|
45
|
+
static cleanFormat(text, prefix = '&') {
|
|
46
|
+
const formatExp = new RegExp(this.formatString.replace('%prefix%', prefix), 'g');
|
|
47
|
+
const rgbExp = new RegExp(this.rgbString.replace('%prefix%', prefix), 'g');
|
|
48
|
+
return text.replace(formatExp, '').replace(rgbExp, '');
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Applies formatting and colors to text for console output using specific codes.
|
|
52
|
+
* This method processes a string, replacing custom formatting and color codes with
|
|
53
|
+
* ANSI escape codes to enable rich text output in terminals that support them.
|
|
54
|
+
*
|
|
55
|
+
* @param text - The input string containing formatting codes.
|
|
56
|
+
* @param prefix - The character used to denote a formatting or color code.
|
|
57
|
+
* Defaults to `&`.
|
|
58
|
+
*
|
|
59
|
+
* @returns The formatted string with ANSI escape codes.
|
|
60
|
+
*
|
|
61
|
+
* ---
|
|
62
|
+
*
|
|
63
|
+
* The formatting and coloring system works by identifying sequences starting with the
|
|
64
|
+
* defined `prefix` followed by a specific code. These codes are then replaced with
|
|
65
|
+
* the corresponding ANSI escape codes.
|
|
66
|
+
*
|
|
67
|
+
* **Available Codes:**
|
|
68
|
+
*
|
|
69
|
+
* | Code | Description | Example |
|
|
70
|
+
* | ---------------- | ----------------------------------------------- | -------------- |
|
|
71
|
+
* | `%prefix%N` | **Bold** | `&NBold Text&R`|
|
|
72
|
+
* | `%prefix%S` | Underline | `&SUnderlined&R`|
|
|
73
|
+
* | `%prefix%P` | Blink (Support varies) | `&PBlinking&R` |
|
|
74
|
+
* | `%prefix%I` | Invert (Swap foreground/background) | `&IInverted&R` |
|
|
75
|
+
* | `%prefix%R` | Reset (All formatting) | `&RNormal&R` |
|
|
76
|
+
*
|
|
77
|
+
* | Standard Colors | Description | Foreground Code | Background Code | Example |
|
|
78
|
+
* | --------------- | ----------------- | --------------- | --------------- | ------------- |
|
|
79
|
+
* | `0` | Black | `%prefix%C0` | `%prefix%B0` | `&C0Black&R` |
|
|
80
|
+
* | `1` | Red | `%prefix%C1` | `%prefix%B1` | `&B1Red BG&R` |
|
|
81
|
+
* | `2` | Green | `%prefix%C2` | `%prefix%B2` | `&C2Green&R`|
|
|
82
|
+
* | `3` | Yellow | `%prefix%C3` | `%prefix%B3` | `&B3Yellow BG&R`|
|
|
83
|
+
* | `4` | Blue | `%prefix%C4` | `%prefix%B4` | `&C4Blue&R` |
|
|
84
|
+
* | `5` | Magenta | `%prefix%C5` | `%prefix%B5` | `&B5Magenta BG&R`|
|
|
85
|
+
* | `6` | Cyan | `%prefix%C6` | `%prefix%B6` | `&C6Cyan&R` |
|
|
86
|
+
* | `7` | White | `%prefix%C7` | `%prefix%B7` | `&B7White BG&R`|
|
|
87
|
+
*
|
|
88
|
+
* | RGB Colors | Description | Example (Text) | Example (BG) |
|
|
89
|
+
* | ------------------- | --------------------------------------------- | -------------- | ------------ |
|
|
90
|
+
* | `%prefix%C(R,G,B)` | Foreground color using RGB (0-255 for R,G,B) | `&C(255,0,0)Red` | N/A |
|
|
91
|
+
* | `%prefix%B(R,G,B)` | Background color using RGB (0-255 for R,G,B) | N/A | `&B(0,0,255)Blue BG` |
|
|
92
|
+
*
|
|
93
|
+
* **Notes:**
|
|
94
|
+
*
|
|
95
|
+
* - Do not include spaces after the commas within the parentheses for RGB codes.
|
|
96
|
+
* - Multiple codes can be combined, e.g., `&N&C1Bold Red Text&R`.
|
|
97
|
+
* - The `%prefix%R` code is automatically appended to the end of the output string
|
|
98
|
+
* to ensure subsequent console output is not affected by the formatting.
|
|
99
|
+
* - Terminal support for colors and formatting may vary.
|
|
100
|
+
* ---
|
|
101
|
+
*
|
|
102
|
+
* **Examples:**
|
|
103
|
+
* - `&N&C4Bold Blue Text&R`: Displays "Bold Blue Text" in bold and blue, then resets formatting.
|
|
104
|
+
* - `&B2Green Background&R`: Displays "Green Background" with a green background and default text color, then resets.
|
|
105
|
+
* - `&C(255,165,0)&SOrange Underlined Text&R`: Displays "Orange Underlined Text" in orange and underlined, then resets.
|
|
106
|
+
* - `&IInverted Colors&R`: Displays "Inverted Colors" with foreground and background colors swapped, then resets.
|
|
107
|
+
* - `&N&C1Error: &R&C7File not found.` : Displays "Error:" in bold red and "File not found." in white, then resets.
|
|
108
|
+
*/
|
|
109
|
+
static formatText(text, prefix = '&') {
|
|
110
|
+
const formatExp = new RegExp(this.formatString.replace('%prefix%', prefix), 'g');
|
|
111
|
+
const rgbExp = new RegExp(this.rgbString.replace('%prefix%', prefix), 'g');
|
|
112
|
+
return `${text
|
|
113
|
+
.replace(formatExp, (result, format) => this.formats[format])
|
|
114
|
+
.replace(rgbExp, (result, type, R, G, B) => (this.formats[type]
|
|
115
|
+
.replace('R', R)
|
|
116
|
+
.replace('G', G)
|
|
117
|
+
.replace('B', B)))}${this.formats.R}`;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Sends a message to the user through the console.
|
|
121
|
+
* @param message - The message(s) you want to send to the user.
|
|
122
|
+
* @param newLine - Whether there is a line break or not.
|
|
123
|
+
*/
|
|
124
|
+
static send(message, newLine) {
|
|
125
|
+
if (typeof message === 'string') {
|
|
126
|
+
process.stdout.write(`${message}${newLine ? '\n' : ''}`, 'utf8');
|
|
127
|
+
}
|
|
128
|
+
else if (Array.isArray(message)) {
|
|
129
|
+
message.forEach((value) => {
|
|
130
|
+
this.send(value);
|
|
131
|
+
});
|
|
132
|
+
if (newLine)
|
|
133
|
+
process.stdout.write(`\n`, 'utf8');
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
process.stdout.write('[consoleUI] You tried to send a failed type \n');
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
export default ConsoleUI;
|
|
141
|
+
//# sourceMappingURL=ConsoleUI.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ConsoleUI.js","sourceRoot":"","sources":["../../src/utilities/ConsoleUI.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,OAAO,SAAS;IACV,MAAM,CAAC,YAAY,GAAG,qCAAqC,CAAC;IAC5D,MAAM,CAAC,SAAS,GAAG,0FAA0F,CAAC;IAC9G,MAAM,CAAU,OAAO,GAAG;QAC9B,6BAA6B;QAC7B,EAAE,EAAE,UAAU,EAAE,QAAQ;QACxB,EAAE,EAAE,UAAU,EAAE,MAAM;QACtB,EAAE,EAAE,UAAU,EAAE,QAAQ;QACxB,EAAE,EAAE,UAAU,EAAE,SAAS;QACzB,EAAE,EAAE,UAAU,EAAE,OAAO;QACvB,EAAE,EAAE,UAAU,EAAE,UAAU;QAC1B,EAAE,EAAE,UAAU,EAAE,OAAO;QACvB,EAAE,EAAE,UAAU,EAAE,QAAQ;QACxB,CAAC,EAAE,kBAAkB,EAAE,UAAU;QACjC,mCAAmC;QACnC,EAAE,EAAE,UAAU,EAAE,mBAAmB;QACnC,EAAE,EAAE,UAAU,EAAE,iBAAiB;QACjC,EAAE,EAAE,UAAU,EAAE,mBAAmB;QACnC,EAAE,EAAE,UAAU,EAAE,oBAAoB;QACpC,EAAE,EAAE,UAAU,EAAE,kBAAkB;QAClC,EAAE,EAAE,UAAU,EAAE,qBAAqB;QACrC,EAAE,EAAE,UAAU,EAAE,kBAAkB;QAClC,EAAE,EAAE,UAAU,EAAE,mBAAmB;QACnC,CAAC,EAAE,kBAAkB,EAAE,UAAU;QACjC,+BAA+B;QAC/B,CAAC,EAAK,SAAS,EAAE,OAAO;QACxB,CAAC,EAAK,SAAS,EA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E,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,IAA2B,CAAC;aACjF,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;aACf,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;aACf,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,CACnB,CACL,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC;IACxB,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,IAAI,CAAC,OAA0B,EAAE,OAAiB;QAC5D,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;YAC9B,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC;QACrE,CAAC;aAAM,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;YAChC,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE;gBACtB,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YACrB,CAAC,CAAC,CAAC;YACH,IAAI,OAAO;gBAAE,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QACpD,CAAC;aAAM,CAAC;YACJ,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,gDAAgD,CAAC,CAAC;QAC3E,CAAC;IACL,CAAC;;AAWL,eAAe,SAAS,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author NetFeez <codefeez.dev@gmail.com>
|
|
3
|
+
* @description Utility to help with debugging tasks and server development.
|
|
4
|
+
* @license Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import Logger from '../logger/Logger.js';
|
|
7
|
+
export declare class DebugUI {
|
|
8
|
+
protected dataHandleInstance: DebugUI.dataHandler;
|
|
9
|
+
prompt: string;
|
|
10
|
+
in: NodeJS.ReadStream;
|
|
11
|
+
out: Logger;
|
|
12
|
+
commandMap: DebugUI.CommandMap;
|
|
13
|
+
constructor(prefix?: string);
|
|
14
|
+
/** Starts the console UI. */
|
|
15
|
+
protected startReadIn(): void;
|
|
16
|
+
/** Stops the console UI. */
|
|
17
|
+
protected stopReadIn(): void;
|
|
18
|
+
/** Starts the console UI. */
|
|
19
|
+
start(): void;
|
|
20
|
+
/** Stops the console UI. */
|
|
21
|
+
stop(): void;
|
|
22
|
+
/**
|
|
23
|
+
* Handles incoming data from the client.
|
|
24
|
+
* @param data - The incoming data buffer.
|
|
25
|
+
*/
|
|
26
|
+
protected dataHandler(data: Buffer): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Gets a command by name.
|
|
29
|
+
* @param command - The name of the command.
|
|
30
|
+
* @returns The command or undefined if not found.
|
|
31
|
+
*/
|
|
32
|
+
getCommand(command: string): DebugUI.Command | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Adds a command to the command map.
|
|
35
|
+
* @param command - The name of the command.
|
|
36
|
+
* @param exec - The command execution function.
|
|
37
|
+
* @param info - Additional information about the command.
|
|
38
|
+
* @returns The DebugUI instance for chaining.
|
|
39
|
+
*/
|
|
40
|
+
addCommand(command: string, exec: DebugUI.commandExec, info?: DebugUI.CommandInfo): this;
|
|
41
|
+
/**
|
|
42
|
+
* Removes a command from the command map.
|
|
43
|
+
* @param command - The name of the command.
|
|
44
|
+
* @returns The DebugUI instance for chaining.
|
|
45
|
+
*/
|
|
46
|
+
removeCommand(command: string): this;
|
|
47
|
+
/** Prints the command map to the console.*/
|
|
48
|
+
protected addDefaultCommands(): void;
|
|
49
|
+
protected showHelp(): void;
|
|
50
|
+
}
|
|
51
|
+
export declare namespace DebugUI {
|
|
52
|
+
type dataHandler = (data: Buffer) => void;
|
|
53
|
+
type commandExec = (this: DebugUI, command: string, args: string[]) => Promise<void> | void;
|
|
54
|
+
interface CommandInfo {
|
|
55
|
+
description?: string;
|
|
56
|
+
usage?: string;
|
|
57
|
+
}
|
|
58
|
+
interface Command extends CommandInfo {
|
|
59
|
+
name: string;
|
|
60
|
+
exec: commandExec;
|
|
61
|
+
}
|
|
62
|
+
interface CommandMap {
|
|
63
|
+
[command: string]: Command;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
export default DebugUI;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author NetFeez <codefeez.dev@gmail.com>
|
|
3
|
+
* @description Utility to help with debugging tasks and server development.
|
|
4
|
+
* @license Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import ConsoleUI from './ConsoleUI.js';
|
|
7
|
+
import Logger from '../logger/Logger.js';
|
|
8
|
+
export class DebugUI {
|
|
9
|
+
dataHandleInstance;
|
|
10
|
+
prompt;
|
|
11
|
+
in;
|
|
12
|
+
out;
|
|
13
|
+
commandMap;
|
|
14
|
+
constructor(prefix = 'C-UI') {
|
|
15
|
+
this.dataHandleInstance = this.dataHandler.bind(this);
|
|
16
|
+
this.prompt = ConsoleUI.formatText("&C(255,180,220)[Vortez] << &C6&S");
|
|
17
|
+
this.in = process.stdin;
|
|
18
|
+
this.out = new Logger({ prefix, debug: 'console-ui' });
|
|
19
|
+
this.commandMap = {};
|
|
20
|
+
this.addDefaultCommands();
|
|
21
|
+
}
|
|
22
|
+
/** Starts the console UI. */
|
|
23
|
+
startReadIn() {
|
|
24
|
+
this.in.on('data', this.dataHandleInstance);
|
|
25
|
+
ConsoleUI.send(this.prompt);
|
|
26
|
+
}
|
|
27
|
+
/** Stops the console UI. */
|
|
28
|
+
stopReadIn() { this.in.off('data', this.dataHandleInstance); }
|
|
29
|
+
/** Starts the console UI. */
|
|
30
|
+
start() { this.startReadIn(); }
|
|
31
|
+
/** Stops the console UI. */
|
|
32
|
+
stop() { this.stopReadIn(); }
|
|
33
|
+
/**
|
|
34
|
+
* Handles incoming data from the client.
|
|
35
|
+
* @param data - The incoming data buffer.
|
|
36
|
+
*/
|
|
37
|
+
async dataHandler(data) {
|
|
38
|
+
const [cmd, ...args] = data.toString('utf-8').trim().split(' ');
|
|
39
|
+
this.out.log(`&C(80,0,80)command received: &C3${cmd}`, args);
|
|
40
|
+
try {
|
|
41
|
+
const command = this.commandMap[cmd];
|
|
42
|
+
if (command)
|
|
43
|
+
await command.exec.bind(this)(cmd, args);
|
|
44
|
+
else
|
|
45
|
+
this.out.error(`&C1Unknown command: &C3${cmd}`);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
this.out.error(`&C1Error executing command: &C3${cmd}`, error);
|
|
49
|
+
}
|
|
50
|
+
if (cmd !== 'exit-debug')
|
|
51
|
+
ConsoleUI.send(this.prompt);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Gets a command by name.
|
|
55
|
+
* @param command - The name of the command.
|
|
56
|
+
* @returns The command or undefined if not found.
|
|
57
|
+
*/
|
|
58
|
+
getCommand(command) { return this.commandMap[command]; }
|
|
59
|
+
/**
|
|
60
|
+
* Adds a command to the command map.
|
|
61
|
+
* @param command - The name of the command.
|
|
62
|
+
* @param exec - The command execution function.
|
|
63
|
+
* @param info - Additional information about the command.
|
|
64
|
+
* @returns The DebugUI instance for chaining.
|
|
65
|
+
*/
|
|
66
|
+
addCommand(command, exec, info) {
|
|
67
|
+
this.commandMap[command] = { name: command, exec, ...info };
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Removes a command from the command map.
|
|
72
|
+
* @param command - The name of the command.
|
|
73
|
+
* @returns The DebugUI instance for chaining.
|
|
74
|
+
*/
|
|
75
|
+
removeCommand(command) {
|
|
76
|
+
delete this.commandMap[command];
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
/** Prints the command map to the console.*/
|
|
80
|
+
addDefaultCommands() {
|
|
81
|
+
this.addCommand('help', this.showHelp.bind(this), { description: '&C6Shows this help', usage: 'help' });
|
|
82
|
+
this.addCommand('exit', () => process.exit(), { description: '&C6Exits the process', usage: 'exit' });
|
|
83
|
+
}
|
|
84
|
+
showHelp() {
|
|
85
|
+
this.out.info('&C(255,180,220)╭─────────────────────────────────────────────');
|
|
86
|
+
this.out.info('&C(255,180,220)│ &C1Vortez &C3Debugger &C1by NetFeez');
|
|
87
|
+
this.out.info('&C(255,180,220)│ &C1Commands:');
|
|
88
|
+
for (const index in this.commandMap) {
|
|
89
|
+
const command = this.commandMap[index];
|
|
90
|
+
this.out.info(`&C(255,180,220)│ > &C3${command.name}`);
|
|
91
|
+
this.out.info(`&C(255,180,220)│ - &C(255,255,255)Usage: &C6${command.usage ?? ''}`);
|
|
92
|
+
this.out.info(`&C(255,180,220)│ - &C(255,255,255)${command.description ?? ''}`);
|
|
93
|
+
}
|
|
94
|
+
this.out.info('&C(255,180,220)╰─────────────────────────────────────────────');
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
export default DebugUI;
|
|
98
|
+
//# sourceMappingURL=DebugUI.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DebugUI.js","sourceRoot":"","sources":["../../src/utilities/DebugUI.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,SAAS,MAAM,gBAAgB,CAAC;AACvC,OAAO,MAAM,MAAM,qBAAqB,CAAC;AAEzC,MAAM,OAAO,OAAO;IACN,kBAAkB,CAAsB;IAC3C,MAAM,CAAS;IACf,EAAE,CAAoB;IACtB,GAAG,CAAS;IACZ,UAAU,CAAqB;IACtC,YAAmB,SAAiB,MAAM;QACtC,IAAI,CAAC,kBAAkB,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACtD,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC,UAAU,CAAC,kCAAkC,CAAC,CAAC;QACvE,IAAI,CAAC,EAAE,GAAG,OAAO,CAAC,KAAK,CAAC;QACxB,IAAI,CAAC,GAAG,GAAG,IAAI,MAAM,CAAC,EAAE,MAAM,EAAE,KAA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