@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.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @oxog/pigment - Zero-dependency terminal styling
2
+ * @oxog/pigment - Terminal styling
3
3
  * MIT License - https://pigment.oxog.dev
4
4
  */
5
5
  "use strict";
@@ -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,
@@ -91,8 +91,464 @@ __export(index_exports, {
91
91
  });
92
92
  module.exports = __toCommonJS(index_exports);
93
93
 
94
- // src/core/factory.ts
95
- 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
+ }
96
552
 
97
553
  // src/constants.ts
98
554
  var ANSI_MODIFIERS = {
@@ -315,6 +771,12 @@ var PigmentError = class _PigmentError extends Error {
315
771
  Error.captureStackTrace(this, _PigmentError);
316
772
  }
317
773
  };
774
+ var PluginError3 = class extends PigmentError {
775
+ constructor(pluginName, message) {
776
+ super(`[${pluginName}] ${message}`, "PLUGIN_ERROR");
777
+ this.name = "PluginError";
778
+ }
779
+ };
318
780
  var ColorError = class extends PigmentError {
319
781
  constructor(message) {
320
782
  super(message, "COLOR_ERROR");
@@ -328,6 +790,23 @@ var StyleError = class extends PigmentError {
328
790
  }
329
791
  };
330
792
 
793
+ // src/utils/rgb-to-ansi256.ts
794
+ function rgbToAnsi256(r, g, b) {
795
+ const clampedR = Math.max(0, Math.min(255, r));
796
+ const clampedG = Math.max(0, Math.min(255, g));
797
+ const clampedB = Math.max(0, Math.min(255, b));
798
+ if (clampedR === clampedG && clampedG === clampedB) {
799
+ if (clampedR < 8) {
800
+ return 16;
801
+ }
802
+ if (clampedR > 248) {
803
+ return 231;
804
+ }
805
+ return Math.round((clampedR - 8) / 247 * 24) + 232;
806
+ }
807
+ return Math.round(clampedR / 51) * 36 + Math.round(clampedG / 51) * 6 + Math.round(clampedB / 51) + 16;
808
+ }
809
+
331
810
  // src/core/ansi.ts
332
811
  function generateModifierCode(name) {
333
812
  const modifier = ANSI_MODIFIERS[name];
@@ -401,6 +880,141 @@ function generateRgbCode(r, g, b, background = false) {
401
880
  background
402
881
  };
403
882
  }
883
+ function ansi256ToAnsi16(code) {
884
+ if (code < 8) {
885
+ return 30 + code;
886
+ }
887
+ if (code < 16) {
888
+ return 90 + (code - 8);
889
+ }
890
+ let red2;
891
+ let green2;
892
+ let blue2;
893
+ if (code >= 232) {
894
+ red2 = ((code - 232) * 10 + 8) / 255;
895
+ green2 = red2;
896
+ blue2 = red2;
897
+ } else {
898
+ const index = code - 16;
899
+ const remainder = index % 36;
900
+ red2 = Math.floor(index / 36) / 5;
901
+ green2 = Math.floor(remainder / 6) / 5;
902
+ blue2 = remainder % 6 / 5;
903
+ }
904
+ const value = Math.max(red2, green2, blue2) * 2;
905
+ if (value === 0) {
906
+ return 30;
907
+ }
908
+ let result = 30 + (Math.round(blue2) << 2 | Math.round(green2) << 1 | Math.round(red2));
909
+ if (value === 2) {
910
+ result += 60;
911
+ }
912
+ return result;
913
+ }
914
+ function effectiveLevel(level) {
915
+ return level ?? 3;
916
+ }
917
+ function ansi16Style(name, paletteCode, background) {
918
+ const code = ansi256ToAnsi16(paletteCode) + (background ? 10 : 0);
919
+ return {
920
+ name,
921
+ code,
922
+ open: `\x1B[${code}m`,
923
+ close: `\x1B[${background ? 49 : 39}m`,
924
+ type: "color",
925
+ background
926
+ };
927
+ }
928
+ function generateAnsi256CodeForLevel(code, background, level) {
929
+ const style = generateAnsi256Code(code, background);
930
+ if (effectiveLevel(level) >= 2) {
931
+ return style;
932
+ }
933
+ return ansi16Style(style.name, code, background);
934
+ }
935
+ function generateRgbCodeForLevel(r, g, b, background, level) {
936
+ const style = generateRgbCode(r, g, b, background);
937
+ const effective = effectiveLevel(level);
938
+ if (effective >= 3) {
939
+ return style;
940
+ }
941
+ const paletteCode = rgbToAnsi256(r, g, b);
942
+ if (effective === 2) {
943
+ return { ...generateAnsi256Code(paletteCode, background), name: style.name };
944
+ }
945
+ return ansi16Style(style.name, paletteCode, background);
946
+ }
947
+ function paintRgb(text, rgb, background, level) {
948
+ if (level === 0 || text === "") {
949
+ return text;
950
+ }
951
+ const style = generateRgbCodeForLevel(rgb.r, rgb.g, rgb.b, background, level);
952
+ return `${style.open}${text}${style.close}`;
953
+ }
954
+
955
+ // src/core/plugin-api.ts
956
+ var REGISTRY_KEY = /* @__PURE__ */ Symbol.for("@oxog/pigment.plugin-api");
957
+ var globalRegistry = globalThis;
958
+ var registry = globalRegistry[REGISTRY_KEY] ??= { apis: /* @__PURE__ */ new WeakMap(), roots: /* @__PURE__ */ new WeakMap() };
959
+ var { apis, roots } = registry;
960
+ var PLUGIN_API_PROVIDERS = {
961
+ gradient: "gradientPlugin",
962
+ rainbow: "gradientPlugin",
963
+ theme: "themePlugin",
964
+ success: "semanticPlugin",
965
+ error: "semanticPlugin",
966
+ warning: "semanticPlugin",
967
+ info: "semanticPlugin",
968
+ debug: "semanticPlugin",
969
+ box: "boxPlugin",
970
+ template: "templatePlugin"
971
+ };
972
+ function registerPluginApi(kernel, name, value) {
973
+ let api = apis.get(kernel);
974
+ if (!api) {
975
+ api = /* @__PURE__ */ new Map();
976
+ apis.set(kernel, api);
977
+ }
978
+ api.set(name, value);
979
+ }
980
+ function getPluginApi(kernel, name) {
981
+ return apis.get(kernel)?.get(name);
982
+ }
983
+ function registerRoot(kernel, root) {
984
+ roots.set(kernel, root);
985
+ }
986
+ function getRoot(kernel) {
987
+ return roots.get(kernel);
988
+ }
989
+ function missingPluginApi(name) {
990
+ const plugin = PLUGIN_API_PROVIDERS[name];
991
+ const fail = () => {
992
+ throw new PluginError3(
993
+ plugin,
994
+ `pigment.${name} needs ${plugin}(): createPigment({ plugins: [${plugin}()] }) or pigment.use(${plugin}())`
995
+ );
996
+ };
997
+ if (name === "theme") {
998
+ return {
999
+ keyword: fail,
1000
+ string: fail,
1001
+ number: fail,
1002
+ comment: fail,
1003
+ error: fail,
1004
+ success: fail,
1005
+ warning: fail,
1006
+ info: fail
1007
+ };
1008
+ }
1009
+ return fail;
1010
+ }
1011
+ function installPlugin(kernel, plugin) {
1012
+ kernel.use(plugin);
1013
+ if (plugin.onInit) {
1014
+ Promise.resolve().then(() => plugin.onInit?.(kernel.getContext())).catch(() => {
1015
+ });
1016
+ }
1017
+ }
404
1018
 
405
1019
  // src/core/proxy.ts
406
1020
  var proxyCache = /* @__PURE__ */ new WeakMap();
@@ -414,28 +1028,28 @@ function createProxyPigment(kernel, styles = []) {
414
1028
  if (prop === "supportsColor") {
415
1029
  return ctx.colorSupport;
416
1030
  }
417
- if (MODIFIER_NAMES.includes(prop)) {
1031
+ if (MODIFIER_NAMES.some((name) => name === prop)) {
418
1032
  const modifierStyle = generateModifierCode(prop);
419
1033
  return createProxyPigment(kernel, [...styles, modifierStyle]);
420
1034
  }
421
- if (COLOR_NAMES.includes(prop) || BACKGROUND_COLOR_NAMES.includes(prop)) {
1035
+ if (COLOR_NAMES.some((name) => name === prop) || BACKGROUND_COLOR_NAMES.some((name) => name === prop)) {
422
1036
  const background = prop.startsWith("bg");
423
1037
  const colorName = background ? prop.slice(2) : prop;
424
1038
  const colorStyle = generateColorCode(colorName, background);
425
1039
  return createProxyPigment(kernel, [...styles, colorStyle]);
426
1040
  }
427
- if (EXTENDED_COLOR_METHODS.includes(prop)) {
1041
+ if (EXTENDED_COLOR_METHODS.some((name) => name === prop)) {
428
1042
  if (prop === "ansi256" || prop === "bgAnsi256") {
429
1043
  return (code) => {
430
1044
  const background = prop === "bgAnsi256";
431
- const ansi256Style = generateAnsi256Code(code, background);
1045
+ const ansi256Style = generateAnsi256CodeForLevel(code, background, ctx.level);
432
1046
  return createProxyPigment(kernel, [...styles, ansi256Style]);
433
1047
  };
434
1048
  }
435
1049
  if (prop === "rgb" || prop === "bgRgb") {
436
1050
  return (r, g, b) => {
437
1051
  const background = prop === "bgRgb";
438
- const rgbStyle = generateRgbCode(r, g, b, background);
1052
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
439
1053
  return createProxyPigment(kernel, [...styles, rgbStyle]);
440
1054
  };
441
1055
  }
@@ -447,40 +1061,49 @@ function createProxyPigment(kernel, styles = []) {
447
1061
  throw new Error("hexToRgb utility not available");
448
1062
  }
449
1063
  const { r, g, b } = hexToRgb2(color);
450
- const rgbStyle = generateRgbCode(r, g, b, background);
1064
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
451
1065
  return createProxyPigment(kernel, [...styles, rgbStyle]);
452
1066
  };
453
1067
  }
454
1068
  if (prop === "hsl" || prop === "bgHsl") {
455
- return (h, s, l) => {
1069
+ return (h2, s, l) => {
456
1070
  const background = prop === "bgHsl";
457
1071
  const hslToRgb2 = ctx.utils?.hslToRgb;
458
1072
  if (!hslToRgb2) {
459
1073
  throw new Error("hslToRgb utility not available");
460
1074
  }
461
- const { r, g, b } = hslToRgb2(h, s, l);
462
- const rgbStyle = generateRgbCode(r, g, b, background);
1075
+ const { r, g, b } = hslToRgb2(h2, s, l);
1076
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
463
1077
  return createProxyPigment(kernel, [...styles, rgbStyle]);
464
1078
  };
465
1079
  }
466
1080
  }
467
1081
  if (prop === "visible") {
468
1082
  const colorSupport = ctx.colorSupport;
469
- return (text) => colorSupport.level > 0 ? text : "";
1083
+ return (text) => colorSupport.level > 0 ? applyStyles(String(text), styles, ctx.level) : "";
1084
+ }
1085
+ if (prop === "use") {
1086
+ return (plugin) => {
1087
+ installPlugin(kernel, plugin);
1088
+ return getRoot(kernel) ?? createProxyPigment(kernel);
1089
+ };
1090
+ }
1091
+ if (Object.prototype.hasOwnProperty.call(PLUGIN_API_PROVIDERS, prop)) {
1092
+ return getPluginApi(kernel, prop) ?? missingPluginApi(prop);
470
1093
  }
471
- return target[prop];
1094
+ return Reflect.get(target, prop);
472
1095
  },
473
1096
  apply(_target, _thisArg, argArray) {
474
1097
  const text = String(argArray[0] ?? "");
475
- return applyStyles(text, styles);
1098
+ return applyStyles(text, styles, kernel.getContext().level);
476
1099
  }
477
1100
  };
478
1101
  const pigment2 = new Proxy((() => ""), handler);
479
1102
  proxyCache.set(pigment2, pigment2);
480
1103
  return pigment2;
481
1104
  }
482
- function applyStyles(text, styles) {
483
- if (styles.length === 0 || text === "") {
1105
+ function applyStyles(text, styles, level) {
1106
+ if (styles.length === 0 || text === "" || level === 0) {
484
1107
  return text;
485
1108
  }
486
1109
  const open = styles.map((style) => style.open).join("");
@@ -498,7 +1121,7 @@ function applyStyles(text, styles) {
498
1121
  function hexToRgb(hex) {
499
1122
  const hexString = hex.trim().replace(/^#/, "");
500
1123
  if (hexString.length === 3) {
501
- const expanded = hexString.split("").map((c) => c + c).join("");
1124
+ const expanded = hexString.split("").map((c2) => c2 + c2).join("");
502
1125
  return parseExpandedHex(expanded);
503
1126
  }
504
1127
  if (hexString.length === 6) {
@@ -525,8 +1148,8 @@ function validateHex(hex) {
525
1148
  }
526
1149
 
527
1150
  // src/utils/hsl-to-rgb.ts
528
- function hslToRgb(h, s, l) {
529
- const hNormalized = h / 360;
1151
+ function hslToRgb(h2, s, l) {
1152
+ const hNormalized = h2 / 360;
530
1153
  const sNormalized = s / 100;
531
1154
  const lNormalized = l / 100;
532
1155
  let r;
@@ -559,7 +1182,13 @@ function hslToRgb(h, s, l) {
559
1182
  // src/core/builder.ts
560
1183
  var BuilderPigment = class {
561
1184
  styles = [];
562
- constructor(_kernel) {
1185
+ kernel;
1186
+ constructor(kernel) {
1187
+ this.kernel = kernel;
1188
+ }
1189
+ /** The instance's color level (true color when there is no kernel). */
1190
+ get level() {
1191
+ return this.kernel?.getContext().level;
563
1192
  }
564
1193
  bold() {
565
1194
  this.styles.push(generateModifierCode("bold"));
@@ -738,26 +1367,26 @@ var BuilderPigment = class {
738
1367
  return this;
739
1368
  }
740
1369
  ansi256(code) {
741
- this.styles.push(generateAnsi256Code(code, false));
1370
+ this.styles.push(generateAnsi256CodeForLevel(code, false, this.level));
742
1371
  return this;
743
1372
  }
744
1373
  bgAnsi256(code) {
745
- this.styles.push(generateAnsi256Code(code, true));
1374
+ this.styles.push(generateAnsi256CodeForLevel(code, true, this.level));
746
1375
  return this;
747
1376
  }
748
1377
  rgb(r, g, b) {
749
- this.styles.push(generateRgbCode(r, g, b, false));
1378
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
750
1379
  return this;
751
1380
  }
752
1381
  bgRgb(r, g, b) {
753
- this.styles.push(generateRgbCode(r, g, b, true));
1382
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
754
1383
  return this;
755
1384
  }
756
1385
  hex(color) {
757
1386
  const rgb = hexToRgb(color);
758
1387
  if (rgb) {
759
1388
  const { r, g, b } = rgb;
760
- this.styles.push(generateRgbCode(r, g, b, false));
1389
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
761
1390
  }
762
1391
  return this;
763
1392
  }
@@ -765,22 +1394,22 @@ var BuilderPigment = class {
765
1394
  const rgb = hexToRgb(color);
766
1395
  if (rgb) {
767
1396
  const { r, g, b } = rgb;
768
- this.styles.push(generateRgbCode(r, g, b, true));
1397
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
769
1398
  }
770
1399
  return this;
771
1400
  }
772
- hsl(h, s, l) {
773
- const { r, g, b } = hslToRgb(h, s, l);
774
- this.styles.push(generateRgbCode(r, g, b, false));
1401
+ hsl(h2, s, l) {
1402
+ const { r, g, b } = hslToRgb(h2, s, l);
1403
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
775
1404
  return this;
776
1405
  }
777
- bgHsl(h, s, l) {
778
- const { r, g, b } = hslToRgb(h, s, l);
779
- this.styles.push(generateRgbCode(r, g, b, true));
1406
+ bgHsl(h2, s, l) {
1407
+ const { r, g, b } = hslToRgb(h2, s, l);
1408
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
780
1409
  return this;
781
1410
  }
782
1411
  paint(text) {
783
- if (this.styles.length === 0 || text === "") {
1412
+ if (this.styles.length === 0 || text === "" || this.level === 0) {
784
1413
  return text;
785
1414
  }
786
1415
  const open = this.styles.map((style) => style.open).join("");
@@ -859,69 +1488,31 @@ function detectColorSupport(overrideLevel) {
859
1488
  };
860
1489
  return cachedColorSupport;
861
1490
  }
862
- if (process.env.NO_COLOR) {
863
- cachedColorSupport = {
864
- level: 0,
865
- hasBasic: false,
866
- has256: false,
867
- has16m: false
868
- };
1491
+ const env = process.env;
1492
+ const forced = parseForceColor(env.FORCE_COLOR);
1493
+ if (forced !== void 0) {
1494
+ cachedColorSupport = toColorSupport(forced);
869
1495
  return cachedColorSupport;
870
1496
  }
871
- if (process.env.FORCE_COLOR) {
872
- const level = parseInt(process.env.FORCE_COLOR, 10);
873
- const clampedLevel = Math.max(0, Math.min(3, level));
874
- cachedColorSupport = {
875
- level: clampedLevel,
876
- hasBasic: clampedLevel >= 1,
877
- has256: clampedLevel >= 2,
878
- has16m: clampedLevel >= 3
879
- };
1497
+ if (env.NO_COLOR) {
1498
+ cachedColorSupport = toColorSupport(0);
880
1499
  return cachedColorSupport;
881
1500
  }
882
- if (!hasTTY() && !process.env.CI) {
883
- cachedColorSupport = {
884
- level: 0,
885
- hasBasic: false,
886
- has256: false,
887
- has16m: false
888
- };
1501
+ if (!hasTTY() && !env.CI || env.TERM === "dumb") {
1502
+ cachedColorSupport = toColorSupport(0);
889
1503
  return cachedColorSupport;
890
1504
  }
891
- const term = process.env.TERM ?? "";
892
- if (term.includes("truecolor") || term.includes("24bit")) {
893
- cachedColorSupport = {
894
- level: 3,
895
- hasBasic: true,
896
- has256: true,
897
- has16m: true
898
- };
1505
+ const term = env.TERM ?? "";
1506
+ const colorTerm = (env.COLORTERM ?? "").toLowerCase();
1507
+ if (colorTerm === "truecolor" || colorTerm === "24bit" || term.includes("truecolor") || term.includes("24bit")) {
1508
+ cachedColorSupport = toColorSupport(3);
899
1509
  return cachedColorSupport;
900
1510
  }
901
1511
  if (term.includes("256color")) {
902
- cachedColorSupport = {
903
- level: 2,
904
- hasBasic: true,
905
- has256: true,
906
- has16m: false
907
- };
908
- return cachedColorSupport;
909
- }
910
- if (process.env.CI) {
911
- cachedColorSupport = {
912
- level: 1,
913
- hasBasic: true,
914
- has256: false,
915
- has16m: false
916
- };
1512
+ cachedColorSupport = toColorSupport(2);
917
1513
  return cachedColorSupport;
918
1514
  }
919
- cachedColorSupport = {
920
- level: 1,
921
- hasBasic: true,
922
- has256: false,
923
- has16m: false
924
- };
1515
+ cachedColorSupport = toColorSupport(1);
925
1516
  return cachedColorSupport;
926
1517
  }
927
1518
  function supportsColor() {
@@ -930,22 +1521,17 @@ function supportsColor() {
930
1521
  function resetColorSupportCache() {
931
1522
  cachedColorSupport = void 0;
932
1523
  }
933
-
934
- // src/utils/rgb-to-ansi256.ts
935
- function rgbToAnsi256(r, g, b) {
936
- const clampedR = Math.max(0, Math.min(255, r));
937
- const clampedG = Math.max(0, Math.min(255, g));
938
- const clampedB = Math.max(0, Math.min(255, b));
939
- if (clampedR === clampedG && clampedG === clampedB) {
940
- if (clampedR < 8) {
941
- return 16;
942
- }
943
- if (clampedR > 248) {
944
- return 231;
945
- }
946
- return Math.round((clampedR - 8) / 247 * 24) + 232;
947
- }
948
- return Math.round(clampedR / 51) * 36 + Math.round(clampedG / 51) * 6 + Math.round(clampedB / 51) + 16;
1524
+ function parseForceColor(value) {
1525
+ if (value === void 0) return void 0;
1526
+ const normalized = value.trim().toLowerCase();
1527
+ if (normalized === "" || normalized === "true") return 1;
1528
+ if (normalized === "false") return 0;
1529
+ const level = Number.parseInt(normalized, 10);
1530
+ if (Number.isNaN(level)) return 1;
1531
+ return Math.max(0, Math.min(3, level));
1532
+ }
1533
+ function toColorSupport(level) {
1534
+ return { level, hasBasic: level >= 1, has256: level >= 2, has16m: level >= 3 };
949
1535
  }
950
1536
 
951
1537
  // src/plugins/core/base-colors.ts
@@ -1057,11 +1643,11 @@ function trueColorPlugin() {
1057
1643
  k.emit("color:rgb", { ...rgb, background });
1058
1644
  });
1059
1645
  k.on("color:hsl", (data) => {
1060
- const { h, s, l, background } = data;
1646
+ const { h: h2, s, l, background } = data;
1061
1647
  if (!utils?.hslToRgb) {
1062
1648
  throw new Error("hslToRgb utility not available");
1063
1649
  }
1064
- const rgb = utils.hslToRgb(h, s, l);
1650
+ const rgb = utils.hslToRgb(h2, s, l);
1065
1651
  k.emit("color:rgb", { ...rgb, background });
1066
1652
  });
1067
1653
  }
@@ -1097,7 +1683,8 @@ function environmentPlugin() {
1097
1683
  k.updateContext({
1098
1684
  environment: {
1099
1685
  isCI: ci,
1100
- noColor: process.env.NO_COLOR !== void 0,
1686
+ // NO_COLOR only counts when non-empty (no-color.org)
1687
+ noColor: Boolean(process.env.NO_COLOR),
1101
1688
  forceColor: process.env.FORCE_COLOR !== void 0,
1102
1689
  term: process.env.TERM ?? ""
1103
1690
  }
@@ -1106,13 +1693,13 @@ function environmentPlugin() {
1106
1693
  k.on("environment:check", () => {
1107
1694
  const ctx = k.getContext();
1108
1695
  const env = ctx.environment;
1696
+ if (env?.forceColor) {
1697
+ const level = parseForceColor(process.env.FORCE_COLOR) ?? 1;
1698
+ return { level, enabled: level > 0 };
1699
+ }
1109
1700
  if (env?.noColor) {
1110
1701
  return { level: 0, enabled: false };
1111
1702
  }
1112
- if (env?.forceColor) {
1113
- const level = parseInt(process.env.FORCE_COLOR ?? "0", 10);
1114
- return { level: Math.max(0, Math.min(3, level)), enabled: true };
1115
- }
1116
1703
  if (ci) {
1117
1704
  return { level: 1, enabled: colorSupport.hasBasic };
1118
1705
  }
@@ -1139,7 +1726,7 @@ function createPigment(options) {
1139
1726
  const noColor = options?.noColor ?? false;
1140
1727
  const forceColor = options?.forceColor ?? false;
1141
1728
  const level = noColor ? 0 : forceColor ? options?.level ?? 3 : colorSupport.level;
1142
- const kernel = (0, import_plugin.createKernel)({
1729
+ const kernel = W({
1143
1730
  context: {
1144
1731
  colorSupport: { ...colorSupport, level },
1145
1732
  level,
@@ -1162,13 +1749,12 @@ function createPigment(options) {
1162
1749
  kernel.use(environmentPlugin());
1163
1750
  if (options?.plugins) {
1164
1751
  for (const plugin of options.plugins) {
1165
- kernel.use(plugin);
1752
+ installPlugin(kernel, plugin);
1166
1753
  }
1167
1754
  }
1168
- kernel.init();
1169
1755
  const pigment2 = createProxyPigment(kernel);
1170
- const builder = new BuilderPigment(kernel);
1171
- Object.defineProperty(pigment2, "builder", { value: builder, enumerable: false });
1756
+ registerRoot(kernel, pigment2);
1757
+ Object.defineProperty(pigment2, "builder", { get: () => new BuilderPigment(kernel), enumerable: false });
1172
1758
  Object.defineProperty(pigment2, "level", { value: level, enumerable: true });
1173
1759
  Object.defineProperty(pigment2, "supportsColor", { value: colorSupport, enumerable: true });
1174
1760
  return pigment2;
@@ -1176,169 +1762,55 @@ function createPigment(options) {
1176
1762
 
1177
1763
  // src/core/chalk.ts
1178
1764
  var Chalk = class {
1179
- level;
1180
1765
  supportsColor;
1766
+ currentLevel;
1181
1767
  pigment;
1182
1768
  constructor(options) {
1183
1769
  const colorSupport = detectColorSupport(options?.level);
1184
- this.level = options?.level ?? colorSupport.level;
1770
+ this.currentLevel = options?.level ?? colorSupport.level;
1185
1771
  this.supportsColor = colorSupport;
1186
- this.pigment = createPigment({ level: this.level });
1187
- }
1188
- red(text) {
1189
- return this.pigment.red(text);
1772
+ this.pigment = createPigment({ level: this.currentLevel });
1190
1773
  }
1191
- green(text) {
1192
- return this.pigment.green(text);
1774
+ /** Color level (0-3). Setting it changes the output of every style. */
1775
+ get level() {
1776
+ return this.currentLevel;
1193
1777
  }
1194
- yellow(text) {
1195
- return this.pigment.yellow(text);
1196
- }
1197
- blue(text) {
1198
- return this.pigment.blue(text);
1199
- }
1200
- magenta(text) {
1201
- return this.pigment.magenta(text);
1202
- }
1203
- cyan(text) {
1204
- return this.pigment.cyan(text);
1205
- }
1206
- white(text) {
1207
- return this.pigment.white(text);
1208
- }
1209
- black(text) {
1210
- return this.pigment.black(text);
1211
- }
1212
- redBright(text) {
1213
- return this.pigment.redBright(text);
1214
- }
1215
- greenBright(text) {
1216
- return this.pigment.greenBright(text);
1217
- }
1218
- yellowBright(text) {
1219
- return this.pigment.yellowBright(text);
1220
- }
1221
- blueBright(text) {
1222
- return this.pigment.blueBright(text);
1223
- }
1224
- magentaBright(text) {
1225
- return this.pigment.magentaBright(text);
1226
- }
1227
- cyanBright(text) {
1228
- return this.pigment.cyanBright(text);
1229
- }
1230
- whiteBright(text) {
1231
- return this.pigment.whiteBright(text);
1232
- }
1233
- gray(text) {
1234
- return this.pigment.gray(text);
1235
- }
1236
- grey(text) {
1237
- return this.pigment.grey(text);
1238
- }
1239
- bgRed(text) {
1240
- return this.pigment.bgRed(text);
1241
- }
1242
- bgGreen(text) {
1243
- return this.pigment.bgGreen(text);
1244
- }
1245
- bgYellow(text) {
1246
- return this.pigment.bgYellow(text);
1247
- }
1248
- bgBlue(text) {
1249
- return this.pigment.bgBlue(text);
1250
- }
1251
- bgMagenta(text) {
1252
- return this.pigment.bgMagenta(text);
1253
- }
1254
- bgCyan(text) {
1255
- return this.pigment.bgCyan(text);
1256
- }
1257
- bgWhite(text) {
1258
- return this.pigment.bgWhite(text);
1259
- }
1260
- bgBlack(text) {
1261
- return this.pigment.bgBlack(text);
1262
- }
1263
- bgRedBright(text) {
1264
- return this.pigment.bgRedBright(text);
1265
- }
1266
- bgGreenBright(text) {
1267
- return this.pigment.bgGreenBright(text);
1268
- }
1269
- bgYellowBright(text) {
1270
- return this.pigment.bgYellowBright(text);
1271
- }
1272
- bgBlueBright(text) {
1273
- return this.pigment.bgBlueBright(text);
1274
- }
1275
- bgMagentaBright(text) {
1276
- return this.pigment.bgMagentaBright(text);
1277
- }
1278
- bgCyanBright(text) {
1279
- return this.pigment.bgCyanBright(text);
1280
- }
1281
- bgWhiteBright(text) {
1282
- return this.pigment.bgWhiteBright(text);
1283
- }
1284
- bgGray(text) {
1285
- return this.pigment.bgGray(text);
1286
- }
1287
- bgGrey(text) {
1288
- return this.pigment.bgGrey(text);
1289
- }
1290
- bold(text) {
1291
- return this.pigment.bold(text);
1292
- }
1293
- dim(text) {
1294
- return this.pigment.dim(text);
1295
- }
1296
- italic(text) {
1297
- return this.pigment.italic(text);
1298
- }
1299
- underline(text) {
1300
- return this.pigment.underline(text);
1301
- }
1302
- strikethrough(text) {
1303
- return this.pigment.strikethrough(text);
1304
- }
1305
- inverse(text) {
1306
- return this.pigment.inverse(text);
1307
- }
1308
- hidden(text) {
1309
- return this.pigment.hidden(text);
1310
- }
1311
- reset(text) {
1312
- return this.pigment.reset(text);
1778
+ set level(level) {
1779
+ this.currentLevel = level;
1780
+ this.pigment = createPigment({ level });
1313
1781
  }
1782
+ /** Like chalk: returns the text only when colors are enabled, otherwise ''. */
1314
1783
  visible(text) {
1315
- return this.supportsColor.level > 0 ? text : text;
1784
+ return this.currentLevel > 0 ? text : "";
1316
1785
  }
1317
1786
  rgb(r, g, b) {
1318
- const coloredPigment = this.pigment.rgb(r, g, b);
1319
- return (text) => coloredPigment(text);
1787
+ return this.pigment.rgb(r, g, b);
1320
1788
  }
1321
1789
  bgRgb(r, g, b) {
1322
- const coloredPigment = this.pigment.bgRgb(r, g, b);
1323
- return (text) => coloredPigment(text);
1790
+ return this.pigment.bgRgb(r, g, b);
1324
1791
  }
1325
1792
  hex(color) {
1326
- const coloredPigment = this.pigment.hex(color);
1327
- return (text) => coloredPigment(text);
1793
+ return this.pigment.hex(color);
1328
1794
  }
1329
1795
  bgHex(color) {
1330
- const coloredPigment = this.pigment.bgHex(color);
1331
- return (text) => coloredPigment(text);
1796
+ return this.pigment.bgHex(color);
1332
1797
  }
1333
1798
  ansi256(code) {
1334
- const coloredPigment = this.pigment.ansi256(code);
1335
- return (text) => coloredPigment(text);
1799
+ return this.pigment.ansi256(code);
1336
1800
  }
1337
1801
  bgAnsi256(code) {
1338
- const coloredPigment = this.pigment.bgAnsi256(code);
1339
- return (text) => coloredPigment(text);
1802
+ return this.pigment.bgAnsi256(code);
1340
1803
  }
1341
1804
  };
1805
+ for (const name of [...MODIFIER_NAMES, ...COLOR_NAMES, ...BACKGROUND_COLOR_NAMES]) {
1806
+ Object.defineProperty(Chalk.prototype, name, {
1807
+ configurable: true,
1808
+ enumerable: false,
1809
+ get() {
1810
+ return this.pigment[name];
1811
+ }
1812
+ });
1813
+ }
1342
1814
 
1343
1815
  // src/core/chalk-default.ts
1344
1816
  var chalk = new Chalk();
@@ -1354,48 +1826,51 @@ function applyStyle(text, open, close) {
1354
1826
  }
1355
1827
 
1356
1828
  // src/core/composer.ts
1357
- var bold = (text) => applyStyle(text, "\x1B[1m", "\x1B[22m");
1358
- var dim = (text) => applyStyle(text, "\x1B[2m", "\x1B[22m");
1359
- var italic = (text) => applyStyle(text, "\x1B[3m", "\x1B[23m");
1360
- var underline = (text) => applyStyle(text, "\x1B[4m", "\x1B[24m");
1361
- var strikethrough = (text) => applyStyle(text, "\x1B[9m", "\x1B[29m");
1362
- var inverse = (text) => applyStyle(text, "\x1B[7m", "\x1B[27m");
1363
- var hidden = (text) => applyStyle(text, "\x1B[8m", "\x1B[28m");
1364
- var reset = (text) => applyStyle(text, "\x1B[0m", "\x1B[0m");
1365
- var black = (text) => applyStyle(text, "\x1B[30m", "\x1B[39m");
1366
- var red = (text) => applyStyle(text, "\x1B[31m", "\x1B[39m");
1367
- var green = (text) => applyStyle(text, "\x1B[32m", "\x1B[39m");
1368
- var yellow = (text) => applyStyle(text, "\x1B[33m", "\x1B[39m");
1369
- var blue = (text) => applyStyle(text, "\x1B[34m", "\x1B[39m");
1370
- var magenta = (text) => applyStyle(text, "\x1B[35m", "\x1B[39m");
1371
- var cyan = (text) => applyStyle(text, "\x1B[36m", "\x1B[39m");
1372
- var white = (text) => applyStyle(text, "\x1B[37m", "\x1B[39m");
1373
- var blackBright = (text) => applyStyle(text, "\x1B[90m", "\x1B[39m");
1374
- var redBright = (text) => applyStyle(text, "\x1B[91m", "\x1B[39m");
1375
- var greenBright = (text) => applyStyle(text, "\x1B[92m", "\x1B[39m");
1376
- var yellowBright = (text) => applyStyle(text, "\x1B[93m", "\x1B[39m");
1377
- var blueBright = (text) => applyStyle(text, "\x1B[94m", "\x1B[39m");
1378
- var magentaBright = (text) => applyStyle(text, "\x1B[95m", "\x1B[39m");
1379
- var cyanBright = (text) => applyStyle(text, "\x1B[96m", "\x1B[39m");
1380
- var whiteBright = (text) => applyStyle(text, "\x1B[97m", "\x1B[39m");
1829
+ function applyStyle2(text, open, close) {
1830
+ return detectColorSupport().level === 0 ? text : applyStyle(text, open, close);
1831
+ }
1832
+ var bold = (text) => applyStyle2(text, "\x1B[1m", "\x1B[22m");
1833
+ var dim = (text) => applyStyle2(text, "\x1B[2m", "\x1B[22m");
1834
+ var italic = (text) => applyStyle2(text, "\x1B[3m", "\x1B[23m");
1835
+ var underline = (text) => applyStyle2(text, "\x1B[4m", "\x1B[24m");
1836
+ var strikethrough = (text) => applyStyle2(text, "\x1B[9m", "\x1B[29m");
1837
+ var inverse = (text) => applyStyle2(text, "\x1B[7m", "\x1B[27m");
1838
+ var hidden = (text) => applyStyle2(text, "\x1B[8m", "\x1B[28m");
1839
+ var reset = (text) => applyStyle2(text, "\x1B[0m", "\x1B[0m");
1840
+ var black = (text) => applyStyle2(text, "\x1B[30m", "\x1B[39m");
1841
+ var red = (text) => applyStyle2(text, "\x1B[31m", "\x1B[39m");
1842
+ var green = (text) => applyStyle2(text, "\x1B[32m", "\x1B[39m");
1843
+ var yellow = (text) => applyStyle2(text, "\x1B[33m", "\x1B[39m");
1844
+ var blue = (text) => applyStyle2(text, "\x1B[34m", "\x1B[39m");
1845
+ var magenta = (text) => applyStyle2(text, "\x1B[35m", "\x1B[39m");
1846
+ var cyan = (text) => applyStyle2(text, "\x1B[36m", "\x1B[39m");
1847
+ var white = (text) => applyStyle2(text, "\x1B[37m", "\x1B[39m");
1848
+ var blackBright = (text) => applyStyle2(text, "\x1B[90m", "\x1B[39m");
1849
+ var redBright = (text) => applyStyle2(text, "\x1B[91m", "\x1B[39m");
1850
+ var greenBright = (text) => applyStyle2(text, "\x1B[92m", "\x1B[39m");
1851
+ var yellowBright = (text) => applyStyle2(text, "\x1B[93m", "\x1B[39m");
1852
+ var blueBright = (text) => applyStyle2(text, "\x1B[94m", "\x1B[39m");
1853
+ var magentaBright = (text) => applyStyle2(text, "\x1B[95m", "\x1B[39m");
1854
+ var cyanBright = (text) => applyStyle2(text, "\x1B[96m", "\x1B[39m");
1855
+ var whiteBright = (text) => applyStyle2(text, "\x1B[97m", "\x1B[39m");
1381
1856
  var gray = blackBright;
1382
1857
  var grey = blackBright;
1383
- var bgBlack = (text) => applyStyle(text, "\x1B[40m", "\x1B[49m");
1384
- var bgRed = (text) => applyStyle(text, "\x1B[41m", "\x1B[49m");
1385
- var bgGreen = (text) => applyStyle(text, "\x1B[42m", "\x1B[49m");
1386
- var bgYellow = (text) => applyStyle(text, "\x1B[43m", "\x1B[49m");
1387
- var bgBlue = (text) => applyStyle(text, "\x1B[44m", "\x1B[49m");
1388
- var bgMagenta = (text) => applyStyle(text, "\x1B[45m", "\x1B[49m");
1389
- var bgCyan = (text) => applyStyle(text, "\x1B[46m", "\x1B[49m");
1390
- var bgWhite = (text) => applyStyle(text, "\x1B[47m", "\x1B[49m");
1391
- var bgBlackBright = (text) => applyStyle(text, "\x1B[100m", "\x1B[49m");
1392
- var bgRedBright = (text) => applyStyle(text, "\x1B[101m", "\x1B[49m");
1393
- var bgGreenBright = (text) => applyStyle(text, "\x1B[102m", "\x1B[49m");
1394
- var bgYellowBright = (text) => applyStyle(text, "\x1B[103m", "\x1B[49m");
1395
- var bgBlueBright = (text) => applyStyle(text, "\x1B[104m", "\x1B[49m");
1396
- var bgMagentaBright = (text) => applyStyle(text, "\x1B[105m", "\x1B[49m");
1397
- var bgCyanBright = (text) => applyStyle(text, "\x1B[106m", "\x1B[49m");
1398
- var bgWhiteBright = (text) => applyStyle(text, "\x1B[107m", "\x1B[49m");
1858
+ var bgBlack = (text) => applyStyle2(text, "\x1B[40m", "\x1B[49m");
1859
+ var bgRed = (text) => applyStyle2(text, "\x1B[41m", "\x1B[49m");
1860
+ var bgGreen = (text) => applyStyle2(text, "\x1B[42m", "\x1B[49m");
1861
+ var bgYellow = (text) => applyStyle2(text, "\x1B[43m", "\x1B[49m");
1862
+ var bgBlue = (text) => applyStyle2(text, "\x1B[44m", "\x1B[49m");
1863
+ var bgMagenta = (text) => applyStyle2(text, "\x1B[45m", "\x1B[49m");
1864
+ var bgCyan = (text) => applyStyle2(text, "\x1B[46m", "\x1B[49m");
1865
+ var bgWhite = (text) => applyStyle2(text, "\x1B[47m", "\x1B[49m");
1866
+ var bgBlackBright = (text) => applyStyle2(text, "\x1B[100m", "\x1B[49m");
1867
+ var bgRedBright = (text) => applyStyle2(text, "\x1B[101m", "\x1B[49m");
1868
+ var bgGreenBright = (text) => applyStyle2(text, "\x1B[102m", "\x1B[49m");
1869
+ var bgYellowBright = (text) => applyStyle2(text, "\x1B[103m", "\x1B[49m");
1870
+ var bgBlueBright = (text) => applyStyle2(text, "\x1B[104m", "\x1B[49m");
1871
+ var bgMagentaBright = (text) => applyStyle2(text, "\x1B[105m", "\x1B[49m");
1872
+ var bgCyanBright = (text) => applyStyle2(text, "\x1B[106m", "\x1B[49m");
1873
+ var bgWhiteBright = (text) => applyStyle2(text, "\x1B[107m", "\x1B[49m");
1399
1874
  var bgGray = bgBlackBright;
1400
1875
  var bgGrey = bgBlackBright;
1401
1876
  function compose(...stylers) {
@@ -1404,9 +1879,6 @@ function compose(...stylers) {
1404
1879
  };
1405
1880
  }
1406
1881
 
1407
- // src/index.ts
1408
- var import_plugin2 = require("@oxog/plugin");
1409
-
1410
1882
  // src/plugins/index.ts
1411
1883
  var plugins_exports = {};
1412
1884
  __export(plugins_exports, {
@@ -1425,6 +1897,33 @@ __export(plugins_exports, {
1425
1897
  });
1426
1898
 
1427
1899
  // src/plugins/optional/gradient.ts
1900
+ function toRgb(color) {
1901
+ if (typeof color !== "string") {
1902
+ const [r, g, b] = color;
1903
+ return { r, g, b };
1904
+ }
1905
+ return color.startsWith("rgb(") ? parseRgb(color) : hexToRgb(color);
1906
+ }
1907
+ function paintGradient(text, startRgb, endRgb, steps, level) {
1908
+ if (text.length === 0) {
1909
+ return text;
1910
+ }
1911
+ const colors = interpolateColors(startRgb, endRgb, text.length, Math.max(2, steps));
1912
+ let result = "";
1913
+ for (let i = 0; i < text.length; i++) {
1914
+ const colorIndex = text.length === 1 ? 0 : Math.floor(i / (text.length - 1) * (colors.length - 1));
1915
+ result += paintRgb(text[i], colors[colorIndex], false, level);
1916
+ }
1917
+ return result;
1918
+ }
1919
+ function paintRainbow(text, steps, level) {
1920
+ const colors = generateRainbowColors(text.length, Math.max(1, steps));
1921
+ let result = "";
1922
+ for (let i = 0; i < text.length; i++) {
1923
+ result += paintRgb(text[i], colors[i % colors.length], false, level);
1924
+ }
1925
+ return result;
1926
+ }
1428
1927
  function gradientPlugin(options) {
1429
1928
  const steps = options?.steps ?? 10;
1430
1929
  return {
@@ -1438,6 +1937,15 @@ function gradientPlugin(options) {
1438
1937
  enabled: true
1439
1938
  }
1440
1939
  });
1940
+ registerPluginApi(kernel, "gradient", (start, end) => {
1941
+ const startRgb = toRgb(start);
1942
+ const endRgb = toRgb(end);
1943
+ if (!startRgb || !endRgb) {
1944
+ throw new ColorError(`Invalid gradient color: ${String(startRgb ? end : start)}`);
1945
+ }
1946
+ return (text) => paintGradient(String(text), startRgb, endRgb, steps, k.getContext().level);
1947
+ });
1948
+ registerPluginApi(kernel, "rainbow", (text) => paintRainbow(String(text), steps, k.getContext().level));
1441
1949
  k.on("style:gradient", (data) => {
1442
1950
  const { start, end, text, steps: gradientSteps = steps } = data;
1443
1951
  const startRgb = start.startsWith("#") ? hexToRgb(start) : start.startsWith("rgb(") ? parseRgb(start) : hexToRgb(start);
@@ -1508,6 +2016,7 @@ function generateRainbowColors(length, steps) {
1508
2016
  }
1509
2017
 
1510
2018
  // src/plugins/optional/theme.ts
2019
+ var THEME_COLOR_TYPES = ["keyword", "string", "number", "comment", "error", "success", "warning", "info"];
1511
2020
  function themePlugin(options) {
1512
2021
  const preset = options?.preset ?? "monokai";
1513
2022
  const custom = options?.custom ?? {};
@@ -1527,6 +2036,17 @@ function themePlugin(options) {
1527
2036
  enabled: true
1528
2037
  }
1529
2038
  });
2039
+ const api = {};
2040
+ for (const type of THEME_COLOR_TYPES) {
2041
+ const color = theme[type];
2042
+ api[type] = (text) => {
2043
+ if (!color) {
2044
+ throw new Error(`Unknown theme color type: ${type}`);
2045
+ }
2046
+ return paintRgb(String(text), hexToRgb(color), false, k.getContext().level);
2047
+ };
2048
+ }
2049
+ registerPluginApi(kernel, "theme", api);
1530
2050
  k.on("style:theme", (data) => {
1531
2051
  const { type, text } = data;
1532
2052
  const color = theme[type];
@@ -1596,6 +2116,10 @@ function semanticPlugin(options) {
1596
2116
  if (!hexToRgb2) {
1597
2117
  throw new Error("hexToRgb utility not available");
1598
2118
  }
2119
+ for (const [name, color] of Object.entries(defaultColors)) {
2120
+ const rgb = hexToRgb2(color);
2121
+ registerPluginApi(kernel, name, (text) => paintRgb(String(text), rgb, false, k.getContext().level));
2122
+ }
1599
2123
  k.on("style:success", (data) => {
1600
2124
  const { r, g, b } = hexToRgb2(defaultColors.success);
1601
2125
  return `\x1B[38;2;${r};${g};${b}m${data.text}\x1B[39m`;
@@ -1637,9 +2161,12 @@ function boxPlugin(options) {
1637
2161
  borders: BOX_BORDERS
1638
2162
  }
1639
2163
  });
1640
- k.on("style:box", (data) => {
1641
- const { text, border: boxBorder = border, padding: boxPadding = padding, bgColor } = data;
2164
+ const draw = (text, opts = {}) => {
2165
+ const { border: boxBorder = border, padding: boxPadding = padding, bgColor } = opts;
1642
2166
  const chars = BOX_BORDERS[boxBorder];
2167
+ if (!chars) {
2168
+ throw new StyleError(`Unknown box border: ${String(boxBorder)}`);
2169
+ }
1643
2170
  const lines = text.split("\n");
1644
2171
  const width = Math.max(...lines.map((l) => l.length)) + boxPadding * 2;
1645
2172
  const pad = " ".repeat(boxPadding);
@@ -1651,21 +2178,51 @@ function boxPlugin(options) {
1651
2178
  const bottom = `${chars.bl}${chars.h.repeat(width)}${chars.br}`;
1652
2179
  const result = [top, ...middle, bottom].join("\n");
1653
2180
  if (bgColor) {
1654
- const ctx = k.getContext();
1655
- const hexToRgb2 = ctx.utils?.hexToRgb;
1656
- if (!hexToRgb2) {
1657
- throw new Error("hexToRgb utility not available");
1658
- }
1659
- const { r, g, b } = hexToRgb2(bgColor);
1660
- return `\x1B[48;2;${r};${g};${b}m${result}\x1B[49m`;
2181
+ return paintRgb(result, hexToRgb(bgColor), true, k.getContext().level);
1661
2182
  }
1662
2183
  return result;
2184
+ };
2185
+ registerPluginApi(kernel, "box", (text, opts) => draw(String(text), opts));
2186
+ k.on("style:box", (data) => {
2187
+ const { text, ...opts } = data;
2188
+ return draw(text, opts);
1663
2189
  });
1664
2190
  }
1665
2191
  };
1666
2192
  }
1667
2193
 
1668
2194
  // src/plugins/optional/template.ts
2195
+ var STYLE_NAMES = /* @__PURE__ */ new Set([...MODIFIER_NAMES, ...COLOR_NAMES, ...BACKGROUND_COLOR_NAMES]);
2196
+ var BLOCK = /\{([A-Za-z]\w*(?:\.[A-Za-z]\w*)*) ([^{}]*)\}/g;
2197
+ var TOKEN = /\uE000(\d+)\uE001/g;
2198
+ function renderTemplate(root, strings, values) {
2199
+ const parts = values.map((value) => String(value));
2200
+ const token = (index) => `\uE000${index}\uE001`;
2201
+ const restore = (text) => text.replace(TOKEN, (_match, index) => parts[Number(index)]);
2202
+ let source = "";
2203
+ for (let i = 0; i < strings.length; i++) {
2204
+ source += strings[i];
2205
+ if (i < values.length) {
2206
+ source += token(i);
2207
+ }
2208
+ }
2209
+ let previous;
2210
+ do {
2211
+ previous = source;
2212
+ source = source.replace(BLOCK, (_match, chain, text) => {
2213
+ let styler = root;
2214
+ for (const name of chain.split(".")) {
2215
+ if (!STYLE_NAMES.has(name)) {
2216
+ throw new StyleError(`Unknown style in template: ${name}`);
2217
+ }
2218
+ styler = styler[name];
2219
+ }
2220
+ parts.push(styler(restore(text)));
2221
+ return token(parts.length - 1);
2222
+ });
2223
+ } while (source !== previous);
2224
+ return restore(source);
2225
+ }
1669
2226
  function templatePlugin() {
1670
2227
  return {
1671
2228
  name: "template",
@@ -1677,6 +2234,11 @@ function templatePlugin() {
1677
2234
  enabled: true
1678
2235
  }
1679
2236
  });
2237
+ registerPluginApi(
2238
+ kernel,
2239
+ "template",
2240
+ (strings, ...values) => renderTemplate(getRoot(kernel) ?? createProxyPigment(k), strings, values)
2241
+ );
1680
2242
  k.on("style:template", (data) => {
1681
2243
  const { strings, values } = data;
1682
2244
  let result = "";
@@ -1713,7 +2275,8 @@ function parseStyleExpression(expression, text, kernel) {
1713
2275
  }
1714
2276
  }
1715
2277
  if (typeof current === "function") {
1716
- return current(text);
2278
+ const style = current;
2279
+ return style(text);
1717
2280
  }
1718
2281
  return text;
1719
2282
  }
@@ -1729,7 +2292,7 @@ function pigmentPlugin(options) {
1729
2292
  const k = kernel;
1730
2293
  const themeColors = THEME_PRESETS[theme] ?? THEME_PRESETS.monokai;
1731
2294
  const pigment2 = createPigment({ level });
1732
- k.pigment = pigment2;
2295
+ Object.assign(k, { pigment: pigment2 });
1733
2296
  k.updateContext({
1734
2297
  ecosystem: {
1735
2298
  theme,