@prozilla-os/shared 1.2.1 → 1.3.0

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/dist/main.d.ts CHANGED
@@ -1,4 +1,12 @@
1
+ /**
2
+ * ANSI escape codes.
3
+ *
4
+ * @see [ANSI escape code - Wikipedia](https://en.wikipedia.org/wiki/ANSI_escape_code).
5
+ */
1
6
  export declare const ANSI: {
7
+ /**
8
+ * Foreground colors.
9
+ */
2
10
  fg: {
3
11
  black: string;
4
12
  red: string;
@@ -9,63 +17,578 @@ export declare const ANSI: {
9
17
  cyan: string;
10
18
  white: string;
11
19
  };
12
- bg: {};
20
+ /**
21
+ * Background colors.
22
+ */
23
+ bg: {
24
+ black: string;
25
+ red: string;
26
+ green: string;
27
+ yellow: string;
28
+ blue: string;
29
+ magenta: string;
30
+ cyan: string;
31
+ white: string;
32
+ };
33
+ /**
34
+ * Screen buffer and clearing controls.
35
+ */
36
+ screen: {
37
+ enterAltBuffer: string;
38
+ exitAltBuffer: string;
39
+ clear: string;
40
+ home: string;
41
+ };
42
+ /**
43
+ * Decorations.
44
+ */
13
45
  decoration: {
14
46
  dim: string;
15
47
  bold: string;
48
+ underline: string;
16
49
  };
17
50
  reset: string;
18
51
  };
19
52
 
53
+ /**
54
+ * Provides utility functions for wrapping text in ANSI escape codes.
55
+ *
56
+ * @see {@link ANSI}
57
+ * @hideconstructor
58
+ */
59
+ export declare class Ansi {
60
+ #private;
61
+ /**
62
+ * Makes text black using {@link ANSI.fg.black}.
63
+ */
64
+ static black(text: string): string;
65
+ /**
66
+ * Makes text red using {@link ANSI.fg.red}.
67
+ */
68
+ static red(text: string): string;
69
+ /**
70
+ * Makes text green using {@link ANSI.fg.green}.
71
+ */
72
+ static green(text: string): string;
73
+ /**
74
+ * Makes text yellow using {@link ANSI.fg.yellow}.
75
+ */
76
+ static yellow(text: string): string;
77
+ /**
78
+ * Makes text blue using {@link ANSI.fg.blue}.
79
+ */
80
+ static blue(text: string): string;
81
+ /**
82
+ * Makes text magenta using {@link ANSI.fg.magenta}.
83
+ */
84
+ static magenta(text: string): string;
85
+ /**
86
+ * Makes text cyan using {@link ANSI.fg.cyan}.
87
+ */
88
+ static cyan(text: string): string;
89
+ /**
90
+ * Makes text white using {@link ANSI.fg.white}.
91
+ */
92
+ static white(text: string): string;
93
+ /**
94
+ * Makes text bold using {@link ANSI.decoration.bold}.
95
+ */
96
+ static bold(text: string): string;
97
+ /**
98
+ * Makes text dim using {@link ANSI.decoration.dim}.
99
+ */
100
+ static dim(text: string): string;
101
+ /**
102
+ * Removes all ANSI escape sequences.
103
+ */
104
+ static strip(text: string): string;
105
+ /**
106
+ * Removes ANSI escape sequences for colors and background colors (SGR).
107
+ */
108
+ static stripColors(text: string): string;
109
+ }
110
+
111
+ /**
112
+ * A function that handles an event asynchronously.
113
+ * @typeParam T - A record of all events.
114
+ * @typeParam K - The event this function handles.
115
+ */
116
+ export declare type AsyncListener<T extends Record<keyof T, unknown[]>, K extends keyof T> = (...args: T[K]) => Promise<void>;
117
+
118
+ declare function bold(text: string): string;
119
+
120
+ /**
121
+ * Clamps a value between an upper and lower bound.
122
+ * @param value - The value to clamp.
123
+ * @param min - The lower bound.
124
+ * @param max - The upper bound.
125
+ * @returns The clamped value.
126
+ */
20
127
  export declare function clamp(value: number, min: number, max: number): number;
21
128
 
22
- export declare class EventEmitter<EventMap extends EventNamesMap> {
129
+ declare function code(text: string): string;
130
+
131
+ declare function codeBlock(text: string): string;
132
+
133
+ /**
134
+ * A simple event emitter.
135
+ * @typeParam T - A record of all events.
136
+ */
137
+ export declare class EventEmitter<T extends Record<keyof T, unknown[]>> {
23
138
  #private;
24
- static EVENT_NAMES: EventNamesMap;
25
139
  /**
26
- * Add event listener for an event
140
+ * Starts listening to an event.
141
+ * @param event - The event to listen to.
142
+ * @param listener - The function to call when the event is emitted.
143
+ * @returns The listener.
27
144
  */
28
- on<Key extends keyof EventMap>(eventName: Key, callback: Listener): Listener;
145
+ on<K extends keyof T>(event: K, listener: Listener<T, K>): Listener<T, K>;
29
146
  /**
30
- * Remove event listener for an event
147
+ * Registers an event listener that is automatically removed when called.
148
+ * @param event - The event to listen to.
149
+ * @param listener - The function to call once the event is emitted.
150
+ * @returns The wrapped listener.
31
151
  */
32
- off<Key extends keyof EventMap>(eventName: Key, callback: Listener): void;
152
+ once<K extends keyof T>(event: K, listener: Listener<T, K>): Listener<T, K>;
33
153
  /**
34
- * Dispatch event
154
+ * Starts listening to an event.
155
+ * @param event - The event to listen to.
156
+ * @param listener - The function to call when the event is emitted.
157
+ * @returns The wrapped listener.
35
158
  */
36
- emit<Key extends keyof EventMap>(eventName: Key, data?: unknown): void;
159
+ onAsync<K extends keyof T>(event: K, listener: AsyncListener<T, K>, onRejected?: Parameters<Promise<void>["catch"]>[0]): Listener<T, K>;
160
+ /**
161
+ * Removes an event listener.
162
+ * @param event - The event to remove the listener from.
163
+ * @param listener - The listener to remove.
164
+ */
165
+ off<K extends keyof T>(event: K, listener: Listener<T, K>): void;
166
+ /**
167
+ * Emits an event to all its listeners.
168
+ * @param event - The event to emit.
169
+ * @param args - The arguments to pass to the listeners.
170
+ */
171
+ emit<K extends keyof T>(event: K, ...args: T[K]): void;
37
172
  }
38
173
 
39
- export declare type EventNamesMap = Record<string, string>;
174
+ /**
175
+ * Formats time relative to now.
176
+ * @param date - The date.
177
+ * @param maxLength - The maximum amount of units, e.g.: 3 => years, months, days.
178
+ */
179
+ export declare function formatRelativeTime(date: Date, maxLength?: number, allowAffixes?: boolean): string;
180
+
181
+ /**
182
+ * Applies a format to a string.
183
+ * @param text - The text to format.
184
+ * @returns The formatted text.
185
+ */
186
+ export declare type Formatter = (text: string) => string;
40
187
 
41
188
  /**
42
- * Format a time relative to now
43
- * @param date - The date
44
- * @param maxLength - The maximum amount of units, e.g.: 3 => years, months, days
189
+ * Formats time in a human-readable format.
190
+ * @param time - Time in milliseconds.
191
+ * @param maxLength - The maximum amount of units, e.g.: 3 => years, months, days.
45
192
  */
46
- export declare function formatRelativeTime(date: Date, maxLength: number | undefined, allowAffixes: boolean): string;
193
+ export declare function formatTime(time: number, maxLength?: number, allowAffixes?: boolean): string;
47
194
 
48
195
  /**
49
- * Format a time
50
- * @param time - Time in milliseconds
51
- * @param maxLength - The maximum amount of units, e.g.: 3 => years, months, days
196
+ * Returns the longest common prefix from a list of strings.
52
197
  */
53
- export declare function formatTime(time: number, maxLength: number | undefined, allowAffixes: boolean): string;
198
+ export declare function getLongestCommonPrefix(strings: string[]): string;
199
+
200
+ declare function heading1(text: string): string;
201
+
202
+ declare function heading2(text: string): string;
203
+
204
+ declare function heading3(text: string): string;
54
205
 
55
- export declare function isValidInteger(number: number | string): number | boolean;
206
+ declare function heading4(text: string): string;
56
207
 
57
- export declare type Listener = (data: unknown) => void;
208
+ declare function heading5(text: string): string;
58
209
 
210
+ declare function heading6(text: string): string;
211
+
212
+ declare function horizontalRule(): string;
213
+
214
+ /**
215
+ * Checks if `input` is a valid integer.
216
+ */
217
+ export declare function isValidInteger(input: number | string): boolean;
218
+
219
+ /**
220
+ * Checks if `input` is a valid number.
221
+ */
222
+ export declare function isValidNumber(input: number | string): boolean;
223
+
224
+ declare function italic(text: string): string;
225
+
226
+ declare function link(label: string, url: string): string;
227
+
228
+ /**
229
+ * A function that handles an event.
230
+ * @typeParam T - A record of all events.
231
+ * @typeParam K - The event this function handles.
232
+ */
233
+ export declare type Listener<T extends Record<keyof T, unknown[]>, K extends keyof T> = (...args: T[K]) => void;
234
+
235
+ /**
236
+ * A simple wrapper for {@link console} that logs formatted text and supports log levels.
237
+ */
238
+ export declare class Logger {
239
+ #private;
240
+ /**
241
+ * The minimum log level or an array of log levels to enable.
242
+ *
243
+ * `undefined` enables all log levels. An array enables all log levels in that array. A single level enables that log level and the ones above.
244
+ * @default LogLevel.Debug
245
+ */
246
+ level?: LogLevel | LogLevel[];
247
+ /**
248
+ * The current level of indentation.
249
+ * @default 0
250
+ */
251
+ indent: number;
252
+ /**
253
+ * The string to use for indentation.
254
+ * @default "\t"
255
+ */
256
+ indentString?: string;
257
+ /**
258
+ * The function to use when highlighting text.
259
+ * @default Ansi.cyan
260
+ * @see {@link Ansi.cyan}
261
+ */
262
+ highlight: Formatter;
263
+ /**
264
+ * The function to use when emphasizing text.
265
+ * @default Ansi.bold
266
+ * @see {@link Ansi.bold}
267
+ */
268
+ emphasize: Formatter;
269
+ /**
270
+ * The prefixes to prepend to logs.
271
+ * @default {
272
+ [LogLevel.Info]: Ansi.cyan("[info]"),
273
+ [LogLevel.Success]: Ansi.green("[success]"),
274
+ [LogLevel.Warning]: Ansi.yellow("[warning]"),
275
+ [LogLevel.Error]: Ansi.red("[error]"),
276
+ }
277
+ */
278
+ prefix: {
279
+ [key in LogLevel | "global"]?: string;
280
+ };
281
+ constructor(options?: {
282
+ /** {@inheritDoc Logger.level}. */
283
+ level?: Logger["level"];
284
+ /**
285
+ * The prefix(es) to prepend to logs.
286
+ *
287
+ * Does not override default prefixes unless specified.
288
+ */
289
+ prefix?: Logger["prefix"] | string;
290
+ });
291
+ /**
292
+ * @param level - The minimum log level.
293
+ */
294
+ constructor(level: LogLevel);
295
+ /**
296
+ * @param levels - The log levels to enable.
297
+ */
298
+ constructor(levels: LogLevel[]);
299
+ /**
300
+ * Resets all properties to their default values.
301
+ */
302
+ reset(): this;
303
+ /**
304
+ * Calls a function while the level of indentation is increased.
305
+ *
306
+ * Increases the level of indentation, then calls the function and finally restores the indentation.
307
+ * @param callback - The function to call.
308
+ * @param indentation - The amount of indentation to use.
309
+ */
310
+ indented(callback: (this: undefined) => void, indentation?: number): this;
311
+ /**
312
+ * Logs every line while the level of indentation is increased.
313
+ *
314
+ * Increases the level of indentation, then logs every line and finally restores the indentation.
315
+ * @param lines - The lines to log.
316
+ * @param indentation - The amount of indentation to use.
317
+ * @param level - The log level.
318
+ */
319
+ indented(lines: unknown[], indentation?: number, level?: LogLevel): this;
320
+ /**
321
+ * Logs a message while the level of indentation is increased.
322
+ *
323
+ * Increases the level of indentation, then logs the message and finally restores the indentation.
324
+ * @param message - The message to log.
325
+ * @param indentation - The amount of indentation to use.
326
+ * @param level - The log level.
327
+ */
328
+ indented(message: unknown, indentation?: number, level?: LogLevel): this;
329
+ /**
330
+ * Increases the level of indentation.
331
+ * @param amount - The amount to increase the level of indentation with.
332
+ */
333
+ tab(amount?: number): this;
334
+ /**
335
+ * Decreases the level of indentation.
336
+ * @param amount - The amount to decrease the level of indentation with.
337
+ */
338
+ shiftTab(amount?: number): this;
339
+ /**
340
+ * Sets the prefix to prepend to all logs.
341
+ * @param prefix - The prefix to use.
342
+ */
343
+ setPrefix(prefix: string): this;
344
+ /**
345
+ * Sets the prefixes to prepend to logs.
346
+ *
347
+ * Will override any existing prefixes.
348
+ * @param prefixes - The prefixes to use.
349
+ */
350
+ setPrefix(prefixes: Logger["prefix"]): this;
351
+ /**
352
+ * Sets the prefix to prepend to logs at a given log level.
353
+ * @param level - The log level.
354
+ * @param prefix - The prefix to use.
355
+ */
356
+ setPrefix(level: LogLevel, prefix: string): this;
357
+ /**
358
+ * Logs an info message about a URL being fetched.
359
+ * @param url - The URL being fetched.
360
+ */
361
+ fetching(url: string): this;
362
+ /**
363
+ * Logs an info message that implies a pending state.
364
+ * @param message - The status message.
365
+ */
366
+ pending(message: string): this;
367
+ /**
368
+ * Logs an error message.
369
+ * @param message - The error message or reason.
370
+ * @param details - The details of the error message.
371
+ */
372
+ error(message: unknown, ...details: unknown[]): this;
373
+ /**
374
+ * Logs a warning message.
375
+ * @param message - The warning message.
376
+ * @param details - The details of the warning message.
377
+ */
378
+ warn(message: string, ...details: unknown[]): this;
379
+ /**
380
+ * Logs a success message.
381
+ * @param message - The success message.
382
+ * @param details - The details of the success message.
383
+ */
384
+ success(message: string, ...details: unknown[]): this;
385
+ /**
386
+ * Logs an info message.
387
+ * @param message - The info message.
388
+ * @param details - The details of the info message.
389
+ */
390
+ info(message: string, ...details: unknown[]): this;
391
+ /**
392
+ * Logs a status message.
393
+ * @param message - The status message.
394
+ * @param details - The details of the status message.
395
+ * @param level - The log level.
396
+ */
397
+ statusMessage(message: string, details: unknown[], level?: LogLevel): this;
398
+ /**
399
+ * Logs the amount of errors and warnings that have been logged since the previous call to this function or the creation of this logger.
400
+ */
401
+ summary(): this;
402
+ /**
403
+ * Logs a labeled parameter.
404
+ *
405
+ * The value is emphasized using {@link emphasize}.
406
+ * @param label - The label of the parameter.
407
+ * @param value - The value of the parameter.
408
+ * @param level - The log level.
409
+ */
410
+ parameter(label: string, value: unknown, level?: LogLevel): this;
411
+ /**
412
+ * Logs properties as a list of key-value pairs.
413
+ * @param properties - The properties to log.
414
+ * @param level - The log level.
415
+ * @see {@link value()}
416
+ */
417
+ properties(properties: Record<string, unknown>, level?: LogLevel): this;
418
+ /**
419
+ * Logs a labeled value.
420
+ *
421
+ * The value is highlighted using {@link highlight}.
422
+ * @param label - The label of the value.
423
+ * @param value - The value.
424
+ * @param level - The log level.
425
+ */
426
+ value(label: string, value: unknown, level?: LogLevel): this;
427
+ /**
428
+ * Logs emphasized text.
429
+ * @param text - The text to log.
430
+ * @param level - The log level.
431
+ */
432
+ emphasized(text: string, level?: LogLevel): this;
433
+ /**
434
+ * Logs highlighted text.
435
+ * @param text - The text to log.
436
+ * @param level - The log level.
437
+ */
438
+ highlighted(text: string, level?: LogLevel): this;
439
+ /**
440
+ * Logs each item on a new line.
441
+ * @param lines - The items to log.
442
+ * @param level - The log level.
443
+ */
444
+ lines(lines: unknown[], level?: LogLevel): this;
445
+ /**
446
+ * Logs a message.
447
+ * @param message - The message to log.
448
+ * @param level - The log level.
449
+ */
450
+ log(message: unknown, level?: LogLevel): this;
451
+ /**
452
+ * Logs text using this logger's format.
453
+ * @param text - The text to log.
454
+ * @param level - The log level.
455
+ */
456
+ text(text: string, level?: LogLevel): this;
457
+ /**
458
+ * Logs an newline character.
459
+ */
460
+ newLine(): this;
461
+ /**
462
+ * Checks if the given log level is enabled.
463
+ * @param level - The log level to check.
464
+ */
465
+ isLevelEnabled(level: LogLevel): boolean;
466
+ /**
467
+ * Formats text using this logger's formatting properties.
468
+ * @param text - The text to format.
469
+ */
470
+ format(text: string): string;
471
+ /**
472
+ * Prepends a prefix to text.
473
+ * @param text - The text to apply the prefix to.
474
+ * @param level - The log level.
475
+ */
476
+ applyPrefix(text: string, level?: LogLevel): string;
477
+ }
478
+
479
+ export declare enum LogLevel {
480
+ Debug = 0,
481
+ Info = 1,
482
+ Success = 2,
483
+ Warning = 3,
484
+ Error = 4
485
+ }
486
+
487
+ export declare namespace Markdown {
488
+ export {
489
+ heading1,
490
+ heading2,
491
+ heading3,
492
+ heading4,
493
+ heading5,
494
+ heading6,
495
+ bold,
496
+ italic,
497
+ code,
498
+ codeBlock,
499
+ strikethrough,
500
+ link,
501
+ quote,
502
+ horizontalRule
503
+ }
504
+ }
505
+
506
+ /**
507
+ * Converts a string to a boolean.
508
+ * @param input - The string to parse.
509
+ */
59
510
  export declare function parseBool(input: string): boolean;
60
511
 
512
+ export declare function parseOptionalFloat(input?: string, defaultValue?: number): number;
513
+
514
+ export declare function parseOptionalInteger(input?: string, defaultValue?: number): number;
515
+
516
+ declare function quote(text: string): string;
517
+
518
+ /**
519
+ * Returns a random item from an array.
520
+ */
61
521
  export declare function randomFromArray<Type>(array: Type[]): Type;
62
522
 
523
+ /**
524
+ * Returns a random value inside a range.
525
+ * @param min - The lower bound.
526
+ * @param max - The upper bound.
527
+ */
63
528
  export declare function randomRange(min: number, max: number): number;
64
529
 
530
+ /**
531
+ * Removes all duplicate items from an array.
532
+ */
65
533
  export declare function removeDuplicatesFromArray<Type>(array: Type[]): Type[];
66
534
 
535
+ /**
536
+ * Removes an item from an array.
537
+ * @param item - The item to remove.
538
+ */
67
539
  export declare function removeFromArray<Type>(item: Type, array: Type[]): void;
68
540
 
69
- export declare function round(value: number, precision: number): number;
541
+ /**
542
+ * Rounds a value.
543
+ * @param value - The value to round.
544
+ * @param precision - The precision to round with.
545
+ */
546
+ export declare function round(value: number, precision?: number): number;
547
+
548
+ declare function strikethrough(text: string): string;
549
+
550
+ export declare class Vector2 {
551
+ x: number;
552
+ y: number;
553
+ constructor(xy: number);
554
+ constructor(x: number, y?: number);
555
+ static get ZERO(): Vector2;
556
+ get clone(): Vector2;
557
+ get magnitude(): number;
558
+ setX(x: number): this;
559
+ setY(y: number): this;
560
+ set(x: number, y: number): this;
561
+ round(): this;
562
+ normalize(): this;
563
+ scale(scalar: number): this;
564
+ getDistanceSquared(x: number, y?: number): number;
565
+ getDistanceSquared(vector2: Vector2): number;
566
+ getDistance(x: number, y?: number): number;
567
+ getDistance(vector2: Vector2): number;
568
+ add(x: number, y?: number): this;
569
+ add(vector2: Vector2): this;
570
+ subtract(x: number, y?: number): this;
571
+ subtract(vector2: Vector2): this;
572
+ multiply(x: number, y?: number): this;
573
+ multiply(vector2: Vector2): this;
574
+ divide(x: number, y?: number): this;
575
+ divide(vector2: Vector2): this;
576
+ lerp(vector2: Vector2, t: number): this;
577
+ static sum(vector2A: Vector2, vector2B: Vector2): Vector2;
578
+ static difference(vector2A: Vector2, vector2B: Vector2): Vector2;
579
+ static product(vector2A: Vector2, vector2B: Vector2): Vector2;
580
+ static division(vector2A: Vector2, vector2B: Vector2): Vector2;
581
+ static scale(vector2: Vector2, scalar: number): Vector2;
582
+ static normalize(vector2: Vector2): Vector2;
583
+ static lerp(vector2A: Vector2, vector2B: Vector2, t: number): Vector2;
584
+ static from({ x, y }: {
585
+ x: number;
586
+ y: number;
587
+ }): Vector2;
588
+ static parseVector(x: number | Vector2, y?: number): {
589
+ x: number;
590
+ y: number;
591
+ };
592
+ }
70
593
 
71
594
  export { }