@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.cjs CHANGED
@@ -49,7 +49,7 @@ __export(index_exports, {
49
49
  blueBright: () => blueBright,
50
50
  bold: () => bold,
51
51
  compose: () => compose,
52
- createKernel: () => import_plugin2.createKernel,
52
+ createKernel: () => W,
53
53
  createPigment: () => createPigment,
54
54
  cyan: () => cyan,
55
55
  cyanBright: () => cyanBright,
@@ -78,6 +78,7 @@ __export(index_exports, {
78
78
  red: () => red,
79
79
  redBright: () => redBright,
80
80
  reset: () => reset,
81
+ resetColorSupportCache: () => resetColorSupportCache,
81
82
  rgbToAnsi256: () => rgbToAnsi256,
82
83
  strikethrough: () => strikethrough,
83
84
  supportsColor: () => supportsColor,
@@ -90,8 +91,464 @@ __export(index_exports, {
90
91
  });
91
92
  module.exports = __toCommonJS(index_exports);
92
93
 
93
- // src/core/factory.ts
94
- var import_plugin = require("@oxog/plugin");
94
+ // node_modules/@oxog/plugin/dist/index.js
95
+ var import_types = require("@oxog/types");
96
+ var import_types2 = require("@oxog/types");
97
+ var x = ((i) => (i.Created = "created", i.Initializing = "initializing", i.Ready = "ready", i.Destroying = "destroying", i.Destroyed = "destroyed", i))(x || {});
98
+ var h = class {
99
+ context;
100
+ constructor(e) {
101
+ this.context = e;
102
+ }
103
+ get() {
104
+ return this.context;
105
+ }
106
+ update(e) {
107
+ this.context = Object.assign(this.context, e);
108
+ }
109
+ deepUpdate(e) {
110
+ let t = this.deepMerge(this.context, e);
111
+ this.context = Object.assign(this.context, t);
112
+ }
113
+ deepMerge(e, t) {
114
+ if (t === null || typeof t != "object") return t;
115
+ if (Array.isArray(t)) return [...t];
116
+ if (e === null || typeof e != "object" || Array.isArray(e)) return { ...t };
117
+ let n = { ...e };
118
+ for (let r of Object.keys(t)) {
119
+ let i = t[r], a = n[r];
120
+ n[r] = this.deepMerge(a, i);
121
+ }
122
+ return n;
123
+ }
124
+ };
125
+ var u = class {
126
+ handlers = /* @__PURE__ */ new Map();
127
+ wildcardHandlers = new Array();
128
+ patternHandlers = /* @__PURE__ */ new Map();
129
+ onceWrappers = /* @__PURE__ */ new Map();
130
+ on(e, t) {
131
+ let n = String(e);
132
+ this.handlers.has(n) || this.handlers.set(n, []);
133
+ let r = this.handlers.get(n), i = t;
134
+ return r.includes(i) || r.push(i), () => {
135
+ let a = this.handlers.get(n);
136
+ if (a) {
137
+ let s = a.indexOf(i);
138
+ s !== -1 && a.splice(s, 1);
139
+ }
140
+ };
141
+ }
142
+ once(e, t) {
143
+ let n = t, r = String(e), i = this.onceWrappers.get(r);
144
+ i || (i = /* @__PURE__ */ new Map(), this.onceWrappers.set(r, i));
145
+ let a = (s) => {
146
+ i.delete(n), this.off(e, a), t(s);
147
+ };
148
+ return i.set(n, a), this.on(e, a);
149
+ }
150
+ off(e, t) {
151
+ let n = String(e), r = this.handlers.get(n);
152
+ 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));
155
+ }
156
+ }
157
+ emit(e, t) {
158
+ let n = String(e), r = this.handlers.get(n);
159
+ if (r) for (let i of [...r]) try {
160
+ i(t);
161
+ } catch {
162
+ }
163
+ for (let i of [...this.wildcardHandlers]) try {
164
+ i(n, t);
165
+ } catch {
166
+ }
167
+ for (let [i, a] of this.patternHandlers) if (this.matchPattern(i, n)) for (let s of [...a]) try {
168
+ s(n, t);
169
+ } catch {
170
+ }
171
+ }
172
+ onWildcard(e) {
173
+ return this.wildcardHandlers.includes(e) || this.wildcardHandlers.push(e), () => {
174
+ let t = this.wildcardHandlers.indexOf(e);
175
+ t !== -1 && this.wildcardHandlers.splice(t, 1);
176
+ };
177
+ }
178
+ onPattern(e, t) {
179
+ this.patternHandlers.has(e) || this.patternHandlers.set(e, []);
180
+ let n = this.patternHandlers.get(e);
181
+ return n.includes(t) || n.push(t), () => {
182
+ let r = this.patternHandlers.get(e);
183
+ if (r) {
184
+ let i = r.indexOf(t);
185
+ i !== -1 && r.splice(i, 1);
186
+ }
187
+ };
188
+ }
189
+ clear() {
190
+ this.handlers.clear(), this.wildcardHandlers.length = 0, this.patternHandlers.clear(), this.onceWrappers.clear();
191
+ }
192
+ matchPattern(e, t) {
193
+ if (e === "*") return true;
194
+ if (e.endsWith(":*")) {
195
+ let n = e.slice(0, -2);
196
+ return t.startsWith(n + ":");
197
+ }
198
+ return e === t;
199
+ }
200
+ };
201
+ var f = class {
202
+ graph = /* @__PURE__ */ new Map();
203
+ addPlugin(e, t = []) {
204
+ this.graph.set(e, [...t]);
205
+ }
206
+ removePlugin(e) {
207
+ this.graph.delete(e);
208
+ for (let t of this.graph.values()) {
209
+ let n = t.indexOf(e);
210
+ n !== -1 && t.splice(n, 1);
211
+ }
212
+ }
213
+ validateSinglePlugin(e) {
214
+ let t = this.graph.get(e);
215
+ 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);
217
+ let n = this.detectCycle();
218
+ if (n && n.includes(e)) throw new import_types.PluginError(`Circular dependency detected: ${n.join(" -> ")}`, "dependency-resolver");
219
+ }
220
+ resolve() {
221
+ let e = /* @__PURE__ */ new Map(), t = [], n = [], r = /* @__PURE__ */ new Map();
222
+ for (let [s, o] of this.graph) {
223
+ let l = o.filter((p) => this.graph.has(p));
224
+ r.set(s, l);
225
+ }
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);
227
+ for (let [s, o] of r) e.set(s, o.length), o.length === 0 && t.push(s);
228
+ 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]);
232
+ }
233
+ let a = 0;
234
+ for (; a < t.length; ) {
235
+ let s = t[a++];
236
+ n.push(s);
237
+ for (let o of i.get(s) ?? []) e.set(o, e.get(o) - 1), e.get(o) === 0 && t.push(o);
238
+ }
239
+ if (n.length !== r.size) {
240
+ let s = this.detectCycle();
241
+ throw new import_types.PluginError(`Circular dependency detected: ${s.join(" -> ")}`, "dependency-resolver");
242
+ }
243
+ return n;
244
+ }
245
+ detectCycle() {
246
+ let r = /* @__PURE__ */ new Map();
247
+ for (let a of this.graph.keys()) r.set(a, 0);
248
+ let i = (a, s) => {
249
+ r.set(a, 1), s.push(a);
250
+ for (let o of this.graph.get(a)) if (this.graph.has(o)) {
251
+ if (r.get(o) === 1) {
252
+ let l = s.indexOf(o);
253
+ return [...s.slice(l), o];
254
+ }
255
+ if (r.get(o) === 0) {
256
+ let l = i(o, s);
257
+ if (l) return l;
258
+ }
259
+ }
260
+ return s.pop(), r.set(a, 2), null;
261
+ };
262
+ for (let a of this.graph.keys()) if (r.get(a) === 0) {
263
+ let s = i(a, []);
264
+ if (s) return s;
265
+ }
266
+ return null;
267
+ }
268
+ getGraph() {
269
+ let e = {};
270
+ for (let [t, n] of this.graph) e[t] = [...n];
271
+ return e;
272
+ }
273
+ clear() {
274
+ this.graph.clear();
275
+ }
276
+ };
277
+ var v = class {
278
+ strategy;
279
+ globalHandler;
280
+ errors = [];
281
+ constructor(e, t) {
282
+ this.strategy = e, this.globalHandler = t;
283
+ }
284
+ getStrategy() {
285
+ return this.strategy;
286
+ }
287
+ async withBoundary(e, t, n) {
288
+ try {
289
+ return await n();
290
+ } catch (r) {
291
+ let i = r instanceof Error ? r : new Error(String(r));
292
+ throw await this.handleError(e, t, i), i;
293
+ }
294
+ }
295
+ async handleError(e, t, n) {
296
+ if (t.onError) try {
297
+ t.onError(n);
298
+ } catch {
299
+ }
300
+ if (this.globalHandler) try {
301
+ this.globalHandler(n, e);
302
+ } catch {
303
+ }
304
+ this.strategy === "collect" && this.errors.push(n);
305
+ }
306
+ throwIfErrors() {
307
+ if (this.strategy === "collect" && this.errors.length > 0) {
308
+ let e = new AggregateError(this.errors, `${this.errors.length} plugin error(s) occurred`);
309
+ throw this.clearErrors(), e;
310
+ }
311
+ }
312
+ clearErrors() {
313
+ this.errors = [];
314
+ }
315
+ getErrors() {
316
+ return Object.freeze([...this.errors]);
317
+ }
318
+ };
319
+ var m = class {
320
+ async initialize(e, t, n, r, i) {
321
+ let a = r.getStrategy();
322
+ for (let s of t) {
323
+ let o = e.get(s);
324
+ if (o && o.state !== "ready") {
325
+ o.state = "initializing";
326
+ try {
327
+ await r.withBoundary(s, o.plugin, async () => {
328
+ o.plugin.onInit && await o.plugin.onInit(n);
329
+ }), o.state = "ready", o.initializedAt = Date.now(), i?.(s);
330
+ } catch (l) {
331
+ if (a === "fail-fast") throw l;
332
+ o.state = "failed";
333
+ }
334
+ }
335
+ }
336
+ }
337
+ async destroy(e, t, n, r) {
338
+ let i = n.getStrategy(), a = [...t].reverse();
339
+ for (let s of a) {
340
+ let o = e.get(s);
341
+ if (o) {
342
+ o.state = "destroying";
343
+ try {
344
+ await n.withBoundary(s, o.plugin, async () => {
345
+ o.plugin.onDestroy && await o.plugin.onDestroy();
346
+ });
347
+ } catch (l) {
348
+ if (i === "fail-fast") throw l;
349
+ }
350
+ o.state = "destroyed", r?.(s);
351
+ }
352
+ }
353
+ }
354
+ };
355
+ var c = class {
356
+ state = "created";
357
+ plugins = /* @__PURE__ */ new Map();
358
+ context;
359
+ events;
360
+ resolver;
361
+ errorBoundary;
362
+ lifecycle;
363
+ config;
364
+ pendingInits = /* @__PURE__ */ new Map();
365
+ emitInternal(e, t) {
366
+ this.events.emit(e, t);
367
+ }
368
+ 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();
370
+ }
371
+ 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");
375
+ let t = { plugin: e, state: "registered" };
376
+ this.plugins.set(e.name, t), this.resolver.addPlugin(e.name, [...e.dependencies || []]);
377
+ try {
378
+ 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") {
379
+ let n = this.initializeWithDependencies(e.name);
380
+ this.pendingInits.set(e.name, n), n.finally(() => {
381
+ this.pendingInits.delete(e.name);
382
+ });
383
+ }
384
+ } catch (n) {
385
+ throw this.pendingInits.delete(e.name), this.plugins.delete(e.name), this.resolver.removePlugin(e.name), n;
386
+ }
387
+ return this;
388
+ }
389
+ async waitForPlugin(e) {
390
+ let t = this.pendingInits.get(e);
391
+ t && await t;
392
+ }
393
+ async waitForAll() {
394
+ await Promise.all(this.pendingInits.values());
395
+ }
396
+ async initializeWithDependencies(e) {
397
+ let t = this.plugins.get(e);
398
+ if (!t) return;
399
+ let n = t.plugin.dependencies || [];
400
+ for (let r of n) {
401
+ let i = this.pendingInits.get(r);
402
+ i && await i;
403
+ }
404
+ for (let r of n) {
405
+ let i = this.plugins.get(r);
406
+ if (!i || i.state !== "ready") {
407
+ t.state = "failed";
408
+ let a = new import_types.PluginError(`Dependency '${r}' is not ready for plugin '${e}'`, e);
409
+ this.emitInternal("plugin:error", { name: e, error: a });
410
+ return;
411
+ }
412
+ }
413
+ await this.initializePlugin(e);
414
+ }
415
+ async initializePlugin(e) {
416
+ let t = this.plugins.get(e);
417
+ if (!(!t || t.state !== "registered")) try {
418
+ t.state = "initializing", await this.errorBoundary.withBoundary(e, t.plugin, async () => {
419
+ t.plugin.onInit && await t.plugin.onInit(this.context.get());
420
+ }), t.state = "ready", t.initializedAt = Date.now(), this.emitInternal("plugin:init", { name: e });
421
+ } catch (n) {
422
+ t.state = "failed", this.emitInternal("plugin:error", { name: e, error: n });
423
+ }
424
+ }
425
+ useAll(e) {
426
+ for (let t of e) this.use(t);
427
+ return this;
428
+ }
429
+ unregister(e) {
430
+ let t = this.plugins.get(e);
431
+ if (!t) return false;
432
+ let n = this.pendingInits.get(e);
433
+ return t.state === "initializing" && n ? (n.finally(() => {
434
+ if (t.plugin.onDestroy) return t.plugin.onDestroy();
435
+ }).then(() => this.emitInternal("plugin:destroy", { name: e })).catch(() => {
436
+ }), this.pendingInits.delete(e)) : t.state === "ready" && t.plugin.onDestroy && Promise.resolve().then(() => t.plugin.onDestroy()).then(() => this.emitInternal("plugin:destroy", { name: e })).catch(() => {
437
+ }), this.plugins.delete(e), this.resolver.removePlugin(e), true;
438
+ }
439
+ async unregisterAsync(e) {
440
+ let t = this.plugins.get(e);
441
+ if (!t) return false;
442
+ let n = this.pendingInits.get(e);
443
+ if (t.state === "initializing" && n) {
444
+ try {
445
+ await n;
446
+ } catch {
447
+ }
448
+ this.pendingInits.delete(e);
449
+ }
450
+ if ((t.state === "ready" || t.state === "failed") && t.plugin.onDestroy) try {
451
+ await t.plugin.onDestroy(), this.emitInternal("plugin:destroy", { name: e });
452
+ } catch {
453
+ }
454
+ return this.plugins.delete(e), this.resolver.removePlugin(e), true;
455
+ }
456
+ async replace(e) {
457
+ await this.unregisterAsync(e.name), this.use(e), this.state === "ready" && await this.waitForPlugin(e.name);
458
+ }
459
+ async reload(e) {
460
+ let t = this.plugins.get(e);
461
+ if (!t) return;
462
+ let n = this.errorBoundary.getStrategy();
463
+ if (t.state === "ready") {
464
+ try {
465
+ t.plugin.onDestroy && await this.errorBoundary.withBoundary(e, t.plugin, async () => t.plugin.onDestroy()), this.emitInternal("plugin:destroy", { name: e });
466
+ } catch (r) {
467
+ if (n === "fail-fast") throw r;
468
+ }
469
+ t.state = "registered";
470
+ try {
471
+ 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 });
472
+ } catch (r) {
473
+ if (n === "fail-fast") throw r;
474
+ t.state = "failed";
475
+ }
476
+ }
477
+ }
478
+ async init() {
479
+ if (this.state !== "created") throw new import_types.PluginError("Kernel already initialized", "kernel");
480
+ this.state = "initializing";
481
+ try {
482
+ this.config.onBeforeInit && await this.config.onBeforeInit();
483
+ let e = this.resolver.resolve();
484
+ 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();
485
+ } catch (e) {
486
+ throw this.state = "created", e;
487
+ }
488
+ }
489
+ async destroy() {
490
+ if (this.state === "destroyed") return;
491
+ this.state = "destroying", this.config.onBeforeDestroy && await this.config.onBeforeDestroy(), this.emitInternal("kernel:destroy", { timestamp: Date.now() }), await Promise.allSettled(this.pendingInits.values());
492
+ let e;
493
+ try {
494
+ e = this.resolver.resolve();
495
+ } catch {
496
+ e = Array.from(this.plugins.keys());
497
+ }
498
+ 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();
499
+ }
500
+ isInitialized() {
501
+ return this.state === "ready";
502
+ }
503
+ isDestroyed() {
504
+ return this.state === "destroyed";
505
+ }
506
+ getPlugin(e) {
507
+ return this.plugins.get(e)?.plugin;
508
+ }
509
+ hasPlugin(e) {
510
+ return this.plugins.has(e);
511
+ }
512
+ listPlugins() {
513
+ return Array.from(this.plugins.values()).map((e) => e.plugin);
514
+ }
515
+ getPluginNames() {
516
+ return Array.from(this.plugins.keys());
517
+ }
518
+ getDependencyGraph() {
519
+ return this.resolver.getGraph();
520
+ }
521
+ on(e, t) {
522
+ return this.events.on(e, t);
523
+ }
524
+ once(e, t) {
525
+ return this.events.once(e, t);
526
+ }
527
+ off(e, t) {
528
+ this.events.off(e, t);
529
+ }
530
+ emit(e, t) {
531
+ this.events.emit(e, t);
532
+ }
533
+ onWildcard(e) {
534
+ return this.events.onWildcard(e);
535
+ }
536
+ onPattern(e, t) {
537
+ return this.events.onPattern(e, t);
538
+ }
539
+ getContext() {
540
+ return this.context.get();
541
+ }
542
+ updateContext(e) {
543
+ this.context.update(e);
544
+ }
545
+ deepUpdateContext(e) {
546
+ this.context.deepUpdate(e);
547
+ }
548
+ };
549
+ function W(d) {
550
+ return new c(d);
551
+ }
95
552
 
