@oxog/pigment 1.1.0 → 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;
@@ -688,6 +688,12 @@ var PigmentError = class _PigmentError extends Error {
688
688
  Error.captureStackTrace(this, _PigmentError);
689
689
  }
690
690
  };
691
+ var PluginError3 = class extends PigmentError {
692
+ constructor(pluginName, message) {
693
+ super(`[${pluginName}] ${message}`, "PLUGIN_ERROR");
694
+ this.name = "PluginError";
695
+ }
696
+ };
691
697
  var ColorError = class extends PigmentError {
692
698
  constructor(message) {
693
699
  super(message, "COLOR_ERROR");
@@ -701,6 +707,23 @@ var StyleError = class extends PigmentError {
701
707
  }
702
708
  };
703
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
+
704
727
  // src/core/ansi.ts
705
728
  function generateModifierCode(name) {
706
729
  const modifier = ANSI_MODIFIERS[name];
@@ -774,6 +797,141 @@ function generateRgbCode(r, g, b, background = false) {
774
797
  background
775
798
  };
776
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
+ }
777
935
 
778
936
  // src/core/proxy.ts
779
937
  var proxyCache = /* @__PURE__ */ new WeakMap();
@@ -801,14 +959,14 @@ function createProxyPigment(kernel, styles = []) {
801
959
  if (prop === "ansi256" || prop === "bgAnsi256") {
802
960
  return (code) => {
803
961
  const background = prop === "bgAnsi256";
804
- const ansi256Style = generateAnsi256Code(code, background);
962
+ const ansi256Style = generateAnsi256CodeForLevel(code, background, ctx.level);
805
963
  return createProxyPigment(kernel, [...styles, ansi256Style]);
806
964
  };
807
965
  }
808
966
  if (prop === "rgb" || prop === "bgRgb") {
809
967
  return (r, g, b) => {
810
968
  const background = prop === "bgRgb";
811
- const rgbStyle = generateRgbCode(r, g, b, background);
969
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
812
970
  return createProxyPigment(kernel, [...styles, rgbStyle]);
813
971
  };
814
972
  }
@@ -820,7 +978,7 @@ function createProxyPigment(kernel, styles = []) {
820
978
  throw new Error("hexToRgb utility not available");
821
979
  }
822
980
  const { r, g, b } = hexToRgb2(color);
823
- const rgbStyle = generateRgbCode(r, g, b, background);
981
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
824
982
  return createProxyPigment(kernel, [...styles, rgbStyle]);
825
983
  };
826
984
  }
@@ -832,28 +990,37 @@ function createProxyPigment(kernel, styles = []) {
832
990
  throw new Error("hslToRgb utility not available");
833
991
  }
834
992
  const { r, g, b } = hslToRgb2(h2, s, l);
835
- const rgbStyle = generateRgbCode(r, g, b, background);
993
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
836
994
  return createProxyPigment(kernel, [...styles, rgbStyle]);
837
995
  };
838
996
  }
839
997
  }
840
998
  if (prop === "visible") {
841
999
  const colorSupport = ctx.colorSupport;
842
- return (text) => colorSupport.level > 0 ? applyStyles(String(text), styles) : "";
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);
843
1010
  }
844
1011
  return Reflect.get(target, prop);
845
1012
  },
846
1013
  apply(_target, _thisArg, argArray) {
847
1014
  const text = String(argArray[0] ?? "");
848
- return applyStyles(text, styles);
1015
+ return applyStyles(text, styles, kernel.getContext().level);
849
1016
  }
850
1017
  };
851
1018
  const pigment2 = new Proxy((() => ""), handler);
852
1019
  proxyCache.set(pigment2, pigment2);
853
1020
  return pigment2;
854
1021
  }
