@oxog/pigment 1.0.1 → 1.0.2

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/README.md CHANGED
@@ -167,7 +167,7 @@ pigment.bgHsl(210, 100, 50)('Blue background');
167
167
  ## Environment Detection
168
168
 
169
169
  ```typescript
170
- import { pigment, supportsColor } from '@oxog/pigment';
170
+ import { pigment, supportsColor, resetColorSupportCache } from '@oxog/pigment';
171
171
 
172
172
  // Check color support
173
173
  console.log(supportsColor);
@@ -175,6 +175,9 @@ console.log(supportsColor);
175
175
 
176
176
  // Override level
177
177
  const pigment = createPigment({ level: 3 });
178
+
179
+ // Reset cached detection (useful for testing)
180
+ resetColorSupportCache();
178
181
  ```
179
182
 
180
183
  ### Environment Variables
@@ -264,12 +264,18 @@ declare function ansi256Plugin(): Plugin<PigmentContext>;
264
264
  declare function trueColorPlugin(): Plugin<PigmentContext>;
265
265
 
266
266
  /**
267
- * Core plugin that provides nested/composed style support.
267
+ * Core plugin that enables nested/composed style support.
268
+ *
269
+ * Nesting is handled directly in applyStyles() by replacing inner reset codes
270
+ * with outer style reopeners. This plugin just sets the enabled flag for
271
+ * compatibility and to indicate nesting support is active.
268
272
  *
269
273
  * @example
270
274
  * ```typescript
271
275
  * const pigment = createPigment();
272
- * pigment.red(`Error: ${pigment.bold('critical')} issue`);
276
+ * // Nested styles work automatically:
277
+ * pigment.red(`Error: ${pigment.blue('details')} here`);
278
+ * // Output: "Error: " (red), "details" (blue), " here" (red again)
273
279
  * ```
274
280
  */
275
281
  declare function nestingPlugin(): Plugin<PigmentContext>;
@@ -264,12 +264,18 @@ declare function ansi256Plugin(): Plugin<PigmentContext>;
264
264
  declare function trueColorPlugin(): Plugin<PigmentContext>;
265
265
 
266
266
  /**
267
- * Core plugin that provides nested/composed style support.
267
+ * Core plugin that enables nested/composed style support.
268
+ *
269
+ * Nesting is handled directly in applyStyles() by replacing inner reset codes
270
+ * with outer style reopeners. This plugin just sets the enabled flag for
271
+ * compatibility and to indicate nesting support is active.
268
272
  *
269
273
  * @example
270
274
  * ```typescript
271
275
  * const pigment = createPigment();
272
- * pigment.red(`Error: ${pigment.bold('critical')} issue`);
276
+ * // Nested styles work automatically:
277
+ * pigment.red(`Error: ${pigment.blue('details')} here`);
278
+ * // Output: "Error: " (red), "details" (blue), " here" (red again)
273
279
  * ```
274
280
  */
275
281
  declare function nestingPlugin(): Plugin<PigmentContext>;
