@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/dist/index.cjs CHANGED
@@ -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";
@@ -91,9 +91,27 @@ __export(index_exports, {
91
91
  });
92
92
  module.exports = __toCommonJS(index_exports);
93
93
 
94
+ // node_modules/@oxog/types/dist/index.js
95
+ 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 || {});
96
+ var u = class extends Error {
97
+ constructor(t, n, s, p) {
98
+ super(t, p);
99
+ this.code = n;
100
+ this.context = s;
101
+ this.name = "OxogError", Object.setPrototypeOf(this, new.target.prototype), Error.captureStackTrace?.(this, new.target);
102
+ }
103
+ name;
104
+ };
105
+ var l = class extends u {
106
+ constructor(t, n, s, p) {
107
+ super(t, "PLUGIN_ERROR", { pluginName: n, ...s }, p);
108
+ this.pluginName = n;
109
+ this.name = "PluginError", Object.setPrototypeOf(this, new.target.prototype), Error.captureStackTrace?.(this, new.target);
110
+ }
111
+ name;
112
+ };
113
+
94
114
  // node_modules/@oxog/plugin/dist/index.js
95
- var import_types = require("@oxog/types");
96
- var import_types2 = require("@oxog/types");
97
115
  var x = ((i) => (i.Created = "created", i.Initializing = "initializing", i.Ready = "ready", i.Destroying = "destroying", i.Destroyed = "destroyed", i))(x || {});
98
116
  var h = class {
99
117
  context;
@@ -122,7 +140,7 @@ var h = class {
122
140
  return n;
123
141
  }
124
142
  };
