@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.
@@ -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
  "use strict";
@@ -329,6 +329,12 @@ var PigmentError = class _PigmentError extends Error {
329
329
  Error.captureStackTrace(this, _PigmentError);
330
330
  }
331
331
  };
332
+ var PluginError = class extends PigmentError {
333
+ constructor(pluginName, message) {
334
+ super(`[${pluginName}] ${message}`, "PLUGIN_ERROR");
335
+ this.name = "PluginError";
336
+ }
337
+ };
332
338
  var ColorError = class extends PigmentError {
333
339
  constructor(message) {
334
340
  super(message, "COLOR_ERROR");
@@ -382,11 +388,11 @@ function trueColorPlugin() {
382
388
  k.emit("color:rgb", { ...rgb, background });
383
389
  });
384
390
  k.on("color:hsl", (data) => {
385
- const { h: h2, s, l, background } = data;
391
+ const { h: h2, s, l: l2, background } = data;
386
392
  if (!utils?.hslToRgb) {
387
393
  throw new Error("hslToRgb utility not available");
388
394
  }
389
- const rgb = utils.hslToRgb(h2, s, l);
395
+ const rgb = utils.hslToRgb(h2, s, l2);
390
396
  k.emit("color:rgb", { ...rgb, background });
391
397
  });
392
398
  }
@@ -577,10 +583,10 @@ function parseExpandedHex(hex) {
577
583
  }
578
584
 
579
585
  // src/utils/hsl-to-rgb.ts
