@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.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";
@@ -771,6 +771,12 @@ var PigmentError = class _PigmentError extends Error {
771
771
  Error.captureStackTrace(this, _PigmentError);
772
772
  }
773
773
  };
774
+ var PluginError3 = class extends PigmentError {
775
+ constructor(pluginName, message) {
776
+ super(`[${pluginName}] ${message}`, "PLUGIN_ERROR");
777
+ this.name = "PluginError";
778
+ }
779
+ };
774
780
  var ColorError = class extends PigmentError {
775
781
  constructor(message) {
776
782
  super(message, "COLOR_ERROR");
@@ -784,6 +790,23 @@ var StyleError = class extends PigmentError {
784
790
  }
785
791
  };
786
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
+
787
810
  // src/core/ansi.ts
788
811
  function generateModifierCode(name) {
789
812
  const modifier = ANSI_MODIFIERS[name];
@@ -857,6 +880,141 @@ function generateRgbCode(r, g, b, background = false) {
857
880
  background
858
881
  };
859
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
+ }
860
1018
 
861
1019
  // src/core/proxy.ts
862
1020
  var proxyCache = /* @__PURE__ */ new WeakMap();
@@ -884,14 +1042,14 @@ function createProxyPigment(kernel, styles = []) {
884
1042
  if (prop === "ansi256" || prop === "bgAnsi256") {
885
1043
  return (code) => {
886
1044
  const background = prop === "bgAnsi256";
887
- const ansi256Style = generateAnsi256Code(code, background);
1045
+ const ansi256Style = generateAnsi256CodeForLevel(code, background, ctx.level);
888
1046
  return createProxyPigment(kernel, [...styles, ansi256Style]);
889
1047
  };
890
1048
  }
891
1049
  if (prop === "rgb" || prop === "bgRgb") {
892
1050
  return (r, g, b) => {
893
1051
  const background = prop === "bgRgb";
894
- const rgbStyle = generateRgbCode(r, g, b, background);
1052
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
895
1053
  return createProxyPigment(kernel, [...styles, rgbStyle]);
896
1054
  };
897
1055
  }
@@ -903,7 +1061,7 @@ function createProxyPigment(kernel, styles = []) {
903
1061
  throw new Error("hexToRgb utility not available");
904
1062
  }
905
1063
  const { r, g, b } = hexToRgb2(color);
906
- const rgbStyle = generateRgbCode(r, g, b, background);
1064
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
907
1065
  return createProxyPigment(kernel, [...styles, rgbStyle]);
908
1066
  };
909
1067
  }
@@ -915,28 +1073,37 @@ function createProxyPigment(kernel, styles = []) {
915
1073
  throw new Error("hslToRgb utility not available");
916
1074
  }
917
1075
  const { r, g, b } = hslToRgb2(h2, s, l);
918
- const rgbStyle = generateRgbCode(r, g, b, background);
1076
+ const rgbStyle = generateRgbCodeForLevel(r, g, b, background, ctx.level);
919
1077
  return createProxyPigment(kernel, [...styles, rgbStyle]);
920
1078
  };
921
1079
  }
922
1080
  }
923
1081
  if (prop === "visible") {
924
1082
  const colorSupport = ctx.colorSupport;
925
- return (text) => colorSupport.level > 0 ? applyStyles(String(text), styles) : "";
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);
926
1093
  }
927
1094
  return Reflect.get(target, prop);
928
1095
  },
929
1096
  apply(_target, _thisArg, argArray) {
930
1097
  const text = String(argArray[0] ?? "");
931
- return applyStyles(text, styles);
1098
+ return applyStyles(text, styles, kernel.getContext().level);
932
1099
  }
933
1100
  };
934
1101
  const pigment2 = new Proxy((() => ""), handler);
935
1102
  proxyCache.set(pigment2, pigment2);
936
1103
  return pigment2;
937
1104
  }