96
553
  // src/constants.ts
97
554
  var ANSI_MODIFIERS = {
@@ -413,17 +870,17 @@ function createProxyPigment(kernel, styles = []) {
413
870
  if (prop === "supportsColor") {
414
871
  return ctx.colorSupport;
415
872
  }
416
- if (MODIFIER_NAMES.includes(prop)) {
873
+ if (MODIFIER_NAMES.some((name) => name === prop)) {
417
874
  const modifierStyle = generateModifierCode(prop);
418
875
  return createProxyPigment(kernel, [...styles, modifierStyle]);
419
876
  }
420
- if (COLOR_NAMES.includes(prop) || BACKGROUND_COLOR_NAMES.includes(prop)) {
877
+ if (COLOR_NAMES.some((name) => name === prop) || BACKGROUND_COLOR_NAMES.some((name) => name === prop)) {
421
878
  const background = prop.startsWith("bg");
422
879
  const colorName = background ? prop.slice(2) : prop;
423
880
  const colorStyle = generateColorCode(colorName, background);
424
881
  return createProxyPigment(kernel, [...styles, colorStyle]);
425
882
  }
426
- if (EXTENDED_COLOR_METHODS.includes(prop)) {
883
+ if (EXTENDED_COLOR_METHODS.some((name) => name === prop)) {
427
884
  if (prop === "ansi256" || prop === "bgAnsi256") {
428
885
  return (code) => {
429
886
  const background = prop === "bgAnsi256";
@@ -451,13 +908,13 @@ function createProxyPigment(kernel, styles = []) {
451
908
  };
452
909
  }
453
910
  if (prop === "hsl" || prop === "bgHsl") {
454
- return (h, s, l) => {
911
+ return (h2, s, l) => {
455
912
  const background = prop === "bgHsl";
456
913
  const hslToRgb2 = ctx.utils?.hslToRgb;
457
914
  if (!hslToRgb2) {
458
915
  throw new Error("hslToRgb utility not available");
459
916
  }
460
- const { r, g, b } = hslToRgb2(h, s, l);
917
+ const { r, g, b } = hslToRgb2(h2, s, l);
461
918
  const rgbStyle = generateRgbCode(r, g, b, background);
462
919
  return createProxyPigment(kernel, [...styles, rgbStyle]);
463
920
  };
@@ -465,9 +922,9 @@ function createProxyPigment(kernel, styles = []) {
465
922
  }
466
923
  if (prop === "visible") {
467
924
  const colorSupport = ctx.colorSupport;
468
- return (text) => colorSupport.level > 0 ? text : text;
925
+ return (text) => colorSupport.level > 0 ? applyStyles(String(text), styles) : "";
469
926
  }
470
- return target[prop];
927
+ return Reflect.get(target, prop);
471
928
  },
472
929
  apply(_target, _thisArg, argArray) {
473
930
  const text = String(argArray[0] ?? "");
@@ -479,19 +936,25 @@ function createProxyPigment(kernel, styles = []) {
479
936
  return pigment2;
480
937
  }
481
938
  function applyStyles(text, styles) {
482
- if (styles.length === 0) {
939
+ if (styles.length === 0 || text === "") {
483
940
  return text;
484
941
  }
485
942
  const open = styles.map((style) => style.open).join("");
486
943
  const close = [...styles].reverse().map((style) => style.close).join("");
487
- return `${open}${text}${close}`;
944
+ let result = text;
945
+ for (const style of styles) {
946
+ if (style.close) {
947
+ result = result.split(style.close).join(style.close + style.open);
948
+ }
949
+ }
950
+ return `${open}${result}${close}`;
488
951
  }
489
952
 
490
953
  // src/utils/hex-to-rgb.ts
491
954
  function hexToRgb(hex) {
492
955
  const hexString = hex.trim().replace(/^#/, "");
493
956
  if (hexString.length === 3) {
494
- const expanded = hexString.split("").map((c) => c + c).join("");
957
+ const expanded = hexString.split("").map((c2) => c2 + c2).join("");
495
958
  return parseExpandedHex(expanded);
496
959
  }
497
960
  if (hexString.length === 6) {
@@ -518,8 +981,8 @@ function validateHex(hex) {
518
981
  }
519
982
 
520
983
  // src/utils/hsl-to-rgb.ts
521
- function hslToRgb(h, s, l) {
522
- const hNormalized = h / 360;
984
+ function hslToRgb(h2, s, l) {
985
+ const hNormalized = h2 / 360;
523
986
  const sNormalized = s / 100;
524
987
  const lNormalized = l / 100;
525
988
  let r;
@@ -762,23 +1225,29 @@ var BuilderPigment = class {
762
1225
  }
763
1226
  return this;
764
1227
  }
765
- hsl(h, s, l) {
766
- const { r, g, b } = hslToRgb(h, s, l);
1228
+ hsl(h2, s, l) {
1229
+ const { r, g, b } = hslToRgb(h2, s, l);
767
1230
  this.styles.push(generateRgbCode(r, g, b, false));
768
1231
  return this;
769
1232
  }
770
- bgHsl(h, s, l) {
771
- const { r, g, b } = hslToRgb(h, s, l);
1233
+ bgHsl(h2, s, l) {
1234
+ const { r, g, b } = hslToRgb(h2, s, l);
772
1235
  this.styles.push(generateRgbCode(r, g, b, true));
773
1236
  return this;
774
1237
  }
775
1238
  paint(text) {
776
- if (this.styles.length === 0) {
1239
+ if (this.styles.length === 0 || text === "") {
777
1240
  return text;
778
1241
  }
779
1242
  const open = this.styles.map((style) => style.open).join("");
780
1243
  const close = [...this.styles].reverse().map((style) => style.close).join("");
781
- return `${open}${text}${close}`;
1244
+ let result = text;
1245
+ for (const style of this.styles) {
1246
+ if (style.close) {
1247
+ result = result.split(style.close).join(style.close + style.open);
1248
+ }
1249
+ }
1250
+ return `${open}${result}${close}`;
782
1251
  }
783
1252
  };
784
1253
 
@@ -830,23 +1299,14 @@ function detectColorSupport(overrideLevel) {
830
1299
  const platform = detectPlatform();
831
1300
  if (platform.isBrowser) {
832
1301
  cachedColorSupport = {
833
- level: 3,
1302
+ level: 1,
834
1303
  hasBasic: true,
835
- has256: true,
836
- has16m: true
837
- };
838
- return cachedColorSupport;
839
- }
840
- if (!platform.isNode) {
841
- cachedColorSupport = {
842
- level: 0,
843
- hasBasic: false,
844
1304
  has256: false,
845
1305
  has16m: false
846
1306
  };
847
1307
  return cachedColorSupport;
848
1308
  }
849
- if (process.env.NO_COLOR) {
1309
+ if (!platform.isNode) {
850
1310
  cachedColorSupport = {
851
1311
  level: 0,
852
1312
  hasBasic: false,
@@ -855,65 +1315,51 @@ function detectColorSupport(overrideLevel) {
855
1315
  };
856
1316
  return cachedColorSupport;
857
1317
  }
858
- if (process.env.FORCE_COLOR) {
859
- const level = parseInt(process.env.FORCE_COLOR, 10);
860
- const clampedLevel = Math.max(0, Math.min(3, level));
861
- cachedColorSupport = {
862
- level: clampedLevel,
863
- hasBasic: clampedLevel >= 1,
864
- has256: clampedLevel >= 2,
865
- has16m: clampedLevel >= 3
866
- };
1318
+ const env = process.env;
1319
+ const forced = parseForceColor(env.FORCE_COLOR);
1320
+ if (forced !== void 0) {
1321
+ cachedColorSupport = toColorSupport(forced);
867
1322
  return cachedColorSupport;
868
1323
  }
869
- if (!hasTTY() && !process.env.CI) {
870
- cachedColorSupport = {
871
- level: 0,
872
- hasBasic: false,
873
- has256: false,
874
- has16m: false
875
- };
1324
+ if (env.NO_COLOR) {
1325
+ cachedColorSupport = toColorSupport(0);
876
1326
  return cachedColorSupport;
877
1327
  }
878
- const term = process.env.TERM ?? "";
879
- if (term.includes("truecolor") || term.includes("24bit")) {
880
- cachedColorSupport = {
881
- level: 3,
882
- hasBasic: true,
883
- has256: true,
884
- has16m: true
885
- };
1328
+ if (!hasTTY() && !env.CI || env.TERM === "dumb") {
1329
+ cachedColorSupport = toColorSupport(0);
886
1330
  return cachedColorSupport;
887
1331
  }
888
- if (term.includes("256color")) {
889
- cachedColorSupport = {
890
- level: 2,
891
- hasBasic: true,
892
- has256: true,
893
- has16m: false
894
- };
1332
+ const term = env.TERM ?? "";
1333
+ const colorTerm = (env.COLORTERM ?? "").toLowerCase();
1334
+ if (colorTerm === "truecolor" || colorTerm === "24bit" || term.includes("truecolor") || term.includes("24bit")) {
1335
+ cachedColorSupport = toColorSupport(3);
895
1336
  return cachedColorSupport;
896
1337
  }
897
- if (process.env.CI) {
898
- cachedColorSupport = {
899
- level: 1,
900
- hasBasic: true,
901
- has256: false,
902
- has16m: false
903
- };
1338
+ if (term.includes("256color")) {
1339
+ cachedColorSupport = toColorSupport(2);
904
1340
  return cachedColorSupport;
905
1341
  }
906
- cachedColorSupport = {
907
- level: 1,
908
- hasBasic: true,
909
- has256: false,
910
- has16m: false
911
- };
1342
+ cachedColorSupport = toColorSupport(1);
912
1343
  return cachedColorSupport;
913
1344
  }
914
1345
  function supportsColor() {
915
1346
  return detectColorSupport();
916
1347
  }
1348
+ function resetColorSupportCache() {
1349
+ cachedColorSupport = void 0;
1350
+ }
1351
+ function parseForceColor(value) {
1352
+ if (value === void 0) return void 0;
1353
+ const normalized = value.trim().toLowerCase();
1354
+ if (normalized === "" || normalized === "true") return 1;
1355
+ if (normalized === "false") return 0;
1356
+ const level = Number.parseInt(normalized, 10);
1357
+ if (Number.isNaN(level)) return 1;
1358
+ return Math.max(0, Math.min(3, level));
1359
+ }
1360
+ function toColorSupport(level) {
1361
+ return { level, hasBasic: level >= 1, has256: level >= 2, has16m: level >= 3 };
1362
+ }
917
1363
 
918
1364
  // src/utils/rgb-to-ansi256.ts
919
1365
  function rgbToAnsi256(r, g, b) {
@@ -1041,11 +1487,11 @@ function trueColorPlugin() {
1041
1487
  k.emit("color:rgb", { ...rgb, background });
1042
1488
  });
1043
1489
  k.on("color:hsl", (data) => {
1044
- const { h, s, l, background } = data;
1490
+ const { h: h2, s, l, background } = data;
1045
1491
  if (!utils?.hslToRgb) {
1046
1492
  throw new Error("hslToRgb utility not available");
1047
1493
  }
1048
- const rgb = utils.hslToRgb(h, s, l);
1494
+ const rgb = utils.hslToRgb(h2, s, l);
1049
1495
  k.emit("color:rgb", { ...rgb, background });
1050
1496
  });
1051
1497
  }
@@ -1064,12 +1510,6 @@ function nestingPlugin() {
1064
1510
  enabled: true
1065
1511
  }
1066
1512
  });
1067
- k.on("style:nest", (data) => {
1068
- const { innerText, outerStyles } = data;
1069
- const open = outerStyles.join("");
1070
- const close = outerStyles.map(() => "\x1B[0m").join("");
1071
- return `${open}${innerText}${close}`;
1072
- });
1073
1513
  }
1074
1514
  };
1075
1515
  }
@@ -1082,26 +1522,28 @@ function environmentPlugin() {
1082
1522
  install(kernel) {
1083
1523
  const k = kernel;
1084
1524
  const ci = isCI();
1085
- const colorSupport = detectColorSupport();
1525
+ const existingContext = k.getContext();
1526
+ const colorSupport = existingContext.colorSupport ?? detectColorSupport();
1086
1527
  k.updateContext({
1087
1528
  environment: {
1088
1529
  isCI: ci,
1089
- noColor: process.env.NO_COLOR !== void 0,
1530
+ // NO_COLOR only counts when non-empty (no-color.org)
1531
+ noColor: Boolean(process.env.NO_COLOR),
1090
1532
  forceColor: process.env.FORCE_COLOR !== void 0,
1091
1533
  term: process.env.TERM ?? ""
1092
- },
1093
- colorSupport
1534
+ }
1535
+ // Don't overwrite colorSupport - it was already set in factory with the correct level
1094
1536
  });
1095
1537
  k.on("environment:check", () => {
1096
1538
  const ctx = k.getContext();
1097
1539
  const env = ctx.environment;
1540
+ if (env?.forceColor) {
1541
+ const level = parseForceColor(process.env.FORCE_COLOR) ?? 1;
1542
+ return { level, enabled: level > 0 };
1543
+ }
1098
1544
  if (env?.noColor) {
1099
1545
  return { level: 0, enabled: false };
1100
1546
  }
1101
- if (env?.forceColor) {
1102
- const level = parseInt(process.env.FORCE_COLOR ?? "0", 10);
1103
- return { level: Math.max(0, Math.min(3, level)), enabled: true };
1104
- }
1105
1547
  if (ci) {
1106
1548
  return { level: 1, enabled: colorSupport.hasBasic };
1107
1549
  }
@@ -1128,7 +1570,7 @@ function createPigment(options) {
1128
1570
  const noColor = options?.noColor ?? false;
1129
1571
  const forceColor = options?.forceColor ?? false;
1130
1572
  const level = noColor ? 0 : forceColor ? options?.level ?? 3 : colorSupport.level;
1131
- const kernel = (0, import_plugin.createKernel)({
1573
+ const kernel = W({
1132
1574
  context: {
1133
1575
  colorSupport: { ...colorSupport, level },
1134
1576
  level,
@@ -1154,7 +1596,7 @@ function createPigment(options) {
1154
1596
  kernel.use(plugin);
1155
1597
  }
1156
1598
  }
1157
- kernel.init();
1599
+ void kernel.init();
1158
1600
  const pigment2 = createProxyPigment(kernel);
1159
1601
  const builder = new BuilderPigment(kernel);
1160
1602
  Object.defineProperty(pigment2, "builder", { value: builder, enumerable: false });
@@ -1300,8 +1742,9 @@ var Chalk = class {
1300
1742
  reset(text) {
1301
1743
  return this.pigment.reset(text);
1302
1744
  }
1745
+ /** Like chalk: returns the text only when colors are supported, otherwise ''. */
1303
1746
  visible(text) {
1304
- return this.supportsColor.level > 0 ? text : text;
1747
+ return this.supportsColor.level > 0 ? text : "";
1305
1748
  }
1306
1749
  rgb(r, g, b) {
1307
1750
  const coloredPigment = this.pigment.rgb(r, g, b);
@@ -1393,9 +1836,6 @@ function compose(...stylers) {
1393
1836
  };
1394
1837
  }
1395
1838
 
1396
- // src/index.ts
1397
- var import_plugin2 = require("@oxog/plugin");
1398
-
1399
1839
  // src/plugins/index.ts
1400
1840
  var plugins_exports = {};
1401
1841
  __export(plugins_exports, {
@@ -1702,7 +2142,8 @@ function parseStyleExpression(expression, text, kernel) {
1702
2142
  }
1703
2143
  }
1704
2144
  if (typeof current === "function") {
1705
- return current(text);
2145
+ const style = current;
2146
+ return style(text);
1706
2147
  }
1707
2148
  return text;
1708
2149
  }
@@ -1718,7 +2159,7 @@ function pigmentPlugin(options) {
1718
2159
  const k = kernel;
1719
2160
  const themeColors = THEME_PRESETS[theme] ?? THEME_PRESETS.monokai;
1720
2161
  const pigment2 = createPigment({ level });
1721
- k.pigment = pigment2;
2162
+ Object.assign(k, { pigment: pigment2 });
1722
2163
  k.updateContext({
1723
2164
  ecosystem: {
1724
2165
  theme,
@@ -1871,6 +2312,7 @@ var index_default = chalk_default_default;
1871
2312
  red,
1872
2313
  redBright,
1873
2314
  reset,
2315
+ resetColorSupportCache,
1874
2316
  rgbToAnsi256,
1875
2317
  strikethrough,
1876
2318
  supportsColor,