@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.
package/dist/index.js CHANGED
@@ -8,8 +8,464 @@ var __export = (target, all) => {
8
8
  __defProp(target, name, { get: all[name], enumerable: true });
9
9
  };
10
10
 
11
- // src/core/factory.ts
12
- import { createKernel } from "@oxog/plugin";
11
+ // node_modules/@oxog/plugin/dist/index.js
12
+ import { PluginError } from "@oxog/types";
13
+ import { PluginError as PluginError2 } from "@oxog/types";
14
+ var x = ((i) => (i.Created = "created", i.Initializing = "initializing", i.Ready = "ready", i.Destroying = "destroying", i.Destroyed = "destroyed", i))(x || {});
15
+ var h = class {
16
+ context;
17
+ constructor(e) {
18
+ this.context = e;
19
+ }
20
+ get() {
21
+ return this.context;
22
+ }
23
+ update(e) {
24
+ this.context = Object.assign(this.context, e);
25
+ }
26
+ deepUpdate(e) {
27
+ let t = this.deepMerge(this.context, e);
28
+ this.context = Object.assign(this.context, t);
29
+ }
30
+ deepMerge(e, t) {
31
+ if (t === null || typeof t != "object") return t;
32
+ if (Array.isArray(t)) return [...t];
33
+ if (e === null || typeof e != "object" || Array.isArray(e)) return { ...t };
34
+ let n = { ...e };
35
+ for (let r of Object.keys(t)) {
36
+ let i = t[r], a = n[r];
37
+ n[r] = this.deepMerge(a, i);
38
+ }
39
+ return n;
40
+ }
41
+ };
42
+ var u = class {
43
+ handlers = /* @__PURE__ */ new Map();
44
+ wildcardHandlers = new Array();
45
+ patternHandlers = /* @__PURE__ */ new Map();
46
+ onceWrappers = /* @__PURE__ */ new Map();
47
+ on(e, t) {
48
+ let n = String(e);
49
+ this.handlers.has(n) || this.handlers.set(n, []);
50
+ let r = this.handlers.get(n), i = t;
51
+ return r.includes(i) || r.push(i), () => {
52
+ let a = this.handlers.get(n);
53
+ if (a) {
54
+ let s = a.indexOf(i);
55
+ s !== -1 && a.splice(s, 1);
56
+ }
57
+ };
58
+ }
59
+ once(e, t) {
60
+ let n = t, r = String(e), i = this.onceWrappers.get(r);
61
+ i || (i = /* @__PURE__ */ new Map(), this.onceWrappers.set(r, i));
62
+ let a = (s) => {
63
+ i.delete(n), this.off(e, a), t(s);
64
+ };
65
+ return i.set(n, a), this.on(e, a);
66
+ }
67
+ off(e, t) {
68
+ let n = String(e), r = this.handlers.get(n);
69
+ if (r) {
70
+ let i = t, a = this.onceWrappers.get(n), s = a?.get(i), o = s || i, l = r.indexOf(o);
71
+ l !== -1 && (r.splice(l, 1), s && a.delete(i));
72
+ }
73
+ }
74
+ emit(e, t) {
75
+ let n = String(e), r = this.handlers.get(n);
76
+ if (r) for (let i of [...r]) try {
77
+ i(t);
78
+ } catch {
79
+ }
80
+ for (let i of [...this.wildcardHandlers]) try {
81
+ i(n, t);
82
+ } catch {
83
+ }
84
+ for (let [i, a] of this.patternHandlers) if (this.matchPattern(i, n)) for (let s of [...a]) try {
85
+ s(n, t);
86
+ } catch {
87
+ }
88
+ }
89
+ onWildcard(e) {
90
+ return this.wildcardHandlers.includes(e) || this.wildcardHandlers.push(e), () => {
91
+ let t = this.wildcardHandlers.indexOf(e);
92
+ t !== -1 && this.wildcardHandlers.splice(t, 1);
93
+ };
94
+ }
95
+ onPattern(e, t) {
96
+ this.patternHandlers.has(e) || this.patternHandlers.set(e, []);
97
+ let n = this.patternHandlers.get(e);
98
+ return n.includes(t) || n.push(t), () => {
99
+ let r = this.patternHandlers.get(e);
100
+ if (r) {
101
+ let i = r.indexOf(t);
102
+ i !== -1 && r.splice(i, 1);
103
+ }
104
+ };
105
+ }
106
+ clear() {
107
+ this.handlers.clear(), this.wildcardHandlers.length = 0, this.patternHandlers.clear(), this.onceWrappers.clear();
108
+ }
109
+ matchPattern(e, t) {
110
+ if (e === "*") return true;
111
+ if (e.endsWith(":*")) {
112
+ let n = e.slice(0, -2);
113
+ return t.startsWith(n + ":");
114
+ }
115
+ return e === t;
116
+ }
117
+ };
118
+ var f = class {
119
+ graph = /* @__PURE__ */ new Map();
120
+ addPlugin(e, t = []) {
121
+ this.graph.set(e, [...t]);
122
+ }
123
+ removePlugin(e) {
124
+ this.graph.delete(e);
125
+ for (let t of this.graph.values()) {
126
+ let n = t.indexOf(e);
127
+ n !== -1 && t.splice(n, 1);
128
+ }
129
+ }
130
+ validateSinglePlugin(e) {
131
+ let t = this.graph.get(e);
132
+ if (!t) return;
133
+ for (let r of t) if (!this.graph.has(r)) throw new PluginError(`Missing dependency: '${r}' required by '${e}'`, e);
134
+ let n = this.detectCycle();
135
+ if (n && n.includes(e)) throw new PluginError(`Circular dependency detected: ${n.join(" -> ")}`, "dependency-resolver");
136
+ }
137
+ resolve() {
138
+ let e = /* @__PURE__ */ new Map(), t = [], n = [], r = /* @__PURE__ */ new Map();
139
+ for (let [s, o] of this.graph) {
140
+ let l = o.filter((p) => this.graph.has(p));
141
+ r.set(s, l);
142
+ }
143
+ 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);
144
+ for (let [s, o] of r) e.set(s, o.length), o.length === 0 && t.push(s);
145
+ let i = /* @__PURE__ */ new Map();
146
+ for (let [s, o] of r) for (let l of o) {
147
+ let p = i.get(l);
148
+ p ? p.push(s) : i.set(l, [s]);
149
+ }
150
+ let a = 0;
151
+ for (; a < t.length; ) {
152
+ let s = t[a++];
153
+ n.push(s);
154
+ for (let o of i.get(s) ?? []) e.set(o, e.get(o) - 1), e.get(o) === 0 && t.push(o);
155
+ }
156
+ if (n.length !== r.size) {
157
+ let s = this.detectCycle();
158
+ throw new PluginError(`Circular dependency detected: ${s.join(" -> ")}`, "dependency-resolver");
159
+ }
160
+ return n;
161
+ }
162
+ detectCycle() {
163
+ let r = /* @__PURE__ */ new Map();
164
+ for (let a of this.graph.keys()) r.set(a, 0);
165
+ let i = (a, s) => {
166
+ r.set(a, 1), s.push(a);
167
+ for (let o of this.graph.get(a)) if (this.graph.has(o)) {
168
+ if (r.get(o) === 1) {
169
+ let l = s.indexOf(o);
170
+ return [...s.slice(l), o];
171
+ }
172
+ if (r.get(o) === 0) {
173
+ let l = i(o, s);
174
+ if (l) return l;
175
+ }
176
+ }
177
+ return s.pop(), r.set(a, 2), null;
178
+ };
179
+ for (let a of this.graph.keys()) if (r.get(a) === 0) {
180
+ let s = i(a, []);
181
+ if (s) return s;
182
+ }
183
+ return null;
184
+ }
185
+ getGraph() {
186
+ let e = {};
187
+ for (let [t, n] of this.graph) e[t] = [...n];
188
+ return e;
189
+ }
190
+ clear() {
191
+ this.graph.clear();
192
+ }
193
+ };
194
+ var v = class {
195
+ strategy;
196
+ globalHandler;
197
+ errors = [];
198
+ constructor(e, t) {
199
+ this.strategy = e, this.globalHandler = t;
200
+ }
201
+ getStrategy() {
202
+ return this.strategy;
203
+ }
204
+ async withBoundary(e, t, n) {
205
+ try {
206
+ return await n();
207
+ } catch (r) {
208
+ let i = r instanceof Error ? r : new Error(String(r));
209
+ throw await this.handleError(e, t, i), i;
210
+ }
211
+ }
212
+ async handleError(e, t, n) {
213
+ if (t.onError) try {
214
+ t.onError(n);
215
+ } catch {
216
+ }
217
+ if (this.globalHandler) try {
218
+ this.globalHandler(n, e);
219
+ } catch {
220
+ }
221
+ this.strategy === "collect" && this.errors.push(n);
222
+ }
223
+ throwIfErrors() {
224
+ if (this.strategy === "collect" && this.errors.length > 0) {
225
+ let e = new AggregateError(this.errors, `${this.errors.length} plugin error(s) occurred`);
226
+ throw this.clearErrors(), e;
227
+ }
228
+ }
229
+ clearErrors() {
230
+ this.errors = [];
231
+ }
232
+ getErrors() {
233
+ return Object.freeze([...this.errors]);
234
+ }
235
+ };
236
+ var m = class {
237
+ async initialize(e, t, n, r, i) {
238
+ let a = r.getStrategy();
239
+ for (let s of t) {
240
+ let o = e.get(s);
241
+ if (o && o.state !== "ready") {
242
+ o.state = "initializing";
243
+ try {
244
+ await r.withBoundary(s, o.plugin, async () => {
245
+ o.plugin.onInit && await o.plugin.onInit(n);
246
+ }), o.state = "ready", o.initializedAt = Date.now(), i?.(s);
247
+ } catch (l) {
248
+ if (a === "fail-fast") throw l;
249
+ o.state = "failed";
250
+ }
251
+ }
252
+ }
253
+ }
254
+ async destroy(e, t, n, r) {
255
+ let i = n.getStrategy(), a = [...t].reverse();
256
+ for (let s of a) {
257
+ let o = e.get(s);
258
+ if (o) {
259
+ o.state = "destroying";
260
+ try {
261
+ await n.withBoundary(s, o.plugin, async () => {
262
+ o.plugin.onDestroy && await o.plugin.onDestroy();
263
+ });
264
+ } catch (l) {
265
+ if (i === "fail-fast") throw l;
266
+ }
267
+ o.state = "destroyed", r?.(s);
268
+ }
269
+ }
270
+ }
271
+ };
272
+ var c = class {
273
+ state = "created";
274
+ plugins = /* @__PURE__ */ new Map();
275
+ context;
276
+ events;
277
+ resolver;
278
+ errorBoundary;
279
+ lifecycle;
280
+ config;
281
+ pendingInits = /* @__PURE__ */ new Map();
282
+ emitInternal(e, t) {
283
+ this.events.emit(e, t);
284
+ }
285
+ constructor(e) {
286
+ 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();
287
+ }
288
+ use(e) {
289
+ if (this.state === "destroyed") throw new PluginError("Cannot add plugins to destroyed kernel", "kernel");
290
+ 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");
291
+ if (this.plugins.has(e.name)) throw new PluginError(`Plugin '${e.name}' is already registered`, "kernel");
292
+ let t = { plugin: e, state: "registered" };
293
+ this.plugins.set(e.name, t), this.resolver.addPlugin(e.name, [...e.dependencies || []]);
294
+ try {
295
+ 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") {
296
+ let n = this.initializeWithDependencies(e.name);
297
+ this.pendingInits.set(e.name, n), n.finally(() => {
298
+ this.pendingInits.delete(e.name);
299
+ });
300
+ }
301
+ } catch (n) {
302
+ throw this.pendingInits.delete(e.name), this.plugins.delete(e.name), this.resolver.removePlugin(e.name), n;
303
+ }
304
+ return this;
305
+ }
306
+ async waitForPlugin(e) {
307
+ let t = this.pendingInits.get(e);
308
+ t && await t;
309
+ }
310
+ async waitForAll() {
311
+ await Promise.all(this.pendingInits.values());
312
+ }
313
+ async initializeWithDependencies(e) {
314
+ let t = this.plugins.get(e);
315
+ if (!t) return;
316
+ let n = t.plugin.dependencies || [];
317
+ for (let r of n) {
318
+ let i = this.pendingInits.get(r);
319
+ i && await i;
320
+ }
321
+ for (let r of n) {
322
+ let i = this.plugins.get(r);
323
+ if (!i || i.state !== "ready") {
324
+ t.state = "failed";
325
+ let a = new PluginError(`Dependency '${r}' is not ready for plugin '${e}'`, e);
326
+ this.emitInternal("plugin:error", { name: e, error: a });
327
+ return;
328
+ }
329
+ }
330
+ await this.initializePlugin(e);
331
+ }
332
+ async initializePlugin(e) {
333
+ let t = this.plugins.get(e);
334
+ if (!(!t || t.state !== "registered")) try {
335
+ t.state = "initializing", await this.errorBoundary.withBoundary(e, t.plugin, async () => {
336
+ t.plugin.onInit && await t.plugin.onInit(this.context.get());
337
+ }), t.state = "ready", t.initializedAt = Date.now(), this.emitInternal("plugin:init", { name: e });
338
+ } catch (n) {
339
+ t.state = "failed", this.emitInternal("plugin:error", { name: e, error: n });
340
+ }
341
+ }
342
+ useAll(e) {
343
+ for (let t of e) this.use(t);
344
+ return this;
345
+ }
346
+ unregister(e) {
347
+ let t = this.plugins.get(e);
348
+ if (!t) return false;
349
+ let n = this.pendingInits.get(e);
350
+ return t.state === "initializing" && n ? (n.finally(() => {
351
+ if (t.plugin.onDestroy) return t.plugin.onDestroy();
352
+ }).then(() => this.emitInternal("plugin:destroy", { name: e })).catch(() => {
353
+ }), this.pendingInits.delete(e)) : t.state === "ready" && t.plugin.onDestroy && Promise.resolve().then(() => t.plugin.onDestroy()).then(() => this.emitInternal("plugin:destroy", { name: e })).catch(() => {
354
+ }), this.plugins.delete(e), this.resolver.removePlugin(e), true;
355
+ }
356
+ async unregisterAsync(e) {
357
+ let t = this.plugins.get(e);
358
+ if (!t) return false;
359
+ let n = this.pendingInits.get(e);
360
+ if (t.state === "initializing" && n) {
361
+ try {
362
+ await n;
363
+ } catch {
364
+ }
365
+ this.pendingInits.delete(e);
366
+ }
367
+ if ((t.state === "ready" || t.state === "failed") && t.plugin.onDestroy) try {
368
+ await t.plugin.onDestroy(), this.emitInternal("plugin:destroy", { name: e });
369
+ } catch {
370
+ }
371
+ return this.plugins.delete(e), this.resolver.removePlugin(e), true;
372
+ }
373
+ async replace(e) {
374
+ await this.unregisterAsync(e.name), this.use(e), this.state === "ready" && await this.waitForPlugin(e.name);
375
+ }
376
+ async reload(e) {
377
+ let t = this.plugins.get(e);
378
+ if (!t) return;
379
+ let n = this.errorBoundary.getStrategy();
380
+ if (t.state === "ready") {
381
+ try {
382
+ t.plugin.onDestroy && await this.errorBoundary.withBoundary(e, t.plugin, async () => t.plugin.onDestroy()), this.emitInternal("plugin:destroy", { name: e });
383
+ } catch (r) {
384
+ if (n === "fail-fast") throw r;
385
+ }
386
+ t.state = "registered";
387
+ try {
388
+ 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 });
389
+ } catch (r) {
390
+ if (n === "fail-fast") throw r;
391
+ t.state = "failed";
392
+ }
393
+ }
394
+ }
395
+ async init() {
396
+ if (this.state !== "created") throw new PluginError("Kernel already initialized", "kernel");
397
+ this.state = "initializing";
398
+ try {
399
+ this.config.onBeforeInit && await this.config.onBeforeInit();
400
+ let e = this.resolver.resolve();
401
+ 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();
402
+ } catch (e) {
403
+ throw this.state = "created", e;
404
+ }
405
+ }
406
+ async destroy() {
407
+ if (this.state === "destroyed") return;
408
+ this.state = "destroying", this.config.onBeforeDestroy && await this.config.onBeforeDestroy(), this.emitInternal("kernel:destroy", { timestamp: Date.now() }), await Promise.allSettled(this.pendingInits.values());
409
+ let e;
410
+ try {
411
+ e = this.resolver.resolve();
412
+ } catch {
413
+ e = Array.from(this.plugins.keys());
414
+ }
415
+ 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();
416
+ }
417
+ isInitialized() {
418
+ return this.state === "ready";
419
+ }
420
+ isDestroyed() {
421
+ return this.state === "destroyed";
422
+ }
423
+ getPlugin(e) {
424
+ return this.plugins.get(e)?.plugin;
425
+ }
426
+ hasPlugin(e) {
427
+ return this.plugins.has(e);
428
+ }
429
+ listPlugins() {
430
+ return Array.from(this.plugins.values()).map((e) => e.plugin);
431
+ }
432
+ getPluginNames() {
433
+ return Array.from(this.plugins.keys());
434
+ }
435
+ getDependencyGraph() {
436
+ return this.resolver.getGraph();
437
+ }
438
+ on(e, t) {
439
+ return this.events.on(e, t);
440
+ }
441
+ once(e, t) {
442
+ return this.events.once(e, t);
443
+ }
444
+ off(e, t) {
445
+ this.events.off(e, t);
446
+ }
447
+ emit(e, t) {
448
+ this.events.emit(e, t);
449
+ }
450
+ onWildcard(e) {
451
+ return this.events.onWildcard(e);
452
+ }
453
+ onPattern(e, t) {
454
+ return this.events.onPattern(e, t);
455
+ }
456
+ getContext() {
457
+ return this.context.get();
458
+ }
459
+ updateContext(e) {
460
+ this.context.update(e);
461
+ }
462
+ deepUpdateContext(e) {
463
+ this.context.deepUpdate(e);
464
+ }
465
+ };
466
+ function W(d) {
467
+ return new c(d);
468
+ }
13
469
 