938
- function applyStyles(text, styles) {
939
- if (styles.length === 0 || text === "") {
1105
+ function applyStyles(text, styles, level) {
1106
+ if (styles.length === 0 || text === "" || level === 0) {
940
1107
  return text;
941
1108
  }
942
1109
  const open = styles.map((style) => style.open).join("");
@@ -1015,7 +1182,13 @@ function hslToRgb(h2, s, l) {
1015
1182
  // src/core/builder.ts
1016
1183
  var BuilderPigment = class {
1017
1184
  styles = [];
1018
- 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;
1019
1192
  }
1020
1193
  bold() {
1021
1194
  this.styles.push(generateModifierCode("bold"));
@@ -1194,26 +1367,26 @@ var BuilderPigment = class {
1194
1367
  return this;
1195
1368
  }
1196
1369
  ansi256(code) {
1197
- this.styles.push(generateAnsi256Code(code, false));
1370
+ this.styles.push(generateAnsi256CodeForLevel(code, false, this.level));
1198
1371
  return this;
1199
1372
  }
1200
1373
  bgAnsi256(code) {
1201
- this.styles.push(generateAnsi256Code(code, true));
1374
+ this.styles.push(generateAnsi256CodeForLevel(code, true, this.level));
1202
1375
  return this;
1203
1376
  }
1204
1377
  rgb(r, g, b) {
1205
- this.styles.push(generateRgbCode(r, g, b, false));
1378
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
1206
1379
  return this;
1207
1380
  }
1208
1381
  bgRgb(r, g, b) {
1209
- this.styles.push(generateRgbCode(r, g, b, true));
1382
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
1210
1383
  return this;
1211
1384
  }
1212
1385
  hex(color) {
1213
1386
  const rgb = hexToRgb(color);
1214
1387
  if (rgb) {
1215
1388
  const { r, g, b } = rgb;
1216
- this.styles.push(generateRgbCode(r, g, b, false));
1389
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
1217
1390
  }
1218
1391
  return this;
1219
1392
  }
@@ -1221,22 +1394,22 @@ var BuilderPigment = class {
1221
1394
  const rgb = hexToRgb(color);
1222
1395
  if (rgb) {
1223
1396
  const { r, g, b } = rgb;
1224
- this.styles.push(generateRgbCode(r, g, b, true));
1397
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
1225
1398
  }
1226
1399
  return this;
1227
1400
  }
1228
1401
  hsl(h2, s, l) {
1229
1402
  const { r, g, b } = hslToRgb(h2, s, l);
1230
- this.styles.push(generateRgbCode(r, g, b, false));
1403
+ this.styles.push(generateRgbCodeForLevel(r, g, b, false, this.level));
1231
1404
  return this;
1232
1405
  }
1233
1406
  bgHsl(h2, s, l) {
1234
1407
  const { r, g, b } = hslToRgb(h2, s, l);
1235
- this.styles.push(generateRgbCode(r, g, b, true));
1408
+ this.styles.push(generateRgbCodeForLevel(r, g, b, true, this.level));
1236
1409
  return this;
1237
1410
  }
1238
1411
  paint(text) {
1239
- if (this.styles.length === 0 || text === "") {
1412
+ if (this.styles.length === 0 || text === "" || this.level === 0) {
1240
1413
  return text;
1241
1414
  }
1242
1415
  const open = this.styles.map((style) => style.open).join("");
@@ -1361,23 +1534,6 @@ function toColorSupport(level) {
1361
1534
  return { level, hasBasic: level >= 1, has256: level >= 2, has16m: level >= 3 };
1362
1535
  }
1363
1536
 
1364
- // src/utils/rgb-to-ansi256.ts
1365
- function rgbToAnsi256(r, g, b) {
1366
- const clampedR = Math.max(0, Math.min(255, r));
1367
- const clampedG = Math.max(0, Math.min(255, g));
1368
- const clampedB = Math.max(0, Math.min(255, b));
1369
- if (clampedR === clampedG && clampedG === clampedB) {
1370
- if (clampedR < 8) {
1371
- return 16;
1372
- }
1373
- if (clampedR > 248) {
1374
- return 231;
1375
- }
1376
- return Math.round((clampedR - 8) / 247 * 24) + 232;
1377
- }
1378
- return Math.round(clampedR / 51) * 36 + Math.round(clampedG / 51) * 6 + Math.round(clampedB / 51) + 16;
1379
- }
1380
-
1381
1537
  // src/plugins/core/base-colors.ts
1382
1538
  function baseColorsPlugin() {
1383
1539
  return {
@@ -1593,13 +1749,12 @@ function createPigment(options) {
1593
1749
  kernel.use(environmentPlugin());
1594
1750
  if (options?.plugins) {
1595
1751
  for (const plugin of options.plugins) {
1596
- kernel.use(plugin);
1752
+ installPlugin(kernel, plugin);
1597
1753
  }
1598
1754
  }
1599
- void kernel.init();
1600
1755
  const pigment2 = createProxyPigment(kernel);
1601
- const builder = new BuilderPigment(kernel);
1602
- Object.defineProperty(pigment2, "builder", { value: builder, enumerable: false });
1756
+ registerRoot(kernel, pigment2);
1757
+ Object.defineProperty(pigment2, "builder", { get: () => new BuilderPigment(kernel), enumerable: false });
1603
1758
  Object.defineProperty(pigment2, "level", { value: level, enumerable: true });
1604
1759
  Object.defineProperty(pigment2, "supportsColor", { value: colorSupport, enumerable: true });
1605
1760
  return pigment2;
@@ -1607,170 +1762,55 @@ function createPigment(options) {
1607
1762
 
1608
1763
  // src/core/chalk.ts
1609
1764
  var Chalk = class {
1610
- level;
1611
1765
  supportsColor;
1766
+ currentLevel;
1612
1767
  pigment;
1613
1768
  constructor(options) {
1614
1769
  const colorSupport = detectColorSupport(options?.level);
1615
- this.level = options?.level ?? colorSupport.level;
1770
+ this.currentLevel = options?.level ?? colorSupport.level;
1616
1771
  this.supportsColor = colorSupport;
1617
- this.pigment = createPigment({ level: this.level });
1618
- }
1619
- red(text) {
1620
- return this.pigment.red(text);
1621
- }
1622
- green(text) {
1623
- return this.pigment.green(text);
1624
- }
1625
- yellow(text) {
1626
- return this.pigment.yellow(text);
1627
- }
1628
- blue(text) {
1629
- return this.pigment.blue(text);
1630
- }
1631
- magenta(text) {
1632
- return this.pigment.magenta(text);
1633
- }
1634
- cyan(text) {
1635
- return this.pigment.cyan(text);
1636
- }
1637
- white(text) {
1638
- return this.pigment.white(text);
1639
- }
1640
- black(text) {
1641
- return this.pigment.black(text);
1642
- }
1643
- redBright(text) {
1644
- return this.pigment.redBright(text);
1645
- }
1646
- greenBright(text) {
1647
- return this.pigment.greenBright(text);
1648
- }
1649
- yellowBright(text) {
1650
- return this.pigment.yellowBright(text);
1651
- }
1652
- blueBright(text) {
1653
- return this.pigment.blueBright(text);
1654
- }
1655
- magentaBright(text) {
1656
- return this.pigment.magentaBright(text);
1657
- }
1658
- cyanBright(text) {
1659
- return this.pigment.cyanBright(text);
1660
- }
1661
- whiteBright(text) {
1662
- return this.pigment.whiteBright(text);
1663
- }
1664
- gray(text) {
1665
- return this.pigment.gray(text);
1666
- }
1667
- grey(text) {
1668
- return this.pigment.grey(text);
1669
- }
1670
- bgRed(text) {
1671
- return this.pigment.bgRed(text);
1672
- }
1673
- bgGreen(text) {
1674
- return this.pigment.bgGreen(text);
1675
- }
1676
- bgYellow(text) {
1677
- return this.pigment.bgYellow(text);
1678
- }
1679
- bgBlue(text) {
1680
- return this.pigment.bgBlue(text);
1681
- }
1682
- bgMagenta(text) {
1683
- return this.pigment.bgMagenta(text);
1684
- }
1685
- bgCyan(text) {
1686
- return this.pigment.bgCyan(text);
1687
- }
1688
- bgWhite(text) {
1689
- return this.pigment.bgWhite(text);
1690
- }
1691
- bgBlack(text) {
1692
- return this.pigment.bgBlack(text);
1693
- }
1694
- bgRedBright(text) {
1695
- return this.pigment.bgRedBright(text);
1696
- }
1697
- bgGreenBright(text) {
1698
- return this.pigment.bgGreenBright(text);
1699
- }
1700
- bgYellowBright(text) {
1701
- return this.pigment.bgYellowBright(text);
1702
- }
1703
- bgBlueBright(text) {
1704
- return this.pigment.bgBlueBright(text);
1705
- }
1706
- bgMagentaBright(text) {
1707
- return this.pigment.bgMagentaBright(text);
1708
- }
1709
- bgCyanBright(text) {
1710
- return this.pigment.bgCyanBright(text);
1711
- }
1712
- bgWhiteBright(text) {
1713
- return this.pigment.bgWhiteBright(text);
1714
- }
1715
- bgGray(text) {
1716
- return this.pigment.bgGray(text);
1717
- }
1718
- bgGrey(text) {
1719
- return this.pigment.bgGrey(text);
1772
+ this.pigment = createPigment({ level: this.currentLevel });
1720
1773
  }
1721
- bold(text) {
1722
- return this.pigment.bold(text);
1774
+ /** Color level (0-3). Setting it changes the output of every style. */
1775
+ get level() {
1776
+ return this.currentLevel;
1723
1777
  }
1724
- dim(text) {
1725
- return this.pigment.dim(text);
1778
+ set level(level) {
1779
+ this.currentLevel = level;
1780
+ this.pigment = createPigment({ level });
1726
1781
  }
1727
- italic(text) {
1728
- return this.pigment.italic(text);
1729
- }
1730
- underline(text) {
1731
- return this.pigment.underline(text);
1732
- }
1733
- strikethrough(text) {
1734
- return this.pigment.strikethrough(text);
1735
- }
1736
- inverse(text) {
1737
- return this.pigment.inverse(text);
1738
- }
1739
- hidden(text) {
1740
- return this.pigment.hidden(text);
1741
- }
1742
- reset(text) {
1743
- return this.pigment.reset(text);
1744
- }
1745
- /** Like chalk: returns the text only when colors are supported, otherwise ''. */
1782
+ /** Like chalk: returns the text only when colors are enabled, otherwise ''. */
1746
1783
  visible(text) {
1747
- return this.supportsColor.level > 0 ? text : "";
1784
+ return this.currentLevel > 0 ? text : "";
1748
1785
  }
1749
1786
  rgb(r, g, b) {
1750
- const coloredPigment = this.pigment.rgb(r, g, b);
1751
- return (text) => coloredPigment(text);
1787
+ return this.pigment.rgb(r, g, b);
1752
1788
  }
1753
1789
  bgRgb(r, g, b) {
1754
- const coloredPigment = this.pigment.bgRgb(r, g, b);
1755
- return (text) => coloredPigment(text);
1790
+ return this.pigment.bgRgb(r, g, b);
1756
1791
  }
1757
1792
  hex(color) {
1758
- const coloredPigment = this.pigment.hex(color);
1759
- return (text) => coloredPigment(text);
1793
+ return this.pigment.hex(color);
1760
1794
  }
1761
1795
  bgHex(color) {
1762
- const coloredPigment = this.pigment.bgHex(color);
1763
- return (text) => coloredPigment(text);
1796
+ return this.pigment.bgHex(color);
1764
1797
  }
1765
1798
  ansi256(code) {
1766
- const coloredPigment = this.pigment.ansi256(code);
1767
- return (text) => coloredPigment(text);
1799
+ return this.pigment.ansi256(code);
1768
1800
  }
1769
1801
  bgAnsi256(code) {
1770
- const coloredPigment = this.pigment.bgAnsi256(code);
1771
- return (text) => coloredPigment(text);
1802
+ return this.pigment.bgAnsi256(code);
1772
1803
  }
1773
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
+ }
1774
1814
 
1775
1815
  // src/core/chalk-default.ts
1776
1816
  var chalk = new Chalk();
@@ -1786,48 +1826,51 @@ function applyStyle(text, open, close) {
1786
1826
  }
1787
1827
 
1788
1828
  // src/core/composer.ts
1789
- var bold = (text) => applyStyle(text, "\x1B[1m", "\x1B[22m");
1790
- var dim = (text) => applyStyle(text, "\x1B[2m", "\x1B[22m");
1791
- var italic = (text) => applyStyle(text, "\x1B[3m", "\x1B[23m");
1792
- var underline = (text) => applyStyle(text, "\x1B[4m", "\x1B[24m");
1793
- var strikethrough = (text) => applyStyle(text, "\x1B[9m", "\x1B[29m");
1794
- var inverse = (text) => applyStyle(text, "\x1B[7m", "\x1B[27m");
1795
- var hidden = (text) => applyStyle(text, "\x1B[8m", "\x1B[28m");
1796
- var reset = (text) => applyStyle(text, "\x1B[0m", "\x1B[0m");
1797
- var black = (text) => applyStyle(text, "\x1B[30m", "\x1B[39m");
1798
- var red = (text) => applyStyle(text, "\x1B[31m", "\x1B[39m");
1799
- var green = (text) => applyStyle(text, "\x1B[32m", "\x1B[39m");
1800
- var yellow = (text) => applyStyle(text, "\x1B[33m", "\x1B[39m");
1801
- var blue = (text) => applyStyle(text, "\x1B[34m", "\x1B[39m");
1802
- var magenta = (text) => applyStyle(text, "\x1B[35m", "\x1B[39m");
1803
- var cyan = (text) => applyStyle(text, "\x1B[36m", "\x1B[39m");
1804
- var white = (text) => applyStyle(text, "\x1B[37m", "\x1B[39m");
1805
- var blackBright = (text) => applyStyle(text, "\x1B[90m", "\x1B[39m");
1806
- var redBright = (text) => applyStyle(text, "\x1B[91m", "\x1B[39m");
1807
- var greenBright = (text) => applyStyle(text, "\x1B[92m", "\x1B[39m");
1808
- var yellowBright = (text) => applyStyle(text, "\x1B[93m", "\x1B[39m");
1809
- var blueBright = (text) => applyStyle(text, "\x1B[94m", "\x1B[39m");
1810
- var magentaBright = (text) => applyStyle(text, "\x1B[95m", "\x1B[39m");
1811
- var cyanBright = (text) => applyStyle(text, "\x1B[96m", "\x1B[39m");
1812
- 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");
1813
1856
  var gray = blackBright;
1814
1857
  var grey = blackBright;
1815
- var bgBlack = (text) => applyStyle(text, "\x1B[40m", "\x1B[49m");
1816
- var bgRed = (text) => applyStyle(text, "\x1B[41m", "\x1B[49m");
1817
- var bgGreen = (text) => applyStyle(text, "\x1B[42m", "\x1B[49m");
1818
- var bgYellow = (text) => applyStyle(text, "\x1B[43m", "\x1B[49m");
1819
- var bgBlue = (text) => applyStyle(text, "\x1B[44m", "\x1B[49m");
1820
- var bgMagenta = (text) => applyStyle(text, "\x1B[45m", "\x1B[49m");
1821
- var bgCyan = (text) => applyStyle(text, "\x1B[46m", "\x1B[49m");
1822
- var bgWhite = (text) => applyStyle(text, "\x1B[47m", "\x1B[49m");
1823
- var bgBlackBright = (text) => applyStyle(text, "\x1B[100m", "\x1B[49m");
1824
- var bgRedBright = (text) => applyStyle(text, "\x1B[101m", "\x1B[49m");
1825
- var bgGreenBright = (text) => applyStyle(text, "\x1B[102m", "\x1B[49m");
1826
- var bgYellowBright = (text) => applyStyle(text, "\x1B[103m", "\x1B[49m");
1827
- var bgBlueBright = (text) => applyStyle(text, "\x1B[104m", "\x1B[49m");
1828
- var bgMagentaBright = (text) => applyStyle(text, "\x1B[105m", "\x1B[49m");
1829
- var bgCyanBright = (text) => applyStyle(text, "\x1B[106m", "\x1B[49m");
1830
- 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");
1831
1874
  var bgGray = bgBlackBright;
1832
1875
  var bgGrey = bgBlackBright;
1833
1876
  function compose(...stylers) {
@@ -1854,6 +1897,33 @@ __export(plugins_exports, {
1854
1897
  });
1855
1898
 
1856
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
+ }
1857
1927
  function gradientPlugin(options) {
1858
1928
  const steps = options?.steps ?? 10;
1859
1929
  return {
@@ -1867,6 +1937,15 @@ function gradientPlugin(options) {
1867
1937
  enabled: true
1868
1938
  }
1869
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));
1870
1949
  k.on("style:gradient", (data) => {
1871
1950
  const { start, end, text, steps: gradientSteps = steps } = data;
1872
1951
  const startRgb = start.startsWith("#") ? hexToRgb(start) : start.startsWith("rgb(") ? parseRgb(start) : hexToRgb(start);
@@ -1937,6 +2016,7 @@ function generateRainbowColors(length, steps) {
1937
2016
  }
1938
2017
 
1939
2018
  // src/plugins/optional/theme.ts
2019
+ var THEME_COLOR_TYPES = ["keyword", "string", "number", "comment", "error", "success", "warning", "info"];
1940
2020
  function themePlugin(options) {
1941
2021
  const preset = options?.preset ?? "monokai";
1942
2022
  const custom = options?.custom ?? {};
@@ -1956,6 +2036,17 @@ function themePlugin(options) {
1956
2036
  enabled: true
1957
2037
  }
1958
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);
1959
2050
  k.on("style:theme", (data) => {
1960
2051
  const { type, text } = data;
1961
2052
  const color = theme[type];
@@ -2025,6 +2116,10 @@ function semanticPlugin(options) {
2025
2116
  if (!hexToRgb2) {
2026
2117
  throw new Error("hexToRgb utility not available");
2027
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
+ }
2028
2123
  k.on("style:success", (data) => {
2029
2124
  const { r, g, b } = hexToRgb2(defaultColors.success);
2030
2125
  return `\x1B[38;2;${r};${g};${b}m${data.text}\x1B[39m`;
@@ -2066,9 +2161,12 @@ function boxPlugin(options) {
2066
2161
  borders: BOX_BORDERS
2067
2162
  }
2068
2163
  });
2069
- k.on("style:box", (data) => {
2070
- 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;
2071
2166
  const chars = BOX_BORDERS[boxBorder];
2167
+ if (!chars) {
2168
+ throw new StyleError(`Unknown box border: ${String(boxBorder)}`);
2169
+ }
2072
2170
  const lines = text.split("\n");
2073
2171
  const width = Math.max(...lines.map((l) => l.length)) + boxPadding * 2;
2074
2172
  const pad = " ".repeat(boxPadding);
@@ -2080,21 +2178,51 @@ function boxPlugin(options) {
2080
2178
  const bottom = `${chars.bl}${chars.h.repeat(width)}${chars.br}`;
2081
2179
  const result = [top, ...middle, bottom].join("\n");
2082
2180
  if (bgColor) {
2083
- const ctx = k.getContext();
2084
- const hexToRgb2 = ctx.utils?.hexToRgb;
2085
- if (!hexToRgb2) {
2086
- throw new Error("hexToRgb utility not available");
2087
- }
2088
- const { r, g, b } = hexToRgb2(bgColor);
2089
- return `\x1B[48;2;${r};${g};${b}m${result}\x1B[49m`;
2181
+ return paintRgb(result, hexToRgb(bgColor), true, k.getContext().level);
2090
2182
  }
2091
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);
2092
2189
  });
2093
2190
  }
2094
2191
  };
2095
2192
  }
2096
2193
 
2097
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
+ }
2098
2226
  function templatePlugin() {
2099
2227
  return {
2100
2228
  name: "template",
@@ -2106,6 +2234,11 @@ function templatePlugin() {
2106
2234
  enabled: true
2107
2235
  }
2108
2236
  });
2237
+ registerPluginApi(
2238
+ kernel,
2239
+ "template",
2240
+ (strings, ...values) => renderTemplate(getRoot(kernel) ?? createProxyPigment(k), strings, values)
2241
+ );
2109
2242
  k.on("style:template", (data) => {
2110
2243
  const { strings, values } = data;
2111
2244
  let result = "";