580
- function hslToRgb(h2, s, l) {
586
+ function hslToRgb(h2, s, l2) {
581
587
  const hNormalized = h2 / 360;
582
588
  const sNormalized = s / 100;
583
- const lNormalized = l / 100;
589
+ const lNormalized = l2 / 100;
584
590
  let r;
585
591
  let g;
586
592
  let b;
@@ -608,7 +614,263 @@ function hslToRgb(h2, s, l) {
608
614
  };
609
615
  }
610
616
 
617
+ // src/utils/rgb-to-ansi256.ts
618
+ function rgbToAnsi256(r, g, b) {
619
+ const clampedR = Math.max(0, Math.min(255, r));
620
+ const clampedG = Math.max(0, Math.min(255, g));
621
+ const clampedB = Math.max(0, Math.min(255, b));
622
+ if (clampedR === clampedG && clampedG === clampedB) {
623
+ if (clampedR < 8) {
624
+ return 16;
625
+ }
626
+ if (clampedR > 248) {
627
+ return 231;
628
+ }
629
+ return Math.round((clampedR - 8) / 247 * 24) + 232;
630
+ }
631
+ return Math.round(clampedR / 51) * 36 + Math.round(clampedG / 51) * 6 + Math.round(clampedB / 51) + 16;
632
+ }
633
+
634
+ // src/core/ansi.ts
635
+ function generateModifierCode(name) {
636
+ const modifier = ANSI_MODIFIERS[name];
637
+ if (!modifier) {
638
+ throw new StyleError(`Unknown modifier: ${name}`);
639
+ }
640
+ return {
641
+ name,
642
+ code: -1,
643
+ open: modifier.open,
644
+ close: modifier.close,
645
+ type: "modifier"
646
+ };
647
+ }
648
+ function generateColorCode(name, background = false) {
649
+ let normalizedName = name;
650
+ const lowerName = name.toLowerCase();
651
+ if (lowerName === "gray" || lowerName === "grey") {
652
+ normalizedName = "blackBright";
653
+ } else if (lowerName === "bggray" || lowerName === "bggrey") {
654
+ normalizedName = "bgBlackBright";
655
+ }
656
+ const foregroundColors = { ...ANSI_FOREGROUND_COLORS, ...ANSI_FOREGROUND_BRIGHT_COLORS };
657
+ const backgroundColors = { ...ANSI_BACKGROUND_COLORS, ...ANSI_BACKGROUND_BRIGHT_COLORS };
658
+ let lookupName = normalizedName;
659
+ if (background && !normalizedName.startsWith("bg")) {
660
+ lookupName = "bg" + normalizedName.charAt(0).toUpperCase() + normalizedName.slice(1);
661
+ }
662
+ const allColors = background ? backgroundColors : foregroundColors;
663
+ const code = allColors[lookupName];
664
+ if (code === void 0) {
665
+ throw new StyleError(`Unknown color: ${name}`);
666
+ }
667
+ const closeCode = background ? 49 : 39;
668
+ return {
669
+ name,
670
+ code,
671
+ open: `\x1B[${code}m`,
672
+ close: `\x1B[${closeCode}m`,
673
+ type: "color",
674
+ background
675
+ };
676
+ }
677
+ function generateAnsi256Code(code, background = false) {
678
+ if (code < 0 || code > 255) {
679
+ throw new StyleError(`ANSI 256 code must be between 0 and 255, got ${code}`);
680
+ }
681
+ const prefix = background ? 48 : 38;
682
+ const closeCode = background ? 49 : 39;
683
+ return {
684
+ name: `ansi256(${code})`,
685
+ code,
686
+ open: `\x1B[${prefix};5;${code}m`,
687
+ close: `\x1B[${closeCode}m`,
688
+ type: "color",
689
+ background
690
+ };
691
+ }
692
+ function generateRgbCode(r, g, b, background = false) {
693
+ if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) {
694
+ throw new StyleError(`RGB values must be between 0 and 255, got (${r}, ${g}, ${b})`);
695
+ }
696
+ const prefix = background ? 48 : 38;
697
+ const closeCode = background ? 49 : 39;
698
+ return {
699
+ name: `rgb(${r}, ${g}, ${b})`,
700
+ code: -1,
701
+ open: `\x1B[${prefix};2;${r};${g};${b}m`,
702
+ close: `\x1B[${closeCode}m`,
703
+ type: "color",
704
+ background
705
+ };
706
+ }
707
+ function ansi256ToAnsi16(code) {
708
+ if (code < 8) {
709
+ return 30 + code;
710
+ }
711
+ if (code < 16) {
712
+ return 90 + (code - 8);
713
+ }
714
+ let red;
715
+ let green;
716
+ let blue;
717
+ if (code >= 232) {
718
+ red = ((code - 232) * 10 + 8) / 255;
719
+ green = red;
720
+ blue = red;
721
+ } else {
722
+ const index = code - 16;
723
+ const remainder = index % 36;
724
+ red = Math.floor(index / 36) / 5;
725
+ green = Math.floor(remainder / 6) / 5;
726
+ blue = remainder % 6 / 5;
727
+ }
728
+ const value = Math.max(red, green, blue) * 2;
729
+ if (value === 0) {
730
+ return 30;
731
+ }
732
+ let result = 30 + (Math.round(blue) << 2 | Math.round(green) << 1 | Math.round(red));
733
+ if (value === 2) {
734
+ result += 60;
735
+ }
736
+ return result;
737
+ }
738
+ function effectiveLevel(level) {
739
+ return level ?? 3;
740
+ }
741
+ function ansi16Style(name, paletteCode, background) {
742
+ const code = ansi256ToAnsi16(paletteCode) + (background ? 10 : 0);
743
+ return {
744
+ name,
745
+ code,
746
+ open: `\x1B[${code}m`,
747
+ close: `\x1B[${background ? 49 : 39}m`,
748
+ type: "color",
749
+ background
750
+ };
751
+ }
752
+ function generateAnsi256CodeForLevel(code, background, level) {
753
+ const style = generateAnsi256Code(code, background);
754
+ if (effectiveLevel(level) >= 2) {
755
+ return style;
756
+ }
757
+ return ansi16Style(style.name, code, background);
758
+ }
759
+ function generateRgbCodeForLevel(r, g, b, background, level) {
760
+ const style = generateRgbCode(r, g, b, background);
761
+ const effective = effectiveLevel(level);
762
+ if (effective >= 3) {
763
+ return style;
764
+ }
765
+ const paletteCode = rgbToAnsi256(r, g, b);
766
+ if (effective === 2) {
767
+ return { ...generateAnsi256Code(paletteCode, background), name: style.name };
768
+ }
769
+ return ansi16Style(style.name, paletteCode, background);
770
+ }
771
+ function paintRgb(text, rgb, background, level) {
772
+ if (level === 0 || text === "") {
773
+ return text;
774
+ }
775
+ const style = generateRgbCodeForLevel(rgb.r, rgb.g, rgb.b, background, level);
776
+ return `${style.open}${text}${style.close}`;
777
+ }
778
+
779
+ // src/core/plugin-api.ts
780
+ var REGISTRY_KEY = /* @__PURE__ */ Symbol.for("@oxog/pigment.plugin-api");
781
+ var globalRegistry = globalThis;
782
+ var registry = globalRegistry[REGISTRY_KEY] ??= { apis: /* @__PURE__ */ new WeakMap(), roots: /* @__PURE__ */ new WeakMap() };
783
+ var { apis, roots } = registry;
784
+ var PLUGIN_API_PROVIDERS = {
785
+ gradient: "gradientPlugin",
786
+ rainbow: "gradientPlugin",
787
+ theme: "themePlugin",
788
+ success: "semanticPlugin",
789
+ error: "semanticPlugin",
790
+ warning: "semanticPlugin",
791
+ info: "semanticPlugin",
792
+ debug: "semanticPlugin",
793
+ box: "boxPlugin",
794
+ template: "templatePlugin"
795
+ };
796
+ function isPluginApiName(name) {
797
+ return Object.prototype.hasOwnProperty.call(PLUGIN_API_PROVIDERS, name);
798
+ }
799
+ function registerPluginApi(kernel, name, value) {
800
+ let api = apis.get(kernel);
801
+ if (!api) {
802
+ api = /* @__PURE__ */ new Map();
803
+ apis.set(kernel, api);
804
+ }
805
+ api.set(name, value);
806
+ }
807
+ function getPluginApi(kernel, name) {
808
+ return apis.get(kernel)?.get(name);
809
+ }
810
+ function registerRoot(kernel, root) {
811
+ roots.set(kernel, root);
812
+ }
813
+ function getRoot(kernel) {
814
+ return roots.get(kernel);
815
+ }
816
+ function missingPluginApi(name) {
817
+ const plugin = PLUGIN_API_PROVIDERS[name];
818
+ const fail = () => {
819
+ throw new PluginError(
820
+ plugin,
821
+ `pigment.${name} needs ${plugin}(): createPigment({ plugins: [${plugin}()] }) or pigment.use(${plugin}())`
822
+ );
823
+ };
824
+ if (name === "theme") {
825
+ return {
826
+ keyword: fail,
827
+ string: fail,
828
+ number: fail,
829
+ comment: fail,
830
+ error: fail,
831
+ success: fail,
832
+ warning: fail,
833
+ info: fail
834
+ };
835
+ }
836
+ return fail;
837
+ }
838
+ function installPlugin(kernel, plugin) {
839
+ kernel.use(plugin);
840
+ if (plugin.onInit) {
841
+ Promise.resolve().then(() => plugin.onInit?.(kernel.getContext())).catch(() => {
842
+ });
843
+ }
844
+ }
845
+
611
846
  // src/plugins/optional/gradient.ts
