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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002 |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
 
 
 
 
x104
x104
 
 
 
 
 
 
 
 
 
 
 
 
x100
x102
x102
 
 
 
 
 
 
x100
x439
x439
x439
x439
x439
x439
 
 
 
 
 
 
x100
x439
x439
x439
x439
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x306
x102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x513
x171
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x312
x104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x456
x152
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x381
x127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x363
x121
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x351
x117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x324
x108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x114
x114
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x351
x117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x312
x104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x312
x104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x318
x106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x309
x103
 
 
 
 
 
 
 
 
 
x100
x130
x130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x540
x108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x520
x104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x105
x110
x110
x110
x770
x110
 
 
x110
x108
x108
x108
x108
x108
x108
x108
x108
x108
x108
x108
 
 
x105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x105
x110
x110
x110
x770
x110
 
 
x110
x108
x108
x108
x108
x108
x108
x108
x108
x108
x108
x108
 
 
x105
 
 
x100
x100
x100
x100
x100
x100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x100
x232
x232 |
I
I
|
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.
// A module to print ANSI terminal colors. Inspired by chalk, kleur, and colors
// on npm.
/**
* String formatters and utilities for dealing with ANSI color codes.
*
* > [!IMPORTANT]
* > If printing directly to the console, it's recommended to style console
* > output using CSS (guide
* > {@linkcode https://developer.mozilla.org/en-US/docs/Web/API/console#styling_console_output | here}).
*
* This module supports `NO_COLOR` environmental variable disabling any coloring
* if `NO_COLOR` is set.
*
* ```ts no-assert
* import {
* bgBlue,
* bgRgb24,
* bgRgb8,
* bold,
* italic,
* red,
* rgb24,
* rgb8,
* } from "@std/fmt/colors";
*
* console.log(bgBlue(italic(red(bold("Hello, World!")))));
*
* // also supports 8bit colors
*
* console.log(rgb8("Hello, World!", 42));
*
* console.log(bgRgb8("Hello, World!", 42));
*
* // and 24bit rgb
*
* console.log(rgb24("Hello, World!", {
* r: 41,
* g: 42,
* b: 43,
* }));
*
* console.log(bgRgb24("Hello, World!", {
* r: 41,
* g: 42,
* b: 43,
* }));
* ```
*
* @module
*/
// deno-lint-ignore no-explicit-any
const { Deno } = globalThis as any;
const noColor = typeof Deno?.noColor === "boolean"
? Deno.noColor as boolean
: false;
interface Code {
open: string;
close: string;
regexp: RegExp;
}
/** RGB 8-bits per channel. Each in range `0->255` or `0x00->0xff` */
export interface Rgb {
/** Red component value */
r: number;
/** Green component value */
g: number;
/** Blue component value */
b: number;
}
let enabled = !noColor;
/**
* Enable or disable text color when styling.
*
* `@std/fmt/colors` automatically detects NO_COLOR environmental variable
* and disables text color. Use this API only when the automatic detection
* doesn't work.
*
* @example Usage
* ```ts no-assert
* import { setColorEnabled } from "@std/fmt/colors";
*
* // Disable text color
* setColorEnabled(false);
*
* // Enable text color
* setColorEnabled(true);
* ```
*
* @param value The boolean value to enable or disable text color
*/
export function setColorEnabled(value: boolean) {
if (Deno?.noColor) {
return;
}
enabled = value;
}
/**
* Get whether text color change is enabled or disabled.
*
* @example Usage
* ```ts no-assert
* import { getColorEnabled } from "@std/fmt/colors";
*
* console.log(getColorEnabled()); // true if enabled, false if disabled
* ```
* @returns `true` if text color is enabled, `false` otherwise
*/
export function getColorEnabled(): boolean {
return enabled;
}
/**
* Builds color code
* @param open
* @param close
*/
function code(open: number[], close: number): Code {
return {
open: `\x1b[${open.join(";")}m`,
close: `\x1b[${close}m`,
regexp: new RegExp(`\\x1b\\[${close}m`, "g"),
};
}
/**
* Applies color and background based on color code and its associated text
* @param str The text to apply color settings to
* @param code The color code to apply
*/
function run(str: string, code: Code): string {
return enabled
? `${code.open}${str.replace(code.regexp, code.open)}${code.close}`
: str;
}
/**
* Reset the text modified.
*
* @example Usage
* ```ts no-assert
* import { reset } from "@std/fmt/colors";
*
* console.log(reset("Hello, world!"));
* ```
*
* @param str The text to reset
* @returns The text with reset color
*/
export function reset(str: string): string {
return run(str, code([0], 0));
}
/**
* Make the text bold.
*
* @example Usage
* ```ts no-assert
* import { bold } from "@std/fmt/colors";
*
* console.log(bold("Hello, world!"));
* ```
*
* @param str The text to make bold
* @returns The bold text
*/
export function bold(str: string): string {
return run(str, code([1], 22));
}
/**
* The text emits only a small amount of light.
*
* @example Usage
* ```ts no-assert
* import { dim } from "@std/fmt/colors";
*
* console.log(dim("Hello, world!"));
* ```
*
* @param str The text to dim
* @returns The dimmed text
*
* Warning: Not all terminal emulators support `dim`.
* For compatibility across all terminals, use {@linkcode gray} or {@linkcode brightBlack} instead.
*/
export function dim(str: string): string {
return run(str, code([2], 22));
}
/**
* Make the text italic.
*
* @example Usage
* ```ts no-assert
* import { italic } from "@std/fmt/colors";
*
* console.log(italic("Hello, world!"));
* ```
*
* @param str The text to make italic
* @returns The italic text
*/
export function italic(str: string): string {
return run(str, code([3], 23));
}
/**
* Make the text underline.
*
* @example Usage
* ```ts no-assert
* import { underline } from "@std/fmt/colors";
*
* console.log(underline("Hello, world!"));
* ```
*
* @param str The text to underline
* @returns The underlined text
*/
export function underline(str: string): string {
return run(str, code([4], 24));
}
/**
* Invert background color and text color.
*
* @example Usage
* ```ts no-assert
* import { inverse } from "@std/fmt/colors";
*
* console.log(inverse("Hello, world!"));
* ```
*
* @param str The text to invert its color
* @returns The inverted text
*/
export function inverse(str: string): string {
return run(str, code([7], 27));
}
/**
* Make the text hidden.
*
* @example Usage
* ```ts no-assert
* import { hidden } from "@std/fmt/colors";
*
* console.log(hidden("Hello, world!"));
* ```
*
* @param str The text to hide
* @returns The hidden text
*/
export function hidden(str: string): string {
return run(str, code([8], 28));
}
/**
* Put horizontal line through the center of the text.
*
* @example Usage
* ```ts no-assert
* import { strikethrough } from "@std/fmt/colors";
*
* console.log(strikethrough("Hello, world!"));
* ```
*
* @param str The text to strike through
* @returns The text with horizontal line through the center
*/
export function strikethrough(str: string): string {
return run(str, code([9], 29));
}
/**
* Set text color to black.
*
* @example Usage
* ```ts no-assert
* import { black } from "@std/fmt/colors";
*
* console.log(black("Hello, world!"));
* ```
*
* @param str The text to make black
* @returns The black text
*/
export function black(str: string): string {
return run(str, code([30], 39));
}
/**
* Set text color to red.
*
* @example Usage
* ```ts no-assert
* import { red } from "@std/fmt/colors";
*
* console.log(red("Hello, world!"));
* ```
*
* @param str The text to make red
* @returns The red text
*/
export function red(str: string): string {
return run(str, code([31], 39));
}
/**
* Set text color to green.
*
* @example Usage
* ```ts no-assert
* import { green } from "@std/fmt/colors";
*
* console.log(green("Hello, world!"));
* ```
*
* @param str The text to make green
* @returns The green text
*/
export function green(str: string): string {
return run(str, code([32], 39));
}
/**
* Set text color to yellow.
*
* @example Usage
* ```ts no-assert
* import { yellow } from "@std/fmt/colors";
*
* console.log(yellow("Hello, world!"));
* ```
*
* @param str The text to make yellow
* @returns The yellow text
*/
export function yellow(str: string): string {
return run(str, code([33], 39));
}
/**
* Set text color to blue.
*
* @example Usage
* ```ts no-assert
* import { blue } from "@std/fmt/colors";
*
* console.log(blue("Hello, world!"));
* ```
*
* @param str The text to make blue
* @returns The blue text
*/
export function blue(str: string): string {
return run(str, code([34], 39));
}
/**
* Set text color to magenta.
*
* @example Usage
* ```ts no-assert
* import { magenta } from "@std/fmt/colors";
*
* console.log(magenta("Hello, world!"));
* ```
*
* @param str The text to make magenta
* @returns The magenta text
*/
export function magenta(str: string): string {
return run(str, code([35], 39));
}
/**
* Set text color to cyan.
*
* @example Usage
* ```ts no-assert
* import { cyan } from "@std/fmt/colors";
*
* console.log(cyan("Hello, world!"));
* ```
*
* @param str The text to make cyan
* @returns The cyan text
*/
export function cyan(str: string): string {
return run(str, code([36], 39));
}
/**
* Set text color to white.
*
* @example Usage
* ```ts no-assert
* import { white } from "@std/fmt/colors";
*
* console.log(white("Hello, world!"));
* ```
*
* @param str The text to make white
* @returns The white text
*/
export function white(str: string): string {
return run(str, code([37], 39));
}
/**
* Set text color to gray.
*
* @example Usage
* ```ts no-assert
* import { gray } from "@std/fmt/colors";
*
* console.log(gray("Hello, world!"));
* ```
*
* @param str The text to make gray
* @returns The gray text
*/
export function gray(str: string): string {
return brightBlack(str);
}
/**
* Set text color to bright black.
*
* @example Usage
* ```ts no-assert
* import { brightBlack } from "@std/fmt/colors";
*
* console.log(brightBlack("Hello, world!"));
* ```
*
* @param str The text to make bright black
* @returns The bright black text
*/
export function brightBlack(str: string): string {
return run(str, code([90], 39));
}
/**
* Set text color to bright red.
*
* @example Usage
* ```ts no-assert
* import { brightRed } from "@std/fmt/colors";
*
* console.log(brightRed("Hello, world!"));
* ```
*
* @param str The text to make bright red
* @returns The bright red text
*/
export function brightRed(str: string): string {
return run(str, code([91], 39));
}
/**
* Set text color to bright green.
*
* @example Usage
* ```ts no-assert
* import { brightGreen } from "@std/fmt/colors";
*
* console.log(brightGreen("Hello, world!"));
* ```
*
* @param str The text to make bright green
* @returns The bright green text
*/
export function brightGreen(str: string): string {
return run(str, code([92], 39));
}
/**
* Set text color to bright yellow.
*
* @example Usage
* ```ts no-assert
* import { brightYellow } from "@std/fmt/colors";
*
* console.log(brightYellow("Hello, world!"));
* ```
*
* @param str The text to make bright yellow
* @returns The bright yellow text
*/
export function brightYellow(str: string): string {
return run(str, code([93], 39));
}
/**
* Set text color to bright blue.
*
* @example Usage
* ```ts no-assert
* import { brightBlue } from "@std/fmt/colors";
*
* console.log(brightBlue("Hello, world!"));
* ```
*
* @param str The text to make bright blue
* @returns The bright blue text
*/
export function brightBlue(str: string): string {
return run(str, code([94], 39));
}
/**
* Set text color to bright magenta.
*
* @example Usage
* ```ts no-assert
* import { brightMagenta } from "@std/fmt/colors";
*
* console.log(brightMagenta("Hello, world!"));
* ```
*
* @param str The text to make bright magenta
* @returns The bright magenta text
*/
export function brightMagenta(str: string): string {
return run(str, code([95], 39));
}
/**
* Set text color to bright cyan.
*
* @example Usage
* ```ts no-assert
* import { brightCyan } from "@std/fmt/colors";
*
* console.log(brightCyan("Hello, world!"));
* ```
*
* @param str The text to make bright cyan
* @returns The bright cyan text
*/
export function brightCyan(str: string): string {
return run(str, code([96], 39));
}
/**
* Set text color to bright white.
*
* @example Usage
* ```ts no-assert
* import { brightWhite } from "@std/fmt/colors";
*
* console.log(brightWhite("Hello, world!"));
* ```
*
* @param str The text to make bright white
* @returns The bright white text
*/
export function brightWhite(str: string): string {
return run(str, code([97], 39));
}
/**
* Set background color to black.
*
* @example Usage
* ```ts no-assert
* import { bgBlack } from "@std/fmt/colors";
*
* console.log(bgBlack("Hello, world!"));
* ```
*
* @param str The text to make its background black
* @returns The text with black background
*/
export function bgBlack(str: string): string {
return run(str, code([40], 49));
}
/**
* Set background color to red.
*
* @example Usage
* ```ts no-assert
* import { bgRed } from "@std/fmt/colors";
*
* console.log(bgRed("Hello, world!"));
* ```
*
* @param str The text to make its background red
* @returns The text with red background
*/
export function bgRed(str: string): string {
return run(str, code([41], 49));
}
/**
* Set background color to green.
*
* @example Usage
* ```ts no-assert
* import { bgGreen } from "@std/fmt/colors";
*
* console.log(bgGreen("Hello, world!"));
* ```
*
* @param str The text to make its background green
* @returns The text with green background
*/
export function bgGreen(str: string): string {
return run(str, code([42], 49));
}
/**
* Set background color to yellow.
*
* @example Usage
* ```ts no-assert
* import { bgYellow } from "@std/fmt/colors";
*
* console.log(bgYellow("Hello, world!"));
* ```
*
* @param str The text to make its background yellow
* @returns The text with yellow background
*/
export function bgYellow(str: string): string {
return run(str, code([43], 49));
}
/**
* Set background color to blue.
*
* @example Usage
* ```ts no-assert
* import { bgBlue } from "@std/fmt/colors";
*
* console.log(bgBlue("Hello, world!"));
* ```
*
* @param str The text to make its background blue
* @returns The text with blue background
*/
export function bgBlue(str: string): string {
return run(str, code([44], 49));
}
/**
* Set background color to magenta.
*
* @example Usage
* ```ts no-assert
* import { bgMagenta } from "@std/fmt/colors";
*
* console.log(bgMagenta("Hello, world!"));
* ```
*
* @param str The text to make its background magenta
* @returns The text with magenta background
*/
export function bgMagenta(str: string): string {
return run(str, code([45], 49));
}
/**
* Set background color to cyan.
*
* @example Usage
* ```ts no-assert
* import { bgCyan } from "@std/fmt/colors";
*
* console.log(bgCyan("Hello, world!"));
* ```
*
* @param str The text to make its background cyan
* @returns The text with cyan background
*/
export function bgCyan(str: string): string {
return run(str, code([46], 49));
}
/**
* Set background color to white.
*
* @example Usage
* ```ts no-assert
* import { bgWhite } from "@std/fmt/colors";
*
* console.log(bgWhite("Hello, world!"));
* ```
*
* @param str The text to make its background white
* @returns The text with white background
*/
export function bgWhite(str: string): string {
return run(str, code([47], 49));
}
/**
* Set background color to bright black.
*
* @example Usage
* ```ts no-assert
* import { bgBrightBlack } from "@std/fmt/colors";
*
* console.log(bgBrightBlack("Hello, world!"));
* ```
*
* @param str The text to make its background bright black
* @returns The text with bright black background
*/
export function bgBrightBlack(str: string): string {
return run(str, code([100], 49));
}
/**
* Set background color to bright red.
*
* @example Usage
* ```ts no-assert
* import { bgBrightRed } from "@std/fmt/colors";
*
* console.log(bgBrightRed("Hello, world!"));
* ```
*
* @param str The text to make its background bright red
* @returns The text with bright red background
*/
export function bgBrightRed(str: string): string {
return run(str, code([101], 49));
}
/**
* Set background color to bright green.
*
* @example Usage
* ```ts no-assert
* import { bgBrightGreen } from "@std/fmt/colors";
*
* console.log(bgBrightGreen("Hello, world!"));
* ```
*
* @param str The text to make its background bright green
* @returns The text with bright green background
*/
export function bgBrightGreen(str: string): string {
return run(str, code([102], 49));
}
/**
* Set background color to bright yellow.
*
* @example Usage
* ```ts no-assert
* import { bgBrightYellow } from "@std/fmt/colors";
*
* console.log(bgBrightYellow("Hello, world!"));
* ```
*
* @param str The text to make its background bright yellow
* @returns The text with bright yellow background
*/
export function bgBrightYellow(str: string): string {
return run(str, code([103], 49));
}
/**
* Set background color to bright blue.
*
* @example Usage
* ```ts no-assert
* import { bgBrightBlue } from "@std/fmt/colors";
*
* console.log(bgBrightBlue("Hello, world!"));
* ```
*
* @param str The text to make its background bright blue
* @returns The text with bright blue background
*/
export function bgBrightBlue(str: string): string {
return run(str, code([104], 49));
}
/**
* Set background color to bright magenta.
*
* @example Usage
* ```ts no-assert
* import { bgBrightMagenta } from "@std/fmt/colors";
*
* console.log(bgBrightMagenta("Hello, world!"));
* ```
*
* @param str The text to make its background bright magenta
* @returns The text with bright magenta background
*/
export function bgBrightMagenta(str: string): string {
return run(str, code([105], 49));
}
/**
* Set background color to bright cyan.
*
* @example Usage
* ```ts no-assert
* import { bgBrightCyan } from "@std/fmt/colors";
*
* console.log(bgBrightCyan("Hello, world!"));
* ```
*
* @param str The text to make its background bright cyan
* @returns The text with bright cyan background
*/
export function bgBrightCyan(str: string): string {
return run(str, code([106], 49));
}
/**
* Set background color to bright white.
*
* @example Usage
* ```ts no-assert
* import { bgBrightWhite } from "@std/fmt/colors";
*
* console.log(bgBrightWhite("Hello, world!"));
* ```
*
* @param str The text to make its background bright white
* @returns The text with bright white background
*/
export function bgBrightWhite(str: string): string {
return run(str, code([107], 49));
}
/* Special Color Sequences */
/**
* Clam and truncate color codes
* @param n The input number
* @param max The number to truncate to
* @param min The number to truncate from
*/
function clampAndTruncate(n: number, max = 255, min = 0): number {
return Math.trunc(Math.max(Math.min(n, max), min));
}
/**
* Set text color using paletted 8bit colors.
* https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
*
* @example Usage
* ```ts no-assert
* import { rgb8 } from "@std/fmt/colors";
*
* console.log(rgb8("Hello, world!", 42));
* ```
*
* @param str The text color to apply paletted 8bit colors to
* @param color The color code
* @returns The text with paletted 8bit color
*/
export function rgb8(str: string, color: number): string {
return run(str, code([38, 5, clampAndTruncate(color)], 39));
}
/**
* Set background color using paletted 8bit colors.
* https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
*
* @example Usage
* ```ts no-assert
* import { bgRgb8 } from "@std/fmt/colors";
*
* console.log(bgRgb8("Hello, world!", 42));
* ```
*
* @param str The text color to apply paletted 8bit background colors to
* @param color code
* @returns The text with paletted 8bit background color
*/
export function bgRgb8(str: string, color: number): string {
return run(str, code([48, 5, clampAndTruncate(color)], 49));
}
/**
* Set text color using 24bit rgb.
* `color` can be a number in range `0x000000` to `0xffffff` or
* an `Rgb`.
*
* @example To produce the color magenta:
* ```ts no-assert
* import { rgb24 } from "@std/fmt/colors";
*
* rgb24("foo", 0xff00ff);
* rgb24("foo", {r: 255, g: 0, b: 255});
* ```
* @param str The text color to apply 24bit rgb to
* @param color The color code
* @returns The text with 24bit rgb color
*/
export function rgb24(str: string, color: number | Rgb): string {
if (typeof color === "number") {
return run(
str,
code(
[38, 2, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff],
39,
),
);
}
return run(
str,
code(
[
38,
2,
clampAndTruncate(color.r),
clampAndTruncate(color.g),
clampAndTruncate(color.b),
],
39,
),
);
}
/**
* Set background color using 24bit rgb.
* `color` can be a number in range `0x000000` to `0xffffff` or
* an `Rgb`.
*
* @example To produce the color magenta:
* ```ts no-assert
* import { bgRgb24 } from "@std/fmt/colors";
*
* bgRgb24("foo", 0xff00ff);
* bgRgb24("foo", {r: 255, g: 0, b: 255});
* ```
* @param str The text color to apply 24bit rgb to
* @param color The color code
* @returns The text with 24bit rgb color
*/
export function bgRgb24(str: string, color: number | Rgb): string {
if (typeof color === "number") {
return run(
str,
code(
[48, 2, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff],
49,
),
);
}
return run(
str,
code(
[
48,
2,
clampAndTruncate(color.r),
clampAndTruncate(color.g),
clampAndTruncate(color.b),
],
49,
),
);
}
// https://github.com/chalk/ansi-regex/blob/02fa893d619d3da85411acc8fd4e2eea0e95a9d9/index.js
const ANSI_PATTERN = new RegExp(
[
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TXZcf-nq-uy=><~]))",
].join("|"),
"g",
);
/**
* Remove ANSI escape codes from the string.
*
* @example Usage
* ```ts no-assert
* import { stripAnsiCode, red } from "@std/fmt/colors";
*
* console.log(stripAnsiCode(red("Hello, world!")));
* ```
*
* @param string The text to remove ANSI escape codes from
* @returns The text without ANSI escape codes
*/
export function stripAnsiCode(string: string): string {
return string.replace(ANSI_PATTERN, "");
}
|