@oxog/pigment 1.1.0 → 1.2.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/README.md +91 -79
- package/dist/index-DsggPsSl.d.cts +1482 -0
- package/dist/index-DsggPsSl.d.ts +1482 -0
- package/dist/index.cjs +423 -269
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +33 -134
- package/dist/index.d.ts +33 -134
- package/dist/index.js +423 -269
- package/dist/index.js.map +1 -1
- package/dist/plugins/index.cjs +510 -244
- package/dist/plugins/index.cjs.map +1 -1
- package/dist/plugins/index.d.cts +1 -3
- package/dist/plugins/index.d.ts +1 -3
- package/dist/plugins/index.js +510 -244
- package/dist/plugins/index.js.map +1 -1
- package/package.json +21 -12
- package/dist/index-CLqJXFfX.d.cts +0 -441
- package/dist/index-CLqJXFfX.d.ts +0 -441
package/dist/plugins/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @oxog/pigment -
|
|
2
|
+
* @oxog/pigment - Terminal styling
|
|
3
3
|
* MIT License - https://pigment.oxog.dev
|
|
4
4
|
*/
|
|
5
5
|
|
|
@@ -293,6 +293,12 @@ var PigmentError = class _PigmentError extends Error {
|
|
|
293
293
|
Error.captureStackTrace(this, _PigmentError);
|
|
294
294
|
}
|
|
295
295
|
};
|
|
296
|
+
var PluginError = class extends PigmentError {
|
|
297
|
+
constructor(pluginName, message) {
|
|
298
|
+
super(`[${pluginName}] ${message}`, "PLUGIN_ERROR");
|
|
299
|
+
this.name = "PluginError";
|
|
300
|
+
}
|
|
301
|
+
};
|
|
296
302
|
var ColorError = class extends PigmentError {
|
|
297
303
|
constructor(message) {
|
|
298
304
|
super(message, "COLOR_ERROR");
|
|
@@ -346,11 +352,11 @@ function trueColorPlugin() {
|
|
|
346
352
|
k.emit("color:rgb", { ...rgb, background });
|
|
347
353
|
});
|
|
348
354
|
k.on("color:hsl", (data) => {
|
|
349
|
-
const { h: h2, s, l, background } = data;
|
|
355
|
+
const { h: h2, s, l: l2, background } = data;
|
|
350
356
|
if (!utils?.hslToRgb) {
|
|
351
357
|
throw new Error("hslToRgb utility not available");
|
|
352
358
|
}
|
|
353
|
-
const rgb = utils.hslToRgb(h2, s,
|
|
359
|
+
const rgb = utils.hslToRgb(h2, s, l2);
|
|
354
360
|
k.emit("color:rgb", { ...rgb, background });
|
|
355
361
|
});
|
|
356
362
|
}
|
|
@@ -541,10 +547,10 @@ function parseExpandedHex(hex) {
|
|
|
541
547
|
}
|
|
542
548
|
|
|
543
549
|
// src/utils/hsl-to-rgb.ts
|
|
544
|
-
function hslToRgb(h2, s,
|
|
550
|
+
function hslToRgb(h2, s, l2) {
|
|
545
551
|
const hNormalized = h2 / 360;
|
|
546
552
|
const sNormalized = s / 100;
|
|
547
|
-
const lNormalized =
|
|
553
|
+
const lNormalized = l2 / 100;
|
|
548
554
|
let r;
|
|
549
555
|
let g;
|
|
550
556
|
let b;
|
|
@@ -572,7 +578,263 @@ function hslToRgb(h2, s, l) {
|
|
|
572
578
|
};
|
|
573
579
|
}
|
|
574
580
|
|
|
581
|
+
// src/utils/rgb-to-ansi256.ts
|
|
582
|
+
function rgbToAnsi256(r, g, b) {
|
|
583
|
+
const clampedR = Math.max(0, Math.min(255, r));
|
|
584
|
+
const clampedG = Math.max(0, Math.min(255, g));
|
|
585
|
+
const clampedB = Math.max(0, Math.min(255, b));
|
|
586
|
+
if (clampedR === clampedG && clampedG === clampedB) {
|
|
587
|
+
if (clampedR < 8) {
|
|
588
|
+
return 16;
|
|
589
|
+
}
|
|
590
|
+
if (clampedR > 248) {
|
|
591
|
+
return 231;
|
|
592
|
+
}
|
|
593
|
+
return Math.round((clampedR - 8) / 247 * 24) + 232;
|
|
594
|
+
}
|
|
595
|
+
return Math.round(clampedR / 51) * 36 + Math.round(clampedG / 51) * 6 + Math.round(clampedB / 51) + 16;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
// src/core/ansi.ts
|
|
599
|
+
function generateModifierCode(name) {
|
|
600
|
+
const modifier = ANSI_MODIFIERS[name];
|
|
601
|
+
if (!modifier) {
|
|
602
|
+
throw new StyleError(`Unknown modifier: ${name}`);
|
|
603
|
+
}
|
|
604
|
+
return {
|
|
605
|
+
name,
|
|
606
|
+
code: -1,
|
|
607
|
+
open: modifier.open,
|
|
608
|
+
close: modifier.close,
|
|
609
|
+
type: "modifier"
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
function generateColorCode(name, background = false) {
|
|
613
|
+
let normalizedName = name;
|
|
614
|
+
const lowerName = name.toLowerCase();
|
|
615
|
+
if (lowerName === "gray" || lowerName === "grey") {
|
|
616
|
+
normalizedName = "blackBright";
|
|
617
|
+
} else if (lowerName === "bggray" || lowerName === "bggrey") {
|
|
618
|
+
normalizedName = "bgBlackBright";
|
|
619
|
+
}
|
|
620
|
+
const foregroundColors = { ...ANSI_FOREGROUND_COLORS, ...ANSI_FOREGROUND_BRIGHT_COLORS };
|
|
621
|
+
const backgroundColors = { ...ANSI_BACKGROUND_COLORS, ...ANSI_BACKGROUND_BRIGHT_COLORS };
|
|
622
|
+
let lookupName = normalizedName;
|
|
623
|
+
if (background && !normalizedName.startsWith("bg")) {
|
|
624
|
+
lookupName = "bg" + normalizedName.charAt(0).toUpperCase() + normalizedName.slice(1);
|
|
625
|
+
}
|
|
626
|
+
const allColors = background ? backgroundColors : foregroundColors;
|
|
627
|
+
const code = allColors[lookupName];
|
|
628
|
+
if (code === void 0) {
|
|
629
|
+
throw new StyleError(`Unknown color: ${name}`);
|
|
630
|
+
}
|
|
631
|
+
const closeCode = background ? 49 : 39;
|
|
632
|
+
return {
|
|
633
|
+
name,
|
|
634
|
+
code,
|
|
635
|
+
open: `\x1B[${code}m`,
|
|
636
|
+
close: `\x1B[${closeCode}m`,
|
|
637
|
+
type: "color",
|
|
638
|
+
background
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
function generateAnsi256Code(code, background = false) {
|
|
642
|
+
if (code < 0 || code > 255) {
|
|
643
|
+
throw new StyleError(`ANSI 256 code must be between 0 and 255, got ${code}`);
|
|
644
|
+
}
|
|
645
|
+
const prefix = background ? 48 : 38;
|
|
646
|
+
const closeCode = background ? 49 : 39;
|
|
647
|
+
return {
|
|
648
|
+
name: `ansi256(${code})`,
|
|
649
|
+
code,
|
|
650
|
+
open: `\x1B[${prefix};5;${code}m`,
|
|
651
|
+
close: `\x1B[${closeCode}m`,
|
|
652
|
+
type: "color",
|
|
653
|
+
background
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
function generateRgbCode(r, g, b, background = false) {
|
|
657
|
+
if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) {
|
|
658
|
+
throw new StyleError(`RGB values must be between 0 and 255, got (${r}, ${g}, ${b})`);
|
|
659
|
+
}
|
|
660
|
+
const prefix = background ? 48 : 38;
|
|
661
|
+
const closeCode = background ? 49 : 39;
|
|
662
|
+
return {
|
|
663
|
+
name: `rgb(${r}, ${g}, ${b})`,
|
|
664
|
+
code: -1,
|
|
665
|
+
open: `\x1B[${prefix};2;${r};${g};${b}m`,
|
|
666
|
+
close: `\x1B[${closeCode}m`,
|
|
667
|
+
type: "color",
|
|
668
|
+
background
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
function ansi256ToAnsi16(code) {
|
|
672
|
+
if (code < 8) {
|
|
673
|
+
return 30 + code;
|
|
674
|
+
}
|
|
675
|
+
if (code < 16) {
|
|
676
|
+
return 90 + (code - 8);
|
|
677
|
+
}
|
|
678
|
+
let red;
|
|
679
|
+
let green;
|
|
680
|
+
let blue;
|
|
681
|
+
if (code >= 232) {
|
|
682
|
+
red = ((code - 232) * 10 + 8) / 255;
|
|
683
|
+
green = red;
|
|
684
|
+
blue = red;
|
|
685
|
+
} else {
|
|
686
|
+
const index = code - 16;
|
|
687
|
+
const remainder = index % 36;
|
|
688
|
+
red = Math.floor(index / 36) / 5;
|
|
689
|
+
green = Math.floor(remainder / 6) / 5;
|
|
690
|
+
blue = remainder % 6 / 5;
|
|
691
|
+
}
|
|
692
|
+
const value = Math.max(red, green, blue) * 2;
|
|
693
|
+
if (value === 0) {
|
|
694
|
+
return 30;
|
|
695
|
+
}
|
|
696
|
+
let result = 30 + (Math.round(blue) << 2 | Math.round(green) << 1 | Math.round(red));
|
|
697
|
+
if (value === 2) {
|
|
698
|
+
result += 60;
|
|
699
|
+
}
|
|
700
|
+
return result;
|
|
701
|
+
}
|
|
702
|
+
function effectiveLevel(level) {
|
|
703
|
+
return level ?? 3;
|
|
704
|
+
}
|
|
705
|
+
function ansi16Style(name, paletteCode, background) {
|
|
706
|
+
const code = ansi256ToAnsi16(paletteCode) + (background ? 10 : 0);
|
|
707
|
+
return {
|
|
708
|
+
name,
|
|
709
|
+
code,
|
|
710
|
+
open: `\x1B[${code}m`,
|
|
711
|
+
close: `\x1B[${background ? 49 : 39}m`,
|
|
712
|
+
type: "color",
|
|
713
|
+
background
|
|
714
|
+
};
|
|
715
|
+
}
|
|
716
|
+
function generateAnsi256CodeForLevel(code, background, level) {
|
|
717
|
+
const style = generateAnsi256Code(code, background);
|
|
718
|
+
if (effectiveLevel(level) >= 2) {
|
|
719
|
+
return style;
|
|
720
|
+
}
|
|
721
|
+
return ansi16Style(style.name, code, background);
|
|
722
|
+
}
|
|
723
|
+
function generateRgbCodeForLevel(r, g, b, background, level) {
|
|
724
|
+
const style = generateRgbCode(r, g, b, background);
|
|
725
|
+
const effective = effectiveLevel(level);
|
|
726
|
+
if (effective >= 3) {
|
|
727
|
+
return style;
|
|
728
|
+
}
|
|
729
|
+
const paletteCode = rgbToAnsi256(r, g, b);
|
|
730
|
+
if (effective === 2) {
|
|
731
|
+
return { ...generateAnsi256Code(paletteCode, background), name: style.name };
|
|
732
|
+
}
|
|
733
|
+
return ansi16Style(style.name, paletteCode, background);
|
|
734
|
+
}
|
|
735
|
+
function paintRgb(text, rgb, background, level) {
|
|
736
|
+
if (level === 0 || text === "") {
|
|
737
|
+
return text;
|
|
738
|
+
}
|
|
739
|
+
const style = generateRgbCodeForLevel(rgb.r, rgb.g, rgb.b, background, level);
|
|
740
|
+
return `${style.open}${text}${style.close}`;
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
// src/core/plugin-api.ts
|
|
744
|
+
var REGISTRY_KEY = /* @__PURE__ */ Symbol.for("@oxog/pigment.plugin-api");
|
|
745
|
+
var globalRegistry = globalThis;
|
|
746
|
+
var registry = globalRegistry[REGISTRY_KEY] ??= { apis: /* @__PURE__ */ new WeakMap(), roots: /* @__PURE__ */ new WeakMap() };
|
|
747
|
+
var { apis, roots } = registry;
|
|
748
|
+
var PLUGIN_API_PROVIDERS = {
|
|
749
|
+
gradient: "gradientPlugin",
|
|
750
|
+
rainbow: "gradientPlugin",
|
|
751
|
+
theme: "themePlugin",
|
|
752
|
+
success: "semanticPlugin",
|
|
753
|
+
error: "semanticPlugin",
|
|
754
|
+
warning: "semanticPlugin",
|
|
755
|
+
info: "semanticPlugin",
|
|
756
|
+
debug: "semanticPlugin",
|
|
757
|
+
box: "boxPlugin",
|
|
758
|
+
template: "templatePlugin"
|
|
759
|
+
};
|
|
760
|
+
function isPluginApiName(name) {
|
|
761
|
+
return Object.prototype.hasOwnProperty.call(PLUGIN_API_PROVIDERS, name);
|
|
762
|
+
}
|
|
763
|
+
function registerPluginApi(kernel, name, value) {
|
|
764
|
+
let api = apis.get(kernel);
|
|
765
|
+
if (!api) {
|
|
766
|
+
api = /* @__PURE__ */ new Map();
|
|
767
|
+
apis.set(kernel, api);
|
|
768
|
+
}
|
|
769
|
+
api.set(name, value);
|
|
770
|
+
}
|
|
771
|
+
function getPluginApi(kernel, name) {
|
|
772
|
+
return apis.get(kernel)?.get(name);
|
|
773
|
+
}
|
|
774
|
+
function registerRoot(kernel, root) {
|
|
775
|
+
roots.set(kernel, root);
|
|
776
|
+
}
|
|
777
|
+
function getRoot(kernel) {
|
|
778
|
+
return roots.get(kernel);
|
|
779
|
+
}
|
|
780
|
+
function missingPluginApi(name) {
|
|
781
|
+
const plugin = PLUGIN_API_PROVIDERS[name];
|
|
782
|
+
const fail = () => {
|
|
783
|
+
throw new PluginError(
|
|
784
|
+
plugin,
|
|
785
|
+
`pigment.${name} needs ${plugin}(): createPigment({ plugins: [${plugin}()] }) or pigment.use(${plugin}())`
|
|
786
|
+
);
|
|
787
|
+
};
|
|
788
|
+
if (name === "theme") {
|
|
789
|
+
return {
|
|
790
|
+
keyword: fail,
|
|
791
|
+
string: fail,
|
|
792
|
+
number: fail,
|
|
793
|
+
comment: fail,
|
|
794
|
+
error: fail,
|
|
795
|
+
success: fail,
|
|
796
|
+
warning: fail,
|
|
797
|
+
info: fail
|
|
798
|
+
};
|
|
799
|
+
}
|
|
800
|
+
return fail;
|
|
801
|
+
}
|
|
802
|
+
function installPlugin(kernel, plugin) {
|
|
803
|
+
kernel.use(plugin);
|
|
804
|
+
if (plugin.onInit) {
|
|
805
|
+
Promise.resolve().then(() => plugin.onInit?.(kernel.getContext())).catch(() => {
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
|
|
575
810
|
// src/plugins/optional/gradient.ts
|
|
811
|
+
function toRgb(color) {
|
|
812
|
+
if (typeof color !== "string") {
|
|
813
|
+
const [r, g, b] = color;
|
|
814
|
+
return { r, g, b };
|
|
815
|
+
}
|
|
816
|
+
return color.startsWith("rgb(") ? parseRgb(color) : hexToRgb(color);
|
|
817
|
+
}
|
|
818
|
+
function paintGradient(text, startRgb, endRgb, steps, level) {
|
|
819
|
+
if (text.length === 0) {
|
|
820
|
+
return text;
|
|
821
|
+
}
|
|
822
|
+
const colors = interpolateColors(startRgb, endRgb, text.length, Math.max(2, steps));
|
|
823
|
+
let result = "";
|
|
824
|
+
for (let i = 0; i < text.length; i++) {
|
|
825
|
+
const colorIndex = text.length === 1 ? 0 : Math.floor(i / (text.length - 1) * (colors.length - 1));
|
|
826
|
+
result += paintRgb(text[i], colors[colorIndex], false, level);
|
|
827
|
+
}
|
|
828
|
+
return result;
|
|
829
|
+
}
|
|
830
|
+
function paintRainbow(text, steps, level) {
|
|
831
|
+
const colors = generateRainbowColors(text.length, Math.max(1, steps));
|
|
832
|
+
let result = "";
|
|
833
|
+
for (let i = 0; i < text.length; i++) {
|
|
834
|
+
result += paintRgb(text[i], colors[i % colors.length], false, level);
|
|
835
|
+
}
|
|
836
|
+
return result;
|
|
837
|
+
}
|
|
576
838
|
function gradientPlugin(options) {
|
|
577
839
|
const steps = options?.steps ?? 10;
|
|
578
840
|
return {
|
|
@@ -586,6 +848,15 @@ function gradientPlugin(options) {
|
|
|
586
848
|
enabled: true
|
|
587
849
|
}
|
|
588
850
|
});
|
|
851
|
+
registerPluginApi(kernel, "gradient", (start, end) => {
|
|
852
|
+
const startRgb = toRgb(start);
|
|
853
|
+
const endRgb = toRgb(end);
|
|
854
|
+
if (!startRgb || !endRgb) {
|
|
855
|
+
throw new ColorError(`Invalid gradient color: ${String(startRgb ? end : start)}`);
|
|
856
|
+
}
|
|
857
|
+
return (text) => paintGradient(String(text), startRgb, endRgb, steps, k.getContext().level);
|
|
858
|
+
});
|
|
859
|
+
registerPluginApi(kernel, "rainbow", (text) => paintRainbow(String(text), steps, k.getContext().level));
|
|
589
860
|
k.on("style:gradient", (data) => {
|
|
590
861
|
const { start, end, text, steps: gradientSteps = steps } = data;
|
|
591
862
|
const startRgb = start.startsWith("#") ? hexToRgb(start) : start.startsWith("rgb(") ? parseRgb(start) : hexToRgb(start);
|
|
@@ -656,6 +927,7 @@ function generateRainbowColors(length, steps) {
|
|
|
656
927
|
}
|
|
657
928
|
|
|
658
929
|
// src/plugins/optional/theme.ts
|
|
930
|
+
var THEME_COLOR_TYPES = ["keyword", "string", "number", "comment", "error", "success", "warning", "info"];
|
|
659
931
|
function themePlugin(options) {
|
|
660
932
|
const preset = options?.preset ?? "monokai";
|
|
661
933
|
const custom = options?.custom ?? {};
|
|
@@ -675,6 +947,17 @@ function themePlugin(options) {
|
|
|
675
947
|
enabled: true
|
|
676
948
|
}
|
|
677
949
|
});
|
|
950
|
+
const api = {};
|
|
951
|
+
for (const type of THEME_COLOR_TYPES) {
|
|
952
|
+
const color = theme[type];
|
|
953
|
+
api[type] = (text) => {
|
|
954
|
+
if (!color) {
|
|
955
|
+
throw new Error(`Unknown theme color type: ${type}`);
|
|
956
|
+
}
|
|
957
|
+
return paintRgb(String(text), hexToRgb(color), false, k.getContext().level);
|
|
958
|
+
};
|
|
959
|
+
}
|
|
960
|
+
registerPluginApi(kernel, "theme", api);
|
|
678
961
|
k.on("style:theme", (data) => {
|
|
679
962
|
const { type, text } = data;
|
|
680
963
|
const color = theme[type];
|
|
@@ -744,6 +1027,10 @@ function semanticPlugin(options) {
|
|
|
744
1027
|
if (!hexToRgb2) {
|
|
745
1028
|
throw new Error("hexToRgb utility not available");
|
|
746
1029
|
}
|
|
1030
|
+
for (const [name, color] of Object.entries(defaultColors)) {
|
|
1031
|
+
const rgb = hexToRgb2(color);
|
|
1032
|
+
registerPluginApi(kernel, name, (text) => paintRgb(String(text), rgb, false, k.getContext().level));
|
|
1033
|
+
}
|
|
747
1034
|
k.on("style:success", (data) => {
|
|
748
1035
|
const { r, g, b } = hexToRgb2(defaultColors.success);
|
|
749
1036
|
return `\x1B[38;2;${r};${g};${b}m${data.text}\x1B[39m`;
|
|
@@ -785,11 +1072,14 @@ function boxPlugin(options) {
|
|
|
785
1072
|
borders: BOX_BORDERS
|
|
786
1073
|
}
|
|
787
1074
|
});
|
|
788
|
-
|
|
789
|
-
const {
|
|
1075
|
+
const draw = (text, opts = {}) => {
|
|
1076
|
+
const { border: boxBorder = border, padding: boxPadding = padding, bgColor } = opts;
|
|
790
1077
|
const chars = BOX_BORDERS[boxBorder];
|
|
1078
|
+
if (!chars) {
|
|
1079
|
+
throw new StyleError(`Unknown box border: ${String(boxBorder)}`);
|
|
1080
|
+
}
|
|
791
1081
|
const lines = text.split("\n");
|
|
792
|
-
const width = Math.max(...lines.map((
|
|
1082
|
+
const width = Math.max(...lines.map((l2) => l2.length)) + boxPadding * 2;
|
|
793
1083
|
const pad = " ".repeat(boxPadding);
|
|
794
1084
|
const top = `${chars.tl}${chars.h.repeat(width)}${chars.tr}`;
|
|
795
1085
|
const middle = lines.map((line) => {
|
|
@@ -799,21 +1089,152 @@ function boxPlugin(options) {
|
|
|
799
1089
|
const bottom = `${chars.bl}${chars.h.repeat(width)}${chars.br}`;
|
|
800
1090
|
const result = [top, ...middle, bottom].join("\n");
|
|
801
1091
|
if (bgColor) {
|
|
802
|
-
|
|
803
|
-
const hexToRgb2 = ctx.utils?.hexToRgb;
|
|
804
|
-
if (!hexToRgb2) {
|
|
805
|
-
throw new Error("hexToRgb utility not available");
|
|
806
|
-
}
|
|
807
|
-
const { r, g, b } = hexToRgb2(bgColor);
|
|
808
|
-
return `\x1B[48;2;${r};${g};${b}m${result}\x1B[49m`;
|
|
1092
|
+
return paintRgb(result, hexToRgb(bgColor), true, k.getContext().level);
|
|
809
1093
|
}
|
|
810
1094
|
return result;
|
|
1095
|
+
};
|
|
1096
|
+
registerPluginApi(kernel, "box", (text, opts) => draw(String(text), opts));
|
|
1097
|
+
k.on("style:box", (data) => {
|
|
1098
|
+
const { text, ...opts } = data;
|
|
1099
|
+
return draw(text, opts);
|
|
811
1100
|
});
|
|
812
1101
|
}
|
|
813
1102
|
};
|
|
814
1103
|
}
|
|
815
1104
|
|
|
1105
|
+
// src/core/proxy.ts
|
|
1106
|
+
var proxyCache = /* @__PURE__ */ new WeakMap();
|
|
1107
|
+
function createProxyPigment(kernel, styles = []) {
|
|
1108
|
+
const handler = {
|
|
1109
|
+
get(target, prop) {
|
|
1110
|
+
const ctx = kernel.getContext();
|
|
1111
|
+
if (prop === "level") {
|
|
1112
|
+
return ctx.level;
|
|
1113
|
+
}
|
|
1114
|
+
if (prop === "supportsColor") {
|
|
1115
|
+
return ctx.colorSupport;
|
|
1116
|
+
}
|
|
1117
|
+
if (MODIFIER_NAMES.some((name) => name === prop)) {
|
|
1118
|
+
const modifierStyle = generateModifierCode(prop);
|
|
1119
|
+
return createProxyPigment(kernel, [...styles, modifierStyle]);
|
|
1120
|
+
}
|
|
1121
|
+
if (COLOR_NAMES.some((name) => name === prop) || BACKGROUND_COLOR_NAMES.some((name) => name === prop)) {
|
|
1122
|
+
const background = prop.startsWith("bg");
|
|
1123
|
+
const colorName = background ? prop.slice(2) : prop;
|
|
1124
|
+
const colorStyle = generateColorCode(colorName, background);
|
|
1125
|
+
return createProxyPigment(kernel, [...styles, colorStyle]);
|
|
1126
|
+
}
|
|
1127
|
+
if (EXTENDED_COLOR_METHODS.some((name) => name === prop)) {
|
|
1128
|
+
if (prop === "ansi256" || prop === "bgAnsi256") {
|
|
1129
|
+
return (code) => {
|
|
1130
|
+
const background = prop === "bgAnsi256";
|
|
1131
|
+
const ansi256Style = generateAnsi256CodeForLevel(code, background, ctx.level);
|
|
1132
|
+
return createProxyPigment(kernel, [...styles, ansi256Style]);
|
|
1133
|
+
};
|
|
1134
|
+
}
|
|
1135
|
+
if (prop === "rgb" || prop === "bgRgb") {
|
|
1136
|
+
return (r, g, b) => {
|
|
1137
|
+
const background = prop === "bgRgb";
|
|
1138
|
+
const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
|
|
1139
|
+
return createProxyPigment(kernel, [...styles, rgbStyle]);
|
|
1140
|
+
};
|
|
1141
|
+
}
|
|
1142
|
+
if (prop === "hex" || prop === "bgHex") {
|
|
1143
|
+
return (color) => {
|
|
1144
|
+
const background = prop === "bgHex";
|
|
1145
|
+
const hexToRgb2 = ctx.utils?.hexToRgb;
|
|
1146
|
+
if (!hexToRgb2) {
|
|
1147
|
+
throw new Error("hexToRgb utility not available");
|
|
1148
|
+
}
|
|
1149
|
+
const { r, g, b } = hexToRgb2(color);
|
|
1150
|
+
const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
|
|
1151
|
+
return createProxyPigment(kernel, [...styles, rgbStyle]);
|
|
1152
|
+
};
|
|
1153
|
+
}
|
|
1154
|
+
if (prop === "hsl" || prop === "bgHsl") {
|
|
1155
|
+
return (h2, s, l2) => {
|
|
1156
|
+
const background = prop === "bgHsl";
|
|
1157
|
+
const hslToRgb2 = ctx.utils?.hslToRgb;
|
|
1158
|
+
if (!hslToRgb2) {
|
|
1159
|
+
throw new Error("hslToRgb utility not available");
|
|
1160
|
+
}
|
|
1161
|
+
const { r, g, b } = hslToRgb2(h2, s, l2);
|
|
1162
|
+
const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
|
|
1163
|
+
return createProxyPigment(kernel, [...styles, rgbStyle]);
|
|
1164
|
+
};
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
if (prop === "visible") {
|
|
1168
|
+
const colorSupport = ctx.colorSupport;
|
|
1169
|
+
return (text) => colorSupport.level > 0 ? applyStyles(String(text), styles, ctx.level) : "";
|
|
1170
|
+
}
|
|
1171
|
+
if (prop === "use") {
|
|
1172
|
+
return (plugin) => {
|
|
1173
|
+
installPlugin(kernel, plugin);
|
|
1174
|
+
return getRoot(kernel) ?? createProxyPigment(kernel);
|
|
1175
|
+
};
|
|
1176
|
+
}
|
|
1177
|
+
if (isPluginApiName(prop)) {
|
|
1178
|
+
return getPluginApi(kernel, prop) ?? missingPluginApi(prop);
|
|
1179
|
+
}
|
|
1180
|
+
return Reflect.get(target, prop);
|
|
1181
|
+
},
|
|
1182
|
+
apply(_target, _thisArg, argArray) {
|
|
1183
|
+
const text = String(argArray[0] ?? "");
|
|
1184
|
+
return applyStyles(text, styles, kernel.getContext().level);
|
|
1185
|
+
}
|
|
1186
|
+
};
|
|
1187
|
+
const pigment = new Proxy((() => ""), handler);
|
|
1188
|
+
proxyCache.set(pigment, pigment);
|
|
1189
|
+
return pigment;
|
|
1190
|
+
}
|
|
1191
|
+
function applyStyles(text, styles, level) {
|
|
1192
|
+
if (styles.length === 0 || text === "" || level === 0) {
|
|
1193
|
+
return text;
|
|
1194
|
+
}
|
|
1195
|
+
const open = styles.map((style) => style.open).join("");
|
|
1196
|
+
const close = [...styles].reverse().map((style) => style.close).join("");
|
|
1197
|
+
let result = text;
|
|
1198
|
+
for (const style of styles) {
|
|
1199
|
+
if (style.close) {
|
|
1200
|
+
result = result.split(style.close).join(style.close + style.open);
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
return `${open}${result}${close}`;
|
|
1204
|
+
}
|
|
1205
|
+
|
|
816
1206
|
// src/plugins/optional/template.ts
|
|
1207
|
+
var STYLE_NAMES = /* @__PURE__ */ new Set([...MODIFIER_NAMES, ...COLOR_NAMES, ...BACKGROUND_COLOR_NAMES]);
|
|
1208
|
+
var BLOCK = /\{([A-Za-z]\w*(?:\.[A-Za-z]\w*)*) ([^{}]*)\}/g;
|
|
1209
|
+
var TOKEN = /\uE000(\d+)\uE001/g;
|
|
1210
|
+
function renderTemplate(root, strings, values) {
|
|
1211
|
+
const parts = values.map((value) => String(value));
|
|
1212
|
+
const token = (index) => `\uE000${index}\uE001`;
|
|
1213
|
+
const restore = (text) => text.replace(TOKEN, (_match, index) => String(parts[Number(index)]));
|
|
1214
|
+
let source = "";
|
|
1215
|
+
for (let i = 0; i < strings.length; i++) {
|
|
1216
|
+
source += strings[i];
|
|
1217
|
+
if (i < values.length) {
|
|
1218
|
+
source += token(i);
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
let previous;
|
|
1222
|
+
do {
|
|
1223
|
+
previous = source;
|
|
1224
|
+
source = source.replace(BLOCK, (_match, chain, text) => {
|
|
1225
|
+
let styler = root;
|
|
1226
|
+
for (const name of chain.split(".")) {
|
|
1227
|
+
if (!STYLE_NAMES.has(name)) {
|
|
1228
|
+
throw new StyleError(`Unknown style in template: ${name}`);
|
|
1229
|
+
}
|
|
1230
|
+
styler = styler[name];
|
|
1231
|
+
}
|
|
1232
|
+
parts.push(styler(restore(text)));
|
|
1233
|
+
return token(parts.length - 1);
|
|
1234
|
+
});
|
|
1235
|
+
} while (source !== previous);
|
|
1236
|
+
return restore(source);
|
|
1237
|
+
}
|
|
817
1238
|
function templatePlugin() {
|
|
818
1239
|
return {
|
|
819
1240
|
name: "template",
|
|
@@ -825,6 +1246,11 @@ function templatePlugin() {
|
|
|
825
1246
|
enabled: true
|
|
826
1247
|
}
|
|
827
1248
|
});
|
|
1249
|
+
registerPluginApi(
|
|
1250
|
+
kernel,
|
|
1251
|
+
"template",
|
|
1252
|
+
(strings, ...values) => renderTemplate(getRoot(kernel) ?? createProxyPigment(k), strings, values)
|
|
1253
|
+
);
|
|
828
1254
|
k.on("style:template", (data) => {
|
|
829
1255
|
const { strings, values } = data;
|
|
830
1256
|
let result = "";
|
|
@@ -867,9 +1293,27 @@ function parseStyleExpression(expression, text, kernel) {
|
|
|
867
1293
|
return text;
|
|
868
1294
|
}
|
|
869
1295
|
|
|
1296
|
+
// node_modules/@oxog/types/dist/index.js
|
|
1297
|
+
var U = ((o) => (o.UNKNOWN = "UNKNOWN", o.VALIDATION_ERROR = "VALIDATION_ERROR", o.PLUGIN_ERROR = "PLUGIN_ERROR", o.NOT_FOUND = "NOT_FOUND", o.TIMEOUT = "TIMEOUT", o.DEPENDENCY_ERROR = "DEPENDENCY_ERROR", o))(U || {});
|
|
1298
|
+
var u = class extends Error {
|
|
1299
|
+
constructor(t, n, s, p) {
|
|
1300
|
+
super(t, p);
|
|
1301
|
+
this.code = n;
|
|
1302
|
+
this.context = s;
|
|
1303
|
+
this.name = "OxogError", Object.setPrototypeOf(this, new.target.prototype), Error.captureStackTrace?.(this, new.target);
|
|
1304
|
+
}
|
|
1305
|
+
name;
|
|
1306
|
+
};
|
|
1307
|
+
var l = class extends u {
|
|
1308
|
+
constructor(t, n, s, p) {
|
|
1309
|
+
super(t, "PLUGIN_ERROR", { pluginName: n, ...s }, p);
|
|
1310
|
+
this.pluginName = n;
|
|
1311
|
+
this.name = "PluginError", Object.setPrototypeOf(this, new.target.prototype), Error.captureStackTrace?.(this, new.target);
|
|
1312
|
+
}
|
|
1313
|
+
name;
|
|
1314
|
+
};
|
|
1315
|
+
|
|
870
1316
|
// node_modules/@oxog/plugin/dist/index.js
|
|
871
|
-
import { PluginError } from "@oxog/types";
|
|
872
|
-
import { PluginError as PluginError2 } from "@oxog/types";
|
|
873
1317
|
var x = ((i) => (i.Created = "created", i.Initializing = "initializing", i.Ready = "ready", i.Destroying = "destroying", i.Destroyed = "destroyed", i))(x || {});
|
|
874
1318
|
var h = class {
|
|
875
1319
|
context;
|
|
@@ -898,7 +1342,7 @@ var h = class {
|
|
|
898
1342
|
return n;
|
|
899
1343
|
}
|
|
900
1344
|
};
|
|
901
|
-
var
|
|
1345
|
+
var u2 = class {
|
|
902
1346
|
handlers = /* @__PURE__ */ new Map();
|
|
903
1347
|
wildcardHandlers = new Array();
|
|
904
1348
|
patternHandlers = /* @__PURE__ */ new Map();
|
|
@@ -926,8 +1370,8 @@ var u = class {
|
|
|
926
1370
|
off(e, t) {
|
|
927
1371
|
let n = String(e), r = this.handlers.get(n);
|
|
928
1372
|
if (r) {
|
|
929
|
-
let i = t, a = this.onceWrappers.get(n), s = a?.get(i), o = s || i,
|
|
930
|
-
|
|
1373
|
+
let i = t, a = this.onceWrappers.get(n), s = a?.get(i), o = s || i, l2 = r.indexOf(o);
|
|
1374
|
+
l2 !== -1 && (r.splice(l2, 1), s && a.delete(i));
|
|
931
1375
|
}
|
|
932
1376
|
}
|
|
933
1377
|
emit(e, t) {
|
|
@@ -989,22 +1433,22 @@ var f = class {
|
|
|
989
1433
|
validateSinglePlugin(e) {
|
|
990
1434
|
let t = this.graph.get(e);
|
|
991
1435
|
if (!t) return;
|
|
992
|
-
for (let r of t) if (!this.graph.has(r)) throw new
|
|
1436
|
+
for (let r of t) if (!this.graph.has(r)) throw new l(`Missing dependency: '${r}' required by '${e}'`, e);
|
|
993
1437
|
let n = this.detectCycle();
|
|
994
|
-
if (n && n.includes(e)) throw new
|
|
1438
|
+
if (n && n.includes(e)) throw new l(`Circular dependency detected: ${n.join(" -> ")}`, "dependency-resolver");
|
|
995
1439
|
}
|
|
996
1440
|
resolve() {
|
|
997
1441
|
let e = /* @__PURE__ */ new Map(), t = [], n = [], r = /* @__PURE__ */ new Map();
|
|
998
1442
|
for (let [s, o] of this.graph) {
|
|
999
|
-
let
|
|
1000
|
-
r.set(s,
|
|
1443
|
+
let l2 = o.filter((p) => this.graph.has(p));
|
|
1444
|
+
r.set(s, l2);
|
|
1001
1445
|
}
|
|
1002
|
-
for (let [s, o] of this.graph) for (let
|
|
1446
|
+
for (let [s, o] of this.graph) for (let l2 of o) if (!this.graph.has(l2)) throw new l(`Missing dependency: '${l2}' required by '${s}'`, s);
|
|
1003
1447
|
for (let [s, o] of r) e.set(s, o.length), o.length === 0 && t.push(s);
|
|
1004
1448
|
let i = /* @__PURE__ */ new Map();
|
|
1005
|
-
for (let [s, o] of r) for (let
|
|
1006
|
-
let p = i.get(
|
|
1007
|
-
p ? p.push(s) : i.set(
|
|
1449
|
+
for (let [s, o] of r) for (let l2 of o) {
|
|
1450
|
+
let p = i.get(l2);
|
|
1451
|
+
p ? p.push(s) : i.set(l2, [s]);
|
|
1008
1452
|
}
|
|
1009
1453
|
let a = 0;
|
|
1010
1454
|
for (; a < t.length; ) {
|
|
@@ -1014,7 +1458,7 @@ var f = class {
|
|
|
1014
1458
|
}
|
|
1015
1459
|
if (n.length !== r.size) {
|
|
1016
1460
|
let s = this.detectCycle();
|
|
1017
|
-
throw new
|
|
1461
|
+
throw new l(`Circular dependency detected: ${s.join(" -> ")}`, "dependency-resolver");
|
|
1018
1462
|
}
|
|
1019
1463
|
return n;
|
|
1020
1464
|
}
|
|
@@ -1025,12 +1469,12 @@ var f = class {
|
|
|
1025
1469
|
r.set(a, 1), s.push(a);
|
|
1026
1470
|
for (let o of this.graph.get(a)) if (this.graph.has(o)) {
|
|
1027
1471
|
if (r.get(o) === 1) {
|
|
1028
|
-
let
|
|
1029
|
-
return [...s.slice(
|
|
1472
|
+
let l2 = s.indexOf(o);
|
|
1473
|
+
return [...s.slice(l2), o];
|
|
1030
1474
|
}
|
|
1031
1475
|
if (r.get(o) === 0) {
|
|
1032
|
-
let
|
|
1033
|
-
if (
|
|
1476
|
+
let l2 = i(o, s);
|
|
1477
|
+
if (l2) return l2;
|
|
1034
1478
|
}
|
|
1035
1479
|
}
|
|
1036
1480
|
return s.pop(), r.set(a, 2), null;
|
|
@@ -1103,8 +1547,8 @@ var m = class {
|
|
|
1103
1547
|
await r.withBoundary(s, o.plugin, async () => {
|
|
1104
1548
|
o.plugin.onInit && await o.plugin.onInit(n);
|
|
1105
1549
|
}), o.state = "ready", o.initializedAt = Date.now(), i?.(s);
|
|
1106
|
-
} catch (
|
|
1107
|
-
if (a === "fail-fast") throw
|
|
1550
|
+
} catch (l2) {
|
|
1551
|
+
if (a === "fail-fast") throw l2;
|
|
1108
1552
|
o.state = "failed";
|
|
1109
1553
|
}
|
|
1110
1554
|
}
|
|
@@ -1120,8 +1564,8 @@ var m = class {
|
|
|
1120
1564
|
await n.withBoundary(s, o.plugin, async () => {
|
|
1121
1565
|
o.plugin.onDestroy && await o.plugin.onDestroy();
|
|
1122
1566
|
});
|
|
1123
|
-
} catch (
|
|
1124
|
-
if (i === "fail-fast") throw
|
|
1567
|
+
} catch (l2) {
|
|
1568
|
+
if (i === "fail-fast") throw l2;
|
|
1125
1569
|
}
|
|
1126
1570
|
o.state = "destroyed", r?.(s);
|
|
1127
1571
|
}
|
|
@@ -1142,12 +1586,12 @@ var c = class {
|
|
|
1142
1586
|
this.events.emit(e, t);
|
|
1143
1587
|
}
|
|
1144
1588
|
constructor(e) {
|
|
1145
|
-
this.config = e || {}, this.context = new h(this.config.context ?? {}), this.events = new
|
|
1589
|
+
this.config = e || {}, this.context = new h(this.config.context ?? {}), this.events = new u2(), this.resolver = new f(), this.errorBoundary = new v(this.config.errorStrategy || "isolate", this.config.onError), this.lifecycle = new m();
|
|
1146
1590
|
}
|
|
1147
1591
|
use(e) {
|
|
1148
|
-
if (this.state === "destroyed") throw new
|
|
1149
|
-
if (this.state === "initializing") throw new
|
|
1150
|
-
if (this.plugins.has(e.name)) throw new
|
|
1592
|
+
if (this.state === "destroyed") throw new l("Cannot add plugins to destroyed kernel", "kernel");
|
|
1593
|
+
if (this.state === "initializing") throw new l("Cannot add plugins while kernel is initializing. Register all plugins before calling init(), or wait until kernel is ready.", "kernel");
|
|
1594
|
+
if (this.plugins.has(e.name)) throw new l(`Plugin '${e.name}' is already registered`, "kernel");
|
|
1151
1595
|
let t = { plugin: e, state: "registered" };
|
|
1152
1596
|
this.plugins.set(e.name, t), this.resolver.addPlugin(e.name, [...e.dependencies || []]);
|
|
1153
1597
|
try {
|
|
@@ -1181,7 +1625,7 @@ var c = class {
|
|
|
1181
1625
|
let i = this.plugins.get(r);
|
|
1182
1626
|
if (!i || i.state !== "ready") {
|
|
1183
1627
|
t.state = "failed";
|
|
1184
|
-
let a = new
|
|
1628
|
+
let a = new l(`Dependency '${r}' is not ready for plugin '${e}'`, e);
|
|
1185
1629
|
this.emitInternal("plugin:error", { name: e, error: a });
|
|
1186
1630
|
return;
|
|
1187
1631
|
}
|
|
@@ -1252,7 +1696,7 @@ var c = class {
|
|
|
1252
1696
|
}
|
|
1253
1697
|
}
|
|
1254
1698
|
async init() {
|
|
1255
|
-
if (this.state !== "created") throw new
|
|
1699
|
+
if (this.state !== "created") throw new l("Kernel already initialized", "kernel");
|
|
1256
1700
|
this.state = "initializing";
|
|
1257
1701
|
try {
|
|
1258
1702
|
this.config.onBeforeInit && await this.config.onBeforeInit();
|
|
@@ -1326,176 +1770,16 @@ function W(d) {
|
|
|
1326
1770
|
return new c(d);
|
|
1327
1771
|
}
|
|
1328
1772
|
|
|
1329
|
-
// src/core/ansi.ts
|
|
1330
|
-
function generateModifierCode(name) {
|
|
1331
|
-
const modifier = ANSI_MODIFIERS[name];
|
|
1332
|
-
if (!modifier) {
|
|
1333
|
-
throw new StyleError(`Unknown modifier: ${name}`);
|
|
1334
|
-
}
|
|
1335
|
-
return {
|
|
1336
|
-
name,
|
|
1337
|
-
code: -1,
|
|
1338
|
-
open: modifier.open,
|
|
1339
|
-
close: modifier.close,
|
|
1340
|
-
type: "modifier"
|
|
1341
|
-
};
|
|
1342
|
-
}
|
|
1343
|
-
function generateColorCode(name, background = false) {
|
|
1344
|
-
let normalizedName = name;
|
|
1345
|
-
const lowerName = name.toLowerCase();
|
|
1346
|
-
if (lowerName === "gray" || lowerName === "grey") {
|
|
1347
|
-
normalizedName = "blackBright";
|
|
1348
|
-
} else if (lowerName === "bggray" || lowerName === "bggrey") {
|
|
1349
|
-
normalizedName = "bgBlackBright";
|
|
1350
|
-
}
|
|
1351
|
-
const foregroundColors = { ...ANSI_FOREGROUND_COLORS, ...ANSI_FOREGROUND_BRIGHT_COLORS };
|
|
1352
|
-
const backgroundColors = { ...ANSI_BACKGROUND_COLORS, ...ANSI_BACKGROUND_BRIGHT_COLORS };
|
|
1353
|
-
let lookupName = normalizedName;
|
|
1354
|
-
if (background && !normalizedName.startsWith("bg")) {
|
|
1355
|
-
lookupName = "bg" + normalizedName.charAt(0).toUpperCase() + normalizedName.slice(1);
|
|
1356
|
-
}
|
|
1357
|
-
const allColors = background ? backgroundColors : foregroundColors;
|
|
1358
|
-
const code = allColors[lookupName];
|
|
1359
|
-
if (code === void 0) {
|
|
1360
|
-
throw new StyleError(`Unknown color: ${name}`);
|
|
1361
|
-
}
|
|
1362
|
-
const closeCode = background ? 49 : 39;
|
|
1363
|
-
return {
|
|
1364
|
-
name,
|
|
1365
|
-
code,
|
|
1366
|
-
open: `\x1B[${code}m`,
|
|
1367
|
-
close: `\x1B[${closeCode}m`,
|
|
1368
|
-
type: "color",
|
|
1369
|
-
background
|
|
1370
|
-
};
|
|
1371
|
-
}
|
|
1372
|
-
function generateAnsi256Code(code, background = false) {
|
|
1373
|
-
if (code < 0 || code > 255) {
|
|
1374
|
-
throw new StyleError(`ANSI 256 code must be between 0 and 255, got ${code}`);
|
|
1375
|
-
}
|
|
1376
|
-
const prefix = background ? 48 : 38;
|
|
1377
|
-
const closeCode = background ? 49 : 39;
|
|
1378
|
-
return {
|
|
1379
|
-
name: `ansi256(${code})`,
|
|
1380
|
-
code,
|
|
1381
|
-
open: `\x1B[${prefix};5;${code}m`,
|
|
1382
|
-
close: `\x1B[${closeCode}m`,
|
|
1383
|
-
type: "color",
|
|
1384
|
-
background
|
|
1385
|
-
};
|
|
1386
|
-
}
|
|
1387
|
-
function generateRgbCode(r, g, b, background = false) {
|
|
1388
|
-
if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) {
|
|
1389
|
-
throw new StyleError(`RGB values must be between 0 and 255, got (${r}, ${g}, ${b})`);
|
|
1390
|
-
}
|
|
1391
|
-
const prefix = background ? 48 : 38;
|
|
1392
|
-
const closeCode = background ? 49 : 39;
|
|
1393
|
-
return {
|
|
1394
|
-
name: `rgb(${r}, ${g}, ${b})`,
|
|
1395
|
-
code: -1,
|
|
1396
|
-
open: `\x1B[${prefix};2;${r};${g};${b}m`,
|
|
1397
|
-
close: `\x1B[${closeCode}m`,
|
|
1398
|
-
type: "color",
|
|
1399
|
-
background
|
|
1400
|
-
};
|
|
1401
|
-
}
|
|
1402
|
-
|
|
1403
|
-
// src/core/proxy.ts
|
|
1404
|
-
var proxyCache = /* @__PURE__ */ new WeakMap();
|
|
1405
|
-
function createProxyPigment(kernel, styles = []) {
|
|
1406
|
-
const handler = {
|
|
1407
|
-
get(target, prop) {
|
|
1408
|
-
const ctx = kernel.getContext();
|
|
1409
|
-
if (prop === "level") {
|
|
1410
|
-
return ctx.level;
|
|
1411
|
-
}
|
|
1412
|
-
if (prop === "supportsColor") {
|
|
1413
|
-
return ctx.colorSupport;
|
|
1414
|
-
}
|
|
1415
|
-
if (MODIFIER_NAMES.some((name) => name === prop)) {
|
|
1416
|
-
const modifierStyle = generateModifierCode(prop);
|
|
1417
|
-
return createProxyPigment(kernel, [...styles, modifierStyle]);
|
|
1418
|
-
}
|
|
1419
|
-
if (COLOR_NAMES.some((name) => name === prop) || BACKGROUND_COLOR_NAMES.some((name) => name === prop)) {
|
|
1420
|
-
const background = prop.startsWith("bg");
|
|
1421
|
-
const colorName = background ? prop.slice(2) : prop;
|
|
1422
|
-
const colorStyle = generateColorCode(colorName, background);
|
|
1423
|
-
return createProxyPigment(kernel, [...styles, colorStyle]);
|
|
1424
|
-
}
|
|
1425
|
-
if (EXTENDED_COLOR_METHODS.some((name) => name === prop)) {
|
|
1426
|
-
if (prop === "ansi256" || prop === "bgAnsi256") {
|
|
1427
|
-
return (code) => {
|
|
1428
|
-
const background = prop === "bgAnsi256";
|
|
1429
|
-
const ansi256Style = generateAnsi256Code(code, background);
|
|
1430
|
-
return createProxyPigment(kernel, [...styles, ansi256Style]);
|
|
1431
|
-
};
|
|
1432
|
-
}
|
|
1433
|
-
if (prop === "rgb" || prop === "bgRgb") {
|
|
1434
|
-
return (r, g, b) => {
|
|
1435
|
-
const background = prop === "bgRgb";
|
|
1436
|
-
const rgbStyle = generateRgbCode(r, g, b, background);
|
|
1437
|
-
return createProxyPigment(kernel, [...styles, rgbStyle]);
|
|
1438
|
-
};
|
|
1439
|
-
}
|
|
1440
|
-
if (prop === "hex" || prop === "bgHex") {
|
|
1441
|
-
return (color) => {
|
|
1442
|
-
const background = prop === "bgHex";
|
|
1443
|
-
const hexToRgb2 = ctx.utils?.hexToRgb;
|
|
1444
|
-
if (!hexToRgb2) {
|
|
1445
|
-
throw new Error("hexToRgb utility not available");
|
|
1446
|
-
}
|
|
1447
|
-
const { r, g, b } = hexToRgb2(color);
|
|
1448
|
-
const rgbStyle = generateRgbCode(r, g, b, background);
|
|
1449
|
-
return createProxyPigment(kernel, [...styles, rgbStyle]);
|
|
1450
|
-
};
|
|
1451
|
-
}
|
|
1452
|
-
if (prop === "hsl" || prop === "bgHsl") {
|
|
1453
|
-
return (h2, s, l) => {
|
|
1454
|
-
const background = prop === "bgHsl";
|
|
1455
|
-
const hslToRgb2 = ctx.utils?.hslToRgb;
|
|
1456
|
-
if (!hslToRgb2) {
|
|
1457
|
-
throw new Error("hslToRgb utility not available");
|
|
1458
|
-
}
|
|
1459
|
-
const { r, g, b } = hslToRgb2(h2, s, l);
|
|
1460
|
-
const rgbStyle = generateRgbCode(r, g, b, background);
|
|
1461
|
-
return createProxyPigment(kernel, [...styles, rgbStyle]);
|
|
1462
|
-
};
|
|
1463
|
-
}
|
|
1464
|
-
}
|
|
1465
|
-
if (prop === "visible") {
|
|
1466
|
-
const colorSupport = ctx.colorSupport;
|
|
1467
|
-
return (text) => colorSupport.level > 0 ? applyStyles(String(text), styles) : "";
|
|
1468
|
-
}
|
|
1469
|
-
return Reflect.get(target, prop);
|
|
1470
|
-
},
|
|
1471
|
-
apply(_target, _thisArg, argArray) {
|
|
1472
|
-
const text = String(argArray[0] ?? "");
|
|
1473
|
-
return applyStyles(text, styles);
|
|
1474
|
-
}
|
|
1475
|
-
};
|
|
1476
|
-
const pigment = new Proxy((() => ""), handler);
|
|
1477
|
-
proxyCache.set(pigment, pigment);
|
|
1478
|
-
return pigment;
|
|
1479
|
-
}
|
|
1480
|
-
function applyStyles(text, styles) {
|
|
1481
|
-
if (styles.length === 0 || text === "") {
|
|
1482
|
-
return text;
|
|
1483
|
-
}
|
|
1484
|
-
const open = styles.map((style) => style.open).join("");
|
|
1485
|
-
const close = [...styles].reverse().map((style) => style.close).join("");
|
|
1486
|
-
let result = text;
|
|
1487
|
-
for (const style of styles) {
|
|
1488
|
-
if (style.close) {
|
|
1489
|
-
result = result.split(style.close).join(style.close + style.open);
|
|
1490
|
-
}
|
|
1491
|
-
}
|
|
1492
|
-
return `${open}${result}${close}`;
|
|
1493
|
-
}
|
|
1494
|
-
|
|
1495
1773
|
// src/core/builder.ts
|
|
1496
1774
|
var BuilderPigment = class {
|
|
1497
1775
|
styles = [];
|
|
1498
|
-
|
|
1776
|
+
kernel;
|
|
1777
|
+
constructor(kernel) {
|
|
1778
|
+
this.kernel = kernel;
|
|
1779
|
+
}
|
|
1780
|
+
/** The instance's color level (true color when there is no kernel). */
|
|
1781
|
+
get level() {
|
|
1782
|
+
return this.kernel?.getContext().level;
|
|
1499
1783
|
}
|
|
1500
1784
|
bold() {
|
|
1501
1785
|
this.styles.push(generateModifierCode("bold"));
|
|
@@ -1674,26 +1958,26 @@ var BuilderPigment = class {
|
|
|
1674
1958
|
return this;
|
|
1675
1959
|
}
|
|
1676
1960
|
ansi256(code) {
|
|
1677
|
-
this.styles.push(
|
|
1961
|
+
this.styles.push(generateAnsi256CodeForLevel(code, false, this.level));
|
|
1678
1962
|
return this;
|
|
1679
1963
|
}
|
|
1680
1964
|
bgAnsi256(code) {
|
|
1681
|
-
this.styles.push(
|
|
1965
|
+
this.styles.push(generateAnsi256CodeForLevel(code, true, this.level));
|
|
1682
1966
|
return this;
|
|
1683
1967
|
}
|
|
1684
1968
|
rgb(r, g, b) {
|
|
1685
|
-
this.styles.push(
|
|
1969
|
+
this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
|
|
1686
1970
|
return this;
|
|
1687
1971
|
}
|
|
1688
1972
|
bgRgb(r, g, b) {
|
|
1689
|
-
this.styles.push(
|
|
1973
|
+
this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
|
|
1690
1974
|
return this;
|
|
1691
1975
|
}
|
|
1692
1976
|
hex(color) {
|
|
1693
1977
|
const rgb = hexToRgb(color);
|
|
1694
1978
|
if (rgb) {
|
|
1695
1979
|
const { r, g, b } = rgb;
|
|
1696
|
-
this.styles.push(
|
|
1980
|
+
this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
|
|
1697
1981
|
}
|
|
1698
1982
|
return this;
|
|
1699
1983
|
}
|
|
@@ -1701,22 +1985,22 @@ var BuilderPigment = class {
|
|
|
1701
1985
|
const rgb = hexToRgb(color);
|
|
1702
1986
|
if (rgb) {
|
|
1703
1987
|
const { r, g, b } = rgb;
|
|
1704
|
-
this.styles.push(
|
|
1988
|
+
this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
|
|
1705
1989
|
}
|
|
1706
1990
|
return this;
|
|
1707
1991
|
}
|
|
1708
|
-
hsl(h2, s,
|
|
1709
|
-
const { r, g, b } = hslToRgb(h2, s,
|
|
1710
|
-
this.styles.push(
|
|
1992
|
+
hsl(h2, s, l2) {
|
|
1993
|
+
const { r, g, b } = hslToRgb(h2, s, l2);
|
|
1994
|
+
this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
|
|
1711
1995
|
return this;
|
|
1712
1996
|
}
|
|
1713
|
-
bgHsl(h2, s,
|
|
1714
|
-
const { r, g, b } = hslToRgb(h2, s,
|
|
1715
|
-
this.styles.push(
|
|
1997
|
+
bgHsl(h2, s, l2) {
|
|
1998
|
+
const { r, g, b } = hslToRgb(h2, s, l2);
|
|
1999
|
+
this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
|
|
1716
2000
|
return this;
|
|
1717
2001
|
}
|
|
1718
2002
|
paint(text) {
|
|
1719
|
-
if (this.styles.length === 0 || text === "") {
|
|
2003
|
+
if (this.styles.length === 0 || text === "" || this.level === 0) {
|
|
1720
2004
|
return text;
|
|
1721
2005
|
}
|
|
1722
2006
|
const open = this.styles.map((style) => style.open).join("");
|
|
@@ -1731,23 +2015,6 @@ var BuilderPigment = class {
|
|
|
1731
2015
|
}
|
|
1732
2016
|
};
|
|
1733
2017
|
|
|
1734
|
-
// src/utils/rgb-to-ansi256.ts
|
|
1735
|
-
function rgbToAnsi256(r, g, b) {
|
|
1736
|
-
const clampedR = Math.max(0, Math.min(255, r));
|
|
1737
|
-
const clampedG = Math.max(0, Math.min(255, g));
|
|
1738
|
-
const clampedB = Math.max(0, Math.min(255, b));
|
|
1739
|
-
if (clampedR === clampedG && clampedG === clampedB) {
|
|
1740
|
-
if (clampedR < 8) {
|
|
1741
|
-
return 16;
|
|
1742
|
-
}
|
|
1743
|
-
if (clampedR > 248) {
|
|
1744
|
-
return 231;
|
|
1745
|
-
}
|
|
1746
|
-
return Math.round((clampedR - 8) / 247 * 24) + 232;
|
|
1747
|
-
}
|
|
1748
|
-
return Math.round(clampedR / 51) * 36 + Math.round(clampedG / 51) * 6 + Math.round(clampedB / 51) + 16;
|
|
1749
|
-
}
|
|
1750
|
-
|
|
1751
2018
|
// src/core/factory.ts
|
|
1752
2019
|
function createPigment(options) {
|
|
1753
2020
|
const colorSupport = detectColorSupport(options?.level);
|
|
@@ -1777,13 +2044,12 @@ function createPigment(options) {
|
|
|
1777
2044
|
kernel.use(environmentPlugin());
|
|
1778
2045
|
if (options?.plugins) {
|
|
1779
2046
|
for (const plugin of options.plugins) {
|
|
1780
|
-
kernel
|
|
2047
|
+
installPlugin(kernel, plugin);
|
|
1781
2048
|
}
|
|
1782
2049
|
}
|
|
1783
|
-
void kernel.init();
|
|
1784
2050
|
const pigment = createProxyPigment(kernel);
|
|
1785
|
-
|
|
1786
|
-
Object.defineProperty(pigment, "builder", {
|
|
2051
|
+
registerRoot(kernel, pigment);
|
|
2052
|
+
Object.defineProperty(pigment, "builder", { get: () => new BuilderPigment(kernel), enumerable: false });
|
|
1787
2053
|
Object.defineProperty(pigment, "level", { value: level, enumerable: true });
|
|
1788
2054
|
Object.defineProperty(pigment, "supportsColor", { value: colorSupport, enumerable: true });
|
|
1789
2055
|
return pigment;
|