855
- function applyStyles(text, styles) {
856
- if (styles.length === 0 || text === "") {
1022
+ function applyStyles(text, styles, level) {
1023
+ if (styles.length === 0 || text === "" || level === 0) {
857
1024
  return text;
858
1025
  }
859
1026
  const open = styles.map((style) => style.open).join("");
@@ -932,7 +1099,13 @@ function hslToRgb(h2, s, l) {
932
1099
  // src/core/builder.ts
933
1100
  var BuilderPigment = class {
934
1101
  styles = [];
935
- 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;
936
1109
  }
937
1110
  bold() {
938
1111
  this.styles.push(generateModifierCode("bold"));
@@ -1111,26 +1284,26 @@ var BuilderPigment = class {
1111
1284
  return this;
1112
1285
  }
1113
1286
  ansi256(code) {
1114
- this.styles.push(generateAnsi256Code(code, false));
1287
+ this.styles.push(generateAnsi256CodeForLevel(code, false, this.level));
1115
1288
  return this;
1116
1289
  }
1117
1290
  bgAnsi256(code) {
1118
- this.styles.push(generateAnsi256Code(code, true));
1291
+ this.styles.push(generateAnsi256CodeForLevel(code, true, this.level));
1119
1292
  return this;
1120
1293
  }
1121
1294
  rgb(r, g, b) {
1122
- this.styles.push(generateRgbCode(r, g, b, false));
1295
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
1123
1296
  return this;
1124
1297
  }
1125
1298
  bgRgb(r, g, b) {
1126
- this.styles.push(generateRgbCode(r, g, b, true));
1299
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
1127
1300
  return this;
1128
1301
  }
1129
1302
  hex(color) {
1130
1303
  const rgb = hexToRgb(color);
1131
1304
  if (rgb) {
1132
1305
  const { r, g, b } = rgb;
1133
- this.styles.push(generateRgbCode(r, g, b, false));
1306
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
1134
1307
  }
1135
1308
  return this;
1136
1309
  }
@@ -1138,22 +1311,22 @@ var BuilderPigment = class {
1138
1311
  const rgb = hexToRgb(color);
1139
1312
  if (rgb) {
1140
1313
  const { r, g, b } = rgb;
1141
- this.styles.push(generateRgbCode(r, g, b, true));
1314
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
1142
1315
  }
1143
1316
  return this;
1144
1317
  }
1145
1318
  hsl(h2, s, l) {
1146
1319
  const { r, g, b } = hslToRgb(h2, s, l);
1147
- this.styles.push(generateRgbCode(r, g, b, false));
1320
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
1148
1321
  return this;
1149
1322
  }
1150
1323
  bgHsl(h2, s, l) {
1151
1324
  const { r, g, b } = hslToRgb(h2, s, l);
1152
- this.styles.push(generateRgbCode(r, g, b, true));
1325
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
1153
1326
  return this;
1154
1327
  }
1155
1328
  paint(text) {
1156
- if (this.styles.length === 0 || text === "") {
1329
+ if (this.styles.length === 0 || text === "" || this.level === 0) {
1157
1330
  return text;
1158
1331
  }
1159
1332
  const open = this.styles.map((style) => style.open).join("");
@@ -1278,23 +1451,6 @@ function toColorSupport(level) {
1278
1451
  return { level, hasBasic: level >= 1, has256: level >= 2, has16m: level >= 3 };
1279
1452
  }
1280
1453
 
1281
- // src/utils/rgb-to-ansi256.ts
1282
- function rgbToAnsi256(r, g, b) {
1283
- const clampedR = Math.max(0, Math.min(255, r));
1284
- const clampedG = Math.max(0, Math.min(255, g));
1285
- const clampedB = Math.max(0, Math.min(255, b));
1286
- if (clampedR === clampedG && clampedG === clampedB) {
1287
- if (clampedR < 8) {
1288
- return 16;
1289
- }
1290
- if (clampedR > 248) {
1291
- return 231;
1292
- }
1293
- return Math.round((clampedR - 8) / 247 * 24) + 232;
1294
- }
1295
- return Math.round(clampedR / 51) * 36 + Math.round(clampedG / 51) * 6 + Math.round(clampedB / 51) + 16;
1296
- }
1297
-
1298
1454
  // src/plugins/core/base-colors.ts
1299
1455
  function baseColorsPlugin() {
1300
1456
  return {
@@ -1510,13 +1666,12 @@ function createPigment(options) {
1510
1666
  kernel.use(environmentPlugin());
1511
1667
  if (options?.plugins) {
1512
1668
  for (const plugin of options.plugins) {
1513
- kernel.use(plugin);
1669
+ installPlugin(kernel, plugin);
1514
1670
  }
1515
1671
  }
1516
- void kernel.init();
1517
1672
  const pigment2 = createProxyPigment(kernel);
1518
- const builder = new BuilderPigment(kernel);
1519
- Object.defineProperty(pigment2, "builder", { value: builder, enumerable: false });
1673
+ registerRoot(kernel, pigment2);
1674
+ Object.defineProperty(pigment2, "builder", { get: () => new BuilderPigment(kernel), enumerable: false });
1520
1675
  Object.defineProperty(pigment2, "level", { value: level, enumerable: true });
1521
1676
  Object.defineProperty(pigment2, "supportsColor", { value: colorSupport, enumerable: true });
1522
1677
  return pigment2;
@@ -1524,170 +1679,55 @@ function createPigment(options) {
1524
1679
 
1525
1680
  // src/core/chalk.ts
1526
1681
  var Chalk = class {
1527
- level;
1528
1682
  supportsColor;
1683
+ currentLevel;
1529
1684
  pigment;
1530
1685
  constructor(options) {
1531
1686
  const colorSupport = detectColorSupport(options?.level);
1532
- this.level = options?.level ?? colorSupport.level;
1687
+ this.currentLevel = options?.level ?? colorSupport.level;
1533
1688
  this.supportsColor = colorSupport;
1534
- this.pigment = createPigment({ level: this.level });
1535
- }
1536
- red(text) {
1537
- return this.pigment.red(text);
1538
- }
1539
- green(text) {
1540
- return this.pigment.green(text);
1541
- }
1542
- yellow(text) {
1543
- return this.pigment.yellow(text);
1544
- }
1545
- blue(text) {
1546
- return this.pigment.blue(text);
1547
- }
1548
- magenta(text) {
1549
- return this.pigment.magenta(text);
1550
- }
1551
- cyan(text) {
1552
- return this.pigment.cyan(text);
1553
- }
1554
- white(text) {
1555
- return this.pigment.white(text);
1556
- }
1557
- black(text) {
1558
- return this.pigment.black(text);
1559
- }
1560
- redBright(text) {
1561
- return this.pigment.redBright(text);
1562
- }
1563
- greenBright(text) {
1564
- return this.pigment.greenBright(text);
1565
- }
1566
- yellowBright(text) {
1567
- return this.pigment.yellowBright(text);
1568
- }
1569
- blueBright(text) {
1570
- return this.pigment.blueBright(text);
1571
- }
1572
- magentaBright(text) {
1573
- return this.pigment.magentaBright(text);
1574
- }
1575
- cyanBright(text) {
1576
- return this.pigment.cyanBright(text);
1577
- }
1578
- whiteBright(text) {
1579
- return this.pigment.whiteBright(text);
1580
- }
1581
- gray(text) {
1582
- return this.pigment.gray(text);
1583
- }
1584
- grey(text) {
1585
- return this.pigment.grey(text);
1586
- }
1587
- bgRed(text) {
1588
- return this.pigment.bgRed(text);
1589
- }
1590
- bgGreen(text) {
1591
- return this.pigment.bgGreen(text);
1592
- }
1593
- bgYellow(text) {
1594
- return this.pigment.bgYellow(text);
1595
- }
1596
- bgBlue(text) {
1597
- return this.pigment.bgBlue(text);
1598
- }
1599
- bgMagenta(text) {
1600
- return this.pigment.bgMagenta(text);
1601
- }
1602
- bgCyan(text) {
1603
- return this.pigment.bgCyan(text);
1604
- }
1605
- bgWhite(text) {
1606
- return this.pigment.bgWhite(text);
1607
- }
1608
- bgBlack(text) {
1609
- return this.pigment.bgBlack(text);
1610
- }
1611
- bgRedBright(text) {
1612
- return this.pigment.bgRedBright(text);
1613
- }
1614
- bgGreenBright(text) {
1615
- return this.pigment.bgGreenBright(text);
1616
- }
1617
- bgYellowBright(text) {
1618
- return this.pigment.bgYellowBright(text);
1619
- }
1620
- bgBlueBright(text) {
1621
- return this.pigment.bgBlueBright(text);
1622
- }
1623
- bgMagentaBright(text) {
1624
- return this.pigment.bgMagentaBright(text);
1625
- }
1626
- bgCyanBright(text) {
1627
- return this.pigment.bgCyanBright(text);
1628
- }
1629
- bgWhiteBright(text) {
1630
- return this.pigment.bgWhiteBright(text);
1631
- }
1632
- bgGray(text) {
1633
- return this.pigment.bgGray(text);
1634
- }
1635
- bgGrey(text) {
1636
- return this.pigment.bgGrey(text);
1689
+ this.pigment = createPigment({ level: this.currentLevel });
1637
1690
  }
1638
- bold(text) {
1639
- return this.pigment.bold(text);
1691
+ /** Color level (0-3). Setting it changes the output of every style. */
1692
+ get level() {
1693
+ return this.currentLevel;
1640
1694
  }
1641
- dim(text) {
1642
- return this.pigment.dim(text);
1695
+ set level(level) {
1696
+ this.currentLevel = level;
1697
+ this.pigment = createPigment({ level });
1643
1698
  }
1644
- italic(text) {
1645
- return this.pigment.italic(text);
1646
- }
1647
- underline(text) {
1648
- return this.pigment.underline(text);
1649
- }
1650
- strikethrough(text) {
1651
- return this.pigment.strikethrough(text);
1652
- }
1653
- inverse(text) {
1654
- return this.pigment.inverse(text);
1655
- }
1656
- hidden(text) {
1657
- return this.pigment.hidden(text);
1658
- }
1659
- reset(text) {
1660
- return this.pigment.reset(text);
1661
- }
1662
- /** Like chalk: returns the text only when colors are supported, otherwise ''. */
1699
+ /** Like chalk: returns the text only when colors are enabled, otherwise ''. */
1663
1700
  visible(text) {
1664
- return this.supportsColor.level > 0 ? text : "";
1701
+ return this.currentLevel > 0 ? text : "";
1665
1702
  }
1666
1703
  rgb(r, g, b) {
1667
- const coloredPigment = this.pigment.rgb(r, g, b);
1668
- return (text) => coloredPigment(text);
1704
+ return this.pigment.rgb(r, g, b);
1669
1705
  }
1670
1706
  bgRgb(r, g, b) {
1671
- const coloredPigment = this.pigment.bgRgb(r, g, b);
1672
- return (text) => coloredPigment(text);
1707
+ return this.pigment.bgRgb(r, g, b);
1673
1708
  }
1674
1709
  hex(color) {
1675
- const coloredPigment = this.pigment.hex(color);
1676
- return (text) => coloredPigment(text);
1710
+ return this.pigment.hex(color);
1677
1711
  }
1678
1712
  bgHex(color) {
1679
- const coloredPigment = this.pigment.bgHex(color);
1680
- return (text) => coloredPigment(text);
1713
+ return this.pigment.bgHex(color);
1681
1714
  }
1682
1715
  ansi256(code) {
1683
- const coloredPigment = this.pigment.ansi256(code);
1684
- return (text) => coloredPigment(text);
1716
+ return this.pigment.ansi256(code);
1685
1717
  }
1686
1718
  bgAnsi256(code) {
1687
- const coloredPigment = this.pigment.bgAnsi256(code);
1688
- return (text) => coloredPigment(text);
1719
+ return this.pigment.bgAnsi256(code);
1689
1720
  }
1690
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
+ }
1691
1731
 
1692
1732
  // src/core/chalk-default.ts
1693
1733
  var chalk = new Chalk();
@@ -1703,48 +1743,51 @@ function applyStyle(text, open, close) {
1703
1743
  }
1704
1744
 
1705
1745
  // src/core/composer.ts
1706
- var bold = (text) => applyStyle(text, "\x1B[1m", "\x1B[22m");
1707
- var dim = (text) => applyStyle(text, "\x1B[2m", "\x1B[22m");
1708
- var italic = (text) => applyStyle(text, "\x1B[3m", "\x1B[23m");
1709
- var underline = (text) => applyStyle(text, "\x1B[4m", "\x1B[24m");
1710
- var strikethrough = (text) => applyStyle(text, "\x1B[9m", "\x1B[29m");
1711
- var inverse = (text) => applyStyle(text, "\x1B[7m", "\x1B[27m");
1712
- var hidden = (text) => applyStyle(text, "\x1B[8m", "\x1B[28m");
1713
- var reset = (text) => applyStyle(text, "\x1B[0m", "\x1B[0m");
1714
- var black = (text) => applyStyle(text, "\x1B[30m", "\x1B[39m");
1715
- var red = (text) => applyStyle(text, "\x1B[31m", "\x1B[39m");
1716
- var green = (text) => applyStyle(text, "\x1B[32m", "\x1B[39m");
1717
- var yellow = (text) => applyStyle(text, "\x1B[33m", "\x1B[39m");
1718
- var blue = (text) => applyStyle(text, "\x1B[34m", "\x1B[39m");
1719
- var magenta = (text) => applyStyle(text, "\x1B[35m", "\x1B[39m");
1720
- var cyan = (text) => applyStyle(text, "\x1B[36m", "\x1B[39m");
1721
- var white = (text) => applyStyle(text, "\x1B[37m", "\x1B[39m");
1722
- var blackBright = (text) => applyStyle(text, "\x1B[90m", "\x1B[39m");
1723
- var redBright = (text) => applyStyle(text, "\x1B[91m", "\x1B[39m");
1724
- var greenBright = (text) => applyStyle(text, "\x1B[92m", "\x1B[39m");
1725
- var yellowBright = (text) => applyStyle(text, "\x1B[93m", "\x1B[39m");
1726
- var blueBright = (text) => applyStyle(text, "\x1B[94m", "\x1B[39m");
1727
- var magentaBright = (text) => applyStyle(text, "\x1B[95m", "\x1B[39m");
1728
- var cyanBright = (text) => applyStyle(text, "\x1B[96m", "\x1B[39m");
1729
- 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");
1730
1773
  var gray = blackBright;
1731
1774
  var grey = blackBright;
1732
- var bgBlack = (text) => applyStyle(text, "\x1B[40m", "\x1B[49m");
1733
- var bgRed = (text) => applyStyle(text, "\x1B[41m", "\x1B[49m");
1734
- var bgGreen = (text) => applyStyle(text, "\x1B[42m", "\x1B[49m");
1735
- var bgYellow = (text) => applyStyle(text, "\x1B[43m", "\x1B[49m");
1736
- var bgBlue = (text) => applyStyle(text, "\x1B[44m", "\x1B[49m");
1737
- var bgMagenta = (text) => applyStyle(text, "\x1B[45m", "\x1B[49m");
1738
- var bgCyan = (text) => applyStyle(text, "\x1B[46m", "\x1B[49m");
1739
- var bgWhite = (text) => applyStyle(text, "\x1B[47m", "\x1B[49m");
1740
- var bgBlackBright = (text) => applyStyle(text, "\x1B[100m", "\x1B[49m");
1741
- var bgRedBright = (text) => applyStyle(text, "\x1B[101m", "\x1B[49m");
1742
- var bgGreenBright = (text) => applyStyle(text, "\x1B[102m", "\x1B[49m");
1743
- var bgYellowBright = (text) => applyStyle(text, "\x1B[103m", "\x1B[49m");
1744
- var bgBlueBright = (text) => applyStyle(text, "\x1B[104m", "\x1B[49m");
1745
- var bgMagentaBright = (text) => applyStyle(text, "\x1B[105m", "\x1B[49m");
1746
- var bgCyanBright = (text) => applyStyle(text, "\x1B[106m", "\x1B[49m");
1747
- 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");
1748
1791
  var bgGray = bgBlackBright;
1749
1792
  var bgGrey = bgBlackBright;
1750
1793
  function compose(...stylers) {
@@ -1771,6 +1814,33 @@ __export(plugins_exports, {
1771
1814
  });
1772
1815
 
1773
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
+ }
1774
1844
  function gradientPlugin(options) {
1775
1845
  const steps = options?.steps ?? 10;
1776
1846
  return {
@@ -1784,6 +1854,15 @@ function gradientPlugin(options) {
1784
1854
  enabled: true
1785
1855
  }
1786
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));
1787
1866
  k.on("style:gradient", (data) => {
1788
1867
  const { start, end, text, steps: gradientSteps = steps } = data;
1789
1868
  const startRgb = start.startsWith("#") ? hexToRgb(start) : start.startsWith("rgb(") ? parseRgb(start) : hexToRgb(start);
@@ -1854,6 +1933,7 @@ function generateRainbowColors(length, steps) {
1854
1933
  }
1855
1934
 
1856
1935
  // src/plugins/optional/theme.ts
1936
+ var THEME_COLOR_TYPES = ["keyword", "string", "number", "comment", "error", "success", "warning", "info"];
1857
1937
  function themePlugin(options) {
1858
1938
  const preset = options?.preset ?? "monokai";
1859
1939
  const custom = options?.custom ?? {};
@@ -1873,6 +1953,17 @@ function themePlugin(options) {
1873
1953
  enabled: true
1874
1954
  }
1875
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);
1876
1967
  k.on("style:theme", (data) => {
1877
1968
  const { type, text } = data;
1878
1969
  const color = theme[type];
@@ -1942,6 +2033,10 @@ function semanticPlugin(options) {
1942
2033
  if (!hexToRgb2) {
1943
2034
  throw new Error("hexToRgb utility not available");
1944
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
+ }
1945
2040
  k.on("style:success", (data) => {
1946
2041
  const { r, g, b } = hexToRgb2(defaultColors.success);
1947
2042
  return `\x1B[38;2;${r};${g};${b}m${data.text}\x1B[39m`;
@@ -1983,9 +2078,12 @@ function boxPlugin(options) {
1983
2078
  borders: BOX_BORDERS
1984
2079
  }
1985
2080
  });
1986
- k.on("style:box", (data) => {
1987
- 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;
1988
2083
  const chars = BOX_BORDERS[boxBorder];
2084
+ if (!chars) {
2085
+ throw new StyleError(`Unknown box border: ${String(boxBorder)}`);
2086
+ }
1989
2087
  const lines = text.split("\n");
1990
2088
  const width = Math.max(...lines.map((l) => l.length)) + boxPadding * 2;
1991
2089
  const pad = " ".repeat(boxPadding);
@@ -1997,21 +2095,51 @@ function boxPlugin(options) {
1997
2095
  const bottom = `${chars.bl}${chars.h.repeat(width)}${chars.br}`;
1998
2096
  const result = [top, ...middle, bottom].join("\n");
1999
2097
  if (bgColor) {
2000
- const ctx = k.getContext();
2001
- const hexToRgb2 = ctx.utils?.hexToRgb;
2002
- if (!hexToRgb2) {
2003
- throw new Error("hexToRgb utility not available");
2004
- }
2005
- const { r, g, b } = hexToRgb2(bgColor);
2006
- return `\x1B[48;2;${r};${g};${b}m${result}\x1B[49m`;
2098
+ return paintRgb(result, hexToRgb(bgColor), true, k.getContext().level);
2007
2099
  }
2008
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);
2009
2106
  });
2010
2107
  }
2011
2108
  };
2012
2109
  }
2013
2110
 
2014
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
+ }
2015
2143
  function templatePlugin() {
2016
2144
  return {
2017
2145
  name: "template",
@@ -2023,6 +2151,11 @@ function templatePlugin() {
2023
2151
  enabled: true
2024
2152
  }
2025
2153
  });
2154
+ registerPluginApi(
2155
+ kernel,
2156
+ "template",
2157
+ (strings, ...values) => renderTemplate(getRoot(kernel) ?? createProxyPigment(k), strings, values)
2158
+ );
2026
2159
  k.on("style:template", (data) => {
2027
2160
  const { strings, values } = data;
2028
2161
  let result = "";