@oxog/pigment 1.1.0 → 1.1.1

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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @oxog/pigment - Zero-dependency terminal styling
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");
@@ -572,7 +578,260 @@ 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 registerPluginApi(kernel, name, value) {
761
+ let api = apis.get(kernel);
762
+ if (!api) {
763
+ api = /* @__PURE__ */ new Map();
764
+ apis.set(kernel, api);
765
+ }
766
+ api.set(name, value);
767
+ }
768
+ function getPluginApi(kernel, name) {
769
+ return apis.get(kernel)?.get(name);
770
+ }
771
+ function registerRoot(kernel, root) {
772
+ roots.set(kernel, root);
773
+ }
774
+ function getRoot(kernel) {
775
+ return roots.get(kernel);
776
+ }
777
+ function missingPluginApi(name) {
778
+ const plugin = PLUGIN_API_PROVIDERS[name];
779
+ const fail = () => {
780
+ throw new PluginError(
781
+ plugin,
782
+ `pigment.${name} needs ${plugin}(): createPigment({ plugins: [${plugin}()] }) or pigment.use(${plugin}())`
783
+ );
784
+ };
785
+ if (name === "theme") {
786
+ return {
787
+ keyword: fail,
788
+ string: fail,
789
+ number: fail,
790
+ comment: fail,
791
+ error: fail,
792
+ success: fail,
793
+ warning: fail,
794
+ info: fail
795
+ };
796
+ }
797
+ return fail;
798
+ }
799
+ function installPlugin(kernel, plugin) {
800
+ kernel.use(plugin);
801
+ if (plugin.onInit) {
802
+ Promise.resolve().then(() => plugin.onInit?.(kernel.getContext())).catch(() => {
803
+ });
804
+ }
805
+ }
806
+
575
807
  // src/plugins/optional/gradient.ts
808
+ function toRgb(color) {
809
+ if (typeof color !== "string") {
810
+ const [r, g, b] = color;
811
+ return { r, g, b };
812
+ }
813
+ return color.startsWith("rgb(") ? parseRgb(color) : hexToRgb(color);
814
+ }
815
+ function paintGradient(text, startRgb, endRgb, steps, level) {
816
+ if (text.length === 0) {
817
+ return text;
818
+ }
819
+ const colors = interpolateColors(startRgb, endRgb, text.length, Math.max(2, steps));
820
+ let result = "";
821
+ for (let i = 0; i < text.length; i++) {
822
+ const colorIndex = text.length === 1 ? 0 : Math.floor(i / (text.length - 1) * (colors.length - 1));
823
+ result += paintRgb(text[i], colors[colorIndex], false, level);
824
+ }
825
+ return result;
826
+ }
827
+ function paintRainbow(text, steps, level) {
828
+ const colors = generateRainbowColors(text.length, Math.max(1, steps));
829
+ let result = "";
830
+ for (let i = 0; i < text.length; i++) {
831
+ result += paintRgb(text[i], colors[i % colors.length], false, level);
832
+ }
833
+ return result;
834
+ }
576
835
  function gradientPlugin(options) {
577
836
  const steps = options?.steps ?? 10;
578
837
  return {
@@ -586,6 +845,15 @@ function gradientPlugin(options) {
586
845
  enabled: true
587
846
  }
588
847
  });