package/dist/index.cjs CHANGED
@@ -78,6 +78,7 @@ __export(index_exports, {
78
78
  red: () => red,
79
79
  redBright: () => redBright,
80
80
  reset: () => reset,
81
+ resetColorSupportCache: () => resetColorSupportCache,
81
82
  rgbToAnsi256: () => rgbToAnsi256,
82
83
  strikethrough: () => strikethrough,
83
84
  supportsColor: () => supportsColor,
@@ -465,7 +466,7 @@ function createProxyPigment(kernel, styles = []) {
465
466
  }
466
467
  if (prop === "visible") {
467
468
  const colorSupport = ctx.colorSupport;
468
- return (text) => colorSupport.level > 0 ? text : text;
469
+ return (text) => colorSupport.level > 0 ? text : "";
469
470
  }
470
471
  return target[prop];
471
472
  },
@@ -479,12 +480,18 @@ function createProxyPigment(kernel, styles = []) {
479
480
  return pigment2;
480
481
  }
481
482
  function applyStyles(text, styles) {
482
- if (styles.length === 0) {
483
+ if (styles.length === 0 || text === "") {
483
484
  return text;
484
485
  }
485
486
  const open = styles.map((style) => style.open).join("");
486
487
  const close = [...styles].reverse().map((style) => style.close).join("");
487
- return `${open}${text}${close}`;
488
+ let result = text;
489
+ for (const style of styles) {
490
+ if (style.close) {
491
+ result = result.split(style.close).join(style.close + style.open);
492
+ }
493
+ }
494
+ return `${open}${result}${close}`;
488
495
  }
489
496
 
490
497
  // src/utils/hex-to-rgb.ts
@@ -773,12 +780,18 @@ var BuilderPigment = class {
773
780
  return this;
774
781
  }
775
782
  paint(text) {
776
- if (this.styles.length === 0) {
783
+ if (this.styles.length === 0 || text === "") {
777
784
  return text;
778
785
  }
779
786
  const open = this.styles.map((style) => style.open).join("");
780
787
  const close = [...this.styles].reverse().map((style) => style.close).join("");
781
- return `${open}${text}${close}`;
788
+ let result = text;
789
+ for (const style of this.styles) {
790
+ if (style.close) {
791
+ result = result.split(style.close).join(style.close + style.open);
792
+ }
793
+ }
794
+ return `${open}${result}${close}`;
782
795
  }
783
796
  };
784
797
 
@@ -830,10 +843,10 @@ function detectColorSupport(overrideLevel) {
830
843
  const platform = detectPlatform();
831
844
  if (platform.isBrowser) {
832
845
  cachedColorSupport = {
833
- level: 3,
846
+ level: 1,
834
847
  hasBasic: true,
835
- has256: true,
836
- has16m: true
848
+ has256: false,
849
+ has16m: false
837
850
  };
838
851
  return cachedColorSupport;
839
852
  }
@@ -914,6 +927,9 @@ function detectColorSupport(overrideLevel) {
914
927
  function supportsColor() {
915
928
  return detectColorSupport();
916
929
  }
930
+ function resetColorSupportCache() {
931
+ cachedColorSupport = void 0;
932
+ }
917
933
 
918
934
  // src/utils/rgb-to-ansi256.ts
919
935
  function rgbToAnsi256(r, g, b) {
@@ -1064,12 +1080,6 @@ function nestingPlugin() {
1064
1080
  enabled: true
1065
1081
  }
1066
1082
  });
1067
- k.on("style:nest", (data) => {
1068
- const { innerText, outerStyles } = data;
1069
- const open = outerStyles.join("");
1070
- const close = outerStyles.map(() => "\x1B[0m").join("");
1071
- return `${open}${innerText}${close}`;
1072
- });
1073
1083
  }
1074
1084
  };
1075
1085
  }
@@ -1082,15 +1092,16 @@ function environmentPlugin() {
1082
1092
  install(kernel) {
1083
1093
  const k = kernel;
1084
1094
  const ci = isCI();
1085
- const colorSupport = detectColorSupport();
1095
+ const existingContext = k.getContext();
1096
+ const colorSupport = existingContext.colorSupport ?? detectColorSupport();
1086
1097
  k.updateContext({
1087
1098
  environment: {
1088
1099
  isCI: ci,
1089
1100
  noColor: process.env.NO_COLOR !== void 0,
1090
1101
  forceColor: process.env.FORCE_COLOR !== void 0,
1091
1102
  term: process.env.TERM ?? ""
1092
- },
1093
- colorSupport
1103
+ }
1104
+ // Don't overwrite colorSupport - it was already set in factory with the correct level
1094
1105
  });
1095
1106
  k.on("environment:check", () => {
1096
1107
  const ctx = k.getContext();
@@ -1871,6 +1882,7 @@ var index_default = chalk_default_default;
1871
1882
  red,
1872
1883
  redBright,
1873
1884
  reset,
1885
+ resetColorSupportCache,
1874
1886
  rgbToAnsi256,
1875
1887
  strikethrough,
1876
1888
  supportsColor,