@crustjs/style 0.0.2 → 0.0.3
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/README.md +63 -0
- package/dist/index.d.ts +197 -2
- package/dist/index.js +108 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -58,6 +58,63 @@ console.log(applyStyle("critical error", boldRed));
|
|
|
58
58
|
|
|
59
59
|
Nested styles are handled safely — no style bleed across boundaries.
|
|
60
60
|
|
|
61
|
+
## Dynamic Colors (Truecolor)
|
|
62
|
+
|
|
63
|
+
Use any RGB or hex color via 24-bit truecolor ANSI sequences:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
import { rgb, bgRgb, hex, bgHex } from "@crustjs/style";
|
|
67
|
+
|
|
68
|
+
// RGB values (0–255)
|
|
69
|
+
console.log(rgb("ocean", 0, 128, 255));
|
|
70
|
+
console.log(bgRgb("warning", 255, 128, 0));
|
|
71
|
+
|
|
72
|
+
// Hex colors (#RGB or #RRGGBB)
|
|
73
|
+
console.log(hex("error", "#ff0000"));
|
|
74
|
+
console.log(hex("short", "#f00"));
|
|
75
|
+
console.log(bgHex("highlight", "#ff8800"));
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Pair Factories
|
|
79
|
+
|
|
80
|
+
Create reusable `AnsiPair` objects for composition:
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import { rgbCode, bgRgbCode, hexCode, bgHexCode, applyStyle, composeStyles, boldCode } from "@crustjs/style";
|
|
84
|
+
|
|
85
|
+
const coral = rgbCode(255, 127, 80);
|
|
86
|
+
console.log(applyStyle("coral text", coral));
|
|
87
|
+
|
|
88
|
+
const boldCoral = composeStyles(boldCode, hexCode("#ff7f50"));
|
|
89
|
+
console.log(applyStyle("bold coral", boldCoral));
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Style Instance
|
|
93
|
+
|
|
94
|
+
Dynamic colors on `createStyle` instances respect mode and truecolor detection:
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
import { createStyle } from "@crustjs/style";
|
|
98
|
+
|
|
99
|
+
const s = createStyle({ mode: "always" });
|
|
100
|
+
console.log(s.rgb("text", 255, 0, 0));
|
|
101
|
+
console.log(s.hex("text", "#ff0000"));
|
|
102
|
+
console.log(s.bgRgb("text", 0, 128, 255));
|
|
103
|
+
console.log(s.bgHex("text", "#0080ff"));
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
In `"auto"` mode, dynamic colors are emitted only when the terminal supports truecolor (detected via `COLORTERM=truecolor|24bit` or `TERM` containing `truecolor`, `24bit`, or `-direct`). When truecolor is not detected, dynamic color methods return plain text while standard 16-color methods continue to work.
|
|
107
|
+
|
|
108
|
+
In `"never"` mode, all dynamic color methods return plain text. In `"always"` mode, truecolor sequences are always emitted.
|
|
109
|
+
|
|
110
|
+
### Terminal Compatibility
|
|
111
|
+
|
|
112
|
+
Dynamic colors use truecolor (24-bit) ANSI sequences. There is no automatic fallback to 256 or 16 colors. On terminals that do not support truecolor:
|
|
113
|
+
|
|
114
|
+
- Colors may be approximated to the nearest supported color
|
|
115
|
+
- Colors may be silently ignored (text renders in default color)
|
|
116
|
+
- No runtime errors will occur
|
|
117
|
+
|
|
61
118
|
## Color Modes
|
|
62
119
|
|
|
63
120
|
Control when ANSI codes are emitted using `createStyle`:
|
|
@@ -92,6 +149,12 @@ const testStyle = createStyle({
|
|
|
92
149
|
mode: "auto",
|
|
93
150
|
overrides: { isTTY: true, noColor: undefined },
|
|
94
151
|
});
|
|
152
|
+
|
|
153
|
+
// Include truecolor overrides for dynamic color testing
|
|
154
|
+
const truecolorStyle = createStyle({
|
|
155
|
+
mode: "auto",
|
|
156
|
+
overrides: { isTTY: true, noColor: undefined, colorTerm: "truecolor" },
|
|
157
|
+
});
|
|
95
158
|
```
|
|
96
159
|
|
|
97
160
|
## Text Utilities
|
package/dist/index.d.ts
CHANGED
|
@@ -312,6 +312,18 @@ interface CapabilityOverrides {
|
|
|
312
312
|
readonly noColor?: string | undefined;
|
|
313
313
|
}
|
|
314
314
|
/**
|
|
315
|
+
* Truecolor capability overrides for deterministic testing.
|
|
316
|
+
*
|
|
317
|
+
* These override environment variable checks used by
|
|
318
|
+
* {@link resolveTrueColor} to detect 24-bit color support.
|
|
319
|
+
*/
|
|
320
|
+
interface TrueColorOverrides {
|
|
321
|
+
/** Override `process.env.COLORTERM`. */
|
|
322
|
+
readonly colorTerm?: string | undefined;
|
|
323
|
+
/** Override `process.env.TERM`. */
|
|
324
|
+
readonly term?: string | undefined;
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
315
327
|
* Configuration options for creating a style instance.
|
|
316
328
|
*
|
|
317
329
|
* @example
|
|
@@ -323,7 +335,7 @@ interface StyleOptions {
|
|
|
323
335
|
/** Color emission mode. Defaults to `"auto"`. */
|
|
324
336
|
readonly mode?: ColorMode;
|
|
325
337
|
/** Capability overrides for deterministic testing. */
|
|
326
|
-
readonly overrides?: CapabilityOverrides;
|
|
338
|
+
readonly overrides?: CapabilityOverrides & TrueColorOverrides;
|
|
327
339
|
}
|
|
328
340
|
/**
|
|
329
341
|
* A style function that applies an ANSI style pair to text,
|
|
@@ -360,8 +372,18 @@ type StyleMethodName2 = StyleMethodName;
|
|
|
360
372
|
interface StyleInstance extends StyleMethodMap {
|
|
361
373
|
/** Whether ANSI codes will be emitted by this instance. */
|
|
362
374
|
readonly enabled: boolean;
|
|
375
|
+
/** Whether truecolor (24-bit) sequences will be emitted by this instance. */
|
|
376
|
+
readonly trueColorEnabled: boolean;
|
|
363
377
|
/** Apply an arbitrary ANSI pair to text, respecting the color mode. */
|
|
364
378
|
readonly apply: (text: string, pair: AnsiPair) => string;
|
|
379
|
+
/** Apply a truecolor foreground RGB color to text. */
|
|
380
|
+
readonly rgb: (text: string, r: number, g: number, b: number) => string;
|
|
381
|
+
/** Apply a truecolor background RGB color to text. */
|
|
382
|
+
readonly bgRgb: (text: string, r: number, g: number, b: number) => string;
|
|
383
|
+
/** Apply a truecolor foreground hex color to text. */
|
|
384
|
+
readonly hex: (text: string, hexColor: string) => string;
|
|
385
|
+
/** Apply a truecolor background hex color to text. */
|
|
386
|
+
readonly bgHex: (text: string, hexColor: string) => string;
|
|
365
387
|
}
|
|
366
388
|
/**
|
|
367
389
|
* Resolve whether ANSI color codes should be emitted.
|
|
@@ -391,6 +413,35 @@ interface StyleInstance extends StyleMethodMap {
|
|
|
391
413
|
* ```
|
|
392
414
|
*/
|
|
393
415
|
declare function resolveCapability(mode: ColorMode, overrides?: CapabilityOverrides): boolean;
|
|
416
|
+
/**
|
|
417
|
+
* Resolve whether the terminal supports truecolor (24-bit) ANSI sequences.
|
|
418
|
+
*
|
|
419
|
+
* Detection heuristics (checked in order):
|
|
420
|
+
* 1. `COLORTERM` environment variable is `"truecolor"` or `"24bit"`.
|
|
421
|
+
* 2. `TERM` environment variable contains `"24bit"`, `"truecolor"`, or `"-direct"`.
|
|
422
|
+
*
|
|
423
|
+
* In `"always"` mode, returns `true` unconditionally.
|
|
424
|
+
* In `"never"` mode, returns `false` unconditionally.
|
|
425
|
+
* In `"auto"` mode, requires base color to be enabled **and** truecolor
|
|
426
|
+
* to be detected.
|
|
427
|
+
*
|
|
428
|
+
* @param mode - The color emission mode.
|
|
429
|
+
* @param overrides - Optional overrides for deterministic testing.
|
|
430
|
+
* @returns `true` if truecolor sequences should be emitted.
|
|
431
|
+
*
|
|
432
|
+
* @example
|
|
433
|
+
* ```ts
|
|
434
|
+
* resolveTrueColor("auto"); // true if TTY + truecolor env detected
|
|
435
|
+
* resolveTrueColor("always"); // true
|
|
436
|
+
* resolveTrueColor("never"); // false
|
|
437
|
+
* resolveTrueColor("auto", {
|
|
438
|
+
* isTTY: true,
|
|
439
|
+
* noColor: undefined,
|
|
440
|
+
* colorTerm: "truecolor",
|
|
441
|
+
* }); // true
|
|
442
|
+
* ```
|
|
443
|
+
*/
|
|
444
|
+
declare function resolveTrueColor(mode: ColorMode, overrides?: CapabilityOverrides & TrueColorOverrides): boolean;
|
|
394
445
|
/** Apply black foreground color. */
|
|
395
446
|
declare function black2(text: string): string;
|
|
396
447
|
/** Apply red foreground color. */
|
|
@@ -506,6 +557,150 @@ declare function createStyle(options?: StyleOptions): StyleInstance;
|
|
|
506
557
|
*/
|
|
507
558
|
declare const style: StyleInstance;
|
|
508
559
|
/**
|
|
560
|
+
* Parse a hex color string into RGB channel values.
|
|
561
|
+
*
|
|
562
|
+
* Supports `#RGB` (shorthand) and `#RRGGBB` (full) formats.
|
|
563
|
+
* The `#` prefix is required.
|
|
564
|
+
*
|
|
565
|
+
* @param hex - The hex color string.
|
|
566
|
+
* @returns A tuple of `[r, g, b]` channel values (0–255).
|
|
567
|
+
* @throws {TypeError} If the string is not a valid hex color.
|
|
568
|
+
*
|
|
569
|
+
* @example
|
|
570
|
+
* ```ts
|
|
571
|
+
* parseHex("#ff0000"); // [255, 0, 0]
|
|
572
|
+
* parseHex("#f00"); // [255, 0, 0]
|
|
573
|
+
* ```
|
|
574
|
+
*/
|
|
575
|
+
declare function parseHex(hex: string): [r: number, g: number, b: number];
|
|
576
|
+
/**
|
|
577
|
+
* Create an {@link AnsiPair} for a truecolor foreground RGB color.
|
|
578
|
+
*
|
|
579
|
+
* Uses the ANSI escape sequence `\x1b[38;2;R;G;Bm` with close `\x1b[39m`.
|
|
580
|
+
*
|
|
581
|
+
* @param r - Red channel (0–255).
|
|
582
|
+
* @param g - Green channel (0–255).
|
|
583
|
+
* @param b - Blue channel (0–255).
|
|
584
|
+
* @returns An ANSI pair for the specified foreground color.
|
|
585
|
+
* @throws {RangeError} If any channel value is invalid.
|
|
586
|
+
*
|
|
587
|
+
* @example
|
|
588
|
+
* ```ts
|
|
589
|
+
* const pair = rgbCode(255, 0, 0);
|
|
590
|
+
* applyStyle("error", pair); // red foreground
|
|
591
|
+
* ```
|
|
592
|
+
*/
|
|
593
|
+
declare function rgbCode(r: number, g: number, b: number): AnsiPair;
|
|
594
|
+
/**
|
|
595
|
+
* Create an {@link AnsiPair} for a truecolor background RGB color.
|
|
596
|
+
*
|
|
597
|
+
* Uses the ANSI escape sequence `\x1b[48;2;R;G;Bm` with close `\x1b[49m`.
|
|
598
|
+
*
|
|
599
|
+
* @param r - Red channel (0–255).
|
|
600
|
+
* @param g - Green channel (0–255).
|
|
601
|
+
* @param b - Blue channel (0–255).
|
|
602
|
+
* @returns An ANSI pair for the specified background color.
|
|
603
|
+
* @throws {RangeError} If any channel value is invalid.
|
|
604
|
+
*
|
|
605
|
+
* @example
|
|
606
|
+
* ```ts
|
|
607
|
+
* const pair = bgRgbCode(255, 128, 0);
|
|
608
|
+
* applyStyle("warning", pair); // orange background
|
|
609
|
+
* ```
|
|
610
|
+
*/
|
|
611
|
+
declare function bgRgbCode(r: number, g: number, b: number): AnsiPair;
|
|
612
|
+
/**
|
|
613
|
+
* Create an {@link AnsiPair} for a truecolor foreground hex color.
|
|
614
|
+
*
|
|
615
|
+
* @param hex - Hex color string (`#RGB` or `#RRGGBB`).
|
|
616
|
+
* @returns An ANSI pair for the specified foreground color.
|
|
617
|
+
* @throws {TypeError} If the hex string is invalid.
|
|
618
|
+
*
|
|
619
|
+
* @example
|
|
620
|
+
* ```ts
|
|
621
|
+
* const pair = hexCode("#ff0000");
|
|
622
|
+
* applyStyle("error", pair); // red foreground
|
|
623
|
+
* ```
|
|
624
|
+
*/
|
|
625
|
+
declare function hexCode(hex: string): AnsiPair;
|
|
626
|
+
/**
|
|
627
|
+
* Create an {@link AnsiPair} for a truecolor background hex color.
|
|
628
|
+
*
|
|
629
|
+
* @param hex - Hex color string (`#RGB` or `#RRGGBB`).
|
|
630
|
+
* @returns An ANSI pair for the specified background color.
|
|
631
|
+
* @throws {TypeError} If the hex string is invalid.
|
|
632
|
+
*
|
|
633
|
+
* @example
|
|
634
|
+
* ```ts
|
|
635
|
+
* const pair = bgHexCode("#ff8800");
|
|
636
|
+
* applyStyle("warning", pair); // orange background
|
|
637
|
+
* ```
|
|
638
|
+
*/
|
|
639
|
+
declare function bgHexCode(hex: string): AnsiPair;
|
|
640
|
+
/**
|
|
641
|
+
* Apply a truecolor foreground RGB color to text.
|
|
642
|
+
*
|
|
643
|
+
* @param text - The text to style.
|
|
644
|
+
* @param r - Red channel (0–255).
|
|
645
|
+
* @param g - Green channel (0–255).
|
|
646
|
+
* @param b - Blue channel (0–255).
|
|
647
|
+
* @returns The styled string.
|
|
648
|
+
* @throws {RangeError} If any channel value is invalid.
|
|
649
|
+
*
|
|
650
|
+
* @example
|
|
651
|
+
* ```ts
|
|
652
|
+
* rgb("error", 255, 0, 0); // red text
|
|
653
|
+
* rgb("ocean", 0, 128, 255); // blue text
|
|
654
|
+
* ```
|
|
655
|
+
*/
|
|
656
|
+
declare function rgb(text: string, r: number, g: number, b: number): string;
|
|
657
|
+
/**
|
|
658
|
+
* Apply a truecolor background RGB color to text.
|
|
659
|
+
*
|
|
660
|
+
* @param text - The text to style.
|
|
661
|
+
* @param r - Red channel (0–255).
|
|
662
|
+
* @param g - Green channel (0–255).
|
|
663
|
+
* @param b - Blue channel (0–255).
|
|
664
|
+
* @returns The styled string.
|
|
665
|
+
* @throws {RangeError} If any channel value is invalid.
|
|
666
|
+
*
|
|
667
|
+
* @example
|
|
668
|
+
* ```ts
|
|
669
|
+
* bgRgb("warning", 255, 128, 0); // orange background
|
|
670
|
+
* ```
|
|
671
|
+
*/
|
|
672
|
+
declare function bgRgb2(text: string, r: number, g: number, b: number): string;
|
|
673
|
+
/**
|
|
674
|
+
* Apply a truecolor foreground hex color to text.
|
|
675
|
+
*
|
|
676
|
+
* @param text - The text to style.
|
|
677
|
+
* @param hexColor - Hex color string (`#RGB` or `#RRGGBB`).
|
|
678
|
+
* @returns The styled string.
|
|
679
|
+
* @throws {TypeError} If the hex string is invalid.
|
|
680
|
+
*
|
|
681
|
+
* @example
|
|
682
|
+
* ```ts
|
|
683
|
+
* hex("error", "#ff0000"); // red text
|
|
684
|
+
* hex("ocean", "#0080ff"); // blue text
|
|
685
|
+
* hex("short", "#f00"); // red text (shorthand)
|
|
686
|
+
* ```
|
|
687
|
+
*/
|
|
688
|
+
declare function hex(text: string, hexColor: string): string;
|
|
689
|
+
/**
|
|
690
|
+
* Apply a truecolor background hex color to text.
|
|
691
|
+
*
|
|
692
|
+
* @param text - The text to style.
|
|
693
|
+
* @param hexColor - Hex color string (`#RGB` or `#RRGGBB`).
|
|
694
|
+
* @returns The styled string.
|
|
695
|
+
* @throws {TypeError} If the hex string is invalid.
|
|
696
|
+
*
|
|
697
|
+
* @example
|
|
698
|
+
* ```ts
|
|
699
|
+
* bgHex("warning", "#ff8800"); // orange background
|
|
700
|
+
* ```
|
|
701
|
+
*/
|
|
702
|
+
declare function bgHex2(text: string, hexColor: string): string;
|
|
703
|
+
/**
|
|
509
704
|
* Apply **bold** (increased intensity) to text.
|
|
510
705
|
*
|
|
511
706
|
* @example
|
|
@@ -949,4 +1144,4 @@ declare function createMarkdownTheme(options?: CreateMarkdownThemeOptions): Mark
|
|
|
949
1144
|
* ```
|
|
950
1145
|
*/
|
|
951
1146
|
declare const defaultTheme: MarkdownTheme;
|
|
952
|
-
export { yellow as yellowCode, yellow2 as yellow, wrapText, white as whiteCode, white2 as white, visibleWidth, unorderedList, underline as underlineCode, underline2 as underline, taskList, table, style, stripAnsi, strikethrough as strikethroughCode, strikethrough2 as strikethrough, resolveCapability, reset, red as redCode, red2 as red, padStart, padEnd, orderedList, magenta as magentaCode, magenta2 as magenta, italic as italicCode, italic2 as italic, inverse as inverseCode, inverse2 as inverse, hidden as hiddenCode, hidden2 as hidden, green as greenCode, green2 as green, gray as grayCode, gray2 as gray, dim as dimCode, dim2 as dim, defaultTheme, cyan as cyanCode, cyan2 as cyan, createStyle, createMarkdownTheme, composeStyles, center, buildDefaultMarkdownTheme, brightYellow as brightYellowCode, brightYellow2 as brightYellow, brightWhite as brightWhiteCode, brightWhite2 as brightWhite, brightRed as brightRedCode, brightRed2 as brightRed, brightMagenta as brightMagentaCode, brightMagenta2 as brightMagenta, brightGreen as brightGreenCode, brightGreen2 as brightGreen, brightCyan as brightCyanCode, brightCyan2 as brightCyan, brightBlue as brightBlueCode, brightBlue2 as brightBlue, bold as boldCode, bold2 as bold, blue as blueCode, blue2 as blue, black as blackCode, black2 as black, bgYellow as bgYellowCode, bgYellow2 as bgYellow, bgWhite as bgWhiteCode, bgWhite2 as bgWhite, bgRed as bgRedCode, bgRed2 as bgRed, bgMagenta as bgMagentaCode, bgMagenta2 as bgMagenta, bgGreen as bgGreenCode, bgGreen2 as bgGreen, bgCyan as bgCyanCode, bgCyan2 as bgCyan, bgBrightYellow as bgBrightYellowCode, bgBrightYellow2 as bgBrightYellow, bgBrightWhite as bgBrightWhiteCode, bgBrightWhite2 as bgBrightWhite, bgBrightRed as bgBrightRedCode, bgBrightRed2 as bgBrightRed, bgBrightMagenta as bgBrightMagentaCode, bgBrightMagenta2 as bgBrightMagenta, bgBrightGreen as bgBrightGreenCode, bgBrightGreen2 as bgBrightGreen, bgBrightCyan as bgBrightCyanCode, bgBrightCyan2 as bgBrightCyan, bgBrightBlue as bgBrightBlueCode, bgBrightBlue2 as bgBrightBlue, bgBrightBlack as bgBrightBlackCode, bgBrightBlack2 as bgBrightBlack, bgBlue as bgBlueCode, bgBlue2 as bgBlue, bgBlack as bgBlackCode, bgBlack2 as bgBlack, applyStyle, WrapOptions, UnorderedListOptions, ThemeSlotFn, TaskListOptions, TaskListItem, TableOptions, StyleOptions, StyleInstance, StyleFn, PartialMarkdownTheme, OrderedListOptions, MarkdownTheme, CreateMarkdownThemeOptions, ColumnAlignment, ColorMode, CapabilityOverrides, AnsiPair };
|
|
1147
|
+
export { yellow as yellowCode, yellow2 as yellow, wrapText, white as whiteCode, white2 as white, visibleWidth, unorderedList, underline as underlineCode, underline2 as underline, taskList, table, style, stripAnsi, strikethrough as strikethroughCode, strikethrough2 as strikethrough, rgbCode, rgb, resolveTrueColor, resolveCapability, reset, red as redCode, red2 as red, parseHex, padStart, padEnd, orderedList, magenta as magentaCode, magenta2 as magenta, italic as italicCode, italic2 as italic, inverse as inverseCode, inverse2 as inverse, hidden as hiddenCode, hidden2 as hidden, hexCode, hex, green as greenCode, green2 as green, gray as grayCode, gray2 as gray, dim as dimCode, dim2 as dim, defaultTheme, cyan as cyanCode, cyan2 as cyan, createStyle, createMarkdownTheme, composeStyles, center, buildDefaultMarkdownTheme, brightYellow as brightYellowCode, brightYellow2 as brightYellow, brightWhite as brightWhiteCode, brightWhite2 as brightWhite, brightRed as brightRedCode, brightRed2 as brightRed, brightMagenta as brightMagentaCode, brightMagenta2 as brightMagenta, brightGreen as brightGreenCode, brightGreen2 as brightGreen, brightCyan as brightCyanCode, brightCyan2 as brightCyan, brightBlue as brightBlueCode, brightBlue2 as brightBlue, bold as boldCode, bold2 as bold, blue as blueCode, blue2 as blue, black as blackCode, black2 as black, bgYellow as bgYellowCode, bgYellow2 as bgYellow, bgWhite as bgWhiteCode, bgWhite2 as bgWhite, bgRgbCode, bgRgb2 as bgRgb, bgRed as bgRedCode, bgRed2 as bgRed, bgMagenta as bgMagentaCode, bgMagenta2 as bgMagenta, bgHexCode, bgHex2 as bgHex, bgGreen as bgGreenCode, bgGreen2 as bgGreen, bgCyan as bgCyanCode, bgCyan2 as bgCyan, bgBrightYellow as bgBrightYellowCode, bgBrightYellow2 as bgBrightYellow, bgBrightWhite as bgBrightWhiteCode, bgBrightWhite2 as bgBrightWhite, bgBrightRed as bgBrightRedCode, bgBrightRed2 as bgBrightRed, bgBrightMagenta as bgBrightMagentaCode, bgBrightMagenta2 as bgBrightMagenta, bgBrightGreen as bgBrightGreenCode, bgBrightGreen2 as bgBrightGreen, bgBrightCyan as bgBrightCyanCode, bgBrightCyan2 as bgBrightCyan, bgBrightBlue as bgBrightBlueCode, bgBrightBlue2 as bgBrightBlue, bgBrightBlack as bgBrightBlackCode, bgBrightBlack2 as bgBrightBlack, bgBlue as bgBlueCode, bgBlue2 as bgBlue, bgBlack as bgBlackCode, bgBlack2 as bgBlack, applyStyle, WrapOptions, UnorderedListOptions, TrueColorOverrides, ThemeSlotFn, TaskListOptions, TaskListItem, TableOptions, StyleOptions, StyleInstance, StyleFn, PartialMarkdownTheme, OrderedListOptions, MarkdownTheme, CreateMarkdownThemeOptions, ColumnAlignment, ColorMode, CapabilityOverrides, AnsiPair };
|
package/dist/index.js
CHANGED
|
@@ -287,6 +287,32 @@ function resolveCapability(mode, overrides) {
|
|
|
287
287
|
}
|
|
288
288
|
return isTTY;
|
|
289
289
|
}
|
|
290
|
+
function resolveTrueColor(mode, overrides) {
|
|
291
|
+
if (mode === "always") {
|
|
292
|
+
return true;
|
|
293
|
+
}
|
|
294
|
+
if (mode === "never") {
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
if (!resolveCapability(mode, overrides)) {
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
const hasColorTermOverride = overrides !== undefined && "colorTerm" in overrides;
|
|
301
|
+
const colorTerm = hasColorTermOverride ? overrides.colorTerm : process.env.COLORTERM;
|
|
302
|
+
const lowerColorTerm = colorTerm?.toLowerCase();
|
|
303
|
+
if (lowerColorTerm === "truecolor" || lowerColorTerm === "24bit") {
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
const hasTermOverride = overrides !== undefined && "term" in overrides;
|
|
307
|
+
const term = hasTermOverride ? overrides.term : process.env.TERM;
|
|
308
|
+
if (term !== undefined) {
|
|
309
|
+
const lower = term.toLowerCase();
|
|
310
|
+
if (lower.includes("24bit") || lower.includes("truecolor") || lower.endsWith("-direct")) {
|
|
311
|
+
return true;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return false;
|
|
315
|
+
}
|
|
290
316
|
// src/styleEngine.ts
|
|
291
317
|
function applyStyle(text, style) {
|
|
292
318
|
if (text === "") {
|
|
@@ -402,6 +428,72 @@ function bgBrightCyan2(text) {
|
|
|
402
428
|
function bgBrightWhite2(text) {
|
|
403
429
|
return applyStyle(text, bgBrightWhite);
|
|
404
430
|
}
|
|
431
|
+
// src/dynamicColors.ts
|
|
432
|
+
function validateChannel(value, channel) {
|
|
433
|
+
if (!Number.isInteger(value) || value < 0 || value > 255) {
|
|
434
|
+
throw new RangeError(`Invalid ${channel} value: ${String(value)}. Must be an integer between 0 and 255.`);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
function validateRgb(r, g, b) {
|
|
438
|
+
validateChannel(r, "red");
|
|
439
|
+
validateChannel(g, "green");
|
|
440
|
+
validateChannel(b, "blue");
|
|
441
|
+
}
|
|
442
|
+
var HEX_SHORT = /^#([0-9a-f]{3})$/i;
|
|
443
|
+
var HEX_LONG = /^#([0-9a-f]{6})$/i;
|
|
444
|
+
function parseHex(hex) {
|
|
445
|
+
const shortMatch = HEX_SHORT.exec(hex);
|
|
446
|
+
if (shortMatch) {
|
|
447
|
+
const digits = shortMatch[1];
|
|
448
|
+
const r = digits.charAt(0);
|
|
449
|
+
const g = digits.charAt(1);
|
|
450
|
+
const b = digits.charAt(2);
|
|
451
|
+
return [
|
|
452
|
+
Number.parseInt(r + r, 16),
|
|
453
|
+
Number.parseInt(g + g, 16),
|
|
454
|
+
Number.parseInt(b + b, 16)
|
|
455
|
+
];
|
|
456
|
+
}
|
|
457
|
+
const longMatch = HEX_LONG.exec(hex);
|
|
458
|
+
if (longMatch) {
|
|
459
|
+
const digits = longMatch[1];
|
|
460
|
+
return [
|
|
461
|
+
Number.parseInt(digits.slice(0, 2), 16),
|
|
462
|
+
Number.parseInt(digits.slice(2, 4), 16),
|
|
463
|
+
Number.parseInt(digits.slice(4, 6), 16)
|
|
464
|
+
];
|
|
465
|
+
}
|
|
466
|
+
throw new TypeError(`Invalid hex color: "${hex}". Expected format: "#RGB" or "#RRGGBB".`);
|
|
467
|
+
}
|
|
468
|
+
function rgbCode(r, g, b) {
|
|
469
|
+
validateRgb(r, g, b);
|
|
470
|
+
return { open: `\x1B[38;2;${r};${g};${b}m`, close: "\x1B[39m" };
|
|
471
|
+
}
|
|
472
|
+
function bgRgbCode(r, g, b) {
|
|
473
|
+
validateRgb(r, g, b);
|
|
474
|
+
return { open: `\x1B[48;2;${r};${g};${b}m`, close: "\x1B[49m" };
|
|
475
|
+
}
|
|
476
|
+
function hexCode(hex) {
|
|
477
|
+
const [r, g, b] = parseHex(hex);
|
|
478
|
+
return rgbCode(r, g, b);
|
|
479
|
+
}
|
|
480
|
+
function bgHexCode(hex) {
|
|
481
|
+
const [r, g, b] = parseHex(hex);
|
|
482
|
+
return bgRgbCode(r, g, b);
|
|
483
|
+
}
|
|
484
|
+
function rgb(text, r, g, b) {
|
|
485
|
+
return applyStyle(text, rgbCode(r, g, b));
|
|
486
|
+
}
|
|
487
|
+
function bgRgb(text, r, g, b) {
|
|
488
|
+
return applyStyle(text, bgRgbCode(r, g, b));
|
|
489
|
+
}
|
|
490
|
+
function hex(text, hexColor) {
|
|
491
|
+
return applyStyle(text, hexCode(hexColor));
|
|
492
|
+
}
|
|
493
|
+
function bgHex(text, hexColor) {
|
|
494
|
+
return applyStyle(text, bgHexCode(hexColor));
|
|
495
|
+
}
|
|
496
|
+
|
|
405
497
|
// src/styleMethodRegistry.ts
|
|
406
498
|
function readStyleMethodPairs() {
|
|
407
499
|
const { reset: _reset, ...pairs } = exports_ansiCodes;
|
|
@@ -464,11 +556,17 @@ function buildStyleMethods(createChainableStyle) {
|
|
|
464
556
|
function createStyle(options) {
|
|
465
557
|
const mode = options?.mode ?? "auto";
|
|
466
558
|
const enabled = resolveCapability(mode, options?.overrides);
|
|
559
|
+
const trueColorEnabled = resolveTrueColor(mode, options?.overrides);
|
|
467
560
|
const createChainableStyle = buildChainableStyleFactory(enabled);
|
|
468
561
|
const methods = buildStyleMethods(createChainableStyle);
|
|
469
562
|
const instance = {
|
|
470
563
|
enabled,
|
|
564
|
+
trueColorEnabled,
|
|
471
565
|
apply: enabled ? (text, pair2) => applyStyle(text, pair2) : (text, _pair) => text,
|
|
566
|
+
rgb: trueColorEnabled ? (text, r, g, b) => rgb(text, r, g, b) : (text, _r, _g, _b) => text,
|
|
567
|
+
bgRgb: trueColorEnabled ? (text, r, g, b) => bgRgb(text, r, g, b) : (text, _r, _g, _b) => text,
|
|
568
|
+
hex: trueColorEnabled ? (text, hexColor) => hex(text, hexColor) : (text, _hexColor) => text,
|
|
569
|
+
bgHex: trueColorEnabled ? (text, hexColor) => bgHex(text, hexColor) : (text, _hexColor) => text,
|
|
472
570
|
...methods
|
|
473
571
|
};
|
|
474
572
|
return Object.freeze(instance);
|
|
@@ -706,10 +804,14 @@ export {
|
|
|
706
804
|
stripAnsi,
|
|
707
805
|
strikethrough as strikethroughCode,
|
|
708
806
|
strikethrough2 as strikethrough,
|
|
807
|
+
rgbCode,
|
|
808
|
+
rgb,
|
|
809
|
+
resolveTrueColor,
|
|
709
810
|
resolveCapability,
|
|
710
811
|
reset,
|
|
711
812
|
red as redCode,
|
|
712
813
|
red2 as red,
|
|
814
|
+
parseHex,
|
|
713
815
|
padStart,
|
|
714
816
|
padEnd,
|
|
715
817
|
orderedList,
|
|
@@ -721,6 +823,8 @@ export {
|
|
|
721
823
|
inverse2 as inverse,
|
|
722
824
|
hidden as hiddenCode,
|
|
723
825
|
hidden2 as hidden,
|
|
826
|
+
hexCode,
|
|
827
|
+
hex,
|
|
724
828
|
green as greenCode,
|
|
725
829
|
green2 as green,
|
|
726
830
|
gray as grayCode,
|
|
@@ -759,10 +863,14 @@ export {
|
|
|
759
863
|
bgYellow2 as bgYellow,
|
|
760
864
|
bgWhite as bgWhiteCode,
|
|
761
865
|
bgWhite2 as bgWhite,
|
|
866
|
+
bgRgbCode,
|
|
867
|
+
bgRgb,
|
|
762
868
|
bgRed as bgRedCode,
|
|
763
869
|
bgRed2 as bgRed,
|
|
764
870
|
bgMagenta as bgMagentaCode,
|
|
765
871
|
bgMagenta2 as bgMagenta,
|
|
872
|
+
bgHexCode,
|
|
873
|
+
bgHex,
|
|
766
874
|
bgGreen as bgGreenCode,
|
|
767
875
|
bgGreen2 as bgGreen,
|
|
768
876
|
bgCyan as bgCyanCode,
|