847
+ function toRgb(color) {
848
+ if (typeof color !== "string") {
849
+ const [r, g, b] = color;
850
+ return { r, g, b };
851
+ }
852
+ return color.startsWith("rgb(") ? parseRgb(color) : hexToRgb(color);
853
+ }
854
+ function paintGradient(text, startRgb, endRgb, steps, level) {
855
+ if (text.length === 0) {
856
+ return text;
857
+ }
858
+ const colors = interpolateColors(startRgb, endRgb, text.length, Math.max(2, steps));
859
+ let result = "";
860
+ for (let i = 0; i < text.length; i++) {
861
+ const colorIndex = text.length === 1 ? 0 : Math.floor(i / (text.length - 1) * (colors.length - 1));
862
+ result += paintRgb(text[i], colors[colorIndex], false, level);
863
+ }
864
+ return result;
865
+ }
866
+ function paintRainbow(text, steps, level) {
867
+ const colors = generateRainbowColors(text.length, Math.max(1, steps));
868
+ let result = "";
869
+ for (let i = 0; i < text.length; i++) {
870
+ result += paintRgb(text[i], colors[i % colors.length], false, level);
871
+ }
872
+ return result;
873
+ }
612
874
  function gradientPlugin(options) {
613
875
  const steps = options?.steps ?? 10;
614
876
  return {
@@ -622,6 +884,15 @@ function gradientPlugin(options) {
622
884
  enabled: true
623
885
  }
624
886
  });