125
- var u = class {
143
+ var u2 = class {
126
144
  handlers = /* @__PURE__ */ new Map();
127
145
  wildcardHandlers = new Array();
128
146
  patternHandlers = /* @__PURE__ */ new Map();
@@ -150,8 +168,8 @@ var u = class {
150
168
  off(e, t) {
151
169
  let n = String(e), r = this.handlers.get(n);
152
170
  if (r) {
153
- let i = t, a = this.onceWrappers.get(n), s = a?.get(i), o = s || i, l = r.indexOf(o);
154
- l !== -1 && (r.splice(l, 1), s && a.delete(i));
171
+ let i = t, a = this.onceWrappers.get(n), s = a?.get(i), o = s || i, l2 = r.indexOf(o);
172
+ l2 !== -1 && (r.splice(l2, 1), s && a.delete(i));
155
173
  }
156
174
  }
157
175
  emit(e, t) {
@@ -213,22 +231,22 @@ var f = class {
213
231
  validateSinglePlugin(e) {
214
232
  let t = this.graph.get(e);
215
233
  if (!t) return;
216
- for (let r of t) if (!this.graph.has(r)) throw new import_types.PluginError(`Missing dependency: '${r}' required by '${e}'`, e);
234
+ for (let r of t) if (!this.graph.has(r)) throw new l(`Missing dependency: '${r}' required by '${e}'`, e);
217
235
  let n = this.detectCycle();
218
- if (n && n.includes(e)) throw new import_types.PluginError(`Circular dependency detected: ${n.join(" -> ")}`, "dependency-resolver");
236
+ if (n && n.includes(e)) throw new l(`Circular dependency detected: ${n.join(" -> ")}`, "dependency-resolver");
219
237
  }
220
238
  resolve() {
221
239
  let e = /* @__PURE__ */ new Map(), t = [], n = [], r = /* @__PURE__ */ new Map();
222
240
  for (let [s, o] of this.graph) {
223
- let l = o.filter((p) => this.graph.has(p));
224
- r.set(s, l);
241
+ let l2 = o.filter((p) => this.graph.has(p));
242
+ r.set(s, l2);
225
243
  }
226
- 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);
244
+ 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);
227
245
  for (let [s, o] of r) e.set(s, o.length), o.length === 0 && t.push(s);
228
246
  let i = /* @__PURE__ */ new Map();
229
- for (let [s, o] of r) for (let l of o) {
230
- let p = i.get(l);
231
- p ? p.push(s) : i.set(l, [s]);
247
+ for (let [s, o] of r) for (let l2 of o) {
248
+ let p = i.get(l2);
249
+ p ? p.push(s) : i.set(l2, [s]);
232
250
  }
233
251
  let a = 0;
234
252
  for (; a < t.length; ) {
@@ -238,7 +256,7 @@ var f = class {
238
256
  }
239
257
  if (n.length !== r.size) {
240
258
  let s = this.detectCycle();
241
- throw new import_types.PluginError(`Circular dependency detected: ${s.join(" -> ")}`, "dependency-resolver");
259
+ throw new l(`Circular dependency detected: ${s.join(" -> ")}`, "dependency-resolver");
242
260
  }
243
261
  return n;
244
262
  }
@@ -249,12 +267,12 @@ var f = class {
249
267
  r.set(a, 1), s.push(a);
250
268
  for (let o of this.graph.get(a)) if (this.graph.has(o)) {
251
269
  if (r.get(o) === 1) {
252
- let l = s.indexOf(o);
253
- return [...s.slice(l), o];
270
+ let l2 = s.indexOf(o);
271
+ return [...s.slice(l2), o];
254
272
  }
255
273
  if (r.get(o) === 0) {
256
- let l = i(o, s);
257
- if (l) return l;
274
+ let l2 = i(o, s);
275
+ if (l2) return l2;
258
276
  }
259
277
  }
260
278
  return s.pop(), r.set(a, 2), null;
@@ -327,8 +345,8 @@ var m = class {
327
345
  await r.withBoundary(s, o.plugin, async () => {
328
346
  o.plugin.onInit && await o.plugin.onInit(n);
329
347
  }), o.state = "ready", o.initializedAt = Date.now(), i?.(s);
330
- } catch (l) {
331
- if (a === "fail-fast") throw l;
348
+ } catch (l2) {
349
+ if (a === "fail-fast") throw l2;
332
350
  o.state = "failed";
333
351
  }
334
352
  }
@@ -344,8 +362,8 @@ var m = class {
344
362
  await n.withBoundary(s, o.plugin, async () => {
345
363
  o.plugin.onDestroy && await o.plugin.onDestroy();
346
364
  });
347
- } catch (l) {
348
- if (i === "fail-fast") throw l;
365
+ } catch (l2) {
366
+ if (i === "fail-fast") throw l2;
349
367
  }
350
368
  o.state = "destroyed", r?.(s);
351
369
  }
@@ -366,12 +384,12 @@ var c = class {
366
384
  this.events.emit(e, t);
367
385
  }
368
386
  constructor(e) {
369
- 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();
387
+ 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();
370
388
  }
371
389
  use(e) {
372
- if (this.state === "destroyed") throw new import_types.PluginError("Cannot add plugins to destroyed kernel", "kernel");
373
- 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");
374
- if (this.plugins.has(e.name)) throw new import_types.PluginError(`Plugin '${e.name}' is already registered`, "kernel");
390
+ if (this.state === "destroyed") throw new l("Cannot add plugins to destroyed kernel", "kernel");
391
+ 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");
392
+ if (this.plugins.has(e.name)) throw new l(`Plugin '${e.name}' is already registered`, "kernel");
375
393
  let t = { plugin: e, state: "registered" };
376
394
  this.plugins.set(e.name, t), this.resolver.addPlugin(e.name, [...e.dependencies || []]);
377
395
  try {
@@ -405,7 +423,7 @@ var c = class {
405
423
  let i = this.plugins.get(r);
406
424
  if (!i || i.state !== "ready") {
407
425
  t.state = "failed";
408
- let a = new import_types.PluginError(`Dependency '${r}' is not ready for plugin '${e}'`, e);
426
+ let a = new l(`Dependency '${r}' is not ready for plugin '${e}'`, e);
409
427
  this.emitInternal("plugin:error", { name: e, error: a });
410
428
  return;
411
429
  }
@@ -476,7 +494,7 @@ var c = class {
476
494
  }
477
495
  }
478
496
  async init() {
479
- if (this.state !== "created") throw new import_types.PluginError("Kernel already initialized", "kernel");
497
+ if (this.state !== "created") throw new l("Kernel already initialized", "kernel");
480
498
  this.state = "initializing";
481
499
  try {
482
500
  this.config.onBeforeInit && await this.config.onBeforeInit();
@@ -771,6 +789,12 @@ var PigmentError = class _PigmentError extends Error {
771
789
  Error.captureStackTrace(this, _PigmentError);
772
790
  }
773
791
  };
792
+ var PluginError = class extends PigmentError {
793
+ constructor(pluginName, message) {
794
+ super(`[${pluginName}] ${message}`, "PLUGIN_ERROR");
795
+ this.name = "PluginError";
796
+ }
797
+ };
774
798
  var ColorError = class extends PigmentError {
775
799
  constructor(message) {
776
800
  super(message, "COLOR_ERROR");
@@ -784,6 +808,23 @@ var StyleError = class extends PigmentError {
784
808
  }
785
809
  };
786
810
 
811
+ // src/utils/rgb-to-ansi256.ts
812
+ function rgbToAnsi256(r, g, b) {
813
+ const clampedR = Math.max(0, Math.min(255, r));
814
+ const clampedG = Math.max(0, Math.min(255, g));
815
+ const clampedB = Math.max(0, Math.min(255, b));
816
+ if (clampedR === clampedG && clampedG === clampedB) {
817
+ if (clampedR < 8) {
818
+ return 16;
819
+ }
820
+ if (clampedR > 248) {
821
+ return 231;
822
+ }
823
+ return Math.round((clampedR - 8) / 247 * 24) + 232;
824
+ }
825
+ return Math.round(clampedR / 51) * 36 + Math.round(clampedG / 51) * 6 + Math.round(clampedB / 51) + 16;
826
+ }
827
+
787
828
  // src/core/ansi.ts
788
829
  function generateModifierCode(name) {
789
830
  const modifier = ANSI_MODIFIERS[name];
@@ -857,6 +898,144 @@ function generateRgbCode(r, g, b, background = false) {
857
898
  background
858
899
  };
859
900
  }
901
+ function ansi256ToAnsi16(code) {
902
+ if (code < 8) {
903
+ return 30 + code;
904
+ }
905
+ if (code < 16) {
906
+ return 90 + (code - 8);
907
+ }
908
+ let red2;
909
+ let green2;
910
+ let blue2;
911
+ if (code >= 232) {
912
+ red2 = ((code - 232) * 10 + 8) / 255;
913
+ green2 = red2;
914
+ blue2 = red2;
915
+ } else {
916
+ const index = code - 16;
917
+ const remainder = index % 36;
918
+ red2 = Math.floor(index / 36) / 5;
919
+ green2 = Math.floor(remainder / 6) / 5;
920
+ blue2 = remainder % 6 / 5;
921
+ }
922
+ const value = Math.max(red2, green2, blue2) * 2;
923
+ if (value === 0) {
924
+ return 30;
925
+ }
926
+ let result = 30 + (Math.round(blue2) << 2 | Math.round(green2) << 1 | Math.round(red2));
927
+ if (value === 2) {
928
+ result += 60;
929
+ }
930
+ return result;
931
+ }
932
+ function effectiveLevel(level) {
933
+ return level ?? 3;
934
+ }
935
+ function ansi16Style(name, paletteCode, background) {
936
+ const code = ansi256ToAnsi16(paletteCode) + (background ? 10 : 0);
937
+ return {
938
+ name,
939
+ code,
940
+ open: `\x1B[${code}m`,
941
+ close: `\x1B[${background ? 49 : 39}m`,
942
+ type: "color",
943
+ background
944
+ };
945
+ }
946
+ function generateAnsi256CodeForLevel(code, background, level) {
947
+ const style = generateAnsi256Code(code, background);
948
+ if (effectiveLevel(level) >= 2) {
949
+ return style;
950
+ }
951
+ return ansi16Style(style.name, code, background);
952
+ }
953
+ function generateRgbCodeForLevel(r, g, b, background, level) {
954
+ const style = generateRgbCode(r, g, b, background);
955
+ const effective = effectiveLevel(level);
956
+ if (effective >= 3) {
957
+ return style;
958
+ }
959
+ const paletteCode = rgbToAnsi256(r, g, b);
960
+ if (effective === 2) {
961
+ return { ...generateAnsi256Code(paletteCode, background), name: style.name };
962
+ }
963
+ return ansi16Style(style.name, paletteCode, background);
964
+ }
965
+ function paintRgb(text, rgb, background, level) {
966
+ if (level === 0 || text === "") {
967
+ return text;
968
+ }
969
+ const style = generateRgbCodeForLevel(rgb.r, rgb.g, rgb.b, background, level);
970
+ return `${style.open}${text}${style.close}`;
971
+ }
972
+
973
+ // src/core/plugin-api.ts
974
+ var REGISTRY_KEY = /* @__PURE__ */ Symbol.for("@oxog/pigment.plugin-api");
975
+ var globalRegistry = globalThis;
976
+ var registry = globalRegistry[REGISTRY_KEY] ??= { apis: /* @__PURE__ */ new WeakMap(), roots: /* @__PURE__ */ new WeakMap() };
977
+ var { apis, roots } = registry;
978
+ var PLUGIN_API_PROVIDERS = {
979
+ gradient: "gradientPlugin",
980
+ rainbow: "gradientPlugin",
981
+ theme: "themePlugin",
982
+ success: "semanticPlugin",
983
+ error: "semanticPlugin",
984
+ warning: "semanticPlugin",
985
+ info: "semanticPlugin",
986
+ debug: "semanticPlugin",
987
+ box: "boxPlugin",
988
+ template: "templatePlugin"
989
+ };
990
+ function isPluginApiName(name) {
991
+ return Object.prototype.hasOwnProperty.call(PLUGIN_API_PROVIDERS, name);
992
+ }
993
+ function registerPluginApi(kernel, name, value) {
994
+ let api = apis.get(kernel);
995
+ if (!api) {
996
+ api = /* @__PURE__ */ new Map();
997
+ apis.set(kernel, api);
998
+ }
999
+ api.set(name, value);
1000
+ }
1001
+ function getPluginApi(kernel, name) {
1002
+ return apis.get(kernel)?.get(name);
1003
+ }
1004
+ function registerRoot(kernel, root) {
1005
+ roots.set(kernel, root);
1006
+ }
1007
+ function getRoot(kernel) {
1008
+ return roots.get(kernel);
1009
+ }
1010
+ function missingPluginApi(name) {
1011
+ const plugin = PLUGIN_API_PROVIDERS[name];
1012
+ const fail = () => {
1013
+ throw new PluginError(
1014
+ plugin,
1015
+ `pigment.${name} needs ${plugin}(): createPigment({ plugins: [${plugin}()] }) or pigment.use(${plugin}())`
1016
+ );
1017
+ };
1018
+ if (name === "theme") {
1019
+ return {
1020
+ keyword: fail,
1021
+ string: fail,
1022
+ number: fail,
1023
+ comment: fail,
1024
+ error: fail,
1025
+ success: fail,
1026
+ warning: fail,
1027
+ info: fail
1028
+ };
1029
+ }
1030
+ return fail;
1031
+ }
1032
+ function installPlugin(kernel, plugin) {
1033
+ kernel.use(plugin);
1034
+ if (plugin.onInit) {
1035
+ Promise.resolve().then(() => plugin.onInit?.(kernel.getContext())).catch(() => {
1036
+ });
1037
+ }
1038
+ }
860
1039
 
861
1040
  // src/core/proxy.ts
862
1041
  var proxyCache = /* @__PURE__ */ new WeakMap();
@@ -884,14 +1063,14 @@ function createProxyPigment(kernel, styles = []) {
884
1063
  if (prop === "ansi256" || prop === "bgAnsi256") {
885
1064
  return (code) => {
886
1065
  const background = prop === "bgAnsi256";
887
- const ansi256Style = generateAnsi256Code(code, background);
1066
+ const ansi256Style = generateAnsi256CodeForLevel(code, background, ctx.level);
888
1067
  return createProxyPigment(kernel, [...styles, ansi256Style]);
889
1068
  };
890
1069
  }
891
1070
  if (prop === "rgb" || prop === "bgRgb") {
892
1071
  return (r, g, b) => {
893
1072
  const background = prop === "bgRgb";
894
- const rgbStyle = generateRgbCode(r, g, b, background);
1073
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
895
1074
  return createProxyPigment(kernel, [...styles, rgbStyle]);
896
1075
  };
897
1076
  }
@@ -903,40 +1082,49 @@ function createProxyPigment(kernel, styles = []) {
903
1082
  throw new Error("hexToRgb utility not available");
904
1083
  }
905
1084
  const { r, g, b } = hexToRgb2(color);
906
- const rgbStyle = generateRgbCode(r, g, b, background);
1085
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
907
1086
  return createProxyPigment(kernel, [...styles, rgbStyle]);
908
1087
  };
909
1088
  }
910
1089
  if (prop === "hsl" || prop === "bgHsl") {
911
- return (h2, s, l) => {
1090
+ return (h2, s, l2) => {
912
1091
  const background = prop === "bgHsl";
913
1092
  const hslToRgb2 = ctx.utils?.hslToRgb;
914
1093
  if (!hslToRgb2) {
915
1094
  throw new Error("hslToRgb utility not available");
916
1095
  }
917
- const { r, g, b } = hslToRgb2(h2, s, l);
918
- const rgbStyle = generateRgbCode(r, g, b, background);
1096
+ const { r, g, b } = hslToRgb2(h2, s, l2);
1097
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
919
1098
  return createProxyPigment(kernel, [...styles, rgbStyle]);
920
1099
  };
921
1100
  }
922
1101
  }
923
1102
  if (prop === "visible") {
924
1103
  const colorSupport = ctx.colorSupport;
925
- return (text) => colorSupport.level > 0 ? applyStyles(String(text), styles) : "";
1104
+ return (text) => colorSupport.level > 0 ? applyStyles(String(text), styles, ctx.level) : "";
1105
+ }
1106
+ if (prop === "use") {
1107
+ return (plugin) => {
1108
+ installPlugin(kernel, plugin);
1109
+ return getRoot(kernel) ?? createProxyPigment(kernel);
1110
+ };
1111
+ }
1112
+ if (isPluginApiName(prop)) {
1113
+ return getPluginApi(kernel, prop) ?? missingPluginApi(prop);
926
1114
  }
927
1115
  return Reflect.get(target, prop);
928
1116
  },
929
1117
  apply(_target, _thisArg, argArray) {
930
1118
  const text = String(argArray[0] ?? "");
931
- return applyStyles(text, styles);
1119
+ return applyStyles(text, styles, kernel.getContext().level);
932
1120
  }
933
1121
  };
934
1122
  const pigment2 = new Proxy((() => ""), handler);
935
1123
  proxyCache.set(pigment2, pigment2);
936
1124
  return pigment2;
937
1125
  }
938
- function applyStyles(text, styles) {
939
- if (styles.length === 0 || text === "") {
1126
+ function applyStyles(text, styles, level) {
1127
+ if (styles.length === 0 || text === "" || level === 0) {
940
1128
  return text;
941
1129
  }
942
1130
  const open = styles.map((style) => style.open).join("");
@@ -981,10 +1169,10 @@ function validateHex(hex) {
981
1169
  }
982
1170
 
983
1171
  // src/utils/hsl-to-rgb.ts
984
- function hslToRgb(h2, s, l) {
1172
+ function hslToRgb(h2, s, l2) {
985
1173
  const hNormalized = h2 / 360;
986
1174
  const sNormalized = s / 100;
987
- const lNormalized = l / 100;
1175
+ const lNormalized = l2 / 100;
988
1176
  let r;
989
1177
  let g;
990
1178
  let b;
@@ -1015,7 +1203,13 @@ function hslToRgb(h2, s, l) {
1015
1203
  // src/core/builder.ts
1016
1204
  var BuilderPigment = class {
1017
1205
  styles = [];
1018
- constructor(_kernel) {
1206
+ kernel;
1207
+ constructor(kernel) {
1208
+ this.kernel = kernel;
1209
+ }
1210
+ /** The instance's color level (true color when there is no kernel). */
1211
+ get level() {
1212
+ return this.kernel?.getContext().level;
1019
1213
  }
1020
1214
  bold() {
1021
1215
  this.styles.push(generateModifierCode("bold"));
@@ -1194,26 +1388,26 @@ var BuilderPigment = class {
1194
1388
  return this;
1195
1389
  }
1196
1390
  ansi256(code) {
1197
- this.styles.push(generateAnsi256Code(code, false));
1391
+ this.styles.push(generateAnsi256CodeForLevel(code, false, this.level));
1198
1392
  return this;
1199
1393
  }
1200
1394
  bgAnsi256(code) {
1201
- this.styles.push(generateAnsi256Code(code, true));
1395
+ this.styles.push(generateAnsi256CodeForLevel(code, true, this.level));
1202
1396
  return this;
1203
1397
  }
1204
1398
  rgb(r, g, b) {
1205
- this.styles.push(generateRgbCode(r, g, b, false));
1399
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
1206
1400
  return this;
1207
1401
  }
1208
1402
  bgRgb(r, g, b) {
1209
- this.styles.push(generateRgbCode(r, g, b, true));
1403
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
1210
1404
  return this;
1211
1405
  }
1212
1406
  hex(color) {
1213
1407
  const rgb = hexToRgb(color);
1214
1408
  if (rgb) {
1215
1409
  const { r, g, b } = rgb;
1216
- this.styles.push(generateRgbCode(r, g, b, false));
1410
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
1217
1411
  }
1218
1412
  return this;
1219
1413
  }
@@ -1221,22 +1415,22 @@ var BuilderPigment = class {
1221
1415
  const rgb = hexToRgb(color);
1222
1416
  if (rgb) {
1223
1417
  const { r, g, b } = rgb;
1224
- this.styles.push(generateRgbCode(r, g, b, true));
1418
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
1225
1419
  }
1226
1420
  return this;
1227
1421
  }
1228
- hsl(h2, s, l) {
1229
- const { r, g, b } = hslToRgb(h2, s, l);
1230
- this.styles.push(generateRgbCode(r, g, b, false));
1422
+ hsl(h2, s, l2) {
1423
+ const { r, g, b } = hslToRgb(h2, s, l2);
1424
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
1231
1425
  return this;
1232
1426
  }
1233
- bgHsl(h2, s, l) {
1234
- const { r, g, b } = hslToRgb(h2, s, l);
1235
- this.styles.push(generateRgbCode(r, g, b, true));
1427
+ bgHsl(h2, s, l2) {
1428
+ const { r, g, b } = hslToRgb(h2, s, l2);
1429
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
1236
1430
  return this;
1237
1431
  }
1238
1432
  paint(text) {
1239
- if (this.styles.length === 0 || text === "") {
1433
+ if (this.styles.length === 0 || text === "" || this.level === 0) {
1240
1434
  return text;
1241
1435
  }
1242
1436
  const open = this.styles.map((style) => style.open).join("");
@@ -1361,23 +1555,6 @@ function toColorSupport(level) {
1361
1555
  return { level, hasBasic: level >= 1, has256: level >= 2, has16m: level >= 3 };
1362
1556
  }
1363
1557
 
1364
- // src/utils/rgb-to-ansi256.ts
1365
- function rgbToAnsi256(r, g, b) {
1366
- const clampedR = Math.max(0, Math.min(255, r));
1367
- const clampedG = Math.max(0, Math.min(255, g));
1368
- const clampedB = Math.max(0, Math.min(255, b));
1369
- if (clampedR === clampedG && clampedG === clampedB) {
1370
- if (clampedR < 8) {
1371
- return 16;
1372
- }
1373
- if (clampedR > 248) {
1374
- return 231;
1375
- }
1376
- return Math.round((clampedR - 8) / 247 * 24) + 232;
1377
- }
1378
- return Math.round(clampedR / 51) * 36 + Math.round(clampedG / 51) * 6 + Math.round(clampedB / 51) + 16;
1379
- }
1380
-
1381
1558
  // src/plugins/core/base-colors.ts
1382
1559
  function baseColorsPlugin() {
1383
1560
  return {
@@ -1487,11 +1664,11 @@ function trueColorPlugin() {
1487
1664
  k.emit("color:rgb", { ...rgb, background });
1488
1665
  });
1489
1666
  k.on("color:hsl", (data) => {
1490
- const { h: h2, s, l, background } = data;
1667
+ const { h: h2, s, l: l2, background } = data;
1491
1668
  if (!utils?.hslToRgb) {
1492
1669
  throw new Error("hslToRgb utility not available");
1493
1670
  }
1494
- const rgb = utils.hslToRgb(h2, s, l);
1671
+ const rgb = utils.hslToRgb(h2, s, l2);
1495
1672
  k.emit("color:rgb", { ...rgb, background });
1496
1673
  });
1497
1674
  }
@@ -1593,13 +1770,12 @@ function createPigment(options) {
1593
1770
  kernel.use(environmentPlugin());
1594
1771
  if (options?.plugins) {
1595
1772
  for (const plugin of options.plugins) {
1596
- kernel.use(plugin);
1773
+ installPlugin(kernel, plugin);
1597
1774
  }
1598
1775
  }
1599
- void kernel.init();
1600
1776
  const pigment2 = createProxyPigment(kernel);
1601
- const builder = new BuilderPigment(kernel);
1602
- Object.defineProperty(pigment2, "builder", { value: builder, enumerable: false });
1777
+ registerRoot(kernel, pigment2);
1778
+ Object.defineProperty(pigment2, "builder", { get: () => new BuilderPigment(kernel), enumerable: false });
1603
1779
  Object.defineProperty(pigment2, "level", { value: level, enumerable: true });
1604
1780
  Object.defineProperty(pigment2, "supportsColor", { value: colorSupport, enumerable: true });
1605
1781
  return pigment2;
@@ -1607,170 +1783,55 @@ function createPigment(options) {
1607
1783
 
1608
1784
  // src/core/chalk.ts
1609
1785
  var Chalk = class {
1610
- level;
1611
1786
  supportsColor;
1787
+ currentLevel;
1612
1788
  pigment;
1613
1789
  constructor(options) {
1614
1790
  const colorSupport = detectColorSupport(options?.level);
1615
- this.level = options?.level ?? colorSupport.level;
1791
+ this.currentLevel = options?.level ?? colorSupport.level;
1616
1792
  this.supportsColor = colorSupport;
1617
- this.pigment = createPigment({ level: this.level });
1618
- }
1619
- red(text) {
1620
- return this.pigment.red(text);
1621
- }
1622
- green(text) {
1623
- return this.pigment.green(text);
1624
- }
1625
- yellow(text) {
1626
- return this.pigment.yellow(text);
1627
- }
1628
- blue(text) {
1629
- return this.pigment.blue(text);
1630
- }
1631
- magenta(text) {
1632
- return this.pigment.magenta(text);
1633
- }
1634
- cyan(text) {
1635
- return this.pigment.cyan(text);
1636
- }
1637
- white(text) {
1638
- return this.pigment.white(text);
1639
- }
1640
- black(text) {
1641
- return this.pigment.black(text);
1642
- }
1643
- redBright(text) {
1644
- return this.pigment.redBright(text);
1645
- }
1646
- greenBright(text) {
1647
- return this.pigment.greenBright(text);
1648
- }
1649
- yellowBright(text) {
1650
- return this.pigment.yellowBright(text);
1651
- }
1652
- blueBright(text) {
1653
- return this.pigment.blueBright(text);
1654
- }
1655
- magentaBright(text) {
1656
- return this.pigment.magentaBright(text);
1657
- }
1658
- cyanBright(text) {
1659
- return this.pigment.cyanBright(text);
1660
- }
1661
- whiteBright(text) {
1662
- return this.pigment.whiteBright(text);
1663
- }
1664
- gray(text) {
1665
- return this.pigment.gray(text);
1666
- }
1667
- grey(text) {
1668
- return this.pigment.grey(text);
1669
- }
1670
- bgRed(text) {
1671
- return this.pigment.bgRed(text);
1672
- }
1673
- bgGreen(text) {
1674
- return this.pigment.bgGreen(text);
1675
- }
1676
- bgYellow(text) {
1677
- return this.pigment.bgYellow(text);
1678
- }
1679
- bgBlue(text) {
1680
- return this.pigment.bgBlue(text);
1681
- }
1682
- bgMagenta(text) {
1683
- return this.pigment.bgMagenta(text);
1684
- }
1685
- bgCyan(text) {
1686
- return this.pigment.bgCyan(text);
1687
- }
1688
- bgWhite(text) {
1689
- return this.pigment.bgWhite(text);
1690
- }
1691
- bgBlack(text) {
1692
- return this.pigment.bgBlack(text);
1693
- }
1694
- bgRedBright(text) {
1695
- return this.pigment.bgRedBright(text);
1696
- }
1697
- bgGreenBright(text) {
1698
- return this.pigment.bgGreenBright(text);
1699
- }
1700
- bgYellowBright(text) {
1701
- return this.pigment.bgYellowBright(text);
1702
- }
1703
- bgBlueBright(text) {
1704
- return this.pigment.bgBlueBright(text);
1705
- }
1706
- bgMagentaBright(text) {
1707
- return this.pigment.bgMagentaBright(text);
1708
- }
1709
- bgCyanBright(text) {
1710
- return this.pigment.bgCyanBright(text);
1711
- }
1712
- bgWhiteBright(text) {
1713
- return this.pigment.bgWhiteBright(text);
1793
+ this.pigment = createPigment({ level: this.currentLevel });
1714
1794
  }
1715
- bgGray(text) {
1716
- return this.pigment.bgGray(text);
1795
+ /** Color level (0-3). Setting it changes the output of every style. */
1796
+ get level() {
1797
+ return this.currentLevel;
1717
1798
  }
1718
- bgGrey(text) {
1719
- return this.pigment.bgGrey(text);
1799
+ set level(level) {
1800
+ this.currentLevel = level;
1801
+ this.pigment = createPigment({ level });
1720
1802
  }
1721
- bold(text) {
1722
- return this.pigment.bold(text);
1723
- }
1724
- dim(text) {
1725
- return this.pigment.dim(text);
1726
- }
1727
- italic(text) {
1728
- return this.pigment.italic(text);
1729
- }
1730
- underline(text) {
1731
- return this.pigment.underline(text);
1732
- }
1733
- strikethrough(text) {
1734
- return this.pigment.strikethrough(text);
1735
- }
1736
- inverse(text) {
1737
- return this.pigment.inverse(text);
1738
- }
1739
- hidden(text) {
1740
- return this.pigment.hidden(text);
1741
- }
1742
- reset(text) {
1743
- return this.pigment.reset(text);
1744
- }
1745
- /** Like chalk: returns the text only when colors are supported, otherwise ''. */
1803
+ /** Like chalk: returns the text only when colors are enabled, otherwise ''. */
1746
1804
  visible(text) {
1747
- return this.supportsColor.level > 0 ? text : "";
1805
+ return this.currentLevel > 0 ? text : "";
1748
1806
  }
1749
1807
  rgb(r, g, b) {
1750
- const coloredPigment = this.pigment.rgb(r, g, b);
1751
- return (text) => coloredPigment(text);
1808
+ return this.pigment.rgb(r, g, b);
1752
1809
  }
1753
1810
  bgRgb(r, g, b) {
1754
- const coloredPigment = this.pigment.bgRgb(r, g, b);
1755
- return (text) => coloredPigment(text);
1811
+ return this.pigment.bgRgb(r, g, b);
1756
1812
  }
1757
1813
  hex(color) {
1758
- const coloredPigment = this.pigment.hex(color);
1759
- return (text) => coloredPigment(text);
1814
+ return this.pigment.hex(color);
1760
1815
  }
1761
1816
  bgHex(color) {
1762
- const coloredPigment = this.pigment.bgHex(color);
1763
- return (text) => coloredPigment(text);
1817
+ return this.pigment.bgHex(color);
1764
1818
  }
1765
1819
  ansi256(code) {
1766
- const coloredPigment = this.pigment.ansi256(code);
1767
- return (text) => coloredPigment(text);
1820
+ return this.pigment.ansi256(code);
1768
1821
  }
1769
1822
  bgAnsi256(code) {
1770
- const coloredPigment = this.pigment.bgAnsi256(code);
1771
- return (text) => coloredPigment(text);
1823
+ return this.pigment.bgAnsi256(code);
1772
1824
  }
1773
1825
  };
1826
+ for (const name of [...MODIFIER_NAMES, ...COLOR_NAMES, ...BACKGROUND_COLOR_NAMES]) {
1827
+ Object.defineProperty(Chalk.prototype, name, {
1828
+ configurable: true,
1829
+ enumerable: false,
1830
+ get() {
1831
+ return this.pigment[name];
1832
+ }
1833
+ });
1834
+ }
1774
1835
 
1775
1836
  // src/core/chalk-default.ts
1776
1837
  var chalk = new Chalk();
@@ -1786,48 +1847,51 @@ function applyStyle(text, open, close) {
1786
1847
  }
1787
1848
 
1788
1849
  // src/core/composer.ts
1789
- var bold = (text) => applyStyle(text, "\x1B[1m", "\x1B[22m");
1790
- var dim = (text) => applyStyle(text, "\x1B[2m", "\x1B[22m");
1791
- var italic = (text) => applyStyle(text, "\x1B[3m", "\x1B[23m");
1792
- var underline = (text) => applyStyle(text, "\x1B[4m", "\x1B[24m");
1793
- var strikethrough = (text) => applyStyle(text, "\x1B[9m", "\x1B[29m");
1794
- var inverse = (text) => applyStyle(text, "\x1B[7m", "\x1B[27m");
1795
- var hidden = (text) => applyStyle(text, "\x1B[8m", "\x1B[28m");
1796
- var reset = (text) => applyStyle(text, "\x1B[0m", "\x1B[0m");
1797
- var black = (text) => applyStyle(text, "\x1B[30m", "\x1B[39m");
1798
- var red = (text) => applyStyle(text, "\x1B[31m", "\x1B[39m");
1799
- var green = (text) => applyStyle(text, "\x1B[32m", "\x1B[39m");
1800
- var yellow = (text) => applyStyle(text, "\x1B[33m", "\x1B[39m");
1801
- var blue = (text) => applyStyle(text, "\x1B[34m", "\x1B[39m");
1802
- var magenta = (text) => applyStyle(text, "\x1B[35m", "\x1B[39m");
1803
- var cyan = (text) => applyStyle(text, "\x1B[36m", "\x1B[39m");
1804
- var white = (text) => applyStyle(text, "\x1B[37m", "\x1B[39m");
1805
- var blackBright = (text) => applyStyle(text, "\x1B[90m", "\x1B[39m");
1806
- var redBright = (text) => applyStyle(text, "\x1B[91m", "\x1B[39m");
1807
- var greenBright = (text) => applyStyle(text, "\x1B[92m", "\x1B[39m");
1808
- var yellowBright = (text) => applyStyle(text, "\x1B[93m", "\x1B[39m");
1809
- var blueBright = (text) => applyStyle(text, "\x1B[94m", "\x1B[39m");
1810
- var magentaBright = (text) => applyStyle(text, "\x1B[95m", "\x1B[39m");
1811
- var cyanBright = (text) => applyStyle(text, "\x1B[96m", "\x1B[39m");
1812
- var whiteBright = (text) => applyStyle(text, "\x1B[97m", "\x1B[39m");
1850
+ function applyStyle2(text, open, close) {
1851
+ return detectColorSupport().level === 0 ? text : applyStyle(text, open, close);
1852
+ }
1853
+ var bold = (text) => applyStyle2(text, "\x1B[1m", "\x1B[22m");
1854
+ var dim = (text) => applyStyle2(text, "\x1B[2m", "\x1B[22m");
1855
+ var italic = (text) => applyStyle2(text, "\x1B[3m", "\x1B[23m");
1856
+ var underline = (text) => applyStyle2(text, "\x1B[4m", "\x1B[24m");
1857
+ var strikethrough = (text) => applyStyle2(text, "\x1B[9m", "\x1B[29m");
1858
+ var inverse = (text) => applyStyle2(text, "\x1B[7m", "\x1B[27m");
1859
+ var hidden = (text) => applyStyle2(text, "\x1B[8m", "\x1B[28m");
1860
+ var reset = (text) => applyStyle2(text, "\x1B[0m", "\x1B[0m");
1861
+ var black = (text) => applyStyle2(text, "\x1B[30m", "\x1B[39m");
1862
+ var red = (text) => applyStyle2(text, "\x1B[31m", "\x1B[39m");
1863
+ var green = (text) => applyStyle2(text, "\x1B[32m", "\x1B[39m");
1864
+ var yellow = (text) => applyStyle2(text, "\x1B[33m", "\x1B[39m");
1865
+ var blue = (text) => applyStyle2(text, "\x1B[34m", "\x1B[39m");
1866
+ var magenta = (text) => applyStyle2(text, "\x1B[35m", "\x1B[39m");
1867
+ var cyan = (text) => applyStyle2(text, "\x1B[36m", "\x1B[39m");
1868
+ var white = (text) => applyStyle2(text, "\x1B[37m", "\x1B[39m");
1869
+ var blackBright = (text) => applyStyle2(text, "\x1B[90m", "\x1B[39m");
1870
+ var redBright = (text) => applyStyle2(text, "\x1B[91m", "\x1B[39m");
1871
+ var greenBright = (text) => applyStyle2(text, "\x1B[92m", "\x1B[39m");
1872
+ var yellowBright = (text) => applyStyle2(text, "\x1B[93m", "\x1B[39m");
1873
+ var blueBright = (text) => applyStyle2(text, "\x1B[94m", "\x1B[39m");
1874
+ var magentaBright = (text) => applyStyle2(text, "\x1B[95m", "\x1B[39m");
1875
+ var cyanBright = (text) => applyStyle2(text, "\x1B[96m", "\x1B[39m");
1876
+ var whiteBright = (text) => applyStyle2(text, "\x1B[97m", "\x1B[39m");
1813
1877
  var gray = blackBright;
1814
1878
  var grey = blackBright;
1815
- var bgBlack = (text) => applyStyle(text, "\x1B[40m", "\x1B[49m");
1816
- var bgRed = (text) => applyStyle(text, "\x1B[41m", "\x1B[49m");
1817
- var bgGreen = (text) => applyStyle(text, "\x1B[42m", "\x1B[49m");
1818
- var bgYellow = (text) => applyStyle(text, "\x1B[43m", "\x1B[49m");
1819
- var bgBlue = (text) => applyStyle(text, "\x1B[44m", "\x1B[49m");
1820
- var bgMagenta = (text) => applyStyle(text, "\x1B[45m", "\x1B[49m");
1821
- var bgCyan = (text) => applyStyle(text, "\x1B[46m", "\x1B[49m");
1822
- var bgWhite = (text) => applyStyle(text, "\x1B[47m", "\x1B[49m");
1823
- var bgBlackBright = (text) => applyStyle(text, "\x1B[100m", "\x1B[49m");
1824
- var bgRedBright = (text) => applyStyle(text, "\x1B[101m", "\x1B[49m");
1825
- var bgGreenBright = (text) => applyStyle(text, "\x1B[102m", "\x1B[49m");
1826
- var bgYellowBright = (text) => applyStyle(text, "\x1B[103m", "\x1B[49m");
1827
- var bgBlueBright = (text) => applyStyle(text, "\x1B[104m", "\x1B[49m");
1828
- var bgMagentaBright = (text) => applyStyle(text, "\x1B[105m", "\x1B[49m");
1829
- var bgCyanBright = (text) => applyStyle(text, "\x1B[106m", "\x1B[49m");
1830
- var bgWhiteBright = (text) => applyStyle(text, "\x1B[107m", "\x1B[49m");
1879
+ var bgBlack = (text) => applyStyle2(text, "\x1B[40m", "\x1B[49m");
1880
+ var bgRed = (text) => applyStyle2(text, "\x1B[41m", "\x1B[49m");
1881
+ var bgGreen = (text) => applyStyle2(text, "\x1B[42m", "\x1B[49m");
1882
+ var bgYellow = (text) => applyStyle2(text, "\x1B[43m", "\x1B[49m");
1883
+ var bgBlue = (text) => applyStyle2(text, "\x1B[44m", "\x1B[49m");
1884
+ var bgMagenta = (text) => applyStyle2(text, "\x1B[45m", "\x1B[49m");
1885
+ var bgCyan = (text) => applyStyle2(text, "\x1B[46m", "\x1B[49m");
1886
+ var bgWhite = (text) => applyStyle2(text, "\x1B[47m", "\x1B[49m");
1887
+ var bgBlackBright = (text) => applyStyle2(text, "\x1B[100m", "\x1B[49m");
1888
+ var bgRedBright = (text) => applyStyle2(text, "\x1B[101m", "\x1B[49m");
1889
+ var bgGreenBright = (text) => applyStyle2(text, "\x1B[102m", "\x1B[49m");
1890
+ var bgYellowBright = (text) => applyStyle2(text, "\x1B[103m", "\x1B[49m");
1891
+ var bgBlueBright = (text) => applyStyle2(text, "\x1B[104m", "\x1B[49m");
1892
+ var bgMagentaBright = (text) => applyStyle2(text, "\x1B[105m", "\x1B[49m");
1893
+ var bgCyanBright = (text) => applyStyle2(text, "\x1B[106m", "\x1B[49m");
1894
+ var bgWhiteBright = (text) => applyStyle2(text, "\x1B[107m", "\x1B[49m");
1831
1895
  var bgGray = bgBlackBright;
1832
1896
  var bgGrey = bgBlackBright;
1833
1897
  function compose(...stylers) {
@@ -1854,6 +1918,33 @@ __export(plugins_exports, {
1854
1918
  });
1855
1919
 
1856
1920
  // src/plugins/optional/gradient.ts
1921
+ function toRgb(color) {
1922
+ if (typeof color !== "string") {
1923
+ const [r, g, b] = color;
1924
+ return { r, g, b };
1925
+ }
1926
+ return color.startsWith("rgb(") ? parseRgb(color) : hexToRgb(color);
1927
+ }
1928
+ function paintGradient(text, startRgb, endRgb, steps, level) {
1929
+ if (text.length === 0) {
1930
+ return text;
1931
+ }
1932
+ const colors = interpolateColors(startRgb, endRgb, text.length, Math.max(2, steps));
1933
+ let result = "";
1934
+ for (let i = 0; i < text.length; i++) {
1935
+ const colorIndex = text.length === 1 ? 0 : Math.floor(i / (text.length - 1) * (colors.length - 1));
1936
+ result += paintRgb(text[i], colors[colorIndex], false, level);
1937
+ }
1938
+ return result;
1939
+ }
1940
+ function paintRainbow(text, steps, level) {
1941
+ const colors = generateRainbowColors(text.length, Math.max(1, steps));
1942
+ let result = "";
1943
+ for (let i = 0; i < text.length; i++) {
1944
+ result += paintRgb(text[i], colors[i % colors.length], false, level);
1945
+ }
1946
+ return result;
1947
+ }
1857
1948
  function gradientPlugin(options) {
1858
1949
  const steps = options?.steps ?? 10;
1859
1950
  return {
@@ -1867,6 +1958,15 @@ function gradientPlugin(options) {
1867
1958
  enabled: true
1868
1959
  }
1869
1960
  });
1961
+ registerPluginApi(kernel, "gradient", (start, end) => {
1962
+ const startRgb = toRgb(start);
1963
+ const endRgb = toRgb(end);
1964
+ if (!startRgb || !endRgb) {
1965
+ throw new ColorError(`Invalid gradient color: ${String(startRgb ? end : start)}`);
1966
+ }
1967
+ return (text) => paintGradient(String(text), startRgb, endRgb, steps, k.getContext().level);
1968
+ });
1969
+ registerPluginApi(kernel, "rainbow", (text) => paintRainbow(String(text), steps, k.getContext().level));
1870
1970
  k.on("style:gradient", (data) => {
1871
1971
  const { start, end, text, steps: gradientSteps = steps } = data;
1872
1972
  const startRgb = start.startsWith("#") ? hexToRgb(start) : start.startsWith("rgb(") ? parseRgb(start) : hexToRgb(start);
@@ -1937,6 +2037,7 @@ function generateRainbowColors(length, steps) {
1937
2037
  }
1938
2038
 
1939
2039
  // src/plugins/optional/theme.ts
2040
+ var THEME_COLOR_TYPES = ["keyword", "string", "number", "comment", "error", "success", "warning", "info"];
1940
2041
  function themePlugin(options) {
1941
2042
  const preset = options?.preset ?? "monokai";
1942
2043
  const custom = options?.custom ?? {};
@@ -1956,6 +2057,17 @@ function themePlugin(options) {
1956
2057
  enabled: true
1957
2058
  }
1958
2059
  });
2060
+ const api = {};
2061
+ for (const type of THEME_COLOR_TYPES) {
2062
+ const color = theme[type];
2063
+ api[type] = (text) => {
2064
+ if (!color) {
2065
+ throw new Error(`Unknown theme color type: ${type}`);
2066
+ }
2067
+ return paintRgb(String(text), hexToRgb(color), false, k.getContext().level);
2068
+ };
2069
+ }
2070
+ registerPluginApi(kernel, "theme", api);
1959
2071
  k.on("style:theme", (data) => {
1960
2072
  const { type, text } = data;
1961
2073
  const color = theme[type];
@@ -2025,6 +2137,10 @@ function semanticPlugin(options) {
2025
2137
  if (!hexToRgb2) {
2026
2138
  throw new Error("hexToRgb utility not available");
2027
2139
  }
2140
+ for (const [name, color] of Object.entries(defaultColors)) {
2141
+ const rgb = hexToRgb2(color);
2142
+ registerPluginApi(kernel, name, (text) => paintRgb(String(text), rgb, false, k.getContext().level));
2143
+ }
2028
2144
  k.on("style:success", (data) => {
2029
2145
  const { r, g, b } = hexToRgb2(defaultColors.success);
2030
2146
  return `\x1B[38;2;${r};${g};${b}m${data.text}\x1B[39m`;
@@ -2066,11 +2182,14 @@ function boxPlugin(options) {
2066
2182
  borders: BOX_BORDERS
2067
2183
  }
2068
2184
  });
2069
- k.on("style:box", (data) => {
2070
- const { text, border: boxBorder = border, padding: boxPadding = padding, bgColor } = data;
2185
+ const draw = (text, opts = {}) => {
2186
+ const { border: boxBorder = border, padding: boxPadding = padding, bgColor } = opts;
2071
2187
  const chars = BOX_BORDERS[boxBorder];
2188
+ if (!chars) {
2189
+ throw new StyleError(`Unknown box border: ${String(boxBorder)}`);
2190
+ }
2072
2191
  const lines = text.split("\n");
2073
- const width = Math.max(...lines.map((l) => l.length)) + boxPadding * 2;
2192
+ const width = Math.max(...lines.map((l2) => l2.length)) + boxPadding * 2;
2074
2193
  const pad = " ".repeat(boxPadding);
2075
2194
  const top = `${chars.tl}${chars.h.repeat(width)}${chars.tr}`;
2076
2195
  const middle = lines.map((line) => {
@@ -2080,21 +2199,51 @@ function boxPlugin(options) {
2080
2199
  const bottom = `${chars.bl}${chars.h.repeat(width)}${chars.br}`;
2081
2200
  const result = [top, ...middle, bottom].join("\n");
2082
2201
  if (bgColor) {
2083
- const ctx = k.getContext();
2084
- const hexToRgb2 = ctx.utils?.hexToRgb;
2085
- if (!hexToRgb2) {
2086
- throw new Error("hexToRgb utility not available");
2087
- }
2088
- const { r, g, b } = hexToRgb2(bgColor);
2089
- return `\x1B[48;2;${r};${g};${b}m${result}\x1B[49m`;
2202
+ return paintRgb(result, hexToRgb(bgColor), true, k.getContext().level);
2090
2203
  }
2091
2204
  return result;
2205
+ };
2206
+ registerPluginApi(kernel, "box", (text, opts) => draw(String(text), opts));
2207
+ k.on("style:box", (data) => {
2208
+ const { text, ...opts } = data;
2209
+ return draw(text, opts);
2092
2210
  });
2093
2211
  }
2094
2212
  };
2095
2213
  }
2096
2214
 
2097
2215
  // src/plugins/optional/template.ts
2216
+ var STYLE_NAMES = /* @__PURE__ */ new Set([...MODIFIER_NAMES, ...COLOR_NAMES, ...BACKGROUND_COLOR_NAMES]);
2217
+ var BLOCK = /\{([A-Za-z]\w*(?:\.[A-Za-z]\w*)*) ([^{}]*)\}/g;
2218
+ var TOKEN = /\uE000(\d+)\uE001/g;
2219
+ function renderTemplate(root, strings, values) {
2220
+ const parts = values.map((value) => String(value));
2221
+ const token = (index) => `\uE000${index}\uE001`;
2222
+ const restore = (text) => text.replace(TOKEN, (_match, index) => String(parts[Number(index)]));
2223
+ let source = "";
2224
+ for (let i = 0; i < strings.length; i++) {
2225
+ source += strings[i];
2226
+ if (i < values.length) {
2227
+ source += token(i);
2228
+ }
2229
+ }
2230
+ let previous;
2231
+ do {
2232
+ previous = source;
2233
+ source = source.replace(BLOCK, (_match, chain, text) => {
2234
+ let styler = root;
2235
+ for (const name of chain.split(".")) {
2236
+ if (!STYLE_NAMES.has(name)) {
2237
+ throw new StyleError(`Unknown style in template: ${name}`);
2238
+ }
2239
+ styler = styler[name];
2240
+ }
2241
+ parts.push(styler(restore(text)));
2242
+ return token(parts.length - 1);
2243
+ });
2244
+ } while (source !== previous);
2245
+ return restore(source);
2246
+ }
2098
2247
  function templatePlugin() {
2099
2248
  return {
2100
2249
  name: "template",
@@ -2106,6 +2255,11 @@ function templatePlugin() {
2106
2255
  enabled: true
2107
2256
  }
2108
2257
  });
2258
+ registerPluginApi(
2259
+ kernel,
2260
+ "template",
2261
+ (strings, ...values) => renderTemplate(getRoot(kernel) ?? createProxyPigment(k), strings, values)
2262
+ );
2109
2263
  k.on("style:template", (data) => {
2110
2264
  const { strings, values } = data;
2111
2265
  let result = "";