@visulima/fmt 2.0.0-alpha.1 → 2.0.0-alpha.11

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/index.mjs CHANGED
@@ -1,651 +1 @@
1
- const colorKeywords = /* @__PURE__ */ new Map([
2
- ["aliceblue", "#f0f8ff"],
3
- ["antiquewhite", "#faebd7"],
4
- ["aqua", "#00ffff"],
5
- ["aquamarine", "#7fffd4"],
6
- ["azure", "#f0ffff"],
7
- ["beige", "#f5f5dc"],
8
- ["bisque", "#ffe4c4"],
9
- ["black", "#000000"],
10
- ["blanchedalmond", "#ffebcd"],
11
- ["blue", "#0000ff"],
12
- ["blueviolet", "#8a2be2"],
13
- ["brown", "#a52a2a"],
14
- ["burlywood", "#deb887"],
15
- ["cadetblue", "#5f9ea0"],
16
- ["chartreuse", "#7fff00"],
17
- ["chocolate", "#d2691e"],
18
- ["coral", "#ff7f50"],
19
- ["cornflowerblue", "#6495ed"],
20
- ["cornsilk", "#fff8dc"],
21
- ["crimson", "#dc143c"],
22
- ["cyan", "#00ffff"],
23
- ["darkblue", "#00008b"],
24
- ["darkcyan", "#008b8b"],
25
- ["darkgoldenrod", "#b8860b"],
26
- ["darkgray", "#a9a9a9"],
27
- ["darkgreen", "#006400"],
28
- ["darkgrey", "#a9a9a9"],
29
- ["darkkhaki", "#bdb76b"],
30
- ["darkmagenta", "#8b008b"],
31
- ["darkolivegreen", "#556b2f"],
32
- ["darkorange", "#ff8c00"],
33
- ["darkorchid", "#9932cc"],
34
- ["darkred", "#8b0000"],
35
- ["darksalmon", "#e9967a"],
36
- ["darkseagreen", "#8fbc8f"],
37
- ["darkslateblue", "#483d8b"],
38
- ["darkslategray", "#2f4f4f"],
39
- ["darkslategrey", "#2f4f4f"],
40
- ["darkturquoise", "#00ced1"],
41
- ["darkviolet", "#9400d3"],
42
- ["deeppink", "#ff1493"],
43
- ["deepskyblue", "#00bfff"],
44
- ["dimgray", "#696969"],
45
- ["dimgrey", "#696969"],
46
- ["dodgerblue", "#1e90ff"],
47
- ["firebrick", "#b22222"],
48
- ["floralwhite", "#fffaf0"],
49
- ["forestgreen", "#228b22"],
50
- ["fuchsia", "#ff00ff"],
51
- ["gainsboro", "#dcdcdc"],
52
- ["ghostwhite", "#f8f8ff"],
53
- ["gold", "#ffd700"],
54
- ["goldenrod", "#daa520"],
55
- ["gray", "#808080"],
56
- ["green", "#008000"],
57
- ["greenyellow", "#adff2f"],
58
- ["grey", "#808080"],
59
- ["honeydew", "#f0fff0"],
60
- ["hotpink", "#ff69b4"],
61
- ["indianred", "#cd5c5c"],
62
- ["indigo", "#4b0082"],
63
- ["ivory", "#fffff0"],
64
- ["khaki", "#f0e68c"],
65
- ["lavender", "#e6e6fa"],
66
- ["lavenderblush", "#fff0f5"],
67
- ["lawngreen", "#7cfc00"],
68
- ["lemonchiffon", "#fffacd"],
69
- ["lightblue", "#add8e6"],
70
- ["lightcoral", "#f08080"],
71
- ["lightcyan", "#e0ffff"],
72
- ["lightgoldenrodyellow", "#fafad2"],
73
- ["lightgray", "#d3d3d3"],
74
- ["lightgreen", "#90ee90"],
75
- ["lightgrey", "#d3d3d3"],
76
- ["lightpink", "#ffb6c1"],
77
- ["lightsalmon", "#ffa07a"],
78
- ["lightseagreen", "#20b2aa"],
79
- ["lightskyblue", "#87cefa"],
80
- ["lightslategray", "#778899"],
81
- ["lightslategrey", "#778899"],
82
- ["lightsteelblue", "#b0c4de"],
83
- ["lightyellow", "#ffffe0"],
84
- ["lime", "#00ff00"],
85
- ["limegreen", "#32cd32"],
86
- ["linen", "#faf0e6"],
87
- ["magenta", "#ff00ff"],
88
- ["maroon", "#800000"],
89
- ["mediumaquamarine", "#66cdaa"],
90
- ["mediumblue", "#0000cd"],
91
- ["mediumorchid", "#ba55d3"],
92
- ["mediumpurple", "#9370db"],
93
- ["mediumseagreen", "#3cb371"],
94
- ["mediumslateblue", "#7b68ee"],
95
- ["mediumspringgreen", "#00fa9a"],
96
- ["mediumturquoise", "#48d1cc"],
97
- ["mediumvioletred", "#c71585"],
98
- ["midnightblue", "#191970"],
99
- ["mintcream", "#f5fffa"],
100
- ["mistyrose", "#ffe4e1"],
101
- ["moccasin", "#ffe4b5"],
102
- ["navajowhite", "#ffdead"],
103
- ["navy", "#000080"],
104
- ["oldlace", "#fdf5e6"],
105
- ["olive", "#808000"],
106
- ["olivedrab", "#6b8e23"],
107
- ["orange", "#ffa500"],
108
- ["orangered", "#ff4500"],
109
- ["orchid", "#da70d6"],
110
- ["palegoldenrod", "#eee8aa"],
111
- ["palegreen", "#98fb98"],
112
- ["paleturquoise", "#afeeee"],
113
- ["palevioletred", "#db7093"],
114
- ["papayawhip", "#ffefd5"],
115
- ["peachpuff", "#ffdab9"],
116
- ["peru", "#cd853f"],
117
- ["pink", "#ffc0cb"],
118
- ["plum", "#dda0dd"],
119
- ["powderblue", "#b0e0e6"],
120
- ["purple", "#800080"],
121
- ["rebeccapurple", "#663399"],
122
- ["red", "#ff0000"],
123
- ["rosybrown", "#bc8f8f"],
124
- ["royalblue", "#4169e1"],
125
- ["saddlebrown", "#8b4513"],
126
- ["salmon", "#fa8072"],
127
- ["sandybrown", "#f4a460"],
128
- ["seagreen", "#2e8b57"],
129
- ["seashell", "#fff5ee"],
130
- ["sienna", "#a0522d"],
131
- ["silver", "#c0c0c0"],
132
- ["skyblue", "#87ceeb"],
133
- ["slateblue", "#6a5acd"],
134
- ["slategray", "#708090"],
135
- ["slategrey", "#708090"],
136
- ["snow", "#fffafa"],
137
- ["springgreen", "#00ff7f"],
138
- ["steelblue", "#4682b4"],
139
- ["tan", "#d2b48c"],
140
- ["teal", "#008080"],
141
- ["thistle", "#d8bfd8"],
142
- ["tomato", "#ff6347"],
143
- ["turquoise", "#40e0d0"],
144
- ["violet", "#ee82ee"],
145
- ["wheat", "#f5deb3"],
146
- ["white", "#ffffff"],
147
- ["whitesmoke", "#f5f5f5"],
148
- ["yellow", "#ffff00"],
149
- ["yellowgreen", "#9acd32"]
150
- ]);
151
- const HASH_PATTERN = /^#([\dA-F]{2})([\dA-F]{2})([\dA-F]{2})([\dA-F]{2})?$/i;
152
- const SMALL_HASH_PATTERN = /^#([\dA-F])([\dA-F])([\dA-F])([\dA-F])?$/i;
153
- const RGB_PATTERN = /^rgba?\(\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*(,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*)?\)$/;
154
- const HSL_PATTERN = /^hsla?\(\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))%\s*,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))%\s*(,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*)?\)$/;
155
- const getDefaultCss = () => {
156
- return {
157
- __proto__: null,
158
- backgroundColor: null,
159
- color: null,
160
- fontStyle: null,
161
- fontWeight: null,
162
- textDecorationColor: null,
163
- textDecorationLine: []
164
- };
165
- };
166
- const SPACE_PATTERN = /\s+/g;
167
- const parseCssColor = (colorString) => {
168
- if (colorKeywords.has(colorString)) {
169
- colorString = colorKeywords.get(colorString);
170
- }
171
- const hashMatch = HASH_PATTERN.exec(colorString);
172
- if (hashMatch) {
173
- return [Number.parseInt(hashMatch[1], 16), Number.parseInt(hashMatch[2], 16), Number.parseInt(hashMatch[3], 16)];
174
- }
175
- const smallHashMatch = SMALL_HASH_PATTERN.exec(colorString);
176
- if (smallHashMatch) {
177
- return [
178
- Number.parseInt(`${smallHashMatch[1]}${smallHashMatch[1]}`, 16),
179
- Number.parseInt(`${smallHashMatch[2]}${smallHashMatch[2]}`, 16),
180
- Number.parseInt(`${smallHashMatch[3]}${smallHashMatch[3]}`, 16)
181
- ];
182
- }
183
- const rgbMatch = RGB_PATTERN.exec(colorString);
184
- if (rgbMatch) {
185
- return [
186
- Math.round(Math.max(0, Math.min(255, Number(rgbMatch[1])))),
187
- Math.round(Math.max(0, Math.min(255, Number(rgbMatch[2])))),
188
- Math.round(Math.max(0, Math.min(255, Number(rgbMatch[3]))))
189
- ];
190
- }
191
- const hslMatch = HSL_PATTERN.exec(colorString);
192
- if (hslMatch) {
193
- let h = Number(hslMatch[1]) % 360;
194
- if (h < 0) {
195
- h += 360;
196
- }
197
- const s = Math.max(0, Math.min(100, Number(hslMatch[2]))) / 100;
198
- const l = Math.max(0, Math.min(100, Number(hslMatch[3]))) / 100;
199
- const c = (1 - Math.abs(2 * l - 1)) * s;
200
- const x = c * (1 - Math.abs(h / 60 % 2 - 1));
201
- const m = l - c / 2;
202
- let r_;
203
- let g_;
204
- let b_;
205
- if (h < 60) {
206
- ({ 0: r_, 1: g_, 2: b_ } = [c, x, 0]);
207
- } else if (h < 120) {
208
- ({ 0: r_, 1: g_, 2: b_ } = [x, c, 0]);
209
- } else if (h < 180) {
210
- ({ 0: r_, 1: g_, 2: b_ } = [0, c, x]);
211
- } else if (h < 240) {
212
- ({ 0: r_, 1: g_, 2: b_ } = [0, x, c]);
213
- } else if (h < 300) {
214
- ({ 0: r_, 1: g_, 2: b_ } = [x, 0, c]);
215
- } else {
216
- ({ 0: r_, 1: g_, 2: b_ } = [c, 0, x]);
217
- }
218
- return [Math.round((r_ + m) * 255), Math.round((g_ + m) * 255), Math.round((b_ + m) * 255)];
219
- }
220
- return null;
221
- };
222
- const colorEquals = (color1, color2) => color1?.[0] === color2?.[0] && color1?.[1] === color2?.[1] && color1?.[2] === color2?.[2];
223
- const parseCss = (cssString) => {
224
- const css = getDefaultCss();
225
- const rawEntries = [];
226
- let inValue = false;
227
- let currentKey = null;
228
- let parenthesesDepth = 0;
229
- let currentPart = "";
230
- for (const c of cssString) {
231
- if (c === "(") {
232
- parenthesesDepth++;
233
- } else if (parenthesesDepth > 0) {
234
- if (c === ")") {
235
- parenthesesDepth--;
236
- }
237
- } else if (inValue) {
238
- if (c === ";") {
239
- const value = currentPart.trim();
240
- if (value !== "") {
241
- rawEntries.push([currentKey, value]);
242
- }
243
- currentKey = null;
244
- currentPart = "";
245
- inValue = false;
246
- continue;
247
- }
248
- } else if (c === ":") {
249
- currentKey = currentPart.trim();
250
- currentPart = "";
251
- inValue = true;
252
- continue;
253
- }
254
- currentPart += c;
255
- }
256
- if (inValue && parenthesesDepth === 0) {
257
- const value = currentPart.trim();
258
- if (value !== "") {
259
- rawEntries.push([currentKey, value]);
260
- }
261
- currentKey = null;
262
- currentPart = "";
263
- }
264
- for (const { 0: key, 1: value } of rawEntries) {
265
- switch (key) {
266
- case "background-color": {
267
- if (value != void 0) {
268
- css.backgroundColor = value;
269
- }
270
- break;
271
- }
272
- case "color": {
273
- if (value != void 0) {
274
- css.color = value;
275
- }
276
- break;
277
- }
278
- case "font-style": {
279
- if (["italic", "oblique", "oblique 14deg"].includes(value)) {
280
- css.fontStyle = "italic";
281
- }
282
- break;
283
- }
284
- case "font-weight": {
285
- if (value === "bold") {
286
- css.fontWeight = value;
287
- }
288
- break;
289
- }
290
- case "text-decoration": {
291
- css.textDecorationColor = null;
292
- css.textDecorationLine = [];
293
- const arguments_ = value.split(SPACE_PATTERN);
294
- for (const argument of arguments_) {
295
- const maybeColor = parseCssColor(argument);
296
- if (maybeColor != void 0) {
297
- css.textDecorationColor = maybeColor;
298
- } else if (["line-through", "overline", "underline"].includes(argument)) {
299
- css.textDecorationLine.push(argument);
300
- }
301
- }
302
- break;
303
- }
304
- case "text-decoration-color": {
305
- const color = parseCssColor(value);
306
- if (color != void 0) {
307
- css.textDecorationColor = color;
308
- }
309
- break;
310
- }
311
- case "text-decoration-line": {
312
- css.textDecorationLine = [];
313
- const lineTypes = value.split(SPACE_PATTERN);
314
- for (const lineType of lineTypes) {
315
- if (["line-through", "overline", "underline"].includes(lineType)) {
316
- css.textDecorationLine.push(lineType);
317
- }
318
- }
319
- break;
320
- }
321
- }
322
- }
323
- return css;
324
- };
325
- const cssToAnsi = (css, previousCss = null) => {
326
- previousCss = previousCss ?? getDefaultCss();
327
- let ansi = "";
328
- if (!colorEquals(css.backgroundColor, previousCss.backgroundColor)) {
329
- if (css.backgroundColor == void 0) {
330
- ansi += "\x1B[49m";
331
- } else {
332
- switch (css.backgroundColor) {
333
- case "black": {
334
- ansi += "\x1B[40m";
335
- break;
336
- }
337
- case "blue": {
338
- ansi += "\x1B[44m";
339
- break;
340
- }
341
- case "cyan": {
342
- ansi += "\x1B[46m";
343
- break;
344
- }
345
- case "green": {
346
- ansi += "\x1B[42m";
347
- break;
348
- }
349
- case "magenta": {
350
- ansi += "\x1B[45m";
351
- break;
352
- }
353
- case "red": {
354
- ansi += "\x1B[41m";
355
- break;
356
- }
357
- case "white": {
358
- ansi += "\x1B[47m";
359
- break;
360
- }
361
- case "yellow": {
362
- ansi += "\x1B[43m";
363
- break;
364
- }
365
- default: {
366
- if (Array.isArray(css.backgroundColor)) {
367
- const { 0: r, 1: g, 2: b } = css.backgroundColor;
368
- ansi += `\x1B[48;2;${r};${g};${b}m`;
369
- } else {
370
- const parsed = parseCssColor(css.backgroundColor);
371
- if (parsed === null) {
372
- ansi += "\x1B[49m";
373
- } else {
374
- const { 0: r, 1: g, 2: b } = parsed;
375
- ansi += `\x1B[48;2;${r};${g};${b}m`;
376
- }
377
- }
378
- }
379
- }
380
- }
381
- }
382
- if (!colorEquals(css.color, previousCss.color)) {
383
- if (css.color == void 0) {
384
- ansi += "\x1B[39m";
385
- } else {
386
- switch (css.color) {
387
- case "black": {
388
- ansi += "\x1B[30m";
389
- break;
390
- }
391
- case "blue": {
392
- ansi += "\x1B[34m";
393
- break;
394
- }
395
- case "cyan": {
396
- ansi += "\x1B[36m";
397
- break;
398
- }
399
- case "green": {
400
- ansi += "\x1B[32m";
401
- break;
402
- }
403
- case "magenta": {
404
- ansi += "\x1B[35m";
405
- break;
406
- }
407
- case "red": {
408
- ansi += "\x1B[31m";
409
- break;
410
- }
411
- case "white": {
412
- ansi += "\x1B[37m";
413
- break;
414
- }
415
- case "yellow": {
416
- ansi += "\x1B[33m";
417
- break;
418
- }
419
- default: {
420
- if (Array.isArray(css.color)) {
421
- const { 0: r, 1: g, 2: b } = css.color;
422
- ansi += `\x1B[38;2;${r};${g};${b}m`;
423
- } else {
424
- const parsed = parseCssColor(css.color);
425
- if (parsed === null) {
426
- ansi += "\x1B[39m";
427
- } else {
428
- const { 0: r, 1: g, 2: b } = parsed;
429
- ansi += `\x1B[38;2;${r};${g};${b}m`;
430
- }
431
- }
432
- }
433
- }
434
- }
435
- }
436
- if (css.fontWeight !== previousCss.fontWeight) {
437
- ansi += css.fontWeight === "bold" ? "\x1B[1m" : "\x1B[22m";
438
- }
439
- if (css.fontStyle !== previousCss.fontStyle) {
440
- ansi += css.fontStyle === "italic" ? "\x1B[3m" : "\x1B[23m";
441
- }
442
- if (!colorEquals(css.textDecorationColor, previousCss.textDecorationColor)) {
443
- if (css.textDecorationColor == void 0) {
444
- ansi += "\x1B[59m";
445
- } else {
446
- const { 0: r, 1: g, 2: b } = css.textDecorationColor;
447
- ansi += `\x1B[58;2;${r};${g};${b}m`;
448
- }
449
- }
450
- if (css.textDecorationLine.includes("line-through") !== previousCss.textDecorationLine.includes("line-through")) {
451
- ansi += css.textDecorationLine.includes("line-through") ? "\x1B[9m" : "\x1B[29m";
452
- }
453
- if (css.textDecorationLine.includes("overline") !== previousCss.textDecorationLine.includes("overline")) {
454
- ansi += css.textDecorationLine.includes("overline") ? "\x1B[53m" : "\x1B[55m";
455
- }
456
- if (css.textDecorationLine.includes("underline") !== previousCss.textDecorationLine.includes("underline")) {
457
- ansi += css.textDecorationLine.includes("underline") ? "\x1B[4m" : "\x1B[24m";
458
- }
459
- return ansi;
460
- };
461
-
462
- const tryStringify = (o) => {
463
- try {
464
- return JSON.stringify(o);
465
- } catch {
466
- return '"[Circular]"';
467
- }
468
- };
469
- const CHAR_PERCENT = "%".codePointAt(0);
470
- const CHAR_s = "s".codePointAt(0);
471
- const CHAR_d = "d".codePointAt(0);
472
- const CHAR_f = "f".codePointAt(0);
473
- const CHAR_i = "i".codePointAt(0);
474
- const CHAR_O = "O".codePointAt(0);
475
- const CHAR_o = "o".codePointAt(0);
476
- const CHAR_j = "j".codePointAt(0);
477
- const CHAR_c = "c".codePointAt(0);
478
- const format = (fmt, arguments_ = [], options = {}) => {
479
- if (typeof fmt !== "string" && typeof fmt !== "object" || fmt === null) {
480
- throw new TypeError(`fmt must be a string or object, got ${fmt === null ? "null" : typeof fmt}`);
481
- }
482
- const stringify = options.stringify ?? tryStringify;
483
- const offset = 1;
484
- if (typeof fmt === "object") {
485
- const argumentsLength = arguments_.length + offset;
486
- if (argumentsLength === 1) {
487
- return "{}";
488
- }
489
- const objects = Array.from({ length: argumentsLength });
490
- objects[0] = stringify(fmt);
491
- for (let index = 1; index < argumentsLength; index++) {
492
- objects[index] = stringify(arguments_[index - offset]);
493
- }
494
- return objects.join(" ");
495
- }
496
- if (arguments_.length === 0) {
497
- return fmt;
498
- }
499
- let result = "";
500
- let a = 1 - offset;
501
- let lastPosition = -1;
502
- let usedStyle = false;
503
- let previousCss = null;
504
- for (let index = 0; index < fmt.length; ) {
505
- if (fmt.codePointAt(index) === CHAR_PERCENT && index + 1 < fmt.length) {
506
- lastPosition = lastPosition > -1 ? lastPosition : 0;
507
- const c = fmt.codePointAt(index + 1);
508
- if (c === void 0) {
509
- ++a;
510
- break;
511
- }
512
- switch (c) {
513
- case CHAR_c: {
514
- if (globalThis.window === void 0) {
515
- const css = parseCss(arguments_[a]);
516
- if (lastPosition < index) {
517
- result += fmt.slice(lastPosition, index);
518
- }
519
- result += cssToAnsi(css, previousCss);
520
- if (result !== "") {
521
- usedStyle = true;
522
- previousCss = css;
523
- }
524
- }
525
- lastPosition = index + 2;
526
- index++;
527
- break;
528
- }
529
- case CHAR_d:
530
- case CHAR_f: {
531
- if (a >= arguments_.length || arguments_[a] == void 0) {
532
- break;
533
- }
534
- if (lastPosition < index) {
535
- result += fmt.slice(lastPosition, index);
536
- }
537
- result += Number(arguments_[a]).toString();
538
- lastPosition = index + 2;
539
- index++;
540
- break;
541
- }
542
- case CHAR_i: {
543
- if (a >= arguments_.length || arguments_[a] == void 0) {
544
- break;
545
- }
546
- if (lastPosition < index) {
547
- result += fmt.slice(lastPosition, index);
548
- }
549
- result += Math.floor(Number(arguments_[a])).toString();
550
- lastPosition = index + 2;
551
- index++;
552
- break;
553
- }
554
- case CHAR_j:
555
- case CHAR_O:
556
- case CHAR_o: {
557
- if (a >= arguments_.length || arguments_[a] === void 0) {
558
- break;
559
- }
560
- if (lastPosition < index) {
561
- result += fmt.slice(lastPosition, index);
562
- }
563
- const temporaryArgument = arguments_[a];
564
- const type = typeof temporaryArgument;
565
- if (type === "string") {
566
- result += `'${temporaryArgument}'`;
567
- lastPosition = index + 2;
568
- break;
569
- }
570
- if (type === "function") {
571
- result += temporaryArgument.name ? `[Function: ${temporaryArgument.name}]` : "[Function: <anonymous>]";
572
- lastPosition = index + 2;
573
- break;
574
- }
575
- result += stringify(temporaryArgument);
576
- lastPosition = index + 2;
577
- index++;
578
- break;
579
- }
580
- case CHAR_PERCENT: {
581
- if (lastPosition < index) {
582
- result += fmt.slice(lastPosition, index);
583
- }
584
- result += "%";
585
- lastPosition = index + 2;
586
- index++;
587
- a--;
588
- break;
589
- }
590
- case CHAR_s: {
591
- if (a >= arguments_.length) {
592
- break;
593
- }
594
- if (lastPosition < index) {
595
- result += fmt.slice(lastPosition, index);
596
- }
597
- result += typeof arguments_[a] === "object" ? stringify(arguments_[a]) : String(arguments_[a]);
598
- lastPosition = index + 2;
599
- index++;
600
- break;
601
- }
602
- default: {
603
- if (typeof options.formatters?.[c] === "function") {
604
- if (lastPosition < index) {
605
- result += fmt.slice(lastPosition, index);
606
- }
607
- result += options.formatters[c](arguments_[a]);
608
- lastPosition = index + 2;
609
- index++;
610
- }
611
- }
612
- }
613
- ++a;
614
- }
615
- ++index;
616
- }
617
- if (lastPosition === -1) {
618
- return fmt;
619
- }
620
- if (lastPosition < fmt.length) {
621
- result += fmt.slice(lastPosition);
622
- }
623
- if (usedStyle) {
624
- result += "\x1B[0m";
625
- }
626
- return result;
627
- };
628
- const build = (options = {}) => {
629
- const formatters = {};
630
- if (typeof options.formatters === "object") {
631
- Object.entries(options.formatters).forEach(([key, formatterFunction]) => {
632
- if (key.length === 0) {
633
- throw new Error(`Formatter %${key} has no characters`);
634
- }
635
- if (key.length > 1) {
636
- throw new Error(`Formatter %${key} has more than one character`);
637
- }
638
- if (typeof formatterFunction !== "function") {
639
- throw new TypeError(`Formatter for %${key} is not a function`);
640
- }
641
- const c = key.codePointAt(0);
642
- if (c === void 0) {
643
- throw new Error(`${key}.codePointAt(0) failed to return a value, please report this issue`);
644
- }
645
- formatters[c] = formatterFunction;
646
- });
647
- }
648
- return (f, arguments_ = [], formatOptions = {}) => format(f, arguments_, { ...formatOptions, formatters });
649
- };
650
-
651
- export { build, format };
1
+ var $=Object.defineProperty;var h=(e,t)=>$(e,"name",{value:t,configurable:!0});var v=Object.defineProperty,g=h((e,t)=>v(e,"name",{value:t,configurable:!0}),"g$1");const y=new Map([["aliceblue","#f0f8ff"],["antiquewhite","#faebd7"],["aqua","#00ffff"],["aquamarine","#7fffd4"],["azure","#f0ffff"],["beige","#f5f5dc"],["bisque","#ffe4c4"],["black","#000000"],["blanchedalmond","#ffebcd"],["blue","#0000ff"],["blueviolet","#8a2be2"],["brown","#a52a2a"],["burlywood","#deb887"],["cadetblue","#5f9ea0"],["chartreuse","#7fff00"],["chocolate","#d2691e"],["coral","#ff7f50"],["cornflowerblue","#6495ed"],["cornsilk","#fff8dc"],["crimson","#dc143c"],["cyan","#00ffff"],["darkblue","#00008b"],["darkcyan","#008b8b"],["darkgoldenrod","#b8860b"],["darkgray","#a9a9a9"],["darkgreen","#006400"],["darkgrey","#a9a9a9"],["darkkhaki","#bdb76b"],["darkmagenta","#8b008b"],["darkolivegreen","#556b2f"],["darkorange","#ff8c00"],["darkorchid","#9932cc"],["darkred","#8b0000"],["darksalmon","#e9967a"],["darkseagreen","#8fbc8f"],["darkslateblue","#483d8b"],["darkslategray","#2f4f4f"],["darkslategrey","#2f4f4f"],["darkturquoise","#00ced1"],["darkviolet","#9400d3"],["deeppink","#ff1493"],["deepskyblue","#00bfff"],["dimgray","#696969"],["dimgrey","#696969"],["dodgerblue","#1e90ff"],["firebrick","#b22222"],["floralwhite","#fffaf0"],["forestgreen","#228b22"],["fuchsia","#ff00ff"],["gainsboro","#dcdcdc"],["ghostwhite","#f8f8ff"],["gold","#ffd700"],["goldenrod","#daa520"],["gray","#808080"],["green","#008000"],["greenyellow","#adff2f"],["grey","#808080"],["honeydew","#f0fff0"],["hotpink","#ff69b4"],["indianred","#cd5c5c"],["indigo","#4b0082"],["ivory","#fffff0"],["khaki","#f0e68c"],["lavender","#e6e6fa"],["lavenderblush","#fff0f5"],["lawngreen","#7cfc00"],["lemonchiffon","#fffacd"],["lightblue","#add8e6"],["lightcoral","#f08080"],["lightcyan","#e0ffff"],["lightgoldenrodyellow","#fafad2"],["lightgray","#d3d3d3"],["lightgreen","#90ee90"],["lightgrey","#d3d3d3"],["lightpink","#ffb6c1"],["lightsalmon","#ffa07a"],["lightseagreen","#20b2aa"],["lightskyblue","#87cefa"],["lightslategray","#778899"],["lightslategrey","#778899"],["lightsteelblue","#b0c4de"],["lightyellow","#ffffe0"],["lime","#00ff00"],["limegreen","#32cd32"],["linen","#faf0e6"],["magenta","#ff00ff"],["maroon","#800000"],["mediumaquamarine","#66cdaa"],["mediumblue","#0000cd"],["mediumorchid","#ba55d3"],["mediumpurple","#9370db"],["mediumseagreen","#3cb371"],["mediumslateblue","#7b68ee"],["mediumspringgreen","#00fa9a"],["mediumturquoise","#48d1cc"],["mediumvioletred","#c71585"],["midnightblue","#191970"],["mintcream","#f5fffa"],["mistyrose","#ffe4e1"],["moccasin","#ffe4b5"],["navajowhite","#ffdead"],["navy","#000080"],["oldlace","#fdf5e6"],["olive","#808000"],["olivedrab","#6b8e23"],["orange","#ffa500"],["orangered","#ff4500"],["orchid","#da70d6"],["palegoldenrod","#eee8aa"],["palegreen","#98fb98"],["paleturquoise","#afeeee"],["palevioletred","#db7093"],["papayawhip","#ffefd5"],["peachpuff","#ffdab9"],["peru","#cd853f"],["pink","#ffc0cb"],["plum","#dda0dd"],["powderblue","#b0e0e6"],["purple","#800080"],["rebeccapurple","#663399"],["red","#ff0000"],["rosybrown","#bc8f8f"],["royalblue","#4169e1"],["saddlebrown","#8b4513"],["salmon","#fa8072"],["sandybrown","#f4a460"],["seagreen","#2e8b57"],["seashell","#fff5ee"],["sienna","#a0522d"],["silver","#c0c0c0"],["skyblue","#87ceeb"],["slateblue","#6a5acd"],["slategray","#708090"],["slategrey","#708090"],["snow","#fffafa"],["springgreen","#00ff7f"],["steelblue","#4682b4"],["tan","#d2b48c"],["teal","#008080"],["thistle","#d8bfd8"],["tomato","#ff6347"],["turquoise","#40e0d0"],["violet","#ee82ee"],["wheat","#f5deb3"],["white","#ffffff"],["whitesmoke","#f5f5f5"],["yellow","#ffff00"],["yellowgreen","#9acd32"]]),A=/^#([\dA-F]{2})([\dA-F]{2})([\dA-F]{2})([\dA-F]{2})?$/i,S=/^#([\dA-F])([\dA-F])([\dA-F])([\dA-F])?$/i,C=/^rgba?\(\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*(,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*)?\)$/,D=/^hsla?\(\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))%\s*,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))%\s*(,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*)?\)$/,B=g(()=>({__proto__:null,backgroundColor:null,color:null,fontStyle:null,fontWeight:null,textDecorationColor:null,textDecorationLine:[]}),"getDefaultCss"),p=/\s+/,m=g(e=>{y.has(e)&&(e=y.get(e));const t=A.exec(e);if(t)return[Number.parseInt(t[1],16),Number.parseInt(t[2],16),Number.parseInt(t[3],16)];const r=S.exec(e);if(r)return[Number.parseInt(`${r[1]}${r[1]}`,16),Number.parseInt(`${r[2]}${r[2]}`,16),Number.parseInt(`${r[3]}${r[3]}`,16)];const n=C.exec(e);if(n)return[Math.round(Math.max(0,Math.min(255,Number(n[1])))),Math.round(Math.max(0,Math.min(255,Number(n[2])))),Math.round(Math.max(0,Math.min(255,Number(n[3]))))];const f=D.exec(e);if(f){let a=Number(f[1])%360;a<0&&(a+=360);const l=Math.max(0,Math.min(100,Number(f[2])))/100,i=Math.max(0,Math.min(100,Number(f[3])))/100,c=(1-Math.abs(2*i-1))*l,u=c*(1-Math.abs(a/60%2-1)),o=i-c/2;let s,d,b;return a<60?{0:s,1:d,2:b}=[c,u,0]:a<120?{0:s,1:d,2:b}=[u,c,0]:a<180?{0:s,1:d,2:b}=[0,c,u]:a<240?{0:s,1:d,2:b}=[0,u,c]:a<300?{0:s,1:d,2:b}=[u,0,c]:{0:s,1:d,2:b}=[c,0,u],[Math.round((s+o)*255),Math.round((d+o)*255),Math.round((b+o)*255)]}return null},"parseCssColor"),k=g((e,t)=>e?.[0]===t?.[0]&&e?.[1]===t?.[1]&&e?.[2]===t?.[2],"colorEquals"),M=g(e=>{const t=B(),r=[];let n=!1,f=null,a=0,l="";for(const i of e){if(i==="(")a+=1;else if(a>0)i===")"&&(a-=1);else if(n){if(i===";"){const c=l.trim();c!==""&&r.push([f,c]),f=null,l="",n=!1;continue}}else if(i===":"){f=l.trim(),l="",n=!0;continue}l+=i}if(n&&a===0){const i=l.trim();i!==""&&r.push([f,i]),f=null,l=""}for(const{0:i,1:c}of r)switch(i){case"background-color":{c!=null&&(t.backgroundColor=c);break}case"color":{c!=null&&(t.color=c);break}case"font-style":{["italic","oblique","oblique 14deg"].includes(c)&&(t.fontStyle="italic");break}case"font-weight":{c==="bold"&&(t.fontWeight=c);break}case"text-decoration":{t.textDecorationColor=null,t.textDecorationLine=[];const u=c.split(p);for(const o of u){const s=m(o);s!=null?t.textDecorationColor=s:["line-through","overline","underline"].includes(o)&&t.textDecorationLine.push(o)}break}case"text-decoration-color":{const u=m(c);u!=null&&(t.textDecorationColor=u);break}case"text-decoration-line":{t.textDecorationLine=[];const u=c.split(p);for(const o of u)["line-through","overline","underline"].includes(o)&&t.textDecorationLine.push(o);break}}return t},"parseCss"),P=g((e,t)=>{t=t??B();let r="";if(!k(e.backgroundColor,t.backgroundColor))if(e.backgroundColor==null)r+="\x1B[49m";else switch(e.backgroundColor){case"black":{r+="\x1B[40m";break}case"blue":{r+="\x1B[44m";break}case"cyan":{r+="\x1B[46m";break}case"green":{r+="\x1B[42m";break}case"magenta":{r+="\x1B[45m";break}case"red":{r+="\x1B[41m";break}case"white":{r+="\x1B[47m";break}case"yellow":{r+="\x1B[43m";break}default:if(Array.isArray(e.backgroundColor)){const{0:n,1:f,2:a}=e.backgroundColor;r+=`\x1B[48;2;${String(n)};${String(f)};${String(a)}m`}else{const n=m(e.backgroundColor);if(n===null)r+="\x1B[49m";else{const{0:f,1:a,2:l}=n;r+=`\x1B[48;2;${String(f)};${String(a)};${String(l)}m`}}}if(!k(e.color,t.color))if(e.color==null)r+="\x1B[39m";else switch(e.color){case"black":{r+="\x1B[30m";break}case"blue":{r+="\x1B[34m";break}case"cyan":{r+="\x1B[36m";break}case"green":{r+="\x1B[32m";break}case"magenta":{r+="\x1B[35m";break}case"red":{r+="\x1B[31m";break}case"white":{r+="\x1B[37m";break}case"yellow":{r+="\x1B[33m";break}default:if(Array.isArray(e.color)){const{0:n,1:f,2:a}=e.color;r+=`\x1B[38;2;${String(n)};${String(f)};${String(a)}m`}else{const n=m(e.color);if(n===null)r+="\x1B[39m";else{const{0:f,1:a,2:l}=n;r+=`\x1B[38;2;${String(f)};${String(a)};${String(l)}m`}}}if(e.fontWeight!==t.fontWeight&&(r+=e.fontWeight==="bold"?"\x1B[1m":"\x1B[22m"),e.fontStyle!==t.fontStyle&&(r+=e.fontStyle==="italic"?"\x1B[3m":"\x1B[23m"),!k(e.textDecorationColor,t.textDecorationColor))if(e.textDecorationColor==null)r+="\x1B[59m";else{const{0:n,1:f,2:a}=e.textDecorationColor;r+=`\x1B[58;2;${String(n)};${String(f)};${String(a)}m`}return e.textDecorationLine.includes("line-through")!==t.textDecorationLine.includes("line-through")&&(r+=e.textDecorationLine.includes("line-through")?"\x1B[9m":"\x1B[29m"),e.textDecorationLine.includes("overline")!==t.textDecorationLine.includes("overline")&&(r+=e.textDecorationLine.includes("overline")?"\x1B[53m":"\x1B[55m"),e.textDecorationLine.includes("underline")!==t.textDecorationLine.includes("underline")&&(r+=e.textDecorationLine.includes("underline")?"\x1B[4m":"\x1B[24m"),r},"cssToAnsi");var N=Object.defineProperty,x=h((e,t)=>N(e,"name",{value:t,configurable:!0}),"y");const F=x(e=>{try{return JSON.stringify(e)}catch{return'"[Circular]"'}},"tryStringify"),w="%".codePointAt(0),L="s".codePointAt(0),j="d".codePointAt(0),q="f".codePointAt(0),E="i".codePointAt(0),I="O".codePointAt(0),O="o".codePointAt(0),T="j".codePointAt(0),W="c".codePointAt(0),_=x((e,t=[],r={})=>{if(typeof e!="string"&&typeof e!="object"||e===null)throw new TypeError(`fmt must be a string or object, got ${e===null?"null":typeof e}`);const n=r.stringify??F,f=1;if(typeof e=="object"){const o=t.length+f;if(o===1)return"{}";const s=Array.from({length:o});s[0]=n(e);for(let d=1;d<o;d+=1)s[d]=n(t[d-f]);return s.join(" ")}if(t.length===0)return e;let a="",l=1-f,i=-1,c=!1,u;for(let o=0;o<e.length;){if(e.codePointAt(o)===w&&o+1<e.length){i=i>-1?i:0;const s=e.codePointAt(o+1);if(s===void 0){l+=1;break}switch(s){case W:{if(globalThis.window===void 0){const d=M(t[l]);i<o&&(a+=e.slice(i,o)),a+=P(d,u),a!==""&&(c=!0,u=d)}i=o+2,o+=1;break}case j:case q:{if(l>=t.length||t[l]==null)break;i<o&&(a+=e.slice(i,o)),a+=Number(t[l]).toString(),i=o+2,o+=1;break}case E:{if(l>=t.length||t[l]==null)break;i<o&&(a+=e.slice(i,o)),a+=Math.floor(Number(t[l])).toString(),i=o+2,o+=1;break}case T:case I:case O:{if(l>=t.length||t[l]===void 0)break;i<o&&(a+=e.slice(i,o));const d=t[l],b=typeof d;if(b==="string"){a+=`'${d}'`,i=o+2;break}if(b==="function"){a+=d.name?`[Function: ${d.name}]`:"[Function: <anonymous>]",i=o+2;break}a+=n(d),i=o+2,o+=1;break}case w:{i<o&&(a+=e.slice(i,o)),a+="%",i=o+2,o+=1,l-=1;break}case L:{if(l>=t.length)break;i<o&&(a+=e.slice(i,o)),a+=typeof t[l]=="object"?n(t[l]):String(t[l]),i=o+2,o+=1;break}default:typeof r.formatters?.[s]=="function"&&(i<o&&(a+=e.slice(i,o)),a+=r.formatters[s](t[l]),i=o+2,o+=1)}l+=1}o+=1}return i===-1?e:(i<e.length&&(a+=e.slice(i)),c&&(a+="\x1B[0m"),a)},"format"),J=x((e={})=>{const t={};return typeof e.formatters=="object"&&Object.entries(e.formatters).forEach(([r,n])=>{if(r.length===0)throw new Error(`Formatter %${r} has no characters`);if(r.length>1)throw new Error(`Formatter %${r} has more than one character`);if(typeof n!="function")throw new TypeError(`Formatter for %${r} is not a function`);const f=r.codePointAt(0);if(f===void 0)throw new Error(`${r}.codePointAt(0) failed to return a value, please report this issue`);t[f]=n}),(r,n=[],f={})=>_(r,n,{...f,formatters:t})},"build");export{J as build,_ as format};