887
+ registerPluginApi(kernel, "gradient", (start, end) => {
888
+ const startRgb = toRgb(start);
889
+ const endRgb = toRgb(end);
890
+ if (!startRgb || !endRgb) {
891
+ throw new ColorError(`Invalid gradient color: ${String(startRgb ? end : start)}`);
892
+ }
893
+ return (text) => paintGradient(String(text), startRgb, endRgb, steps, k.getContext().level);
894
+ });
895
+ registerPluginApi(kernel, "rainbow", (text) => paintRainbow(String(text), steps, k.getContext().level));
625
896
  k.on("style:gradient", (data) => {
626
897
  const { start, end, text, steps: gradientSteps = steps } = data;
627
898
  const startRgb = start.startsWith("#") ? hexToRgb(start) : start.startsWith("rgb(") ? parseRgb(start) : hexToRgb(start);
@@ -692,6 +963,7 @@ function generateRainbowColors(length, steps) {
692
963
  }
693
964
 
694
965
  // src/plugins/optional/theme.ts
966
+ var THEME_COLOR_TYPES = ["keyword", "string", "number", "comment", "error", "success", "warning", "info"];
695
967
  function themePlugin(options) {
696
968
  const preset = options?.preset ?? "monokai";
697
969
  const custom = options?.custom ?? {};
@@ -711,6 +983,17 @@ function themePlugin(options) {
711
983
  enabled: true
712
984
  }
713
985
  });
986
+ const api = {};
987
+ for (const type of THEME_COLOR_TYPES) {
988
+ const color = theme[type];
989
+ api[type] = (text) => {
990
+ if (!color) {
991
+ throw new Error(`Unknown theme color type: ${type}`);
992
+ }
993
+ return paintRgb(String(text), hexToRgb(color), false, k.getContext().level);
994
+ };
995
+ }
996
+ registerPluginApi(kernel, "theme", api);
714
997
  k.on("style:theme", (data) => {
715
998
  const { type, text } = data;
716
999
  const color = theme[type];
@@ -780,6 +1063,10 @@ function semanticPlugin(options) {
780
1063
  if (!hexToRgb2) {
781
1064
  throw new Error("hexToRgb utility not available");
782
1065
  }
1066
+ for (const [name, color] of Object.entries(defaultColors)) {
1067
+ const rgb = hexToRgb2(color);
1068
+ registerPluginApi(kernel, name, (text) => paintRgb(String(text), rgb, false, k.getContext().level));
1069
+ }
783
1070
  k.on("style:success", (data) => {
784
1071
  const { r, g, b } = hexToRgb2(defaultColors.success);
785
1072
  return `\x1B[38;2;${r};${g};${b}m${data.text}\x1B[39m`;
@@ -821,11 +1108,14 @@ function boxPlugin(options) {
821
1108
  borders: BOX_BORDERS
822
1109
  }
823
1110
  });
824
- k.on("style:box", (data) => {
825
- const { text, border: boxBorder = border, padding: boxPadding = padding, bgColor } = data;
1111
+ const draw = (text, opts = {}) => {
1112
+ const { border: boxBorder = border, padding: boxPadding = padding, bgColor } = opts;
826
1113
  const chars = BOX_BORDERS[boxBorder];
1114
+ if (!chars) {
1115
+ throw new StyleError(`Unknown box border: ${String(boxBorder)}`);
1116
+ }
827
1117
  const lines = text.split("\n");
828
- const width = Math.max(...lines.map((l) => l.length)) + boxPadding * 2;
1118
+ const width = Math.max(...lines.map((l2) => l2.length)) + boxPadding * 2;
829
1119
  const pad = " ".repeat(boxPadding);
830
1120
  const top = `${chars.tl}${chars.h.repeat(width)}${chars.tr}`;
831
1121
  const middle = lines.map((line) => {
@@ -835,21 +1125,152 @@ function boxPlugin(options) {
835
1125
  const bottom = `${chars.bl}${chars.h.repeat(width)}${chars.br}`;
836
1126
  const result = [top, ...middle, bottom].join("\n");
837
1127
  if (bgColor) {
838
- const ctx = k.getContext();
839
- const hexToRgb2 = ctx.utils?.hexToRgb;
840
- if (!hexToRgb2) {
841
- throw new Error("hexToRgb utility not available");
842
- }
843
- const { r, g, b } = hexToRgb2(bgColor);
844
- return `\x1B[48;2;${r};${g};${b}m${result}\x1B[49m`;
1128
+ return paintRgb(result, hexToRgb(bgColor), true, k.getContext().level);
845
1129
  }
846
1130
  return result;
1131
+ };
1132
+ registerPluginApi(kernel, "box", (text, opts) => draw(String(text), opts));
1133
+ k.on("style:box", (data) => {
1134
+ const { text, ...opts } = data;
1135
+ return draw(text, opts);
847
1136
  });
848
1137
  }
849
1138
  };
850
1139
  }
851
1140
 
1141
+ // src/core/proxy.ts
1142
+ var proxyCache = /* @__PURE__ */ new WeakMap();
1143
+ function createProxyPigment(kernel, styles = []) {
1144
+ const handler = {
1145
+ get(target, prop) {
1146
+ const ctx = kernel.getContext();
1147
+ if (prop === "level") {
1148
+ return ctx.level;
1149
+ }
1150
+ if (prop === "supportsColor") {
1151
+ return ctx.colorSupport;
1152
+ }
1153
+ if (MODIFIER_NAMES.some((name) => name === prop)) {
1154
+ const modifierStyle = generateModifierCode(prop);
1155
+ return createProxyPigment(kernel, [...styles, modifierStyle]);
1156
+ }
1157
+ if (COLOR_NAMES.some((name) => name === prop) || BACKGROUND_COLOR_NAMES.some((name) => name === prop)) {
1158
+ const background = prop.startsWith("bg");
1159
+ const colorName = background ? prop.slice(2) : prop;
1160
+ const colorStyle = generateColorCode(colorName, background);
1161
+ return createProxyPigment(kernel, [...styles, colorStyle]);
1162
+ }
1163
+ if (EXTENDED_COLOR_METHODS.some((name) => name === prop)) {
1164
+ if (prop === "ansi256" || prop === "bgAnsi256") {
1165
+ return (code) => {
1166
+ const background = prop === "bgAnsi256";
1167
+ const ansi256Style = generateAnsi256CodeForLevel(code, background, ctx.level);
1168
+ return createProxyPigment(kernel, [...styles, ansi256Style]);
1169
+ };
1170
+ }
1171
+ if (prop === "rgb" || prop === "bgRgb") {
1172
+ return (r, g, b) => {
1173
+ const background = prop === "bgRgb";
1174
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
1175
+ return createProxyPigment(kernel, [...styles, rgbStyle]);
1176
+ };
1177
+ }
1178
+ if (prop === "hex" || prop === "bgHex") {
1179
+ return (color) => {
1180
+ const background = prop === "bgHex";
1181
+ const hexToRgb2 = ctx.utils?.hexToRgb;
1182
+ if (!hexToRgb2) {
1183
+ throw new Error("hexToRgb utility not available");
1184
+ }
1185
+ const { r, g, b } = hexToRgb2(color);
1186
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
1187
+ return createProxyPigment(kernel, [...styles, rgbStyle]);
1188
+ };
1189
+ }
1190
+ if (prop === "hsl" || prop === "bgHsl") {
1191
+ return (h2, s, l2) => {
1192
+ const background = prop === "bgHsl";
1193
+ const hslToRgb2 = ctx.utils?.hslToRgb;
1194
+ if (!hslToRgb2) {
1195
+ throw new Error("hslToRgb utility not available");
1196
+ }
1197
+ const { r, g, b } = hslToRgb2(h2, s, l2);
1198
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
1199
+ return createProxyPigment(kernel, [...styles, rgbStyle]);
1200
+ };
1201
+ }
1202
+ }
1203
+ if (prop === "visible") {
1204
+ const colorSupport = ctx.colorSupport;
1205
+ return (text) => colorSupport.level > 0 ? applyStyles(String(text), styles, ctx.level) : "";
1206
+ }
1207
+ if (prop === "use") {
1208
+ return (plugin) => {
1209
+ installPlugin(kernel, plugin);
1210
+ return getRoot(kernel) ?? createProxyPigment(kernel);
1211
+ };
1212
+ }
1213
+ if (isPluginApiName(prop)) {
1214
+ return getPluginApi(kernel, prop) ?? missingPluginApi(prop);
1215
+ }
1216
+ return Reflect.get(target, prop);
1217
+ },
1218
+ apply(_target, _thisArg, argArray) {
1219
+ const text = String(argArray[0] ?? "");
1220
+ return applyStyles(text, styles, kernel.getContext().level);
1221
+ }
1222
+ };
1223
+ const pigment = new Proxy((() => ""), handler);
1224
+ proxyCache.set(pigment, pigment);
1225
+ return pigment;
1226
+ }
1227
+ function applyStyles(text, styles, level) {
1228
+ if (styles.length === 0 || text === "" || level === 0) {
1229
+ return text;
1230
+ }
1231
+ const open = styles.map((style) => style.open).join("");
1232
+ const close = [...styles].reverse().map((style) => style.close).join("");
1233
+ let result = text;
1234
+ for (const style of styles) {
1235
+ if (style.close) {
1236
+ result = result.split(style.close).join(style.close + style.open);
1237
+ }
1238
+ }
1239
+ return `${open}${result}${close}`;
1240
+ }
1241
+
852
1242
  // src/plugins/optional/template.ts
1243
+ var STYLE_NAMES = /* @__PURE__ */ new Set([...MODIFIER_NAMES, ...COLOR_NAMES, ...BACKGROUND_COLOR_NAMES]);
1244
+ var BLOCK = /\{([A-Za-z]\w*(?:\.[A-Za-z]\w*)*) ([^{}]*)\}/g;
1245
+ var TOKEN = /\uE000(\d+)\uE001/g;
1246
+ function renderTemplate(root, strings, values) {
1247
+ const parts = values.map((value) => String(value));
1248
+ const token = (index) => `\uE000${index}\uE001`;
1249
+ const restore = (text) => text.replace(TOKEN, (_match, index) => String(parts[Number(index)]));
1250
+ let source = "";
1251
+ for (let i = 0; i < strings.length; i++) {
1252
+ source += strings[i];
1253
+ if (i < values.length) {
1254
+ source += token(i);
1255
+ }
1256
+ }
1257
+ let previous;
1258
+ do {
1259
+ previous = source;
1260
+ source = source.replace(BLOCK, (_match, chain, text) => {
1261
+ let styler = root;
1262
+ for (const name of chain.split(".")) {
1263
+ if (!STYLE_NAMES.has(name)) {
1264
+ throw new StyleError(`Unknown style in template: ${name}`);
1265
+ }
1266
+ styler = styler[name];
1267
+ }
1268
+ parts.push(styler(restore(text)));
1269
+ return token(parts.length - 1);
1270
+ });
1271
+ } while (source !== previous);
1272
+ return restore(source);
1273
+ }
853
1274
  function templatePlugin() {
854
1275
  return {
855
1276
  name: "template",
@@ -861,6 +1282,11 @@ function templatePlugin() {
861
1282
  enabled: true
862
1283
  }
863
1284
  });
1285
+ registerPluginApi(
1286
+ kernel,
1287
+ "template",
1288
+ (strings, ...values) => renderTemplate(getRoot(kernel) ?? createProxyPigment(k), strings, values)
1289
+ );
864
1290
  k.on("style:template", (data) => {
865
1291
  const { strings, values } = data;
866
1292
  let result = "";
@@ -903,9 +1329,27 @@ function parseStyleExpression(expression, text, kernel) {
903
1329
  return text;
904
1330
  }
905
1331
 
1332
+ // node_modules/@oxog/types/dist/index.js
1333
+ 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 || {});
1334
+ var u = class extends Error {
1335
+ constructor(t, n, s, p) {
1336
+ super(t, p);
1337
+ this.code = n;
1338
+ this.context = s;
1339
+ this.name = "OxogError", Object.setPrototypeOf(this, new.target.prototype), Error.captureStackTrace?.(this, new.target);
1340
+ }
1341
+ name;
1342
+ };
1343
+ var l = class extends u {
1344
+ constructor(t, n, s, p) {
1345
+ super(t, "PLUGIN_ERROR", { pluginName: n, ...s }, p);
1346
+ this.pluginName = n;
1347
+ this.name = "PluginError", Object.setPrototypeOf(this, new.target.prototype), Error.captureStackTrace?.(this, new.target);
1348
+ }
1349
+ name;
1350
+ };
1351
+
906
1352
  // node_modules/@oxog/plugin/dist/index.js
907
- var import_types = require("@oxog/types");
908
- var import_types2 = require("@oxog/types");
909
1353
  var x = ((i) => (i.Created = "created", i.Initializing = "initializing", i.Ready = "ready", i.Destroying = "destroying", i.Destroyed = "destroyed", i))(x || {});
910
1354
  var h = class {
911
1355
  context;
@@ -934,7 +1378,7 @@ var h = class {
934
1378
  return n;
935
1379
  }
936
1380
  };
937
- var u = class {
1381
+ var u2 = class {
938
1382
  handlers = /* @__PURE__ */ new Map();
939
1383
  wildcardHandlers = new Array();
940
1384
  patternHandlers = /* @__PURE__ */ new Map();
@@ -962,8 +1406,8 @@ var u = class {
962
1406
  off(e, t) {
963
1407
  let n = String(e), r = this.handlers.get(n);
964
1408
  if (r) {
965
- let i = t, a = this.onceWrappers.get(n), s = a?.get(i), o = s || i, l = r.indexOf(o);
966
- l !== -1 && (r.splice(l, 1), s && a.delete(i));
1409
+ let i = t, a = this.onceWrappers.get(n), s = a?.get(i), o = s || i, l2 = r.indexOf(o);
1410
+ l2 !== -1 && (r.splice(l2, 1), s && a.delete(i));
967
1411
  }
968
1412
  }
969
1413
  emit(e, t) {
@@ -1025,22 +1469,22 @@ var f = class {
1025
1469
  validateSinglePlugin(e) {
1026
1470
  let t = this.graph.get(e);
1027
1471
  if (!t) return;
1028
- for (let r of t) if (!this.graph.has(r)) throw new import_types.PluginError(`Missing dependency: '${r}' required by '${e}'`, e);
1472
+ for (let r of t) if (!this.graph.has(r)) throw new l(`Missing dependency: '${r}' required by '${e}'`, e);
1029
1473
  let n = this.detectCycle();
1030
- if (n && n.includes(e)) throw new import_types.PluginError(`Circular dependency detected: ${n.join(" -> ")}`, "dependency-resolver");
1474
+ if (n && n.includes(e)) throw new l(`Circular dependency detected: ${n.join(" -> ")}`, "dependency-resolver");
1031
1475
  }
1032
1476
  resolve() {
1033
1477
  let e = /* @__PURE__ */ new Map(), t = [], n = [], r = /* @__PURE__ */ new Map();
1034
1478
  for (let [s, o] of this.graph) {
1035
- let l = o.filter((p) => this.graph.has(p));
1036
- r.set(s, l);
1479
+ let l2 = o.filter((p) => this.graph.has(p));
1480
+ r.set(s, l2);
1037
1481
  }
1038
- for (let [s, o] of this.graph) for (let l of o) if (!this.graph.has(l)) throw new import_types.PluginError(`Missing dependency: '${l}' required by '${s}'`, s);
1482
+ 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);
1039
1483
  for (let [s, o] of r) e.set(s, o.length), o.length === 0 && t.push(s);
1040
1484
  let i = /* @__PURE__ */ new Map();
1041
- for (let [s, o] of r) for (let l of o) {
1042
- let p = i.get(l);
1043
- p ? p.push(s) : i.set(l, [s]);
1485
+ for (let [s, o] of r) for (let l2 of o) {
1486
+ let p = i.get(l2);
1487
+ p ? p.push(s) : i.set(l2, [s]);
1044
1488
  }
1045
1489
  let a = 0;
1046
1490
  for (; a < t.length; ) {
@@ -1050,7 +1494,7 @@ var f = class {
1050
1494
  }
1051
1495
  if (n.length !== r.size) {
1052
1496
  let s = this.detectCycle();
1053
- throw new import_types.PluginError(`Circular dependency detected: ${s.join(" -> ")}`, "dependency-resolver");
1497
+ throw new l(`Circular dependency detected: ${s.join(" -> ")}`, "dependency-resolver");
1054
1498
  }
1055
1499
  return n;
1056
1500
  }
@@ -1061,12 +1505,12 @@ var f = class {
1061
1505
  r.set(a, 1), s.push(a);
1062
1506
  for (let o of this.graph.get(a)) if (this.graph.has(o)) {
1063
1507
  if (r.get(o) === 1) {
1064
- let l = s.indexOf(o);
1065
- return [...s.slice(l), o];
1508
+ let l2 = s.indexOf(o);
1509
+ return [...s.slice(l2), o];
1066
1510
  }
1067
1511
  if (r.get(o) === 0) {
1068
- let l = i(o, s);
1069
- if (l) return l;
1512
+ let l2 = i(o, s);
1513
+ if (l2) return l2;
1070
1514
  }
1071
1515
  }
1072
1516
  return s.pop(), r.set(a, 2), null;
@@ -1139,8 +1583,8 @@ var m = class {
1139
1583
  await r.withBoundary(s, o.plugin, async () => {
1140
1584
  o.plugin.onInit && await o.plugin.onInit(n);
1141
1585
  }), o.state = "ready", o.initializedAt = Date.now(), i?.(s);
1142
- } catch (l) {
1143
- if (a === "fail-fast") throw l;
1586
+ } catch (l2) {
1587
+ if (a === "fail-fast") throw l2;
1144
1588
  o.state = "failed";
1145
1589
  }
1146
1590
  }
@@ -1156,8 +1600,8 @@ var m = class {
1156
1600
  await n.withBoundary(s, o.plugin, async () => {
1157
1601
  o.plugin.onDestroy && await o.plugin.onDestroy();
1158
1602
  });
1159
- } catch (l) {
1160
- if (i === "fail-fast") throw l;
1603
+ } catch (l2) {
1604
+ if (i === "fail-fast") throw l2;
1161
1605
  }
1162
1606
  o.state = "destroyed", r?.(s);
1163
1607
  }
@@ -1178,12 +1622,12 @@ var c = class {
1178
1622
  this.events.emit(e, t);
1179
1623
  }
1180
1624
  constructor(e) {
1181
- 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();
1625
+ 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();
1182
1626
  }
1183
1627
  use(e) {
1184
- if (this.state === "destroyed") throw new import_types.PluginError("Cannot add plugins to destroyed kernel", "kernel");
1185
- if (this.state === "initializing") throw new import_types.PluginError("Cannot add plugins while kernel is initializing. Register all plugins before calling init(), or wait until kernel is ready.", "kernel");
1186
- if (this.plugins.has(e.name)) throw new import_types.PluginError(`Plugin '${e.name}' is already registered`, "kernel");
1628
+ if (this.state === "destroyed") throw new l("Cannot add plugins to destroyed kernel", "kernel");
1629
+ 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");
1630
+ if (this.plugins.has(e.name)) throw new l(`Plugin '${e.name}' is already registered`, "kernel");
1187
1631
  let t = { plugin: e, state: "registered" };
1188
1632
  this.plugins.set(e.name, t), this.resolver.addPlugin(e.name, [...e.dependencies || []]);
1189
1633
  try {
@@ -1217,7 +1661,7 @@ var c = class {
1217
1661
  let i = this.plugins.get(r);
1218
1662
  if (!i || i.state !== "ready") {
1219
1663
  t.state = "failed";
1220
- let a = new import_types.PluginError(`Dependency '${r}' is not ready for plugin '${e}'`, e);
1664
+ let a = new l(`Dependency '${r}' is not ready for plugin '${e}'`, e);
1221
1665
  this.emitInternal("plugin:error", { name: e, error: a });
1222
1666
  return;
1223
1667
  }
@@ -1288,7 +1732,7 @@ var c = class {
1288
1732
  }
1289
1733
  }
1290
1734
  async init() {
1291
- if (this.state !== "created") throw new import_types.PluginError("Kernel already initialized", "kernel");
1735
+ if (this.state !== "created") throw new l("Kernel already initialized", "kernel");
1292
1736
  this.state = "initializing";
1293
1737
  try {
1294
1738
  this.config.onBeforeInit && await this.config.onBeforeInit();
@@ -1362,176 +1806,16 @@ function W(d) {
1362
1806
  return new c(d);
1363
1807
  }
1364
1808
 
1365
- // src/core/ansi.ts
1366
- function generateModifierCode(name) {
1367
- const modifier = ANSI_MODIFIERS[name];
1368
- if (!modifier) {
1369
- throw new StyleError(`Unknown modifier: ${name}`);
1370
- }
1371
- return {
1372
- name,
1373
- code: -1,
1374
- open: modifier.open,
1375
- close: modifier.close,
1376
- type: "modifier"
1377
- };
1378
- }
1379
- function generateColorCode(name, background = false) {
1380
- let normalizedName = name;
1381
- const lowerName = name.toLowerCase();
1382
- if (lowerName === "gray" || lowerName === "grey") {
1383
- normalizedName = "blackBright";
1384
- } else if (lowerName === "bggray" || lowerName === "bggrey") {
1385
- normalizedName = "bgBlackBright";
1386
- }
1387
- const foregroundColors = { ...ANSI_FOREGROUND_COLORS, ...ANSI_FOREGROUND_BRIGHT_COLORS };
1388
- const backgroundColors = { ...ANSI_BACKGROUND_COLORS, ...ANSI_BACKGROUND_BRIGHT_COLORS };
1389
- let lookupName = normalizedName;
1390
- if (background && !normalizedName.startsWith("bg")) {
1391
- lookupName = "bg" + normalizedName.charAt(0).toUpperCase() + normalizedName.slice(1);
1392
- }
1393
- const allColors = background ? backgroundColors : foregroundColors;
1394
- const code = allColors[lookupName];
1395
- if (code === void 0) {
1396
- throw new StyleError(`Unknown color: ${name}`);
1397
- }
1398
- const closeCode = background ? 49 : 39;
1399
- return {
1400
- name,
1401
- code,
1402
- open: `\x1B[${code}m`,
1403
- close: `\x1B[${closeCode}m`,
1404
- type: "color",
1405
- background
1406
- };
1407
- }
1408
- function generateAnsi256Code(code, background = false) {
1409
- if (code < 0 || code > 255) {
1410
- throw new StyleError(`ANSI 256 code must be between 0 and 255, got ${code}`);
1411
- }
1412
- const prefix = background ? 48 : 38;
1413
- const closeCode = background ? 49 : 39;
1414
- return {
1415
- name: `ansi256(${code})`,
1416
- code,
1417
- open: `\x1B[${prefix};5;${code}m`,
1418
- close: `\x1B[${closeCode}m`,
1419
- type: "color",
1420
- background
1421
- };
1422
- }
1423
- function generateRgbCode(r, g, b, background = false) {
1424
- if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) {
1425
- throw new StyleError(`RGB values must be between 0 and 255, got (${r}, ${g}, ${b})`);
1426
- }
1427
- const prefix = background ? 48 : 38;
1428
- const closeCode = background ? 49 : 39;
1429
- return {
1430
- name: `rgb(${r}, ${g}, ${b})`,
1431
- code: -1,
1432
- open: `\x1B[${prefix};2;${r};${g};${b}m`,
1433
- close: `\x1B[${closeCode}m`,
1434
- type: "color",
1435
- background
1436
- };
1437
- }
1438
-
1439
- // src/core/proxy.ts
1440
- var proxyCache = /* @__PURE__ */ new WeakMap();
1441
- function createProxyPigment(kernel, styles = []) {
1442
- const handler = {
1443
- get(target, prop) {
1444
- const ctx = kernel.getContext();
1445
- if (prop === "level") {
1446
- return ctx.level;
1447
- }
1448
- if (prop === "supportsColor") {
1449
- return ctx.colorSupport;
1450
- }
1451
- if (MODIFIER_NAMES.some((name) => name === prop)) {
1452
- const modifierStyle = generateModifierCode(prop);
1453
- return createProxyPigment(kernel, [...styles, modifierStyle]);
1454
- }
1455
- if (COLOR_NAMES.some((name) => name === prop) || BACKGROUND_COLOR_NAMES.some((name) => name === prop)) {
1456
- const background = prop.startsWith("bg");
1457
- const colorName = background ? prop.slice(2) : prop;
1458
- const colorStyle = generateColorCode(colorName, background);
1459
- return createProxyPigment(kernel, [...styles, colorStyle]);
1460
- }
1461
- if (EXTENDED_COLOR_METHODS.some((name) => name === prop)) {
1462
- if (prop === "ansi256" || prop === "bgAnsi256") {
1463
- return (code) => {
1464
- const background = prop === "bgAnsi256";
1465
- const ansi256Style = generateAnsi256Code(code, background);
1466
- return createProxyPigment(kernel, [...styles, ansi256Style]);
1467
- };
1468
- }
1469
- if (prop === "rgb" || prop === "bgRgb") {
1470
- return (r, g, b) => {
1471
- const background = prop === "bgRgb";
1472
- const rgbStyle = generateRgbCode(r, g, b, background);
1473
- return createProxyPigment(kernel, [...styles, rgbStyle]);
1474
- };
1475
- }
1476
- if (prop === "hex" || prop === "bgHex") {
1477
- return (color) => {
1478
- const background = prop === "bgHex";
1479
- const hexToRgb2 = ctx.utils?.hexToRgb;
1480
- if (!hexToRgb2) {
1481
- throw new Error("hexToRgb utility not available");
1482
- }
1483
- const { r, g, b } = hexToRgb2(color);
1484
- const rgbStyle = generateRgbCode(r, g, b, background);
1485
- return createProxyPigment(kernel, [...styles, rgbStyle]);
1486
- };
1487
- }
1488
- if (prop === "hsl" || prop === "bgHsl") {
1489
- return (h2, s, l) => {
1490
- const background = prop === "bgHsl";
1491
- const hslToRgb2 = ctx.utils?.hslToRgb;
1492
- if (!hslToRgb2) {
1493
- throw new Error("hslToRgb utility not available");
1494
- }
1495
- const { r, g, b } = hslToRgb2(h2, s, l);
1496
- const rgbStyle = generateRgbCode(r, g, b, background);
1497
- return createProxyPigment(kernel, [...styles, rgbStyle]);
1498
- };
1499
- }
1500
- }
1501
- if (prop === "visible") {
1502
- const colorSupport = ctx.colorSupport;
1503
- return (text) => colorSupport.level > 0 ? applyStyles(String(text), styles) : "";
1504
- }
1505
- return Reflect.get(target, prop);
1506
- },
1507
- apply(_target, _thisArg, argArray) {
1508
- const text = String(argArray[0] ?? "");
1509
- return applyStyles(text, styles);
1510
- }
1511
- };
1512
- const pigment = new Proxy((() => ""), handler);
1513
- proxyCache.set(pigment, pigment);
1514
- return pigment;
1515
- }
1516
- function applyStyles(text, styles) {
1517
- if (styles.length === 0 || text === "") {
1518
- return text;
1519
- }
1520
- const open = styles.map((style) => style.open).join("");
1521
- const close = [...styles].reverse().map((style) => style.close).join("");
1522
- let result = text;
1523
- for (const style of styles) {
1524
- if (style.close) {
1525
- result = result.split(style.close).join(style.close + style.open);
1526
- }
1527
- }
1528
- return `${open}${result}${close}`;
1529
- }
1530
-
1531
1809
  // src/core/builder.ts
1532
1810
  var BuilderPigment = class {
1533
1811
  styles = [];
1534
- constructor(_kernel) {
1812
+ kernel;
1813
+ constructor(kernel) {
1814
+ this.kernel = kernel;
1815
+ }
1816
+ /** The instance's color level (true color when there is no kernel). */
1817
+ get level() {
1818
+ return this.kernel?.getContext().level;
1535
1819
  }
1536
1820
  bold() {
1537
1821
  this.styles.push(generateModifierCode("bold"));
@@ -1710,26 +1994,26 @@ var BuilderPigment = class {
1710
1994
  return this;
1711
1995
  }
1712
1996
  ansi256(code) {
1713
- this.styles.push(generateAnsi256Code(code, false));
1997
+ this.styles.push(generateAnsi256CodeForLevel(code, false, this.level));
1714
1998
  return this;
1715
1999
  }
1716
2000
  bgAnsi256(code) {
1717
- this.styles.push(generateAnsi256Code(code, true));
2001
+ this.styles.push(generateAnsi256CodeForLevel(code, true, this.level));
1718
2002
  return this;
1719
2003
  }
1720
2004
  rgb(r, g, b) {
1721
- this.styles.push(generateRgbCode(r, g, b, false));
2005
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
1722
2006
  return this;
1723
2007
  }
1724
2008
  bgRgb(r, g, b) {
1725
- this.styles.push(generateRgbCode(r, g, b, true));
2009
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
1726
2010
  return this;
1727
2011
  }
1728
2012
  hex(color) {
1729
2013
  const rgb = hexToRgb(color);
1730
2014
  if (rgb) {
1731
2015
  const { r, g, b } = rgb;
1732
- this.styles.push(generateRgbCode(r, g, b, false));
2016
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
1733
2017
  }
1734
2018
  return this;
1735
2019
  }
@@ -1737,22 +2021,22 @@ var BuilderPigment = class {
1737
2021
  const rgb = hexToRgb(color);
1738
2022
  if (rgb) {
1739
2023
  const { r, g, b } = rgb;
1740
- this.styles.push(generateRgbCode(r, g, b, true));
2024
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
1741
2025
  }
1742
2026
  return this;
1743
2027
  }
1744
- hsl(h2, s, l) {
1745
- const { r, g, b } = hslToRgb(h2, s, l);
1746
- this.styles.push(generateRgbCode(r, g, b, false));
2028
+ hsl(h2, s, l2) {
2029
+ const { r, g, b } = hslToRgb(h2, s, l2);
2030
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
1747
2031
  return this;
1748
2032
  }
1749
- bgHsl(h2, s, l) {
1750
- const { r, g, b } = hslToRgb(h2, s, l);
1751
- this.styles.push(generateRgbCode(r, g, b, true));
2033
+ bgHsl(h2, s, l2) {
2034
+ const { r, g, b } = hslToRgb(h2, s, l2);
2035
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
1752
2036
  return this;
1753
2037
  }
1754
2038
  paint(text) {
1755
- if (this.styles.length === 0 || text === "") {
2039
+ if (this.styles.length === 0 || text === "" || this.level === 0) {
1756
2040
  return text;
1757
2041
  }
1758
2042
  const open = this.styles.map((style) => style.open).join("");
@@ -1767,23 +2051,6 @@ var BuilderPigment = class {
1767
2051
  }
1768
2052
  };
1769
2053
 
1770
- // src/utils/rgb-to-ansi256.ts
1771
- function rgbToAnsi256(r, g, b) {
1772
- const clampedR = Math.max(0, Math.min(255, r));
1773
- const clampedG = Math.max(0, Math.min(255, g));
1774
- const clampedB = Math.max(0, Math.min(255, b));
1775
- if (clampedR === clampedG && clampedG === clampedB) {
1776
- if (clampedR < 8) {
1777
- return 16;
1778
- }
1779
- if (clampedR > 248) {
1780
- return 231;
1781
- }
1782
- return Math.round((clampedR - 8) / 247 * 24) + 232;
1783
- }
1784
- return Math.round(clampedR / 51) * 36 + Math.round(clampedG / 51) * 6 + Math.round(clampedB / 51) + 16;
1785
- }
1786
-
1787
2054
  // src/core/factory.ts
1788
2055
  function createPigment(options) {
1789
2056
  const colorSupport = detectColorSupport(options?.level);
@@ -1813,13 +2080,12 @@ function createPigment(options) {
1813
2080
  kernel.use(environmentPlugin());
1814
2081
  if (options?.plugins) {
1815
2082
  for (const plugin of options.plugins) {
1816
- kernel.use(plugin);
2083
+ installPlugin(kernel, plugin);
1817
2084
  }
1818
2085
  }
1819
- void kernel.init();
1820
2086
  const pigment = createProxyPigment(kernel);
1821
- const builder = new BuilderPigment(kernel);
1822
- Object.defineProperty(pigment, "builder", { value: builder, enumerable: false });
2087
+ registerRoot(kernel, pigment);
2088
+ Object.defineProperty(pigment, "builder", { get: () => new BuilderPigment(kernel), enumerable: false });
1823
2089
  Object.defineProperty(pigment, "level", { value: level, enumerable: true });
1824
2090
  Object.defineProperty(pigment, "supportsColor", { value: colorSupport, enumerable: true });
1825
2091
  return pigment;