@oxog/pigment 1.0.2 → 1.1.1

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
@@ -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
  var __defProp = Object.defineProperty;
@@ -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 = {
@@ -232,6 +688,12 @@ var PigmentError = class _PigmentError extends Error {
232
688
  Error.captureStackTrace(this, _PigmentError);
233
689
  }
234
690
  };
691
+ var PluginError3 = class extends PigmentError {
692
+ constructor(pluginName, message) {
693
+ super(`[${pluginName}] ${message}`, "PLUGIN_ERROR");
694
+ this.name = "PluginError";
695
+ }
696
+ };
235
697
  var ColorError = class extends PigmentError {
236
698
  constructor(message) {
237
699
  super(message, "COLOR_ERROR");
@@ -245,6 +707,23 @@ var StyleError = class extends PigmentError {
245
707
  }
246
708
  };
247
709
 
710
+ // src/utils/rgb-to-ansi256.ts
711
+ function rgbToAnsi256(r, g, b) {
712
+ const clampedR = Math.max(0, Math.min(255, r));
713
+ const clampedG = Math.max(0, Math.min(255, g));
714
+ const clampedB = Math.max(0, Math.min(255, b));
715
+ if (clampedR === clampedG && clampedG === clampedB) {
716
+ if (clampedR < 8) {
717
+ return 16;
718
+ }
719
+ if (clampedR > 248) {
720
+ return 231;
721
+ }
722
+ return Math.round((clampedR - 8) / 247 * 24) + 232;
723
+ }
724
+ return Math.round(clampedR / 51) * 36 + Math.round(clampedG / 51) * 6 + Math.round(clampedB / 51) + 16;
725
+ }
726
+
248
727
  // src/core/ansi.ts
249
728
  function generateModifierCode(name) {
250
729
  const modifier = ANSI_MODIFIERS[name];
@@ -318,6 +797,141 @@ function generateRgbCode(r, g, b, background = false) {
318
797
  background
319
798
  };
320
799
  }
800
+ function ansi256ToAnsi16(code) {
801
+ if (code < 8) {
802
+ return 30 + code;
803
+ }
804
+ if (code < 16) {
805
+ return 90 + (code - 8);
806
+ }
807
+ let red2;
808
+ let green2;
809
+ let blue2;
810
+ if (code >= 232) {
811
+ red2 = ((code - 232) * 10 + 8) / 255;
812
+ green2 = red2;
813
+ blue2 = red2;
814
+ } else {
815
+ const index = code - 16;
816
+ const remainder = index % 36;
817
+ red2 = Math.floor(index / 36) / 5;
818
+ green2 = Math.floor(remainder / 6) / 5;
819
+ blue2 = remainder % 6 / 5;
820
+ }
821
+ const value = Math.max(red2, green2, blue2) * 2;
822
+ if (value === 0) {
823
+ return 30;
824
+ }
825
+ let result = 30 + (Math.round(blue2) << 2 | Math.round(green2) << 1 | Math.round(red2));
826
+ if (value === 2) {
827
+ result += 60;
828
+ }
829
+ return result;
830
+ }
831
+ function effectiveLevel(level) {
832
+ return level ?? 3;
833
+ }
834
+ function ansi16Style(name, paletteCode, background) {
835
+ const code = ansi256ToAnsi16(paletteCode) + (background ? 10 : 0);
836
+ return {
837
+ name,
838
+ code,
839
+ open: `\x1B[${code}m`,
840
+ close: `\x1B[${background ? 49 : 39}m`,
841
+ type: "color",
842
+ background
843
+ };
844
+ }
845
+ function generateAnsi256CodeForLevel(code, background, level) {
846
+ const style = generateAnsi256Code(code, background);
847
+ if (effectiveLevel(level) >= 2) {
848
+ return style;
849
+ }
850
+ return ansi16Style(style.name, code, background);
851
+ }
852
+ function generateRgbCodeForLevel(r, g, b, background, level) {
853
+ const style = generateRgbCode(r, g, b, background);
854
+ const effective = effectiveLevel(level);
855
+ if (effective >= 3) {
856
+ return style;
857
+ }
858
+ const paletteCode = rgbToAnsi256(r, g, b);
859
+ if (effective === 2) {
860
+ return { ...generateAnsi256Code(paletteCode, background), name: style.name };
861
+ }
862
+ return ansi16Style(style.name, paletteCode, background);
863
+ }
864
+ function paintRgb(text, rgb, background, level) {
865
+ if (level === 0 || text === "") {
866
+ return text;
867
+ }
868
+ const style = generateRgbCodeForLevel(rgb.r, rgb.g, rgb.b, background, level);
869
+ return `${style.open}${text}${style.close}`;
870
+ }
871
+
872
+ // src/core/plugin-api.ts
873
+ var REGISTRY_KEY = /* @__PURE__ */ Symbol.for("@oxog/pigment.plugin-api");
874
+ var globalRegistry = globalThis;
875
+ var registry = globalRegistry[REGISTRY_KEY] ??= { apis: /* @__PURE__ */ new WeakMap(), roots: /* @__PURE__ */ new WeakMap() };
876
+ var { apis, roots } = registry;
877
+ var PLUGIN_API_PROVIDERS = {
878
+ gradient: "gradientPlugin",
879
+ rainbow: "gradientPlugin",
880
+ theme: "themePlugin",
881
+ success: "semanticPlugin",
882
+ error: "semanticPlugin",
883
+ warning: "semanticPlugin",
884
+ info: "semanticPlugin",
885
+ debug: "semanticPlugin",
886
+ box: "boxPlugin",
887
+ template: "templatePlugin"
888
+ };
889
+ function registerPluginApi(kernel, name, value) {
890
+ let api = apis.get(kernel);
891
+ if (!api) {
892
+ api = /* @__PURE__ */ new Map();
893
+ apis.set(kernel, api);
894
+ }
895
+ api.set(name, value);
896
+ }
897
+ function getPluginApi(kernel, name) {
898
+ return apis.get(kernel)?.get(name);
899
+ }
900
+ function registerRoot(kernel, root) {
901
+ roots.set(kernel, root);
902
+ }
903
+ function getRoot(kernel) {
904
+ return roots.get(kernel);
905
+ }
906
+ function missingPluginApi(name) {
907
+ const plugin = PLUGIN_API_PROVIDERS[name];
908
+ const fail = () => {
909
+ throw new PluginError3(
910
+ plugin,
911
+ `pigment.${name} needs ${plugin}(): createPigment({ plugins: [${plugin}()] }) or pigment.use(${plugin}())`
912
+ );
913
+ };
914
+ if (name === "theme") {
915
+ return {
916
+ keyword: fail,
917
+ string: fail,
918
+ number: fail,
919
+ comment: fail,
920
+ error: fail,
921
+ success: fail,
922
+ warning: fail,
923
+ info: fail
924
+ };
925
+ }
926
+ return fail;
927
+ }
928
+ function installPlugin(kernel, plugin) {
929
+ kernel.use(plugin);
930
+ if (plugin.onInit) {
931
+ Promise.resolve().then(() => plugin.onInit?.(kernel.getContext())).catch(() => {
932
+ });
933
+ }
934
+ }
321
935
 
322
936
  // src/core/proxy.ts
323
937
  var proxyCache = /* @__PURE__ */ new WeakMap();
@@ -331,28 +945,28 @@ function createProxyPigment(kernel, styles = []) {
331
945
  if (prop === "supportsColor") {
332
946
  return ctx.colorSupport;
333
947
  }
334
- if (MODIFIER_NAMES.includes(prop)) {
948
+ if (MODIFIER_NAMES.some((name) => name === prop)) {
335
949
  const modifierStyle = generateModifierCode(prop);
336
950
  return createProxyPigment(kernel, [...styles, modifierStyle]);
337
951
  }
338
- if (COLOR_NAMES.includes(prop) || BACKGROUND_COLOR_NAMES.includes(prop)) {
952
+ if (COLOR_NAMES.some((name) => name === prop) || BACKGROUND_COLOR_NAMES.some((name) => name === prop)) {
339
953
  const background = prop.startsWith("bg");
340
954
  const colorName = background ? prop.slice(2) : prop;
341
955
  const colorStyle = generateColorCode(colorName, background);
342
956
  return createProxyPigment(kernel, [...styles, colorStyle]);
343
957
  }
344
- if (EXTENDED_COLOR_METHODS.includes(prop)) {
958
+ if (EXTENDED_COLOR_METHODS.some((name) => name === prop)) {
345
959
  if (prop === "ansi256" || prop === "bgAnsi256") {
346
960
  return (code) => {
347
961
  const background = prop === "bgAnsi256";
348
- const ansi256Style = generateAnsi256Code(code, background);
962
+ const ansi256Style = generateAnsi256CodeForLevel(code, background, ctx.level);
349
963
  return createProxyPigment(kernel, [...styles, ansi256Style]);
350
964
  };
351
965
  }
352
966
  if (prop === "rgb" || prop === "bgRgb") {
353
967
  return (r, g, b) => {
354
968
  const background = prop === "bgRgb";
355
- const rgbStyle = generateRgbCode(r, g, b, background);
969
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
356
970
  return createProxyPigment(kernel, [...styles, rgbStyle]);
357
971
  };
358
972
  }
@@ -364,40 +978,49 @@ function createProxyPigment(kernel, styles = []) {
364
978
  throw new Error("hexToRgb utility not available");
365
979
  }
366
980
  const { r, g, b } = hexToRgb2(color);
367
- const rgbStyle = generateRgbCode(r, g, b, background);
981
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
368
982
  return createProxyPigment(kernel, [...styles, rgbStyle]);
369
983
  };
370
984
  }
371
985
  if (prop === "hsl" || prop === "bgHsl") {
372
- return (h, s, l) => {
986
+ return (h2, s, l) => {
373
987
  const background = prop === "bgHsl";
374
988
  const hslToRgb2 = ctx.utils?.hslToRgb;
375
989
  if (!hslToRgb2) {
376
990
  throw new Error("hslToRgb utility not available");
377
991
  }
378
- const { r, g, b } = hslToRgb2(h, s, l);
379
- const rgbStyle = generateRgbCode(r, g, b, background);
992
+ const { r, g, b } = hslToRgb2(h2, s, l);
993
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
380
994
  return createProxyPigment(kernel, [...styles, rgbStyle]);
381
995
  };
382
996
  }
383
997
  }
384
998
  if (prop === "visible") {
385
999
  const colorSupport = ctx.colorSupport;
386
- return (text) => colorSupport.level > 0 ? text : "";
1000
+ return (text) => colorSupport.level > 0 ? applyStyles(String(text), styles, ctx.level) : "";
1001
+ }
1002
+ if (prop === "use") {
1003
+ return (plugin) => {
1004
+ installPlugin(kernel, plugin);
1005
+ return getRoot(kernel) ?? createProxyPigment(kernel);
1006
+ };
1007
+ }
1008
+ if (Object.prototype.hasOwnProperty.call(PLUGIN_API_PROVIDERS, prop)) {
1009
+ return getPluginApi(kernel, prop) ?? missingPluginApi(prop);
387
1010
  }
388
- return target[prop];
1011
+ return Reflect.get(target, prop);
389
1012
  },
390
1013
  apply(_target, _thisArg, argArray) {
391
1014
  const text = String(argArray[0] ?? "");
392
- return applyStyles(text, styles);
1015
+ return applyStyles(text, styles, kernel.getContext().level);
393
1016
  }
394
1017
  };
395
1018
  const pigment2 = new Proxy((() => ""), handler);
396
1019
  proxyCache.set(pigment2, pigment2);
397
1020
  return pigment2;
398
1021
  }
399
- function applyStyles(text, styles) {
400
- if (styles.length === 0 || text === "") {
1022
+ function applyStyles(text, styles, level) {
1023
+ if (styles.length === 0 || text === "" || level === 0) {
401
1024
  return text;
402
1025
  }
403
1026
  const open = styles.map((style) => style.open).join("");
@@ -415,7 +1038,7 @@ function applyStyles(text, styles) {
415
1038
  function hexToRgb(hex) {
416
1039
  const hexString = hex.trim().replace(/^#/, "");
417
1040
  if (hexString.length === 3) {
418
- const expanded = hexString.split("").map((c) => c + c).join("");
1041
+ const expanded = hexString.split("").map((c2) => c2 + c2).join("");
419
1042
  return parseExpandedHex(expanded);
420
1043
  }
421
1044
  if (hexString.length === 6) {
@@ -442,8 +1065,8 @@ function validateHex(hex) {
442
1065
  }
443
1066
 
444
1067
  // src/utils/hsl-to-rgb.ts
445
- function hslToRgb(h, s, l) {
446
- const hNormalized = h / 360;
1068
+ function hslToRgb(h2, s, l) {
1069
+ const hNormalized = h2 / 360;
447
1070
  const sNormalized = s / 100;
448
1071
  const lNormalized = l / 100;
449
1072
  let r;
@@ -476,7 +1099,13 @@ function hslToRgb(h, s, l) {
476
1099
  // src/core/builder.ts
477
1100
  var BuilderPigment = class {
478
1101
  styles = [];
479
- constructor(_kernel) {
1102
+ kernel;
1103
+ constructor(kernel) {
1104
+ this.kernel = kernel;
1105
+ }
1106
+ /** The instance's color level (true color when there is no kernel). */
1107
+ get level() {
1108
+ return this.kernel?.getContext().level;
480
1109
  }
481
1110
  bold() {
482
1111
  this.styles.push(generateModifierCode("bold"));
@@ -655,26 +1284,26 @@ var BuilderPigment = class {
655
1284
  return this;
656
1285
  }
657
1286
  ansi256(code) {
658
- this.styles.push(generateAnsi256Code(code, false));
1287
+ this.styles.push(generateAnsi256CodeForLevel(code, false, this.level));
659
1288
  return this;
660
1289
  }
661
1290
  bgAnsi256(code) {
662
- this.styles.push(generateAnsi256Code(code, true));
1291
+ this.styles.push(generateAnsi256CodeForLevel(code, true, this.level));
663
1292
  return this;
664
1293
  }
665
1294
  rgb(r, g, b) {
666
- this.styles.push(generateRgbCode(r, g, b, false));
1295
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
667
1296
  return this;
668
1297
  }
669
1298
  bgRgb(r, g, b) {
670
- this.styles.push(generateRgbCode(r, g, b, true));
1299
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
671
1300
  return this;
672
1301
  }
673
1302
  hex(color) {
674
1303
  const rgb = hexToRgb(color);
675
1304
  if (rgb) {
676
1305
  const { r, g, b } = rgb;
677
- this.styles.push(generateRgbCode(r, g, b, false));
1306
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
678
1307
  }
679
1308
  return this;
680
1309
  }
@@ -682,22 +1311,22 @@ var BuilderPigment = class {
682
1311
  const rgb = hexToRgb(color);
683
1312
  if (rgb) {
684
1313
  const { r, g, b } = rgb;
685
- this.styles.push(generateRgbCode(r, g, b, true));
1314
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
686
1315
  }
687
1316
  return this;
688
1317
  }
689
- hsl(h, s, l) {
690
- const { r, g, b } = hslToRgb(h, s, l);
691
- this.styles.push(generateRgbCode(r, g, b, false));
1318
+ hsl(h2, s, l) {
1319
+ const { r, g, b } = hslToRgb(h2, s, l);
1320
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
692
1321
  return this;
693
1322
  }
694
- bgHsl(h, s, l) {
695
- const { r, g, b } = hslToRgb(h, s, l);
696
- this.styles.push(generateRgbCode(r, g, b, true));
1323
+ bgHsl(h2, s, l) {
1324
+ const { r, g, b } = hslToRgb(h2, s, l);
1325
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
697
1326
  return this;
698
1327
  }
699
1328
  paint(text) {
700
- if (this.styles.length === 0 || text === "") {
1329
+ if (this.styles.length === 0 || text === "" || this.level === 0) {
701
1330
  return text;
702
1331
  }
703
1332
  const open = this.styles.map((style) => style.open).join("");
@@ -776,69 +1405,31 @@ function detectColorSupport(overrideLevel) {
776
1405
  };
777
1406
  return cachedColorSupport;
778
1407
  }
779
- if (process.env.NO_COLOR) {
780
- cachedColorSupport = {
781
- level: 0,
782
- hasBasic: false,
783
- has256: false,
784
- has16m: false
785
- };
1408
+ const env = process.env;
1409
+ const forced = parseForceColor(env.FORCE_COLOR);
1410
+ if (forced !== void 0) {
1411
+ cachedColorSupport = toColorSupport(forced);
786
1412
  return cachedColorSupport;
787
1413
  }
788
- if (process.env.FORCE_COLOR) {
789
- const level = parseInt(process.env.FORCE_COLOR, 10);
790
- const clampedLevel = Math.max(0, Math.min(3, level));
791
- cachedColorSupport = {
792
- level: clampedLevel,
793
- hasBasic: clampedLevel >= 1,
794
- has256: clampedLevel >= 2,
795
- has16m: clampedLevel >= 3
796
- };
1414
+ if (env.NO_COLOR) {
1415
+ cachedColorSupport = toColorSupport(0);
797
1416
  return cachedColorSupport;
798
1417
  }
799
- if (!hasTTY() && !process.env.CI) {
800
- cachedColorSupport = {
801
- level: 0,
802
- hasBasic: false,
803
- has256: false,
804
- has16m: false
805
- };
1418
+ if (!hasTTY() && !env.CI || env.TERM === "dumb") {
1419
+ cachedColorSupport = toColorSupport(0);
806
1420
  return cachedColorSupport;
807
1421
  }
808
- const term = process.env.TERM ?? "";
809
- if (term.includes("truecolor") || term.includes("24bit")) {
810
- cachedColorSupport = {
811
- level: 3,
812
- hasBasic: true,
813
- has256: true,
814
- has16m: true
815
- };
1422
+ const term = env.TERM ?? "";
1423
+ const colorTerm = (env.COLORTERM ?? "").toLowerCase();
1424
+ if (colorTerm === "truecolor" || colorTerm === "24bit" || term.includes("truecolor") || term.includes("24bit")) {
1425
+ cachedColorSupport = toColorSupport(3);
816
1426
  return cachedColorSupport;
817
1427
  }
818
1428
  if (term.includes("256color")) {
819
- cachedColorSupport = {
820
- level: 2,
821
- hasBasic: true,
822
- has256: true,
823
- has16m: false
824
- };
825
- return cachedColorSupport;
826
- }
827
- if (process.env.CI) {
828
- cachedColorSupport = {
829
- level: 1,
830
- hasBasic: true,
831
- has256: false,
832
- has16m: false
833
- };
1429
+ cachedColorSupport = toColorSupport(2);
834
1430
  return cachedColorSupport;
835
1431
  }
836
- cachedColorSupport = {
837
- level: 1,
838
- hasBasic: true,
839
- has256: false,
840
- has16m: false
841
- };
1432
+ cachedColorSupport = toColorSupport(1);
842
1433
  return cachedColorSupport;
843
1434
  }
844
1435
  function supportsColor() {
@@ -847,22 +1438,17 @@ function supportsColor() {
847
1438
  function resetColorSupportCache() {
848
1439
  cachedColorSupport = void 0;
849
1440
  }
850
-
851
- // src/utils/rgb-to-ansi256.ts
852
- function rgbToAnsi256(r, g, b) {
853
- const clampedR = Math.max(0, Math.min(255, r));
854
- const clampedG = Math.max(0, Math.min(255, g));
855
- const clampedB = Math.max(0, Math.min(255, b));
856
- if (clampedR === clampedG && clampedG === clampedB) {
857
- if (clampedR < 8) {
858
- return 16;
859
- }
860
- if (clampedR > 248) {
861
- return 231;
862
- }
863
- return Math.round((clampedR - 8) / 247 * 24) + 232;
864
- }
865
- return Math.round(clampedR / 51) * 36 + Math.round(clampedG / 51) * 6 + Math.round(clampedB / 51) + 16;
1441
+ function parseForceColor(value) {
1442
+ if (value === void 0) return void 0;
1443
+ const normalized = value.trim().toLowerCase();
1444
+ if (normalized === "" || normalized === "true") return 1;
1445
+ if (normalized === "false") return 0;
1446
+ const level = Number.parseInt(normalized, 10);
1447
+ if (Number.isNaN(level)) return 1;
1448
+ return Math.max(0, Math.min(3, level));
1449
+ }
1450
+ function toColorSupport(level) {
1451
+ return { level, hasBasic: level >= 1, has256: level >= 2, has16m: level >= 3 };
866
1452
  }
867
1453
 
868
1454
  // src/plugins/core/base-colors.ts
@@ -974,11 +1560,11 @@ function trueColorPlugin() {
974
1560
  k.emit("color:rgb", { ...rgb, background });
975
1561
  });
976
1562
  k.on("color:hsl", (data) => {
977
- const { h, s, l, background } = data;
1563
+ const { h: h2, s, l, background } = data;
978
1564
  if (!utils?.hslToRgb) {
979
1565
  throw new Error("hslToRgb utility not available");
980
1566
  }
981
- const rgb = utils.hslToRgb(h, s, l);
1567
+ const rgb = utils.hslToRgb(h2, s, l);
982
1568
  k.emit("color:rgb", { ...rgb, background });
983
1569
  });
984
1570
  }
@@ -1014,7 +1600,8 @@ function environmentPlugin() {
1014
1600
  k.updateContext({
1015
1601
  environment: {
1016
1602
  isCI: ci,
1017
- noColor: process.env.NO_COLOR !== void 0,
1603
+ // NO_COLOR only counts when non-empty (no-color.org)
1604
+ noColor: Boolean(process.env.NO_COLOR),
1018
1605
  forceColor: process.env.FORCE_COLOR !== void 0,
1019
1606
  term: process.env.TERM ?? ""
1020
1607
  }
@@ -1023,13 +1610,13 @@ function environmentPlugin() {
1023
1610
  k.on("environment:check", () => {
1024
1611
  const ctx = k.getContext();
1025
1612
  const env = ctx.environment;
1613
+ if (env?.forceColor) {
1614
+ const level = parseForceColor(process.env.FORCE_COLOR) ?? 1;
1615
+ return { level, enabled: level > 0 };
1616
+ }
1026
1617
  if (env?.noColor) {
1027
1618
  return { level: 0, enabled: false };
1028
1619
  }
1029
- if (env?.forceColor) {
1030
- const level = parseInt(process.env.FORCE_COLOR ?? "0", 10);
1031
- return { level: Math.max(0, Math.min(3, level)), enabled: true };
1032
- }
1033
1620
  if (ci) {
1034
1621
  return { level: 1, enabled: colorSupport.hasBasic };
1035
1622
  }
@@ -1056,7 +1643,7 @@ function createPigment(options) {
1056
1643
  const noColor = options?.noColor ?? false;
1057
1644
  const forceColor = options?.forceColor ?? false;
1058
1645
  const level = noColor ? 0 : forceColor ? options?.level ?? 3 : colorSupport.level;
1059
- const kernel = createKernel({
1646
+ const kernel = W({
1060
1647
  context: {
1061
1648
  colorSupport: { ...colorSupport, level },
1062
1649
  level,
@@ -1079,13 +1666,12 @@ function createPigment(options) {
1079
1666
  kernel.use(environmentPlugin());
1080
1667
  if (options?.plugins) {
1081
1668
  for (const plugin of options.plugins) {
1082
- kernel.use(plugin);
1669
+ installPlugin(kernel, plugin);
1083
1670
  }
1084
1671
  }
1085
- kernel.init();
1086
1672
  const pigment2 = createProxyPigment(kernel);
1087
- const builder = new BuilderPigment(kernel);
1088
- Object.defineProperty(pigment2, "builder", { value: builder, enumerable: false });
1673
+ registerRoot(kernel, pigment2);
1674
+ Object.defineProperty(pigment2, "builder", { get: () => new BuilderPigment(kernel), enumerable: false });
1089
1675
  Object.defineProperty(pigment2, "level", { value: level, enumerable: true });
1090
1676
  Object.defineProperty(pigment2, "supportsColor", { value: colorSupport, enumerable: true });
1091
1677
  return pigment2;
@@ -1093,169 +1679,55 @@ function createPigment(options) {
1093
1679
 
1094
1680
  // src/core/chalk.ts
1095
1681
  var Chalk = class {
1096
- level;
1097
1682
  supportsColor;
1683
+ currentLevel;
1098
1684
  pigment;
1099
1685
  constructor(options) {
1100
1686
  const colorSupport = detectColorSupport(options?.level);
1101
- this.level = options?.level ?? colorSupport.level;
1687
+ this.currentLevel = options?.level ?? colorSupport.level;
1102
1688
  this.supportsColor = colorSupport;
1103
- this.pigment = createPigment({ level: this.level });
1104
- }
1105
- red(text) {
1106
- return this.pigment.red(text);
1689
+ this.pigment = createPigment({ level: this.currentLevel });
1107
1690
  }
1108
- green(text) {
1109
- return this.pigment.green(text);
1691
+ /** Color level (0-3). Setting it changes the output of every style. */
1692
+ get level() {
1693
+ return this.currentLevel;
1110
1694
  }
1111
- yellow(text) {
1112
- return this.pigment.yellow(text);
1113
- }
1114
- blue(text) {
1115
- return this.pigment.blue(text);
1116
- }
1117
- magenta(text) {
1118
- return this.pigment.magenta(text);
1119
- }
1120
- cyan(text) {
1121
- return this.pigment.cyan(text);
1122
- }
1123
- white(text) {
1124
- return this.pigment.white(text);
1125
- }
1126
- black(text) {
1127
- return this.pigment.black(text);
1128
- }
1129
- redBright(text) {
1130
- return this.pigment.redBright(text);
1131
- }
1132
- greenBright(text) {
1133
- return this.pigment.greenBright(text);
1134
- }
1135
- yellowBright(text) {
1136
- return this.pigment.yellowBright(text);
1137
- }
1138
- blueBright(text) {
1139
- return this.pigment.blueBright(text);
1140
- }
1141
- magentaBright(text) {
1142
- return this.pigment.magentaBright(text);
1143
- }
1144
- cyanBright(text) {
1145
- return this.pigment.cyanBright(text);
1146
- }
1147
- whiteBright(text) {
1148
- return this.pigment.whiteBright(text);
1149
- }
1150
- gray(text) {
1151
- return this.pigment.gray(text);
1152
- }
1153
- grey(text) {
1154
- return this.pigment.grey(text);
1155
- }
1156
- bgRed(text) {
1157
- return this.pigment.bgRed(text);
1158
- }
1159
- bgGreen(text) {
1160
- return this.pigment.bgGreen(text);
1161
- }
1162
- bgYellow(text) {
1163
- return this.pigment.bgYellow(text);
1164
- }
1165
- bgBlue(text) {
1166
- return this.pigment.bgBlue(text);
1167
- }
1168
- bgMagenta(text) {
1169
- return this.pigment.bgMagenta(text);
1170
- }
1171
- bgCyan(text) {
1172
- return this.pigment.bgCyan(text);
1173
- }
1174
- bgWhite(text) {
1175
- return this.pigment.bgWhite(text);
1176
- }
1177
- bgBlack(text) {
1178
- return this.pigment.bgBlack(text);
1179
- }
1180
- bgRedBright(text) {
1181
- return this.pigment.bgRedBright(text);
1182
- }
1183
- bgGreenBright(text) {
1184
- return this.pigment.bgGreenBright(text);
1185
- }
1186
- bgYellowBright(text) {
1187
- return this.pigment.bgYellowBright(text);
1188
- }
1189
- bgBlueBright(text) {
1190
- return this.pigment.bgBlueBright(text);
1191
- }
1192
- bgMagentaBright(text) {
1193
- return this.pigment.bgMagentaBright(text);
1194
- }
1195
- bgCyanBright(text) {
1196
- return this.pigment.bgCyanBright(text);
1197
- }
1198
- bgWhiteBright(text) {
1199
- return this.pigment.bgWhiteBright(text);
1200
- }
1201
- bgGray(text) {
1202
- return this.pigment.bgGray(text);
1203
- }
1204
- bgGrey(text) {
1205
- return this.pigment.bgGrey(text);
1206
- }
1207
- bold(text) {
1208
- return this.pigment.bold(text);
1209
- }
1210
- dim(text) {
1211
- return this.pigment.dim(text);
1212
- }
1213
- italic(text) {
1214
- return this.pigment.italic(text);
1215
- }
1216
- underline(text) {
1217
- return this.pigment.underline(text);
1218
- }
1219
- strikethrough(text) {
1220
- return this.pigment.strikethrough(text);
1221
- }
1222
- inverse(text) {
1223
- return this.pigment.inverse(text);
1224
- }
1225
- hidden(text) {
1226
- return this.pigment.hidden(text);
1227
- }
1228
- reset(text) {
1229
- return this.pigment.reset(text);
1695
+ set level(level) {
1696
+ this.currentLevel = level;
1697
+ this.pigment = createPigment({ level });
1230
1698
  }
1699
+ /** Like chalk: returns the text only when colors are enabled, otherwise ''. */
1231
1700
  visible(text) {
1232
- return this.supportsColor.level > 0 ? text : text;
1701
+ return this.currentLevel > 0 ? text : "";
1233
1702
  }
1234
1703
  rgb(r, g, b) {
1235
- const coloredPigment = this.pigment.rgb(r, g, b);
1236
- return (text) => coloredPigment(text);
1704
+ return this.pigment.rgb(r, g, b);
1237
1705
  }
1238
1706
  bgRgb(r, g, b) {
1239
- const coloredPigment = this.pigment.bgRgb(r, g, b);
1240
- return (text) => coloredPigment(text);
1707
+ return this.pigment.bgRgb(r, g, b);
1241
1708
  }
1242
1709
  hex(color) {
1243
- const coloredPigment = this.pigment.hex(color);
1244
- return (text) => coloredPigment(text);
1710
+ return this.pigment.hex(color);
1245
1711
  }
1246
1712
  bgHex(color) {
1247
- const coloredPigment = this.pigment.bgHex(color);
1248
- return (text) => coloredPigment(text);
1713
+ return this.pigment.bgHex(color);
1249
1714
  }
1250
1715
  ansi256(code) {
1251
- const coloredPigment = this.pigment.ansi256(code);
1252
- return (text) => coloredPigment(text);
1716
+ return this.pigment.ansi256(code);
1253
1717
  }
1254
1718
  bgAnsi256(code) {
1255
- const coloredPigment = this.pigment.bgAnsi256(code);
1256
- return (text) => coloredPigment(text);
1719
+ return this.pigment.bgAnsi256(code);
1257
1720
  }
1258
1721
  };
1722
+ for (const name of [...MODIFIER_NAMES, ...COLOR_NAMES, ...BACKGROUND_COLOR_NAMES]) {
1723
+ Object.defineProperty(Chalk.prototype, name, {
1724
+ configurable: true,
1725
+ enumerable: false,
1726
+ get() {
1727
+ return this.pigment[name];
1728
+ }
1729
+ });
1730
+ }
1259
1731
 
1260
1732
  // src/core/chalk-default.ts
1261
1733
  var chalk = new Chalk();
@@ -1271,48 +1743,51 @@ function applyStyle(text, open, close) {
1271
1743
  }
1272
1744
 
1273
1745
  // src/core/composer.ts
1274
- var bold = (text) => applyStyle(text, "\x1B[1m", "\x1B[22m");
1275
- var dim = (text) => applyStyle(text, "\x1B[2m", "\x1B[22m");
1276
- var italic = (text) => applyStyle(text, "\x1B[3m", "\x1B[23m");
1277
- var underline = (text) => applyStyle(text, "\x1B[4m", "\x1B[24m");
1278
- var strikethrough = (text) => applyStyle(text, "\x1B[9m", "\x1B[29m");
1279
- var inverse = (text) => applyStyle(text, "\x1B[7m", "\x1B[27m");
1280
- var hidden = (text) => applyStyle(text, "\x1B[8m", "\x1B[28m");
1281
- var reset = (text) => applyStyle(text, "\x1B[0m", "\x1B[0m");
1282
- var black = (text) => applyStyle(text, "\x1B[30m", "\x1B[39m");
1283
- var red = (text) => applyStyle(text, "\x1B[31m", "\x1B[39m");
1284
- var green = (text) => applyStyle(text, "\x1B[32m", "\x1B[39m");
1285
- var yellow = (text) => applyStyle(text, "\x1B[33m", "\x1B[39m");
1286
- var blue = (text) => applyStyle(text, "\x1B[34m", "\x1B[39m");
1287
- var magenta = (text) => applyStyle(text, "\x1B[35m", "\x1B[39m");
1288
- var cyan = (text) => applyStyle(text, "\x1B[36m", "\x1B[39m");
1289
- var white = (text) => applyStyle(text, "\x1B[37m", "\x1B[39m");
1290
- var blackBright = (text) => applyStyle(text, "\x1B[90m", "\x1B[39m");
1291
- var redBright = (text) => applyStyle(text, "\x1B[91m", "\x1B[39m");
1292
- var greenBright = (text) => applyStyle(text, "\x1B[92m", "\x1B[39m");
1293
- var yellowBright = (text) => applyStyle(text, "\x1B[93m", "\x1B[39m");
1294
- var blueBright = (text) => applyStyle(text, "\x1B[94m", "\x1B[39m");
1295
- var magentaBright = (text) => applyStyle(text, "\x1B[95m", "\x1B[39m");
1296
- var cyanBright = (text) => applyStyle(text, "\x1B[96m", "\x1B[39m");
1297
- var whiteBright = (text) => applyStyle(text, "\x1B[97m", "\x1B[39m");
1746
+ function applyStyle2(text, open, close) {
1747
+ return detectColorSupport().level === 0 ? text : applyStyle(text, open, close);
1748
+ }
1749
+ var bold = (text) => applyStyle2(text, "\x1B[1m", "\x1B[22m");
1750
+ var dim = (text) => applyStyle2(text, "\x1B[2m", "\x1B[22m");
1751
+ var italic = (text) => applyStyle2(text, "\x1B[3m", "\x1B[23m");
1752
+ var underline = (text) => applyStyle2(text, "\x1B[4m", "\x1B[24m");
1753
+ var strikethrough = (text) => applyStyle2(text, "\x1B[9m", "\x1B[29m");
1754
+ var inverse = (text) => applyStyle2(text, "\x1B[7m", "\x1B[27m");
1755
+ var hidden = (text) => applyStyle2(text, "\x1B[8m", "\x1B[28m");
1756
+ var reset = (text) => applyStyle2(text, "\x1B[0m", "\x1B[0m");
1757
+ var black = (text) => applyStyle2(text, "\x1B[30m", "\x1B[39m");
1758
+ var red = (text) => applyStyle2(text, "\x1B[31m", "\x1B[39m");
1759
+ var green = (text) => applyStyle2(text, "\x1B[32m", "\x1B[39m");
1760
+ var yellow = (text) => applyStyle2(text, "\x1B[33m", "\x1B[39m");
1761
+ var blue = (text) => applyStyle2(text, "\x1B[34m", "\x1B[39m");
1762
+ var magenta = (text) => applyStyle2(text, "\x1B[35m", "\x1B[39m");
1763
+ var cyan = (text) => applyStyle2(text, "\x1B[36m", "\x1B[39m");
1764
+ var white = (text) => applyStyle2(text, "\x1B[37m", "\x1B[39m");
1765
+ var blackBright = (text) => applyStyle2(text, "\x1B[90m", "\x1B[39m");
1766
+ var redBright = (text) => applyStyle2(text, "\x1B[91m", "\x1B[39m");
1767
+ var greenBright = (text) => applyStyle2(text, "\x1B[92m", "\x1B[39m");
1768
+ var yellowBright = (text) => applyStyle2(text, "\x1B[93m", "\x1B[39m");
1769
+ var blueBright = (text) => applyStyle2(text, "\x1B[94m", "\x1B[39m");
1770
+ var magentaBright = (text) => applyStyle2(text, "\x1B[95m", "\x1B[39m");
1771
+ var cyanBright = (text) => applyStyle2(text, "\x1B[96m", "\x1B[39m");
1772
+ var whiteBright = (text) => applyStyle2(text, "\x1B[97m", "\x1B[39m");
1298
1773
  var gray = blackBright;
1299
1774
  var grey = blackBright;
1300
- var bgBlack = (text) => applyStyle(text, "\x1B[40m", "\x1B[49m");
1301
- var bgRed = (text) => applyStyle(text, "\x1B[41m", "\x1B[49m");
1302
- var bgGreen = (text) => applyStyle(text, "\x1B[42m", "\x1B[49m");
1303
- var bgYellow = (text) => applyStyle(text, "\x1B[43m", "\x1B[49m");
1304
- var bgBlue = (text) => applyStyle(text, "\x1B[44m", "\x1B[49m");
1305
- var bgMagenta = (text) => applyStyle(text, "\x1B[45m", "\x1B[49m");
1306
- var bgCyan = (text) => applyStyle(text, "\x1B[46m", "\x1B[49m");
1307
- var bgWhite = (text) => applyStyle(text, "\x1B[47m", "\x1B[49m");
1308
- var bgBlackBright = (text) => applyStyle(text, "\x1B[100m", "\x1B[49m");
1309
- var bgRedBright = (text) => applyStyle(text, "\x1B[101m", "\x1B[49m");
1310
- var bgGreenBright = (text) => applyStyle(text, "\x1B[102m", "\x1B[49m");
1311
- var bgYellowBright = (text) => applyStyle(text, "\x1B[103m", "\x1B[49m");
1312
- var bgBlueBright = (text) => applyStyle(text, "\x1B[104m", "\x1B[49m");
1313
- var bgMagentaBright = (text) => applyStyle(text, "\x1B[105m", "\x1B[49m");
1314
- var bgCyanBright = (text) => applyStyle(text, "\x1B[106m", "\x1B[49m");
1315
- var bgWhiteBright = (text) => applyStyle(text, "\x1B[107m", "\x1B[49m");
1775
+ var bgBlack = (text) => applyStyle2(text, "\x1B[40m", "\x1B[49m");
1776
+ var bgRed = (text) => applyStyle2(text, "\x1B[41m", "\x1B[49m");
1777
+ var bgGreen = (text) => applyStyle2(text, "\x1B[42m", "\x1B[49m");
1778
+ var bgYellow = (text) => applyStyle2(text, "\x1B[43m", "\x1B[49m");
1779
+ var bgBlue = (text) => applyStyle2(text, "\x1B[44m", "\x1B[49m");
1780
+ var bgMagenta = (text) => applyStyle2(text, "\x1B[45m", "\x1B[49m");
1781
+ var bgCyan = (text) => applyStyle2(text, "\x1B[46m", "\x1B[49m");
1782
+ var bgWhite = (text) => applyStyle2(text, "\x1B[47m", "\x1B[49m");
1783
+ var bgBlackBright = (text) => applyStyle2(text, "\x1B[100m", "\x1B[49m");
1784
+ var bgRedBright = (text) => applyStyle2(text, "\x1B[101m", "\x1B[49m");
1785
+ var bgGreenBright = (text) => applyStyle2(text, "\x1B[102m", "\x1B[49m");
1786
+ var bgYellowBright = (text) => applyStyle2(text, "\x1B[103m", "\x1B[49m");
1787
+ var bgBlueBright = (text) => applyStyle2(text, "\x1B[104m", "\x1B[49m");
1788
+ var bgMagentaBright = (text) => applyStyle2(text, "\x1B[105m", "\x1B[49m");
1789
+ var bgCyanBright = (text) => applyStyle2(text, "\x1B[106m", "\x1B[49m");
1790
+ var bgWhiteBright = (text) => applyStyle2(text, "\x1B[107m", "\x1B[49m");
1316
1791
  var bgGray = bgBlackBright;
1317
1792
  var bgGrey = bgBlackBright;
1318
1793
  function compose(...stylers) {
@@ -1321,9 +1796,6 @@ function compose(...stylers) {
1321
1796
  };
1322
1797
  }
1323
1798
 
1324
- // src/index.ts
1325
- import { createKernel as createKernel2 } from "@oxog/plugin";
1326
-
1327
1799
  // src/plugins/index.ts
1328
1800
  var plugins_exports = {};
1329
1801
  __export(plugins_exports, {
@@ -1342,6 +1814,33 @@ __export(plugins_exports, {
1342
1814
  });
1343
1815
 
1344
1816
  // src/plugins/optional/gradient.ts
1817
+ function toRgb(color) {
1818
+ if (typeof color !== "string") {
1819
+ const [r, g, b] = color;
1820
+ return { r, g, b };
1821
+ }
1822
+ return color.startsWith("rgb(") ? parseRgb(color) : hexToRgb(color);
1823
+ }
1824
+ function paintGradient(text, startRgb, endRgb, steps, level) {
1825
+ if (text.length === 0) {
1826
+ return text;
1827
+ }
1828
+ const colors = interpolateColors(startRgb, endRgb, text.length, Math.max(2, steps));
1829
+ let result = "";
1830
+ for (let i = 0; i < text.length; i++) {
1831
+ const colorIndex = text.length === 1 ? 0 : Math.floor(i / (text.length - 1) * (colors.length - 1));
1832
+ result += paintRgb(text[i], colors[colorIndex], false, level);
1833
+ }
1834
+ return result;
1835
+ }
1836
+ function paintRainbow(text, steps, level) {
1837
+ const colors = generateRainbowColors(text.length, Math.max(1, steps));
1838
+ let result = "";
1839
+ for (let i = 0; i < text.length; i++) {
1840
+ result += paintRgb(text[i], colors[i % colors.length], false, level);
1841
+ }
1842
+ return result;
1843
+ }
1345
1844
  function gradientPlugin(options) {
1346
1845
  const steps = options?.steps ?? 10;
1347
1846
  return {
@@ -1355,6 +1854,15 @@ function gradientPlugin(options) {
1355
1854
  enabled: true
1356
1855
  }
1357
1856
  });
1857
+ registerPluginApi(kernel, "gradient", (start, end) => {
1858
+ const startRgb = toRgb(start);
1859
+ const endRgb = toRgb(end);
1860
+ if (!startRgb || !endRgb) {
1861
+ throw new ColorError(`Invalid gradient color: ${String(startRgb ? end : start)}`);
1862
+ }
1863
+ return (text) => paintGradient(String(text), startRgb, endRgb, steps, k.getContext().level);
1864
+ });
1865
+ registerPluginApi(kernel, "rainbow", (text) => paintRainbow(String(text), steps, k.getContext().level));
1358
1866
  k.on("style:gradient", (data) => {
1359
1867
  const { start, end, text, steps: gradientSteps = steps } = data;
1360
1868
  const startRgb = start.startsWith("#") ? hexToRgb(start) : start.startsWith("rgb(") ? parseRgb(start) : hexToRgb(start);
@@ -1425,6 +1933,7 @@ function generateRainbowColors(length, steps) {
1425
1933
  }
1426
1934
 
1427
1935
  // src/plugins/optional/theme.ts
1936
+ var THEME_COLOR_TYPES = ["keyword", "string", "number", "comment", "error", "success", "warning", "info"];
1428
1937
  function themePlugin(options) {
1429
1938
  const preset = options?.preset ?? "monokai";
1430
1939
  const custom = options?.custom ?? {};
@@ -1444,6 +1953,17 @@ function themePlugin(options) {
1444
1953
  enabled: true
1445
1954
  }
1446
1955
  });
1956
+ const api = {};
1957
+ for (const type of THEME_COLOR_TYPES) {
1958
+ const color = theme[type];
1959
+ api[type] = (text) => {
1960
+ if (!color) {
1961
+ throw new Error(`Unknown theme color type: ${type}`);
1962
+ }
1963
+ return paintRgb(String(text), hexToRgb(color), false, k.getContext().level);
1964
+ };
1965
+ }
1966
+ registerPluginApi(kernel, "theme", api);
1447
1967
  k.on("style:theme", (data) => {
1448
1968
  const { type, text } = data;
1449
1969
  const color = theme[type];
@@ -1513,6 +2033,10 @@ function semanticPlugin(options) {
1513
2033
  if (!hexToRgb2) {
1514
2034
  throw new Error("hexToRgb utility not available");
1515
2035
  }
2036
+ for (const [name, color] of Object.entries(defaultColors)) {
2037
+ const rgb = hexToRgb2(color);
2038
+ registerPluginApi(kernel, name, (text) => paintRgb(String(text), rgb, false, k.getContext().level));
2039
+ }
1516
2040
  k.on("style:success", (data) => {
1517
2041
  const { r, g, b } = hexToRgb2(defaultColors.success);
1518
2042
  return `\x1B[38;2;${r};${g};${b}m${data.text}\x1B[39m`;
@@ -1554,9 +2078,12 @@ function boxPlugin(options) {
1554
2078
  borders: BOX_BORDERS
1555
2079
  }
1556
2080
  });
1557
- k.on("style:box", (data) => {
1558
- const { text, border: boxBorder = border, padding: boxPadding = padding, bgColor } = data;
2081
+ const draw = (text, opts = {}) => {
2082
+ const { border: boxBorder = border, padding: boxPadding = padding, bgColor } = opts;
1559
2083
  const chars = BOX_BORDERS[boxBorder];
2084
+ if (!chars) {
2085
+ throw new StyleError(`Unknown box border: ${String(boxBorder)}`);
2086
+ }
1560
2087
  const lines = text.split("\n");
1561
2088
  const width = Math.max(...lines.map((l) => l.length)) + boxPadding * 2;
1562
2089
  const pad = " ".repeat(boxPadding);
@@ -1568,21 +2095,51 @@ function boxPlugin(options) {
1568
2095
  const bottom = `${chars.bl}${chars.h.repeat(width)}${chars.br}`;
1569
2096
  const result = [top, ...middle, bottom].join("\n");
1570
2097
  if (bgColor) {
1571
- const ctx = k.getContext();
1572
- const hexToRgb2 = ctx.utils?.hexToRgb;
1573
- if (!hexToRgb2) {
1574
- throw new Error("hexToRgb utility not available");
1575
- }
1576
- const { r, g, b } = hexToRgb2(bgColor);
1577
- return `\x1B[48;2;${r};${g};${b}m${result}\x1B[49m`;
2098
+ return paintRgb(result, hexToRgb(bgColor), true, k.getContext().level);
1578
2099
  }
1579
2100
  return result;
2101
+ };
2102
+ registerPluginApi(kernel, "box", (text, opts) => draw(String(text), opts));
2103
+ k.on("style:box", (data) => {
2104
+ const { text, ...opts } = data;
2105
+ return draw(text, opts);
1580
2106
  });
1581
2107
  }
1582
2108
  };
1583
2109
  }
1584
2110
 
1585
2111
  // src/plugins/optional/template.ts
2112
+ var STYLE_NAMES = /* @__PURE__ */ new Set([...MODIFIER_NAMES, ...COLOR_NAMES, ...BACKGROUND_COLOR_NAMES]);
2113
+ var BLOCK = /\{([A-Za-z]\w*(?:\.[A-Za-z]\w*)*) ([^{}]*)\}/g;
2114
+ var TOKEN = /\uE000(\d+)\uE001/g;
2115
+ function renderTemplate(root, strings, values) {
2116
+ const parts = values.map((value) => String(value));
2117
+ const token = (index) => `\uE000${index}\uE001`;
2118
+ const restore = (text) => text.replace(TOKEN, (_match, index) => parts[Number(index)]);
2119
+ let source = "";
2120
+ for (let i = 0; i < strings.length; i++) {
2121
+ source += strings[i];
2122
+ if (i < values.length) {
2123
+ source += token(i);
2124
+ }
2125
+ }
2126
+ let previous;
2127
+ do {
2128
+ previous = source;
2129
+ source = source.replace(BLOCK, (_match, chain, text) => {
2130
+ let styler = root;
2131
+ for (const name of chain.split(".")) {
2132
+ if (!STYLE_NAMES.has(name)) {
2133
+ throw new StyleError(`Unknown style in template: ${name}`);
2134
+ }
2135
+ styler = styler[name];
2136
+ }
2137
+ parts.push(styler(restore(text)));
2138
+ return token(parts.length - 1);
2139
+ });
2140
+ } while (source !== previous);
2141
+ return restore(source);
2142
+ }
1586
2143
  function templatePlugin() {
1587
2144
  return {
1588
2145
  name: "template",
@@ -1594,6 +2151,11 @@ function templatePlugin() {
1594
2151
  enabled: true
1595
2152
  }
1596
2153
  });
2154
+ registerPluginApi(
2155
+ kernel,
2156
+ "template",
2157
+ (strings, ...values) => renderTemplate(getRoot(kernel) ?? createProxyPigment(k), strings, values)
2158
+ );
1597
2159
  k.on("style:template", (data) => {
1598
2160
  const { strings, values } = data;
1599
2161
  let result = "";
@@ -1630,7 +2192,8 @@ function parseStyleExpression(expression, text, kernel) {
1630
2192
  }
1631
2193
  }
1632
2194
  if (typeof current === "function") {
1633
- return current(text);
2195
+ const style = current;
2196
+ return style(text);
1634
2197
  }
1635
2198
  return text;
1636
2199
  }
@@ -1646,7 +2209,7 @@ function pigmentPlugin(options) {
1646
2209
  const k = kernel;
1647
2210
  const themeColors = THEME_PRESETS[theme] ?? THEME_PRESETS.monokai;
1648
2211
  const pigment2 = createPigment({ level });
1649
- k.pigment = pigment2;
2212
+ Object.assign(k, { pigment: pigment2 });
1650
2213
  k.updateContext({
1651
2214
  ecosystem: {
1652
2215
  theme,
@@ -1770,7 +2333,7 @@ export {
1770
2333
  blueBright,
1771
2334
  bold,
1772
2335
  compose,
1773
- createKernel2 as createKernel,
2336
+ W as createKernel,
1774
2337
  createPigment,
1775
2338
  cyan,
1776
2339
  cyanBright,