14
470
  // src/constants.ts
15
471
  var ANSI_MODIFIERS = {
@@ -331,17 +787,17 @@ function createProxyPigment(kernel, styles = []) {
331
787
  if (prop === "supportsColor") {
332
788
  return ctx.colorSupport;
333
789
  }
334
- if (MODIFIER_NAMES.includes(prop)) {
790
+ if (MODIFIER_NAMES.some((name) => name === prop)) {
335
791
  const modifierStyle = generateModifierCode(prop);
336
792
  return createProxyPigment(kernel, [...styles, modifierStyle]);
337
793
  }
338
- if (COLOR_NAMES.includes(prop) || BACKGROUND_COLOR_NAMES.includes(prop)) {
794
+ if (COLOR_NAMES.some((name) => name === prop) || BACKGROUND_COLOR_NAMES.some((name) => name === prop)) {
339
795
  const background = prop.startsWith("bg");
340
796
  const colorName = background ? prop.slice(2) : prop;
341
797
  const colorStyle = generateColorCode(colorName, background);
342
798
  return createProxyPigment(kernel, [...styles, colorStyle]);
343
799
  }
344
- if (EXTENDED_COLOR_METHODS.includes(prop)) {
800
+ if (EXTENDED_COLOR_METHODS.some((name) => name === prop)) {
345
801
  if (prop === "ansi256" || prop === "bgAnsi256") {
346
802
  return (code) => {
347
803
  const background = prop === "bgAnsi256";
@@ -369,13 +825,13 @@ function createProxyPigment(kernel, styles = []) {
369
825
  };
370
826
  }
371
827
  if (prop === "hsl" || prop === "bgHsl") {
372
- return (h, s, l) => {
828
+ return (h2, s, l) => {
373
829
  const background = prop === "bgHsl";
374
830
  const hslToRgb2 = ctx.utils?.hslToRgb;
375
831
  if (!hslToRgb2) {
376
832
  throw new Error("hslToRgb utility not available");
377
833
  }
378
- const { r, g, b } = hslToRgb2(h, s, l);
834
+ const { r, g, b } = hslToRgb2(h2, s, l);
379
835
  const rgbStyle = generateRgbCode(r, g, b, background);
380
836
  return createProxyPigment(kernel, [...styles, rgbStyle]);
381
837
  };
@@ -383,9 +839,9 @@ function createProxyPigment(kernel, styles = []) {
383
839
  }
384
840
  if (prop === "visible") {
385
841
  const colorSupport = ctx.colorSupport;
386
- return (text) => colorSupport.level > 0 ? text : text;
842
+ return (text) => colorSupport.level > 0 ? applyStyles(String(text), styles) : "";
387
843
  }
388
- return target[prop];
844
+ return Reflect.get(target, prop);
389
845
  },
390
846
  apply(_target, _thisArg, argArray) {
391
847
  const text = String(argArray[0] ?? "");
@@ -397,19 +853,25 @@ function createProxyPigment(kernel, styles = []) {
397
853
  return pigment2;
398
854
  }
399
855
  function applyStyles(text, styles) {
400
- if (styles.length === 0) {
856
+ if (styles.length === 0 || text === "") {
401
857
  return text;
402
858
  }
403
859
  const open = styles.map((style) => style.open).join("");
404
860
  const close = [...styles].reverse().map((style) => style.close).join("");
405
- return `${open}${text}${close}`;
861
+ let result = text;
862
+ for (const style of styles) {
863
+ if (style.close) {
864
+ result = result.split(style.close).join(style.close + style.open);
865
+ }
866
+ }
867
+ return `${open}${result}${close}`;
406
868
  }
407
869
 
408
870
  // src/utils/hex-to-rgb.ts
409
871
  function hexToRgb(hex) {
410
872
  const hexString = hex.trim().replace(/^#/, "");
411
873
  if (hexString.length === 3) {
412
- const expanded = hexString.split("").map((c) => c + c).join("");
874
+ const expanded = hexString.split("").map((c2) => c2 + c2).join("");
413
875
  return parseExpandedHex(expanded);
414
876
  }
415
877
  if (hexString.length === 6) {
@@ -436,8 +898,8 @@ function validateHex(hex) {
436
898
  }
437
899
 
438
900
  // src/utils/hsl-to-rgb.ts
439
- function hslToRgb(h, s, l) {
440
- const hNormalized = h / 360;
901
+ function hslToRgb(h2, s, l) {
902
+ const hNormalized = h2 / 360;
441
903
  const sNormalized = s / 100;
442
904
  const lNormalized = l / 100;
443
905
  let r;
@@ -680,23 +1142,29 @@ var BuilderPigment = class {
680
1142
  }
681
1143
  return this;
682
1144
  }
683
- hsl(h, s, l) {
684
- const { r, g, b } = hslToRgb(h, s, l);
1145
+ hsl(h2, s, l) {
1146
+ const { r, g, b } = hslToRgb(h2, s, l);
685
1147
  this.styles.push(generateRgbCode(r, g, b, false));
686
1148
  return this;
687
1149
  }
688
- bgHsl(h, s, l) {
689
- const { r, g, b } = hslToRgb(h, s, l);
1150
+ bgHsl(h2, s, l) {
1151
+ const { r, g, b } = hslToRgb(h2, s, l);
690
1152
  this.styles.push(generateRgbCode(r, g, b, true));
691
1153
  return this;
692
1154
  }
693
1155
  paint(text) {
694
- if (this.styles.length === 0) {
1156
+ if (this.styles.length === 0 || text === "") {
695
1157
  return text;
696
1158
  }
697
1159
  const open = this.styles.map((style) => style.open).join("");
698
1160
  const close = [...this.styles].reverse().map((style) => style.close).join("");
699
- return `${open}${text}${close}`;
1161
+ let result = text;
1162
+ for (const style of this.styles) {
1163
+ if (style.close) {
1164
+ result = result.split(style.close).join(style.close + style.open);
1165
+ }
1166
+ }
1167
+ return `${open}${result}${close}`;
700
1168
  }
701
1169
  };
702
1170
 
@@ -748,23 +1216,14 @@ function detectColorSupport(overrideLevel) {
748
1216
  const platform = detectPlatform();
749
1217
  if (platform.isBrowser) {
750
1218
  cachedColorSupport = {
751
- level: 3,
1219
+ level: 1,
752
1220
  hasBasic: true,
753
- has256: true,
754
- has16m: true
755
- };
756
- return cachedColorSupport;
757
- }
758
- if (!platform.isNode) {
759
- cachedColorSupport = {
760
- level: 0,
761
- hasBasic: false,
762
1221
  has256: false,
763
1222
  has16m: false
764
1223
  };
765
1224
  return cachedColorSupport;
766
1225
  }
767
- if (process.env.NO_COLOR) {
1226
+ if (!platform.isNode) {
768
1227
  cachedColorSupport = {
769
1228
  level: 0,
770
1229
  hasBasic: false,
@@ -773,65 +1232,51 @@ function detectColorSupport(overrideLevel) {
773
1232
  };
774
1233
  return cachedColorSupport;
775
1234
  }
776
- if (process.env.FORCE_COLOR) {
777
- const level = parseInt(process.env.FORCE_COLOR, 10);
778
- const clampedLevel = Math.max(0, Math.min(3, level));
779
- cachedColorSupport = {
780
- level: clampedLevel,
781
- hasBasic: clampedLevel >= 1,
782
- has256: clampedLevel >= 2,
783
- has16m: clampedLevel >= 3
784
- };
1235
+ const env = process.env;
1236
+ const forced = parseForceColor(env.FORCE_COLOR);
1237
+ if (forced !== void 0) {
1238
+ cachedColorSupport = toColorSupport(forced);
785
1239
  return cachedColorSupport;
786
1240
  }
787
- if (!hasTTY() && !process.env.CI) {
788
- cachedColorSupport = {
789
- level: 0,
790
- hasBasic: false,
791
- has256: false,
792
- has16m: false
793
- };
1241
+ if (env.NO_COLOR) {
1242
+ cachedColorSupport = toColorSupport(0);
794
1243
  return cachedColorSupport;
795
1244
  }
796
- const term = process.env.TERM ?? "";
797
- if (term.includes("truecolor") || term.includes("24bit")) {
798
- cachedColorSupport = {
799
- level: 3,
800
- hasBasic: true,
801
- has256: true,
802
- has16m: true
803
- };
1245
+ if (!hasTTY() && !env.CI || env.TERM === "dumb") {
1246
+ cachedColorSupport = toColorSupport(0);
804
1247
  return cachedColorSupport;
805
1248
  }
806
- if (term.includes("256color")) {
807
- cachedColorSupport = {
808
- level: 2,
809
- hasBasic: true,
810
- has256: true,
811
- has16m: false
812
- };
1249
+ const term = env.TERM ?? "";
1250
+ const colorTerm = (env.COLORTERM ?? "").toLowerCase();
1251
+ if (colorTerm === "truecolor" || colorTerm === "24bit" || term.includes("truecolor") || term.includes("24bit")) {
1252
+ cachedColorSupport = toColorSupport(3);
813
1253
  return cachedColorSupport;
814
1254
  }
815
- if (process.env.CI) {
816
- cachedColorSupport = {
817
- level: 1,
818
- hasBasic: true,
819
- has256: false,
820
- has16m: false
821
- };
1255
+ if (term.includes("256color")) {
1256
+ cachedColorSupport = toColorSupport(2);
822
1257
  return cachedColorSupport;
823
1258
  }
824
- cachedColorSupport = {
825
- level: 1,
826
- hasBasic: true,
827
- has256: false,
828
- has16m: false
829
- };
1259
+ cachedColorSupport = toColorSupport(1);
830
1260
  return cachedColorSupport;
831
1261
  }
832
1262
  function supportsColor() {
833
1263
  return detectColorSupport();
834
1264
  }
1265
+ function resetColorSupportCache() {
1266
+ cachedColorSupport = void 0;
1267
+ }
1268
+ function parseForceColor(value) {
1269
+ if (value === void 0) return void 0;
1270
+ const normalized = value.trim().toLowerCase();
1271
+ if (normalized === "" || normalized === "true") return 1;
1272
+ if (normalized === "false") return 0;
1273
+ const level = Number.parseInt(normalized, 10);
1274
+ if (Number.isNaN(level)) return 1;
1275
+ return Math.max(0, Math.min(3, level));
1276
+ }
1277
+ function toColorSupport(level) {
1278
+ return { level, hasBasic: level >= 1, has256: level >= 2, has16m: level >= 3 };
1279
+ }
835
1280
 
836
1281
  // src/utils/rgb-to-ansi256.ts
837
1282
  function rgbToAnsi256(r, g, b) {
@@ -959,11 +1404,11 @@ function trueColorPlugin() {
959
1404
  k.emit("color:rgb", { ...rgb, background });
960
1405
  });
961
1406
  k.on("color:hsl", (data) => {
962
- const { h, s, l, background } = data;
1407
+ const { h: h2, s, l, background } = data;
963
1408
  if (!utils?.hslToRgb) {
964
1409
  throw new Error("hslToRgb utility not available");
965
1410
  }
966
- const rgb = utils.hslToRgb(h, s, l);
1411
+ const rgb = utils.hslToRgb(h2, s, l);
967
1412
  k.emit("color:rgb", { ...rgb, background });
968
1413
  });
969
1414
  }
@@ -982,12 +1427,6 @@ function nestingPlugin() {
982
1427
  enabled: true
983
1428
  }
984
1429
  });
985
- k.on("style:nest", (data) => {
986
- const { innerText, outerStyles } = data;
987
- const open = outerStyles.join("");
988
- const close = outerStyles.map(() => "\x1B[0m").join("");
989
- return `${open}${innerText}${close}`;
990
- });
991
1430
  }
992
1431
  };
993
1432
  }
@@ -1000,26 +1439,28 @@ function environmentPlugin() {
1000
1439
  install(kernel) {
1001
1440
  const k = kernel;
1002
1441
  const ci = isCI();
1003
- const colorSupport = detectColorSupport();
1442
+ const existingContext = k.getContext();
1443
+ const colorSupport = existingContext.colorSupport ?? detectColorSupport();
1004
1444
  k.updateContext({
1005
1445
  environment: {
1006
1446
  isCI: ci,
1007
- noColor: process.env.NO_COLOR !== void 0,
1447
+ // NO_COLOR only counts when non-empty (no-color.org)
1448
+ noColor: Boolean(process.env.NO_COLOR),
1008
1449
  forceColor: process.env.FORCE_COLOR !== void 0,
1009
1450
  term: process.env.TERM ?? ""
1010
- },
1011
- colorSupport
1451
+ }
1452
+ // Don't overwrite colorSupport - it was already set in factory with the correct level
1012
1453
  });
1013
1454
  k.on("environment:check", () => {
1014
1455
  const ctx = k.getContext();
1015
1456
  const env = ctx.environment;
1457
+ if (env?.forceColor) {
1458
+ const level = parseForceColor(process.env.FORCE_COLOR) ?? 1;
1459
+ return { level, enabled: level > 0 };
1460
+ }
1016
1461
  if (env?.noColor) {
1017
1462
  return { level: 0, enabled: false };
1018
1463
  }
1019
- if (env?.forceColor) {
1020
- const level = parseInt(process.env.FORCE_COLOR ?? "0", 10);
1021
- return { level: Math.max(0, Math.min(3, level)), enabled: true };
1022
- }
1023
1464
  if (ci) {
1024
1465
  return { level: 1, enabled: colorSupport.hasBasic };
1025
1466
  }
@@ -1046,7 +1487,7 @@ function createPigment(options) {
1046
1487
  const noColor = options?.noColor ?? false;
1047
1488
  const forceColor = options?.forceColor ?? false;
1048
1489
  const level = noColor ? 0 : forceColor ? options?.level ?? 3 : colorSupport.level;
1049
- const kernel = createKernel({
1490
+ const kernel = W({
1050
1491
  context: {
1051
1492
  colorSupport: { ...colorSupport, level },
1052
1493
  level,
@@ -1072,7 +1513,7 @@ function createPigment(options) {
1072
1513
  kernel.use(plugin);
1073
1514
  }
1074
1515
  }
1075
- kernel.init();
1516
+ void kernel.init();
1076
1517
  const pigment2 = createProxyPigment(kernel);
1077
1518
  const builder = new BuilderPigment(kernel);
1078
1519
  Object.defineProperty(pigment2, "builder", { value: builder, enumerable: false });
@@ -1218,8 +1659,9 @@ var Chalk = class {
1218
1659
  reset(text) {
1219
1660
  return this.pigment.reset(text);
1220
1661
  }
1662
+ /** Like chalk: returns the text only when colors are supported, otherwise ''. */
1221
1663
  visible(text) {
1222
- return this.supportsColor.level > 0 ? text : text;
1664
+ return this.supportsColor.level > 0 ? text : "";
1223
1665
  }
1224
1666
  rgb(r, g, b) {
1225
1667
  const coloredPigment = this.pigment.rgb(r, g, b);
@@ -1311,9 +1753,6 @@ function compose(...stylers) {
1311
1753
  };
1312
1754
  }
1313
1755
 
1314
- // src/index.ts
1315
- import { createKernel as createKernel2 } from "@oxog/plugin";
1316
-
1317
1756
  // src/plugins/index.ts
1318
1757
  var plugins_exports = {};
1319
1758
  __export(plugins_exports, {
@@ -1620,7 +2059,8 @@ function parseStyleExpression(expression, text, kernel) {
1620
2059
  }
1621
2060
  }
1622
2061
  if (typeof current === "function") {
1623
- return current(text);
2062
+ const style = current;
2063
+ return style(text);
1624
2064
  }
1625
2065
  return text;
1626
2066
  }
@@ -1636,7 +2076,7 @@ function pigmentPlugin(options) {
1636
2076
  const k = kernel;
1637
2077
  const themeColors = THEME_PRESETS[theme] ?? THEME_PRESETS.monokai;
1638
2078
  const pigment2 = createPigment({ level });
1639
- k.pigment = pigment2;
2079
+ Object.assign(k, { pigment: pigment2 });
1640
2080
  k.updateContext({
1641
2081
  ecosystem: {
1642
2082
  theme,
@@ -1760,7 +2200,7 @@ export {
1760
2200
  blueBright,
1761
2201
  bold,
1762
2202
  compose,
1763
- createKernel2 as createKernel,
2203
+ W as createKernel,
1764
2204
  createPigment,
1765
2205
  cyan,
1766
2206
  cyanBright,
@@ -1789,6 +2229,7 @@ export {
1789
2229
  red,
1790
2230
  redBright,
1791
2231
  reset,
2232
+ resetColorSupportCache,
1792
2233
  rgbToAnsi256,
1793
2234
  strikethrough,
1794
2235
  supportsColor,