@oxog/pigment 1.0.1 → 1.1.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.
@@ -346,11 +346,11 @@ function trueColorPlugin() {
346
346
  k.emit("color:rgb", { ...rgb, background });
347
347
  });
348
348
  k.on("color:hsl", (data) => {
349
- const { h, s, l, background } = data;
349
+ const { h: h2, s, l, background } = data;
350
350
  if (!utils?.hslToRgb) {
351
351
  throw new Error("hslToRgb utility not available");
352
352
  }
353
- const rgb = utils.hslToRgb(h, s, l);
353
+ const rgb = utils.hslToRgb(h2, s, l);
354
354
  k.emit("color:rgb", { ...rgb, background });
355
355
  });
356
356
  }
@@ -369,12 +369,6 @@ function nestingPlugin() {
369
369
  enabled: true
370
370
  }
371
371
  });
372
- k.on("style:nest", (data) => {
373
- const { innerText, outerStyles } = data;
374
- const open = outerStyles.join("");
375
- const close = outerStyles.map(() => "\x1B[0m").join("");
376
- return `${open}${innerText}${close}`;
377
- });
378
372
  }
379
373
  };
380
374
  }
@@ -418,23 +412,14 @@ function detectColorSupport(overrideLevel) {
418
412
  const platform = detectPlatform();
419
413
  if (platform.isBrowser) {
420
414
  cachedColorSupport = {
421
- level: 3,
415
+ level: 1,
422
416
  hasBasic: true,
423
- has256: true,
424
- has16m: true
425
- };
426
- return cachedColorSupport;
427
- }
428
- if (!platform.isNode) {
429
- cachedColorSupport = {
430
- level: 0,
431
- hasBasic: false,
432
417
  has256: false,
433
418
  has16m: false
434
419
  };
435
420
  return cachedColorSupport;
436
421
  }
437
- if (process.env.NO_COLOR) {
422
+ if (!platform.isNode) {
438
423
  cachedColorSupport = {
439
424
  level: 0,
440
425
  hasBasic: false,
@@ -443,62 +428,45 @@ function detectColorSupport(overrideLevel) {
443
428
  };
444
429
  return cachedColorSupport;
445
430
  }
446
- if (process.env.FORCE_COLOR) {
447
- const level = parseInt(process.env.FORCE_COLOR, 10);
448
- const clampedLevel = Math.max(0, Math.min(3, level));
449
- cachedColorSupport = {
450
- level: clampedLevel,
451
- hasBasic: clampedLevel >= 1,
452
- has256: clampedLevel >= 2,
453
- has16m: clampedLevel >= 3
454
- };
431
+ const env = process.env;
432
+ const forced = parseForceColor(env.FORCE_COLOR);
433
+ if (forced !== void 0) {
434
+ cachedColorSupport = toColorSupport(forced);
455
435
  return cachedColorSupport;
456
436
  }
457
- if (!hasTTY() && !process.env.CI) {
458
- cachedColorSupport = {
459
- level: 0,
460
- hasBasic: false,
461
- has256: false,
462
- has16m: false
463
- };
437
+ if (env.NO_COLOR) {
438
+ cachedColorSupport = toColorSupport(0);
464
439
  return cachedColorSupport;
465
440
  }
466
- const term = process.env.TERM ?? "";
467
- if (term.includes("truecolor") || term.includes("24bit")) {
468
- cachedColorSupport = {
469
- level: 3,
470
- hasBasic: true,
471
- has256: true,
472
- has16m: true
473
- };
441
+ if (!hasTTY() && !env.CI || env.TERM === "dumb") {
442
+ cachedColorSupport = toColorSupport(0);
474
443
  return cachedColorSupport;
475
444
  }
476
- if (term.includes("256color")) {
477
- cachedColorSupport = {
478
- level: 2,
479
- hasBasic: true,
480
- has256: true,
481
- has16m: false
482
- };
445
+ const term = env.TERM ?? "";
446
+ const colorTerm = (env.COLORTERM ?? "").toLowerCase();
447
+ if (colorTerm === "truecolor" || colorTerm === "24bit" || term.includes("truecolor") || term.includes("24bit")) {
448
+ cachedColorSupport = toColorSupport(3);
483
449
  return cachedColorSupport;
484
450
  }
485
- if (process.env.CI) {
486
- cachedColorSupport = {
487
- level: 1,
488
- hasBasic: true,
489
- has256: false,
490
- has16m: false
491
- };
451
+ if (term.includes("256color")) {
452
+ cachedColorSupport = toColorSupport(2);
492
453
  return cachedColorSupport;
493
454
  }
494
- cachedColorSupport = {
495
- level: 1,
496
- hasBasic: true,
497
- has256: false,
498
- has16m: false
499
- };
455
+ cachedColorSupport = toColorSupport(1);
500
456
  return cachedColorSupport;
501
457
  }
458
+ function parseForceColor(value) {
459
+ if (value === void 0) return void 0;
460
+ const normalized = value.trim().toLowerCase();
461
+ if (normalized === "" || normalized === "true") return 1;
462
+ if (normalized === "false") return 0;
463
+ const level = Number.parseInt(normalized, 10);
464
+ if (Number.isNaN(level)) return 1;
465
+ return Math.max(0, Math.min(3, level));
466
+ }
467
+ function toColorSupport(level) {
468
+ return { level, hasBasic: level >= 1, has256: level >= 2, has16m: level >= 3 };
469
+ }
502
470
 
503
471
  // src/plugins/core/environment.ts
504
472
  function environmentPlugin() {
@@ -508,26 +476,28 @@ function environmentPlugin() {
508
476
  install(kernel) {
509
477
  const k = kernel;
510
478
  const ci = isCI();
511
- const colorSupport = detectColorSupport();
479
+ const existingContext = k.getContext();
480
+ const colorSupport = existingContext.colorSupport ?? detectColorSupport();
512
481
  k.updateContext({
513
482
  environment: {
514
483
  isCI: ci,
515
- noColor: process.env.NO_COLOR !== void 0,
484
+ // NO_COLOR only counts when non-empty (no-color.org)
485
+ noColor: Boolean(process.env.NO_COLOR),
516
486
  forceColor: process.env.FORCE_COLOR !== void 0,
517
487
  term: process.env.TERM ?? ""
518
- },
519
- colorSupport
488
+ }
489
+ // Don't overwrite colorSupport - it was already set in factory with the correct level
520
490
  });
521
491
  k.on("environment:check", () => {
522
492
  const ctx = k.getContext();
523
493
  const env = ctx.environment;
494
+ if (env?.forceColor) {
495
+ const level = parseForceColor(process.env.FORCE_COLOR) ?? 1;
496
+ return { level, enabled: level > 0 };
497
+ }
524
498
  if (env?.noColor) {
525
499
  return { level: 0, enabled: false };
526
500
  }
527
- if (env?.forceColor) {
528
- const level = parseInt(process.env.FORCE_COLOR ?? "0", 10);
529
- return { level: Math.max(0, Math.min(3, level)), enabled: true };
530
- }
531
501
  if (ci) {
532
502
  return { level: 1, enabled: colorSupport.hasBasic };
533
503
  }
@@ -552,7 +522,7 @@ function environmentPlugin() {
552
522
  function hexToRgb(hex) {
553
523
  const hexString = hex.trim().replace(/^#/, "");
554
524
  if (hexString.length === 3) {
555
- const expanded = hexString.split("").map((c) => c + c).join("");
525
+ const expanded = hexString.split("").map((c2) => c2 + c2).join("");
556
526
  return parseExpandedHex(expanded);
557
527
  }
558
528
  if (hexString.length === 6) {
@@ -571,8 +541,8 @@ function parseExpandedHex(hex) {
571
541
  }
572
542
 
573
543
  // src/utils/hsl-to-rgb.ts
574
- function hslToRgb(h, s, l) {
575
- const hNormalized = h / 360;
544
+ function hslToRgb(h2, s, l) {
545
+ const hNormalized = h2 / 360;
576
546
  const sNormalized = s / 100;
577
547
  const lNormalized = l / 100;
578
548
  let r;
@@ -891,13 +861,470 @@ function parseStyleExpression(expression, text, kernel) {
891
861
  }
892
862
  }
893
863
  if (typeof current === "function") {
894
- return current(text);
864
+ const style = current;
865
+ return style(text);
895
866
  }
896
867
  return text;
897
868
  }
898
869
 
899
- // src/core/factory.ts
900
- import { createKernel } from "@oxog/plugin";
870
+ // node_modules/@oxog/plugin/dist/index.js
871
+ import { PluginError } from "@oxog/types";
872
+ import { PluginError as PluginError2 } from "@oxog/types";
873
+ var x = ((i) => (i.Created = "created", i.Initializing = "initializing", i.Ready = "ready", i.Destroying = "destroying", i.Destroyed = "destroyed", i))(x || {});
874
+ var h = class {
875
+ context;
876
+ constructor(e) {
877
+ this.context = e;
878
+ }
879
+ get() {
880
+ return this.context;
881
+ }
882
+ update(e) {
883
+ this.context = Object.assign(this.context, e);
884
+ }
885
+ deepUpdate(e) {
886
+ let t = this.deepMerge(this.context, e);
887
+ this.context = Object.assign(this.context, t);
888
+ }
889
+ deepMerge(e, t) {
890
+ if (t === null || typeof t != "object") return t;
891
+ if (Array.isArray(t)) return [...t];
892
+ if (e === null || typeof e != "object" || Array.isArray(e)) return { ...t };
893
+ let n = { ...e };
894
+ for (let r of Object.keys(t)) {
895
+ let i = t[r], a = n[r];
896
+ n[r] = this.deepMerge(a, i);
897
+ }
898
+ return n;
899
+ }
900
+ };
901
+ var u = class {
902
+ handlers = /* @__PURE__ */ new Map();
903
+ wildcardHandlers = new Array();
904
+ patternHandlers = /* @__PURE__ */ new Map();
905
+ onceWrappers = /* @__PURE__ */ new Map();
906
+ on(e, t) {
907
+ let n = String(e);
908
+ this.handlers.has(n) || this.handlers.set(n, []);
909
+ let r = this.handlers.get(n), i = t;
910
+ return r.includes(i) || r.push(i), () => {
911
+ let a = this.handlers.get(n);
912
+ if (a) {
913
+ let s = a.indexOf(i);
914
+ s !== -1 && a.splice(s, 1);
915
+ }
916
+ };
917
+ }
918
+ once(e, t) {
919
+ let n = t, r = String(e), i = this.onceWrappers.get(r);
920
+ i || (i = /* @__PURE__ */ new Map(), this.onceWrappers.set(r, i));
921
+ let a = (s) => {
922
+ i.delete(n), this.off(e, a), t(s);
923
+ };
924
+ return i.set(n, a), this.on(e, a);
925
+ }
926
+ off(e, t) {
927
+ let n = String(e), r = this.handlers.get(n);
928
+ if (r) {
929
+ let i = t, a = this.onceWrappers.get(n), s = a?.get(i), o = s || i, l = r.indexOf(o);
930
+ l !== -1 && (r.splice(l, 1), s && a.delete(i));
931
+ }
932
+ }
933
+ emit(e, t) {
934
+ let n = String(e), r = this.handlers.get(n);
935
+ if (r) for (let i of [...r]) try {
936
+ i(t);
937
+ } catch {
938
+ }
939
+ for (let i of [...this.wildcardHandlers]) try {
940
+ i(n, t);
941
+ } catch {
942
+ }
943
+ for (let [i, a] of this.patternHandlers) if (this.matchPattern(i, n)) for (let s of [...a]) try {
944
+ s(n, t);
945
+ } catch {
946
+ }
947
+ }
948
+ onWildcard(e) {
949
+ return this.wildcardHandlers.includes(e) || this.wildcardHandlers.push(e), () => {
950
+ let t = this.wildcardHandlers.indexOf(e);
951
+ t !== -1 && this.wildcardHandlers.splice(t, 1);
952
+ };
953
+ }
954
+ onPattern(e, t) {
955
+ this.patternHandlers.has(e) || this.patternHandlers.set(e, []);
956
+ let n = this.patternHandlers.get(e);
957
+ return n.includes(t) || n.push(t), () => {
958
+ let r = this.patternHandlers.get(e);
959
+ if (r) {
960
+ let i = r.indexOf(t);
961
+ i !== -1 && r.splice(i, 1);
962
+ }
963
+ };
964
+ }
965
+ clear() {
966
+ this.handlers.clear(), this.wildcardHandlers.length = 0, this.patternHandlers.clear(), this.onceWrappers.clear();
967
+ }
968
+ matchPattern(e, t) {
969
+ if (e === "*") return true;
970
+ if (e.endsWith(":*")) {
971
+ let n = e.slice(0, -2);
972
+ return t.startsWith(n + ":");
973
+ }
974
+ return e === t;
975
+ }
976
+ };
977
+ var f = class {
978
+ graph = /* @__PURE__ */ new Map();
979
+ addPlugin(e, t = []) {
980
+ this.graph.set(e, [...t]);
981
+ }
982
+ removePlugin(e) {
983
+ this.graph.delete(e);
984
+ for (let t of this.graph.values()) {
985
+ let n = t.indexOf(e);
986
+ n !== -1 && t.splice(n, 1);
987
+ }
988
+ }
989
+ validateSinglePlugin(e) {
990
+ let t = this.graph.get(e);
991
+ if (!t) return;
992
+ for (let r of t) if (!this.graph.has(r)) throw new PluginError(`Missing dependency: '${r}' required by '${e}'`, e);
993
+ let n = this.detectCycle();
994
+ if (n && n.includes(e)) throw new PluginError(`Circular dependency detected: ${n.join(" -> ")}`, "dependency-resolver");
995
+ }
996
+ resolve() {
997
+ let e = /* @__PURE__ */ new Map(), t = [], n = [], r = /* @__PURE__ */ new Map();
998
+ for (let [s, o] of this.graph) {
999
+ let l = o.filter((p) => this.graph.has(p));
1000
+ r.set(s, l);
1001
+ }
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);
1003
+ for (let [s, o] of r) e.set(s, o.length), o.length === 0 && t.push(s);
1004
+ let i = /* @__PURE__ */ new Map();
1005
+ for (let [s, o] of r) for (let l of o) {
1006
+ let p = i.get(l);
1007
+ p ? p.push(s) : i.set(l, [s]);
1008
+ }
1009
+ let a = 0;
1010
+ for (; a < t.length; ) {
1011
+ let s = t[a++];
1012
+ n.push(s);
1013
+ for (let o of i.get(s) ?? []) e.set(o, e.get(o) - 1), e.get(o) === 0 && t.push(o);
1014
+ }
1015
+ if (n.length !== r.size) {
1016
+ let s = this.detectCycle();
1017
+ throw new PluginError(`Circular dependency detected: ${s.join(" -> ")}`, "dependency-resolver");
1018
+ }
1019
+ return n;
1020
+ }
1021
+ detectCycle() {
1022
+ let r = /* @__PURE__ */ new Map();
1023
+ for (let a of this.graph.keys()) r.set(a, 0);
1024
+ let i = (a, s) => {
1025
+ r.set(a, 1), s.push(a);
1026
+ for (let o of this.graph.get(a)) if (this.graph.has(o)) {
1027
+ if (r.get(o) === 1) {
1028
+ let l = s.indexOf(o);
1029
+ return [...s.slice(l), o];
1030
+ }
1031
+ if (r.get(o) === 0) {
1032
+ let l = i(o, s);
1033
+ if (l) return l;
1034
+ }
1035
+ }
1036
+ return s.pop(), r.set(a, 2), null;
1037
+ };
1038
+ for (let a of this.graph.keys()) if (r.get(a) === 0) {
1039
+ let s = i(a, []);
1040
+ if (s) return s;
1041
+ }
1042
+ return null;
1043
+ }
1044
+ getGraph() {
1045
+ let e = {};
1046
+ for (let [t, n] of this.graph) e[t] = [...n];
1047
+ return e;
1048
+ }
1049
+ clear() {
1050
+ this.graph.clear();
1051
+ }
1052
+ };
1053
+ var v = class {
1054
+ strategy;
1055
+ globalHandler;
1056
+ errors = [];
1057
+ constructor(e, t) {
1058
+ this.strategy = e, this.globalHandler = t;
1059
+ }
1060
+ getStrategy() {
1061
+ return this.strategy;
1062
+ }
1063
+ async withBoundary(e, t, n) {
1064
+ try {
1065
+ return await n();
1066
+ } catch (r) {
1067
+ let i = r instanceof Error ? r : new Error(String(r));
1068
+ throw await this.handleError(e, t, i), i;
1069
+ }
1070
+ }
1071
+ async handleError(e, t, n) {
1072
+ if (t.onError) try {
1073
+ t.onError(n);
1074
+ } catch {
1075
+ }
1076
+ if (this.globalHandler) try {
1077
+ this.globalHandler(n, e);
1078
+ } catch {
1079
+ }
1080
+ this.strategy === "collect" && this.errors.push(n);
1081
+ }
1082
+ throwIfErrors() {
1083
+ if (this.strategy === "collect" && this.errors.length > 0) {
1084
+ let e = new AggregateError(this.errors, `${this.errors.length} plugin error(s) occurred`);
1085
+ throw this.clearErrors(), e;
1086
+ }
1087
+ }
1088
+ clearErrors() {
1089
+ this.errors = [];
1090
+ }
1091
+ getErrors() {
1092
+ return Object.freeze([...this.errors]);
1093
+ }
1094
+ };
1095
+ var m = class {
1096
+ async initialize(e, t, n, r, i) {
1097
+ let a = r.getStrategy();
1098
+ for (let s of t) {
1099
+ let o = e.get(s);
1100
+ if (o && o.state !== "ready") {
1101
+ o.state = "initializing";
1102
+ try {
1103
+ await r.withBoundary(s, o.plugin, async () => {
1104
+ o.plugin.onInit && await o.plugin.onInit(n);
1105
+ }), o.state = "ready", o.initializedAt = Date.now(), i?.(s);
1106
+ } catch (l) {
1107
+ if (a === "fail-fast") throw l;
1108
+ o.state = "failed";
1109
+ }
1110
+ }
1111
+ }
1112
+ }
1113
+ async destroy(e, t, n, r) {
1114
+ let i = n.getStrategy(), a = [...t].reverse();
1115
+ for (let s of a) {
1116
+ let o = e.get(s);
1117
+ if (o) {
1118
+ o.state = "destroying";
1119
+ try {
1120
+ await n.withBoundary(s, o.plugin, async () => {
1121
+ o.plugin.onDestroy && await o.plugin.onDestroy();
1122
+ });
1123
+ } catch (l) {
1124
+ if (i === "fail-fast") throw l;
1125
+ }
1126
+ o.state = "destroyed", r?.(s);
1127
+ }
1128
+ }
1129
+ }
1130
+ };
1131
+ var c = class {
1132
+ state = "created";
1133
+ plugins = /* @__PURE__ */ new Map();
1134
+ context;
1135
+ events;
1136
+ resolver;
1137
+ errorBoundary;
1138
+ lifecycle;
1139
+ config;
1140
+ pendingInits = /* @__PURE__ */ new Map();
1141
+ emitInternal(e, t) {
1142
+ this.events.emit(e, t);
1143
+ }
1144
+ constructor(e) {
1145
+ 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
+ }
1147
+ 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");
1151
+ let t = { plugin: e, state: "registered" };
1152
+ this.plugins.set(e.name, t), this.resolver.addPlugin(e.name, [...e.dependencies || []]);
1153
+ try {
1154
+ if (this.state === "ready" && this.resolver.validateSinglePlugin(e.name), e.install(this), this.emitInternal("plugin:install", { name: e.name, version: e.version }), this.state === "ready") {
1155
+ let n = this.initializeWithDependencies(e.name);
1156
+ this.pendingInits.set(e.name, n), n.finally(() => {
1157
+ this.pendingInits.delete(e.name);
1158
+ });
1159
+ }
1160
+ } catch (n) {
1161
+ throw this.pendingInits.delete(e.name), this.plugins.delete(e.name), this.resolver.removePlugin(e.name), n;
1162
+ }
1163
+ return this;
1164
+ }
1165
+ async waitForPlugin(e) {
1166
+ let t = this.pendingInits.get(e);
1167
+ t && await t;
1168
+ }
1169
+ async waitForAll() {
1170
+ await Promise.all(this.pendingInits.values());
1171
+ }
1172
+ async initializeWithDependencies(e) {
1173
+ let t = this.plugins.get(e);
1174
+ if (!t) return;
1175
+ let n = t.plugin.dependencies || [];
1176
+ for (let r of n) {
1177
+ let i = this.pendingInits.get(r);
1178
+ i && await i;
1179
+ }
1180
+ for (let r of n) {
1181
+ let i = this.plugins.get(r);
1182
+ if (!i || i.state !== "ready") {
1183
+ t.state = "failed";
1184
+ let a = new PluginError(`Dependency '${r}' is not ready for plugin '${e}'`, e);
1185
+ this.emitInternal("plugin:error", { name: e, error: a });
1186
+ return;
1187
+ }
1188
+ }
1189
+ await this.initializePlugin(e);
1190
+ }
1191
+ async initializePlugin(e) {
1192
+ let t = this.plugins.get(e);
1193
+ if (!(!t || t.state !== "registered")) try {
1194
+ t.state = "initializing", await this.errorBoundary.withBoundary(e, t.plugin, async () => {
1195
+ t.plugin.onInit && await t.plugin.onInit(this.context.get());
1196
+ }), t.state = "ready", t.initializedAt = Date.now(), this.emitInternal("plugin:init", { name: e });
1197
+ } catch (n) {
1198
+ t.state = "failed", this.emitInternal("plugin:error", { name: e, error: n });
1199
+ }
1200
+ }
1201
+ useAll(e) {
1202
+ for (let t of e) this.use(t);
1203
+ return this;
1204
+ }
1205
+ unregister(e) {
1206
+ let t = this.plugins.get(e);
1207
+ if (!t) return false;
1208
+ let n = this.pendingInits.get(e);
1209
+ return t.state === "initializing" && n ? (n.finally(() => {
1210
+ if (t.plugin.onDestroy) return t.plugin.onDestroy();
1211
+ }).then(() => this.emitInternal("plugin:destroy", { name: e })).catch(() => {
1212
+ }), this.pendingInits.delete(e)) : t.state === "ready" && t.plugin.onDestroy && Promise.resolve().then(() => t.plugin.onDestroy()).then(() => this.emitInternal("plugin:destroy", { name: e })).catch(() => {
1213
+ }), this.plugins.delete(e), this.resolver.removePlugin(e), true;
1214
+ }
1215
+ async unregisterAsync(e) {
1216
+ let t = this.plugins.get(e);
1217
+ if (!t) return false;
1218
+ let n = this.pendingInits.get(e);
1219
+ if (t.state === "initializing" && n) {
1220
+ try {
1221
+ await n;
1222
+ } catch {
1223
+ }
1224
+ this.pendingInits.delete(e);
1225
+ }
1226
+ if ((t.state === "ready" || t.state === "failed") && t.plugin.onDestroy) try {
1227
+ await t.plugin.onDestroy(), this.emitInternal("plugin:destroy", { name: e });
1228
+ } catch {
1229
+ }
1230
+ return this.plugins.delete(e), this.resolver.removePlugin(e), true;
1231
+ }
1232
+ async replace(e) {
1233
+ await this.unregisterAsync(e.name), this.use(e), this.state === "ready" && await this.waitForPlugin(e.name);
1234
+ }
1235
+ async reload(e) {
1236
+ let t = this.plugins.get(e);
1237
+ if (!t) return;
1238
+ let n = this.errorBoundary.getStrategy();
1239
+ if (t.state === "ready") {
1240
+ try {
1241
+ t.plugin.onDestroy && await this.errorBoundary.withBoundary(e, t.plugin, async () => t.plugin.onDestroy()), this.emitInternal("plugin:destroy", { name: e });
1242
+ } catch (r) {
1243
+ if (n === "fail-fast") throw r;
1244
+ }
1245
+ t.state = "registered";
1246
+ try {
1247
+ t.plugin.onInit && await this.errorBoundary.withBoundary(e, t.plugin, async () => t.plugin.onInit(this.context.get())), t.state = "ready", this.emitInternal("plugin:init", { name: e });
1248
+ } catch (r) {
1249
+ if (n === "fail-fast") throw r;
1250
+ t.state = "failed";
1251
+ }
1252
+ }
1253
+ }
1254
+ async init() {
1255
+ if (this.state !== "created") throw new PluginError("Kernel already initialized", "kernel");
1256
+ this.state = "initializing";
1257
+ try {
1258
+ this.config.onBeforeInit && await this.config.onBeforeInit();
1259
+ let e = this.resolver.resolve();
1260
+ this.emitInternal("kernel:init", { timestamp: Date.now() }), await this.lifecycle.initialize(this.plugins, e, this.context.get(), this.errorBoundary, (t) => this.emitInternal("plugin:init", { name: t })), this.state = "ready", this.emitInternal("kernel:ready", { timestamp: Date.now(), plugins: Array.from(this.plugins.keys()) }), this.config.onAfterInit && await this.config.onAfterInit(), this.errorBoundary.throwIfErrors();
1261
+ } catch (e) {
1262
+ throw this.state = "created", e;
1263
+ }
1264
+ }
1265
+ async destroy() {
1266
+ if (this.state === "destroyed") return;
1267
+ this.state = "destroying", this.config.onBeforeDestroy && await this.config.onBeforeDestroy(), this.emitInternal("kernel:destroy", { timestamp: Date.now() }), await Promise.allSettled(this.pendingInits.values());
1268
+ let e;
1269
+ try {
1270
+ e = this.resolver.resolve();
1271
+ } catch {
1272
+ e = Array.from(this.plugins.keys());
1273
+ }
1274
+ await this.lifecycle.destroy(this.plugins, e, this.errorBoundary, (t) => this.emitInternal("plugin:destroy", { name: t })), this.state = "destroyed", this.emitInternal("kernel:destroyed", { timestamp: Date.now() }), this.events.clear(), this.config.onAfterDestroy && await this.config.onAfterDestroy();
1275
+ }
1276
+ isInitialized() {
1277
+ return this.state === "ready";
1278
+ }
1279
+ isDestroyed() {
1280
+ return this.state === "destroyed";
1281
+ }
1282
+ getPlugin(e) {
1283
+ return this.plugins.get(e)?.plugin;
1284
+ }
1285
+ hasPlugin(e) {
1286
+ return this.plugins.has(e);
1287
+ }
1288
+ listPlugins() {
1289
+ return Array.from(this.plugins.values()).map((e) => e.plugin);
1290
+ }
1291
+ getPluginNames() {
1292
+ return Array.from(this.plugins.keys());
1293
+ }
1294
+ getDependencyGraph() {
1295
+ return this.resolver.getGraph();
1296
+ }
1297
+ on(e, t) {
1298
+ return this.events.on(e, t);
1299
+ }
1300
+ once(e, t) {
1301
+ return this.events.once(e, t);
1302
+ }
1303
+ off(e, t) {
1304
+ this.events.off(e, t);
1305
+ }
1306
+ emit(e, t) {
1307
+ this.events.emit(e, t);
1308
+ }
1309
+ onWildcard(e) {
1310
+ return this.events.onWildcard(e);
1311
+ }
1312
+ onPattern(e, t) {
1313
+ return this.events.onPattern(e, t);
1314
+ }
1315
+ getContext() {
1316
+ return this.context.get();
1317
+ }
1318
+ updateContext(e) {
1319
+ this.context.update(e);
1320
+ }
1321
+ deepUpdateContext(e) {
1322
+ this.context.deepUpdate(e);
1323
+ }
1324
+ };
1325
+ function W(d) {
1326
+ return new c(d);
1327
+ }
901
1328
 
902
1329
  // src/core/ansi.ts
903
1330
  function generateModifierCode(name) {
@@ -985,17 +1412,17 @@ function createProxyPigment(kernel, styles = []) {
985
1412
  if (prop === "supportsColor") {
986
1413
  return ctx.colorSupport;
987
1414
  }
988
- if (MODIFIER_NAMES.includes(prop)) {
1415
+ if (MODIFIER_NAMES.some((name) => name === prop)) {
989
1416
  const modifierStyle = generateModifierCode(prop);
990
1417
  return createProxyPigment(kernel, [...styles, modifierStyle]);
991
1418
  }
992
- if (COLOR_NAMES.includes(prop) || BACKGROUND_COLOR_NAMES.includes(prop)) {
1419
+ if (COLOR_NAMES.some((name) => name === prop) || BACKGROUND_COLOR_NAMES.some((name) => name === prop)) {
993
1420
  const background = prop.startsWith("bg");
994
1421
  const colorName = background ? prop.slice(2) : prop;
995
1422
  const colorStyle = generateColorCode(colorName, background);
996
1423
  return createProxyPigment(kernel, [...styles, colorStyle]);
997
1424
  }
998
- if (EXTENDED_COLOR_METHODS.includes(prop)) {
1425
+ if (EXTENDED_COLOR_METHODS.some((name) => name === prop)) {
999
1426
  if (prop === "ansi256" || prop === "bgAnsi256") {
1000
1427
  return (code) => {
1001
1428
  const background = prop === "bgAnsi256";
@@ -1023,13 +1450,13 @@ function createProxyPigment(kernel, styles = []) {
1023
1450
  };
1024
1451
  }
1025
1452
  if (prop === "hsl" || prop === "bgHsl") {
1026
- return (h, s, l) => {
1453
+ return (h2, s, l) => {
1027
1454
  const background = prop === "bgHsl";
1028
1455
  const hslToRgb2 = ctx.utils?.hslToRgb;
1029
1456
  if (!hslToRgb2) {
1030
1457
  throw new Error("hslToRgb utility not available");
1031
1458
  }
1032
- const { r, g, b } = hslToRgb2(h, s, l);
1459
+ const { r, g, b } = hslToRgb2(h2, s, l);
1033
1460
  const rgbStyle = generateRgbCode(r, g, b, background);
1034
1461
  return createProxyPigment(kernel, [...styles, rgbStyle]);
1035
1462
  };
@@ -1037,9 +1464,9 @@ function createProxyPigment(kernel, styles = []) {
1037
1464
  }
1038
1465
  if (prop === "visible") {
1039
1466
  const colorSupport = ctx.colorSupport;
1040
- return (text) => colorSupport.level > 0 ? text : text;
1467
+ return (text) => colorSupport.level > 0 ? applyStyles(String(text), styles) : "";
1041
1468
  }
1042
- return target[prop];
1469
+ return Reflect.get(target, prop);
1043
1470
  },
1044
1471
  apply(_target, _thisArg, argArray) {
1045
1472
  const text = String(argArray[0] ?? "");
@@ -1051,12 +1478,18 @@ function createProxyPigment(kernel, styles = []) {
1051
1478
  return pigment;
1052
1479
  }
1053
1480
  function applyStyles(text, styles) {
1054
- if (styles.length === 0) {
1481
+ if (styles.length === 0 || text === "") {
1055
1482
  return text;
1056
1483
  }
1057
1484
  const open = styles.map((style) => style.open).join("");
1058
1485
  const close = [...styles].reverse().map((style) => style.close).join("");
1059
- return `${open}${text}${close}`;
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}`;
1060
1493
  }
1061
1494
 
1062
1495
  // src/core/builder.ts
@@ -1272,23 +1705,29 @@ var BuilderPigment = class {
1272
1705
  }
1273
1706
  return this;
1274
1707
  }
1275
- hsl(h, s, l) {
1276
- const { r, g, b } = hslToRgb(h, s, l);
1708
+ hsl(h2, s, l) {
1709
+ const { r, g, b } = hslToRgb(h2, s, l);
1277
1710
  this.styles.push(generateRgbCode(r, g, b, false));
1278
1711
  return this;
1279
1712
  }
1280
- bgHsl(h, s, l) {
1281
- const { r, g, b } = hslToRgb(h, s, l);
1713
+ bgHsl(h2, s, l) {
1714
+ const { r, g, b } = hslToRgb(h2, s, l);
1282
1715
  this.styles.push(generateRgbCode(r, g, b, true));
1283
1716
  return this;
1284
1717
  }
1285
1718
  paint(text) {
1286
- if (this.styles.length === 0) {
1719
+ if (this.styles.length === 0 || text === "") {
1287
1720
  return text;
1288
1721
  }
1289
1722
  const open = this.styles.map((style) => style.open).join("");
1290
1723
  const close = [...this.styles].reverse().map((style) => style.close).join("");
1291
- return `${open}${text}${close}`;
1724
+ let result = text;
1725
+ for (const style of this.styles) {
1726
+ if (style.close) {
1727
+ result = result.split(style.close).join(style.close + style.open);
1728
+ }
1729
+ }
1730
+ return `${open}${result}${close}`;
1292
1731
  }
1293
1732
  };
1294
1733
 
@@ -1315,7 +1754,7 @@ function createPigment(options) {
1315
1754
  const noColor = options?.noColor ?? false;
1316
1755
  const forceColor = options?.forceColor ?? false;
1317
1756
  const level = noColor ? 0 : forceColor ? options?.level ?? 3 : colorSupport.level;
1318
- const kernel = createKernel({
1757
+ const kernel = W({
1319
1758
  context: {
1320
1759
  colorSupport: { ...colorSupport, level },
1321
1760
  level,
@@ -1341,7 +1780,7 @@ function createPigment(options) {
1341
1780
  kernel.use(plugin);
1342
1781
  }
1343
1782
  }
1344
- kernel.init();
1783
+ void kernel.init();
1345
1784
  const pigment = createProxyPigment(kernel);
1346
1785
  const builder = new BuilderPigment(kernel);
1347
1786
  Object.defineProperty(pigment, "builder", { value: builder, enumerable: false });
@@ -1361,7 +1800,7 @@ function pigmentPlugin(options) {
1361
1800
  const k = kernel;
1362
1801
  const themeColors = THEME_PRESETS[theme] ?? THEME_PRESETS.monokai;
1363
1802
  const pigment = createPigment({ level });
1364
- k.pigment = pigment;
1803
+ Object.assign(k, { pigment });
1365
1804
  k.updateContext({
1366
1805
  ecosystem: {
1367
1806
  theme,