848
+ registerPluginApi(kernel, "gradient", (start, end) => {
849
+ const startRgb = toRgb(start);
850
+ const endRgb = toRgb(end);
851
+ if (!startRgb || !endRgb) {
852
+ throw new ColorError(`Invalid gradient color: ${String(startRgb ? end : start)}`);
853
+ }
854
+ return (text) => paintGradient(String(text), startRgb, endRgb, steps, k.getContext().level);
855
+ });
856
+ registerPluginApi(kernel, "rainbow", (text) => paintRainbow(String(text), steps, k.getContext().level));
589
857
  k.on("style:gradient", (data) => {
590
858
  const { start, end, text, steps: gradientSteps = steps } = data;
591
859
  const startRgb = start.startsWith("#") ? hexToRgb(start) : start.startsWith("rgb(") ? parseRgb(start) : hexToRgb(start);
@@ -656,6 +924,7 @@ function generateRainbowColors(length, steps) {
656
924
  }
657
925
 
658
926
  // src/plugins/optional/theme.ts
927
+ var THEME_COLOR_TYPES = ["keyword", "string", "number", "comment", "error", "success", "warning", "info"];
659
928
  function themePlugin(options) {
660
929
  const preset = options?.preset ?? "monokai";
661
930
  const custom = options?.custom ?? {};
@@ -675,6 +944,17 @@ function themePlugin(options) {
675
944
  enabled: true
676
945
  }
677
946
  });
947
+ const api = {};
948
+ for (const type of THEME_COLOR_TYPES) {
949
+ const color = theme[type];
950
+ api[type] = (text) => {
951
+ if (!color) {
952
+ throw new Error(`Unknown theme color type: ${type}`);
953
+ }
954
+ return paintRgb(String(text), hexToRgb(color), false, k.getContext().level);
955
+ };
956
+ }
957
+ registerPluginApi(kernel, "theme", api);
678
958
  k.on("style:theme", (data) => {
679
959
  const { type, text } = data;
680
960
  const color = theme[type];
@@ -744,6 +1024,10 @@ function semanticPlugin(options) {
744
1024
  if (!hexToRgb2) {
745
1025
  throw new Error("hexToRgb utility not available");
746
1026
  }
1027
+ for (const [name, color] of Object.entries(defaultColors)) {
1028
+ const rgb = hexToRgb2(color);
1029
+ registerPluginApi(kernel, name, (text) => paintRgb(String(text), rgb, false, k.getContext().level));
1030
+ }
747
1031
  k.on("style:success", (data) => {
748
1032
  const { r, g, b } = hexToRgb2(defaultColors.success);
749
1033
  return `\x1B[38;2;${r};${g};${b}m${data.text}\x1B[39m`;
@@ -785,9 +1069,12 @@ function boxPlugin(options) {
785
1069
  borders: BOX_BORDERS
786
1070
  }
787
1071
  });
788
- k.on("style:box", (data) => {
789
- const { text, border: boxBorder = border, padding: boxPadding = padding, bgColor } = data;
1072
+ const draw = (text, opts = {}) => {
1073
+ const { border: boxBorder = border, padding: boxPadding = padding, bgColor } = opts;
790
1074
  const chars = BOX_BORDERS[boxBorder];
1075
+ if (!chars) {
1076
+ throw new StyleError(`Unknown box border: ${String(boxBorder)}`);
1077
+ }
791
1078
  const lines = text.split("\n");
792
1079
  const width = Math.max(...lines.map((l) => l.length)) + boxPadding * 2;
793
1080
  const pad = " ".repeat(boxPadding);
@@ -799,21 +1086,152 @@ function boxPlugin(options) {
799
1086
  const bottom = `${chars.bl}${chars.h.repeat(width)}${chars.br}`;
800
1087
  const result = [top, ...middle, bottom].join("\n");
801
1088
  if (bgColor) {
802
- const ctx = k.getContext();
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`;
1089
+ return paintRgb(result, hexToRgb(bgColor), true, k.getContext().level);
809
1090
  }
810
1091
  return result;
1092
+ };
1093
+ registerPluginApi(kernel, "box", (text, opts) => draw(String(text), opts));
1094
+ k.on("style:box", (data) => {
1095
+ const { text, ...opts } = data;
1096
+ return draw(text, opts);
811
1097
  });
812
1098
  }
813
1099
  };
814
1100
  }
815
1101
 
1102
+ // src/core/proxy.ts
1103
+ var proxyCache = /* @__PURE__ */ new WeakMap();
1104
+ function createProxyPigment(kernel, styles = []) {
1105
+ const handler = {
1106
+ get(target, prop) {
1107
+ const ctx = kernel.getContext();
1108
+ if (prop === "level") {
1109
+ return ctx.level;
1110
+ }
1111
+ if (prop === "supportsColor") {
1112
+ return ctx.colorSupport;
1113
+ }
1114
+ if (MODIFIER_NAMES.some((name) => name === prop)) {
1115
+ const modifierStyle = generateModifierCode(prop);
1116
+ return createProxyPigment(kernel, [...styles, modifierStyle]);
1117
+ }
1118
+ if (COLOR_NAMES.some((name) => name === prop) || BACKGROUND_COLOR_NAMES.some((name) => name === prop)) {
1119
+ const background = prop.startsWith("bg");
1120
+ const colorName = background ? prop.slice(2) : prop;
1121
+ const colorStyle = generateColorCode(colorName, background);
1122
+ return createProxyPigment(kernel, [...styles, colorStyle]);
1123
+ }
1124
+ if (EXTENDED_COLOR_METHODS.some((name) => name === prop)) {
1125
+ if (prop === "ansi256" || prop === "bgAnsi256") {
1126
+ return (code) => {
1127
+ const background = prop === "bgAnsi256";
1128
+ const ansi256Style = generateAnsi256CodeForLevel(code, background, ctx.level);
1129
+ return createProxyPigment(kernel, [...styles, ansi256Style]);
1130
+ };
1131
+ }
1132
+ if (prop === "rgb" || prop === "bgRgb") {
1133
+ return (r, g, b) => {
1134
+ const background = prop === "bgRgb";
1135
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
1136
+ return createProxyPigment(kernel, [...styles, rgbStyle]);
1137
+ };
1138
+ }
1139
+ if (prop === "hex" || prop === "bgHex") {
1140
+ return (color) => {
1141
+ const background = prop === "bgHex";
1142
+ const hexToRgb2 = ctx.utils?.hexToRgb;
1143
+ if (!hexToRgb2) {
1144
+ throw new Error("hexToRgb utility not available");
1145
+ }
1146
+ const { r, g, b } = hexToRgb2(color);
1147
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
1148
+ return createProxyPigment(kernel, [...styles, rgbStyle]);
1149
+ };
1150
+ }
1151
+ if (prop === "hsl" || prop === "bgHsl") {
1152
+ return (h2, s, l) => {
1153
+ const background = prop === "bgHsl";
1154
+ const hslToRgb2 = ctx.utils?.hslToRgb;
1155
+ if (!hslToRgb2) {
1156
+ throw new Error("hslToRgb utility not available");
1157
+ }
1158
+ const { r, g, b } = hslToRgb2(h2, s, l);
1159
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
1160
+ return createProxyPigment(kernel, [...styles, rgbStyle]);
1161
+ };
1162
+ }
1163
+ }
1164
+ if (prop === "visible") {
1165
+ const colorSupport = ctx.colorSupport;
1166
+ return (text) => colorSupport.level > 0 ? applyStyles(String(text), styles, ctx.level) : "";
1167
+ }
1168
+ if (prop === "use") {
1169
+ return (plugin) => {
1170
+ installPlugin(kernel, plugin);
1171
+ return getRoot(kernel) ?? createProxyPigment(kernel);
1172
+ };
1173
+ }
1174
+ if (Object.prototype.hasOwnProperty.call(PLUGIN_API_PROVIDERS, prop)) {
1175
+ return getPluginApi(kernel, prop) ?? missingPluginApi(prop);
1176
+ }
1177
+ return Reflect.get(target, prop);
1178
+ },
1179
+ apply(_target, _thisArg, argArray) {
1180
+ const text = String(argArray[0] ?? "");
1181
+ return applyStyles(text, styles, kernel.getContext().level);
1182
+ }
1183
+ };
1184
+ const pigment = new Proxy((() => ""), handler);
1185
+ proxyCache.set(pigment, pigment);
1186
+ return pigment;
1187
+ }
1188
+ function applyStyles(text, styles, level) {
1189
+ if (styles.length === 0 || text === "" || level === 0) {
1190
+ return text;
1191
+ }
1192
+ const open = styles.map((style) => style.open).join("");
1193
+ const close = [...styles].reverse().map((style) => style.close).join("");
1194
+ let result = text;
1195
+ for (const style of styles) {
1196
+ if (style.close) {
1197
+ result = result.split(style.close).join(style.close + style.open);
1198
+ }
1199
+ }
1200
+ return `${open}${result}${close}`;
1201
+ }
1202
+
816
1203
  // src/plugins/optional/template.ts
1204
+ var STYLE_NAMES = /* @__PURE__ */ new Set([...MODIFIER_NAMES, ...COLOR_NAMES, ...BACKGROUND_COLOR_NAMES]);
1205
+ var BLOCK = /\{([A-Za-z]\w*(?:\.[A-Za-z]\w*)*) ([^{}]*)\}/g;
1206
+ var TOKEN = /\uE000(\d+)\uE001/g;
1207
+ function renderTemplate(root, strings, values) {
1208
+ const parts = values.map((value) => String(value));
1209
+ const token = (index) => `\uE000${index}\uE001`;
1210
+ const restore = (text) => text.replace(TOKEN, (_match, index) => parts[Number(index)]);
1211
+ let source = "";
1212
+ for (let i = 0; i < strings.length; i++) {
1213
+ source += strings[i];
1214
+ if (i < values.length) {
1215
+ source += token(i);
1216
+ }
1217
+ }
1218
+ let previous;
1219
+ do {
1220
+ previous = source;
1221
+ source = source.replace(BLOCK, (_match, chain, text) => {
1222
+ let styler = root;
1223
+ for (const name of chain.split(".")) {
1224
+ if (!STYLE_NAMES.has(name)) {
1225
+ throw new StyleError(`Unknown style in template: ${name}`);
1226
+ }
1227
+ styler = styler[name];
1228
+ }
1229
+ parts.push(styler(restore(text)));
1230
+ return token(parts.length - 1);
1231
+ });
1232
+ } while (source !== previous);
1233
+ return restore(source);
1234
+ }
817
1235
  function templatePlugin() {
818
1236
  return {
819
1237
  name: "template",
@@ -825,6 +1243,11 @@ function templatePlugin() {
825
1243
  enabled: true
826
1244
  }
827
1245
  });
1246
+ registerPluginApi(
1247
+ kernel,
1248
+ "template",
1249
+ (strings, ...values) => renderTemplate(getRoot(kernel) ?? createProxyPigment(k), strings, values)
1250
+ );
828
1251
  k.on("style:template", (data) => {
829
1252
  const { strings, values } = data;
830
1253
  let result = "";
@@ -868,8 +1291,8 @@ function parseStyleExpression(expression, text, kernel) {
868
1291
  }
869
1292
 
870
1293
  // node_modules/@oxog/plugin/dist/index.js
871
- import { PluginError } from "@oxog/types";
872
1294
  import { PluginError as PluginError2 } from "@oxog/types";
1295
+ import { PluginError as PluginError3 } from "@oxog/types";
873
1296
  var x = ((i) => (i.Created = "created", i.Initializing = "initializing", i.Ready = "ready", i.Destroying = "destroying", i.Destroyed = "destroyed", i))(x || {});
874
1297
  var h = class {
875
1298
  context;
@@ -989,9 +1412,9 @@ var f = class {
989
1412
  validateSinglePlugin(e) {
990
1413
  let t = this.graph.get(e);
991
1414
  if (!t) return;
992
- for (let r of t) if (!this.graph.has(r)) throw new PluginError(`Missing dependency: '${r}' required by '${e}'`, e);
1415
+ for (let r of t) if (!this.graph.has(r)) throw new PluginError2(`Missing dependency: '${r}' required by '${e}'`, e);
993
1416
  let n = this.detectCycle();
994
- if (n && n.includes(e)) throw new PluginError(`Circular dependency detected: ${n.join(" -> ")}`, "dependency-resolver");
1417
+ if (n && n.includes(e)) throw new PluginError2(`Circular dependency detected: ${n.join(" -> ")}`, "dependency-resolver");
995
1418
  }
996
1419
  resolve() {
997
1420
  let e = /* @__PURE__ */ new Map(), t = [], n = [], r = /* @__PURE__ */ new Map();
@@ -999,7 +1422,7 @@ var f = class {
999
1422
  let l = o.filter((p) => this.graph.has(p));
1000
1423
  r.set(s, l);
1001
1424
  }
1002
- for (let [s, o] of this.graph) for (let l of o) if (!this.graph.has(l)) throw new PluginError(`Missing dependency: '${l}' required by '${s}'`, s);
1425
+ for (let [s, o] of this.graph) for (let l of o) if (!this.graph.has(l)) throw new PluginError2(`Missing dependency: '${l}' required by '${s}'`, s);
1003
1426
  for (let [s, o] of r) e.set(s, o.length), o.length === 0 && t.push(s);
1004
1427
  let i = /* @__PURE__ */ new Map();
1005
1428
  for (let [s, o] of r) for (let l of o) {
@@ -1014,7 +1437,7 @@ var f = class {
1014
1437
  }
1015
1438
  if (n.length !== r.size) {
1016
1439
  let s = this.detectCycle();
1017
- throw new PluginError(`Circular dependency detected: ${s.join(" -> ")}`, "dependency-resolver");
1440
+ throw new PluginError2(`Circular dependency detected: ${s.join(" -> ")}`, "dependency-resolver");
1018
1441
  }
1019
1442
  return n;
1020
1443
  }
@@ -1145,9 +1568,9 @@ var c = class {
1145
1568
  this.config = e || {}, this.context = new h(this.config.context ?? {}), this.events = new u(), this.resolver = new f(), this.errorBoundary = new v(this.config.errorStrategy || "isolate", this.config.onError), this.lifecycle = new m();
1146
1569
  }
1147
1570
  use(e) {
1148
- if (this.state === "destroyed") throw new PluginError("Cannot add plugins to destroyed kernel", "kernel");
1149
- if (this.state === "initializing") throw new PluginError("Cannot add plugins while kernel is initializing. Register all plugins before calling init(), or wait until kernel is ready.", "kernel");
1150
- if (this.plugins.has(e.name)) throw new PluginError(`Plugin '${e.name}' is already registered`, "kernel");
1571
+ if (this.state === "destroyed") throw new PluginError2("Cannot add plugins to destroyed kernel", "kernel");
1572
+ if (this.state === "initializing") throw new PluginError2("Cannot add plugins while kernel is initializing. Register all plugins before calling init(), or wait until kernel is ready.", "kernel");
1573
+ if (this.plugins.has(e.name)) throw new PluginError2(`Plugin '${e.name}' is already registered`, "kernel");
1151
1574
  let t = { plugin: e, state: "registered" };
1152
1575
  this.plugins.set(e.name, t), this.resolver.addPlugin(e.name, [...e.dependencies || []]);
1153
1576
  try {
@@ -1181,7 +1604,7 @@ var c = class {
1181
1604
  let i = this.plugins.get(r);
1182
1605
  if (!i || i.state !== "ready") {
1183
1606
  t.state = "failed";
1184
- let a = new PluginError(`Dependency '${r}' is not ready for plugin '${e}'`, e);
1607
+ let a = new PluginError2(`Dependency '${r}' is not ready for plugin '${e}'`, e);
1185
1608
  this.emitInternal("plugin:error", { name: e, error: a });
1186
1609
  return;
1187
1610
  }
@@ -1252,7 +1675,7 @@ var c = class {
1252
1675
  }
1253
1676
  }
1254
1677
  async init() {
1255
- if (this.state !== "created") throw new PluginError("Kernel already initialized", "kernel");
1678
+ if (this.state !== "created") throw new PluginError2("Kernel already initialized", "kernel");
1256
1679
  this.state = "initializing";
1257
1680
  try {
1258
1681
  this.config.onBeforeInit && await this.config.onBeforeInit();
@@ -1326,176 +1749,16 @@ function W(d) {
1326
1749
  return new c(d);
1327
1750
  }
1328
1751
 
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
1752
  // src/core/builder.ts
1496
1753
  var BuilderPigment = class {
1497
1754
  styles = [];
1498
- constructor(_kernel) {
1755
+ kernel;
1756
+ constructor(kernel) {
1757
+ this.kernel = kernel;
1758
+ }
1759
+ /** The instance's color level (true color when there is no kernel). */
1760
+ get level() {
1761
+ return this.kernel?.getContext().level;
1499
1762
  }
1500
1763
  bold() {
1501
1764
  this.styles.push(generateModifierCode("bold"));
@@ -1674,26 +1937,26 @@ var BuilderPigment = class {
1674
1937
  return this;
1675
1938
  }
1676
1939
  ansi256(code) {
1677
- this.styles.push(generateAnsi256Code(code, false));
1940
+ this.styles.push(generateAnsi256CodeForLevel(code, false, this.level));
1678
1941
  return this;
1679
1942
  }
1680
1943
  bgAnsi256(code) {
1681
- this.styles.push(generateAnsi256Code(code, true));
1944
+ this.styles.push(generateAnsi256CodeForLevel(code, true, this.level));
1682
1945
  return this;
1683
1946
  }
1684
1947
  rgb(r, g, b) {
1685
- this.styles.push(generateRgbCode(r, g, b, false));
1948
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
1686
1949
  return this;
1687
1950
  }
1688
1951
  bgRgb(r, g, b) {
1689
- this.styles.push(generateRgbCode(r, g, b, true));
1952
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
1690
1953
  return this;
1691
1954
  }
1692
1955
  hex(color) {
1693
1956
  const rgb = hexToRgb(color);
1694
1957
  if (rgb) {
1695
1958
  const { r, g, b } = rgb;
1696
- this.styles.push(generateRgbCode(r, g, b, false));
1959
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
1697
1960
  }
1698
1961
  return this;
1699
1962
  }
@@ -1701,22 +1964,22 @@ var BuilderPigment = class {
1701
1964
  const rgb = hexToRgb(color);
1702
1965
  if (rgb) {
1703
1966
  const { r, g, b } = rgb;
1704
- this.styles.push(generateRgbCode(r, g, b, true));
1967
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
1705
1968
  }
1706
1969
  return this;
1707
1970
  }
1708
1971
  hsl(h2, s, l) {
1709
1972
  const { r, g, b } = hslToRgb(h2, s, l);
1710
- this.styles.push(generateRgbCode(r, g, b, false));
1973
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
1711
1974
  return this;
1712
1975
  }
1713
1976
  bgHsl(h2, s, l) {
1714
1977
  const { r, g, b } = hslToRgb(h2, s, l);
1715
- this.styles.push(generateRgbCode(r, g, b, true));
1978
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
1716
1979
  return this;
1717
1980
  }
1718
1981
  paint(text) {
1719
- if (this.styles.length === 0 || text === "") {
1982
+ if (this.styles.length === 0 || text === "" || this.level === 0) {
1720
1983
  return text;
1721
1984
  }
1722
1985
  const open = this.styles.map((style) => style.open).join("");
@@ -1731,23 +1994,6 @@ var BuilderPigment = class {
1731
1994
  }
1732
1995
  };
1733
1996
 
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
1997
  // src/core/factory.ts
1752
1998
  function createPigment(options) {
1753
1999
  const colorSupport = detectColorSupport(options?.level);
@@ -1777,13 +2023,12 @@ function createPigment(options) {
1777
2023
  kernel.use(environmentPlugin());
1778
2024
  if (options?.plugins) {
1779
2025
  for (const plugin of options.plugins) {
1780
- kernel.use(plugin);
2026
+ installPlugin(kernel, plugin);
1781
2027
  }
1782
2028
  }
1783
- void kernel.init();
1784
2029
  const pigment = createProxyPigment(kernel);
1785
- const builder = new BuilderPigment(kernel);
1786
- Object.defineProperty(pigment, "builder", { value: builder, enumerable: false });
2030
+ registerRoot(kernel, pigment);
2031
+ Object.defineProperty(pigment, "builder", { get: () => new BuilderPigment(kernel), enumerable: false });
1787
2032
  Object.defineProperty(pigment, "level", { value: level, enumerable: true });
1788
2033
  Object.defineProperty(pigment, "supportsColor", { value: colorSupport, enumerable: true });
1789
2034
  return pigment;