colorino 0.12.7 → 0.13.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.
@@ -60,6 +60,8 @@ var ColorLevel = /* @__PURE__ */ ((ColorLevel2) => {
60
60
  function isConsoleMethod(level) {
61
61
  return ["log", "info", "warn", "error", "trace", "debug"].includes(level);
62
62
  }
63
+ const ColorinoBrowserColorized = Symbol("colorino.browserColorized");
64
+ const ColorinoBrowserObject = Symbol("colorino.browserObject");
63
65
 
64
66
  const catppuccinMochaPalette = {
65
67
  log: "#cdd6f4",
@@ -144,6 +146,33 @@ function determineBaseTheme(themeOpt, detectedBrowserTheme) {
144
146
  return baseThemeName;
145
147
  }
146
148
 
149
+ class TypeValidator {
150
+ static isNull(value) {
151
+ return value === null;
152
+ }
153
+ static isObject(value) {
154
+ return typeof value === "object" && value !== null;
155
+ }
156
+ static isString(value) {
157
+ return typeof value === "string";
158
+ }
159
+ static isError(value) {
160
+ return value instanceof Error;
161
+ }
162
+ static isBrowserColorizedArg(value) {
163
+ return typeof value === "object" && value !== null && ColorinoBrowserColorized in value;
164
+ }
165
+ static isBrowserObjectArg(value) {
166
+ return typeof value === "object" && value !== null && ColorinoBrowserObject in value;
167
+ }
168
+ static isAnsiColoredString(value) {
169
+ return TypeValidator.isString(value) && /\x1b\[[0-9;]*m/.test(value);
170
+ }
171
+ static isFormattableObject(value) {
172
+ return TypeValidator.isObject(value) && !TypeValidator.isError(value) && !TypeValidator.isBrowserColorizedArg(value);
173
+ }
174
+ }
175
+
147
176
  class MyColorino {
148
177
  constructor(initialPalette, _userPalette, _validator, _browserColorSupportDetector, _nodeColorSupportDetector, _options = {}) {
149
178
  this._userPalette = _userPalette;
@@ -162,7 +191,7 @@ class MyColorino {
162
191
  const themeOpt = this._options.theme ?? "auto";
163
192
  if (themeOpt === "auto" && this._nodeColorSupportDetector) {
164
193
  this._nodeColorSupportDetector.onTheme((resolvedTheme) => {
165
- this._appllyResolvedTheme(resolvedTheme);
194
+ this._applyResolvedTheme(resolvedTheme);
166
195
  });
167
196
  }
168
197
  }
@@ -170,12 +199,6 @@ class MyColorino {
170
199
  _colorLevel;
171
200
  isBrowser;
172
201
  _palette;
173
- _appllyResolvedTheme(resolvedTheme) {
174
- const themeOpt = this._options.theme ?? "auto";
175
- const baseThemeName = determineBaseTheme(themeOpt, resolvedTheme);
176
- const basePalette = themePalettes[baseThemeName];
177
- this._palette = { ...basePalette, ...this._userPalette };
178
- }
179
202
  log(...args) {
180
203
  this._out("log", args);
181
204
  }
@@ -194,6 +217,32 @@ class MyColorino {
194
217
  debug(...args) {
195
218
  this._out("debug", args);
196
219
  }
220
+ colorize(text, hex) {
221
+ if (this._colorLevel === ColorLevel.NO_COLOR || this._colorLevel === "UnknownEnv") {
222
+ return text;
223
+ }
224
+ if (this.isBrowser) {
225
+ return {
226
+ [ColorinoBrowserColorized]: true,
227
+ text,
228
+ hex
229
+ };
230
+ }
231
+ const ansiPrefix = this._toAnsiPrefix(hex);
232
+ if (!ansiPrefix) {
233
+ return text;
234
+ }
235
+ return `${ansiPrefix}${text}\x1B[0m`;
236
+ }
237
+ _isAnsiColoredString(value) {
238
+ return typeof value === "string" && /\x1b\[[0-9;]*m/.test(value);
239
+ }
240
+ _applyResolvedTheme(resolvedTheme) {
241
+ const themeOpt = this._options.theme ?? "auto";
242
+ const baseThemeName = determineBaseTheme(themeOpt, resolvedTheme);
243
+ const basePalette = themePalettes[baseThemeName];
244
+ this._palette = { ...basePalette, ...this._userPalette };
245
+ }
197
246
  _detectColorSupport() {
198
247
  if (this.isBrowser) {
199
248
  return this._browserColorSupportDetector?.getColorLevel() ?? "UnknownEnv";
@@ -213,7 +262,7 @@ class MyColorino {
213
262
  _formatValue(value, maxDepth = this._options.maxDepth ?? 5) {
214
263
  const seen = /* @__PURE__ */ new WeakSet();
215
264
  const sanitize = (val, currentDepth) => {
216
- if (val === null || typeof val !== "object") return val;
265
+ if (val == null || typeof val !== "object") return val;
217
266
  if (seen.has(val)) return "[Circular]";
218
267
  seen.add(val);
219
268
  if (currentDepth >= maxDepth) return "[Object]";
@@ -231,21 +280,36 @@ class MyColorino {
231
280
  };
232
281
  return JSON.stringify(sanitize(value, 0), null, 2);
233
282
  }
283
+ _normalizeString(value) {
284
+ if (value instanceof String) return value.valueOf();
285
+ return value;
286
+ }
234
287
  _processArgs(args) {
235
288
  const processedArgs = [];
236
289
  let previousWasObject = false;
237
- for (const arg of args) {
238
- const isFormattableObject = arg !== null && typeof arg === "object" && typeof arg !== "string" && !(arg instanceof Error);
239
- const isError = arg instanceof Error;
240
- if (isFormattableObject) {
241
- processedArgs.push(`
290
+ for (const rawArg of args) {
291
+ const arg = this._normalizeString(rawArg);
292
+ if (TypeValidator.isBrowserColorizedArg(arg)) {
293
+ processedArgs.push(arg);
294
+ previousWasObject = false;
295
+ continue;
296
+ }
297
+ if (TypeValidator.isFormattableObject(arg)) {
298
+ if (this.isBrowser) {
299
+ processedArgs.push({
300
+ [ColorinoBrowserObject]: true,
301
+ value: arg
302
+ });
303
+ } else {
304
+ processedArgs.push(`
242
305
  ${this._formatValue(arg)}`);
306
+ }
243
307
  previousWasObject = true;
244
- } else if (isError) {
308
+ } else if (TypeValidator.isError(arg)) {
245
309
  processedArgs.push("\n", this._cleanErrorStack(arg));
246
310
  previousWasObject = true;
247
311
  } else {
248
- if (typeof arg === "string" && previousWasObject) {
312
+ if (TypeValidator.isString(arg) && previousWasObject) {
249
313
  processedArgs.push(`
250
314
  ${arg}`);
251
315
  } else {
@@ -257,48 +321,78 @@ ${arg}`);
257
321
  return processedArgs;
258
322
  }
259
323
  _applyBrowserColors(consoleMethod, args) {
260
- const hex = this._palette[consoleMethod];
261
- if (typeof args[0] === "string") {
262
- return [`%c${args[0]}`, `color:${hex}`, ...args.slice(1)];
324
+ const formatParts = [];
325
+ const cssArgs = [];
326
+ const otherArgs = [];
327
+ const paletteHex = this._palette[consoleMethod];
328
+ for (const rawArg of args) {
329
+ const arg = this._normalizeString(rawArg);
330
+ if (TypeValidator.isBrowserColorizedArg(arg)) {
331
+ formatParts.push(`%c${arg.text}`);
332
+ cssArgs.push(`color:${arg.hex}`);
333
+ continue;
334
+ }
335
+ if (TypeValidator.isBrowserObjectArg(arg)) {
336
+ formatParts.push("%o");
337
+ otherArgs.push(arg.value);
338
+ continue;
339
+ }
340
+ if (TypeValidator.isString(arg)) {
341
+ formatParts.push(`%c${arg}`);
342
+ cssArgs.push(`color:${paletteHex}`);
343
+ continue;
344
+ }
345
+ formatParts.push("%o");
346
+ otherArgs.push(arg);
263
347
  }
264
- return args;
348
+ if (formatParts.length === 0) {
349
+ return args;
350
+ }
351
+ return [formatParts.join(""), ...cssArgs, ...otherArgs];
265
352
  }
266
- _applyNodeColors(consoleMethod, args) {
267
- const hex = this._palette[consoleMethod];
268
- let ansiCode;
353
+ _toAnsiPrefix(hex) {
354
+ if (this._colorLevel === ColorLevel.NO_COLOR || this._colorLevel === "UnknownEnv") {
355
+ return "";
356
+ }
269
357
  switch (this._colorLevel) {
270
358
  case ColorLevel.TRUECOLOR: {
271
359
  const [r, g, b] = colorConverter.hex.toRgb(hex);
272
- ansiCode = `\x1B[38;2;${r};${g};${b}m`;
273
- break;
360
+ return `\x1B[38;2;${r};${g};${b}m`;
274
361
  }
275
362
  case ColorLevel.ANSI256: {
276
363
  const code = colorConverter.hex.toAnsi256(hex);
277
- ansiCode = `\x1B[38;5;${code}m`;
278
- break;
364
+ return `\x1B[38;5;${code}m`;
279
365
  }
280
366
  case ColorLevel.ANSI:
281
367
  default: {
282
368
  const code = colorConverter.hex.toAnsi16(hex);
283
- ansiCode = `\x1B[${code}m`;
284
- break;
369
+ return `\x1B[${code}m`;
285
370
  }
286
371
  }
372
+ }
373
+ _applyNodeColors(consoleMethod, args) {
287
374
  const coloredArgs = [...args];
288
375
  const firstStringIndex = coloredArgs.findIndex(
289
376
  (arg) => typeof arg === "string"
290
377
  );
291
- if (firstStringIndex !== -1) {
292
- coloredArgs[firstStringIndex] = `${ansiCode}${coloredArgs[firstStringIndex]}\x1B[0m`;
378
+ if (firstStringIndex === -1) {
379
+ return coloredArgs;
380
+ }
381
+ const first = coloredArgs[firstStringIndex];
382
+ if (this._isAnsiColoredString(first)) {
383
+ return coloredArgs;
384
+ }
385
+ const hex = this._palette[consoleMethod];
386
+ const ansiPrefix = this._toAnsiPrefix(hex);
387
+ if (!ansiPrefix) {
388
+ return coloredArgs;
293
389
  }
390
+ coloredArgs[firstStringIndex] = `${ansiPrefix}${String(first)}\x1B[0m`;
294
391
  return coloredArgs;
295
392
  }
296
393
  _output(consoleMethod, args) {
297
- if (consoleMethod === "trace") {
298
- this._printCleanTrace(args);
299
- } else {
300
- console[consoleMethod](...args);
301
- }
394
+ if (consoleMethod === "trace") this._printCleanTrace(args);
395
+ else console[consoleMethod](...args);
302
396
  }
303
397
  _out(level, args) {
304
398
  const consoleMethod = isConsoleMethod(level) ? level : "log";
@@ -820,11 +914,11 @@ class Err {
820
914
  }
821
915
  Result.fromThrowable;
822
916
 
823
- class ColorinoError extends Error {
917
+ class InputValidationError extends Error {
824
918
  constructor(message) {
825
919
  super(message);
826
- this.name = "ColorinoError";
827
- Object.setPrototypeOf(this, ColorinoError.prototype);
920
+ this.name = "InputValidationError";
921
+ Object.setPrototypeOf(this, InputValidationError.prototype);
828
922
  }
829
923
  }
830
924
 
@@ -833,7 +927,7 @@ class InputValidator {
833
927
  const trimmedHex = hex.trim();
834
928
  const isHexValid = /^#[0-9A-F]{6}$/i.test(trimmedHex);
835
929
  if (!isHexValid) {
836
- return err(new ColorinoError(`Invalid hex color: '${hex}'`));
930
+ return err(new InputValidationError(`Invalid hex color: '${hex}'`));
837
931
  }
838
932
  return ok(true);
839
933
  }
package/dist/browser.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const inputValidator = require('./shared/colorino.BgxwS1Lc.cjs');
3
+ const inputValidator = require('./shared/colorino.Dcy2ipG7.cjs');
4
4
 
5
5
  class BrowserColorSupportDetector {
6
6
  constructor(_window, _navigator, _overrideTheme) {
@@ -1,5 +1,5 @@
1
- import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.DWWtObdr.cjs';
2
- export { L as LogLevel, T as ThemeName, t as themePalettes } from './shared/colorino.DWWtObdr.cjs';
1
+ import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.FdIbpxRG.cjs';
2
+ export { L as LogLevel, T as ThemeName, t as themePalettes } from './shared/colorino.FdIbpxRG.cjs';
3
3
 
4
4
  declare function createColorino(userPalette?: Partial<Palette>, options?: ColorinoOptions): Colorino;
5
5
 
@@ -1,5 +1,5 @@
1
- import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.DWWtObdr.mjs';
2
- export { L as LogLevel, T as ThemeName, t as themePalettes } from './shared/colorino.DWWtObdr.mjs';
1
+ import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.FdIbpxRG.mjs';
2
+ export { L as LogLevel, T as ThemeName, t as themePalettes } from './shared/colorino.FdIbpxRG.mjs';
3
3
 
4
4
  declare function createColorino(userPalette?: Partial<Palette>, options?: ColorinoOptions): Colorino;
5
5
 
package/dist/browser.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.DWWtObdr.js';
2
- export { L as LogLevel, T as ThemeName, t as themePalettes } from './shared/colorino.DWWtObdr.js';
1
+ import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.FdIbpxRG.js';
2
+ export { L as LogLevel, T as ThemeName, t as themePalettes } from './shared/colorino.FdIbpxRG.js';
3
3
 
4
4
  declare function createColorino(userPalette?: Partial<Palette>, options?: ColorinoOptions): Colorino;
5
5
 
package/dist/browser.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { C as ColorLevel, t as themePalettes, M as MyColorino, I as InputValidator, d as determineBaseTheme } from './shared/colorino.ClDpLv-k.mjs';
1
+ import { C as ColorLevel, t as themePalettes, M as MyColorino, I as InputValidator, d as determineBaseTheme } from './shared/colorino.DEvR4n1Y.mjs';
2
2
 
3
3
  class BrowserColorSupportDetector {
4
4
  constructor(_window, _navigator, _overrideTheme) {
package/dist/node.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const inputValidator = require('./shared/colorino.BgxwS1Lc.cjs');
3
+ const inputValidator = require('./shared/colorino.Dcy2ipG7.cjs');
4
4
  const node_child_process = require('node:child_process');
5
5
  const node_url = require('node:url');
6
6
  const node_path = require('node:path');
package/dist/node.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.DWWtObdr.cjs';
2
- export { L as LogLevel, T as ThemeName, t as themePalettes } from './shared/colorino.DWWtObdr.cjs';
1
+ import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.FdIbpxRG.cjs';
2
+ export { L as LogLevel, T as ThemeName, t as themePalettes } from './shared/colorino.FdIbpxRG.cjs';
3
3
 
4
4
  declare function createColorino(userPalette?: Partial<Palette>, options?: ColorinoOptions): Colorino;
5
5
 
package/dist/node.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.DWWtObdr.mjs';
2
- export { L as LogLevel, T as ThemeName, t as themePalettes } from './shared/colorino.DWWtObdr.mjs';
1
+ import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.FdIbpxRG.mjs';
2
+ export { L as LogLevel, T as ThemeName, t as themePalettes } from './shared/colorino.FdIbpxRG.mjs';
3
3
 
4
4
  declare function createColorino(userPalette?: Partial<Palette>, options?: ColorinoOptions): Colorino;
5
5
 
package/dist/node.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.DWWtObdr.js';
2
- export { L as LogLevel, T as ThemeName, t as themePalettes } from './shared/colorino.DWWtObdr.js';
1
+ import { P as Palette, C as ColorinoOptions, a as Colorino } from './shared/colorino.FdIbpxRG.js';
2
+ export { L as LogLevel, T as ThemeName, t as themePalettes } from './shared/colorino.FdIbpxRG.js';
3
3
 
4
4
  declare function createColorino(userPalette?: Partial<Palette>, options?: ColorinoOptions): Colorino;
5
5
 
package/dist/node.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { C as ColorLevel, d as determineBaseTheme, t as themePalettes, M as MyColorino, I as InputValidator } from './shared/colorino.ClDpLv-k.mjs';
1
+ import { C as ColorLevel, d as determineBaseTheme, t as themePalettes, M as MyColorino, I as InputValidator } from './shared/colorino.DEvR4n1Y.mjs';
2
2
  import { spawnSync } from 'node:child_process';
3
3
  import { fileURLToPath } from 'node:url';
4
4
  import { dirname, join } from 'node:path';
@@ -60,6 +60,8 @@ var ColorLevel = /* @__PURE__ */ ((ColorLevel2) => {
60
60
  function isConsoleMethod(level) {
61
61
  return ["log", "info", "warn", "error", "trace", "debug"].includes(level);
62
62
  }
63
+ const ColorinoBrowserColorized = Symbol("colorino.browserColorized");
64
+ const ColorinoBrowserObject = Symbol("colorino.browserObject");
63
65
 
64
66
  const catppuccinMochaPalette = {
65
67
  log: "#cdd6f4",
@@ -144,6 +146,33 @@ function determineBaseTheme(themeOpt, detectedBrowserTheme) {
144
146
  return baseThemeName;
145
147
  }
146
148
 
149
+ class TypeValidator {
150
+ static isNull(value) {
151
+ return value === null;
152
+ }
153
+ static isObject(value) {
154
+ return typeof value === "object" && value !== null;
155
+ }
156
+ static isString(value) {
157
+ return typeof value === "string";
158
+ }
159
+ static isError(value) {
160
+ return value instanceof Error;
161
+ }
162
+ static isBrowserColorizedArg(value) {
163
+ return typeof value === "object" && value !== null && ColorinoBrowserColorized in value;
164
+ }
165
+ static isBrowserObjectArg(value) {
166
+ return typeof value === "object" && value !== null && ColorinoBrowserObject in value;
167
+ }
168
+ static isAnsiColoredString(value) {
169
+ return TypeValidator.isString(value) && /\x1b\[[0-9;]*m/.test(value);
170
+ }
171
+ static isFormattableObject(value) {
172
+ return TypeValidator.isObject(value) && !TypeValidator.isError(value) && !TypeValidator.isBrowserColorizedArg(value);
173
+ }
174
+ }
175
+
147
176
  class MyColorino {
148
177
  constructor(initialPalette, _userPalette, _validator, _browserColorSupportDetector, _nodeColorSupportDetector, _options = {}) {
149
178
  this._userPalette = _userPalette;
@@ -162,7 +191,7 @@ class MyColorino {
162
191
  const themeOpt = this._options.theme ?? "auto";
163
192
  if (themeOpt === "auto" && this._nodeColorSupportDetector) {
164
193
  this._nodeColorSupportDetector.onTheme((resolvedTheme) => {
165
- this._appllyResolvedTheme(resolvedTheme);
194
+ this._applyResolvedTheme(resolvedTheme);
166
195
  });
167
196
  }
168
197
  }
@@ -170,12 +199,6 @@ class MyColorino {
170
199
  _colorLevel;
171
200
  isBrowser;
172
201
  _palette;
173
- _appllyResolvedTheme(resolvedTheme) {
174
- const themeOpt = this._options.theme ?? "auto";
175
- const baseThemeName = determineBaseTheme(themeOpt, resolvedTheme);
176
- const basePalette = themePalettes[baseThemeName];
177
- this._palette = { ...basePalette, ...this._userPalette };
178
- }
179
202
  log(...args) {
180
203
  this._out("log", args);
181
204
  }
@@ -194,6 +217,32 @@ class MyColorino {
194
217
  debug(...args) {
195
218
  this._out("debug", args);
196
219
  }
220
+ colorize(text, hex) {
221
+ if (this._colorLevel === ColorLevel.NO_COLOR || this._colorLevel === "UnknownEnv") {
222
+ return text;
223
+ }
224
+ if (this.isBrowser) {
225
+ return {
226
+ [ColorinoBrowserColorized]: true,
227
+ text,
228
+ hex
229
+ };
230
+ }
231
+ const ansiPrefix = this._toAnsiPrefix(hex);
232
+ if (!ansiPrefix) {
233
+ return text;
234
+ }
235
+ return `${ansiPrefix}${text}\x1B[0m`;
236
+ }
237
+ _isAnsiColoredString(value) {
238
+ return typeof value === "string" && /\x1b\[[0-9;]*m/.test(value);
239
+ }
240
+ _applyResolvedTheme(resolvedTheme) {
241
+ const themeOpt = this._options.theme ?? "auto";
242
+ const baseThemeName = determineBaseTheme(themeOpt, resolvedTheme);
243
+ const basePalette = themePalettes[baseThemeName];
244
+ this._palette = { ...basePalette, ...this._userPalette };
245
+ }
197
246
  _detectColorSupport() {
198
247
  if (this.isBrowser) {
199
248
  return this._browserColorSupportDetector?.getColorLevel() ?? "UnknownEnv";
@@ -213,7 +262,7 @@ class MyColorino {
213
262
  _formatValue(value, maxDepth = this._options.maxDepth ?? 5) {
214
263
  const seen = /* @__PURE__ */ new WeakSet();
215
264
  const sanitize = (val, currentDepth) => {
216
- if (val === null || typeof val !== "object") return val;
265
+ if (val == null || typeof val !== "object") return val;
217
266
  if (seen.has(val)) return "[Circular]";
218
267
  seen.add(val);
219
268
  if (currentDepth >= maxDepth) return "[Object]";
@@ -231,21 +280,36 @@ class MyColorino {
231
280
  };
232
281
  return JSON.stringify(sanitize(value, 0), null, 2);
233
282
  }
283
+ _normalizeString(value) {
284
+ if (value instanceof String) return value.valueOf();
285
+ return value;
286
+ }
234
287
  _processArgs(args) {
235
288
  const processedArgs = [];
236
289
  let previousWasObject = false;
237
- for (const arg of args) {
238
- const isFormattableObject = arg !== null && typeof arg === "object" && typeof arg !== "string" && !(arg instanceof Error);
239
- const isError = arg instanceof Error;
240
- if (isFormattableObject) {
241
- processedArgs.push(`
290
+ for (const rawArg of args) {
291
+ const arg = this._normalizeString(rawArg);
292
+ if (TypeValidator.isBrowserColorizedArg(arg)) {
293
+ processedArgs.push(arg);
294
+ previousWasObject = false;
295
+ continue;
296
+ }
297
+ if (TypeValidator.isFormattableObject(arg)) {
298
+ if (this.isBrowser) {
299
+ processedArgs.push({
300
+ [ColorinoBrowserObject]: true,
301
+ value: arg
302
+ });
303
+ } else {
304
+ processedArgs.push(`
242
305
  ${this._formatValue(arg)}`);
306
+ }
243
307
  previousWasObject = true;
244
- } else if (isError) {
308
+ } else if (TypeValidator.isError(arg)) {
245
309
  processedArgs.push("\n", this._cleanErrorStack(arg));
246
310
  previousWasObject = true;
247
311
  } else {
248
- if (typeof arg === "string" && previousWasObject) {
312
+ if (TypeValidator.isString(arg) && previousWasObject) {
249
313
  processedArgs.push(`
250
314
  ${arg}`);
251
315
  } else {
@@ -257,48 +321,78 @@ ${arg}`);
257
321
  return processedArgs;
258
322
  }
259
323
  _applyBrowserColors(consoleMethod, args) {
260
- const hex = this._palette[consoleMethod];
261
- if (typeof args[0] === "string") {
262
- return [`%c${args[0]}`, `color:${hex}`, ...args.slice(1)];
324
+ const formatParts = [];
325
+ const cssArgs = [];
326
+ const otherArgs = [];
327
+ const paletteHex = this._palette[consoleMethod];
328
+ for (const rawArg of args) {
329
+ const arg = this._normalizeString(rawArg);
330
+ if (TypeValidator.isBrowserColorizedArg(arg)) {
331
+ formatParts.push(`%c${arg.text}`);
332
+ cssArgs.push(`color:${arg.hex}`);
333
+ continue;
334
+ }
335
+ if (TypeValidator.isBrowserObjectArg(arg)) {
336
+ formatParts.push("%o");
337
+ otherArgs.push(arg.value);
338
+ continue;
339
+ }
340
+ if (TypeValidator.isString(arg)) {
341
+ formatParts.push(`%c${arg}`);
342
+ cssArgs.push(`color:${paletteHex}`);
343
+ continue;
344
+ }
345
+ formatParts.push("%o");
346
+ otherArgs.push(arg);
263
347
  }
264
- return args;
348
+ if (formatParts.length === 0) {
349
+ return args;
350
+ }
351
+ return [formatParts.join(""), ...cssArgs, ...otherArgs];
265
352
  }
266
- _applyNodeColors(consoleMethod, args) {
267
- const hex = this._palette[consoleMethod];
268
- let ansiCode;
353
+ _toAnsiPrefix(hex) {
354
+ if (this._colorLevel === ColorLevel.NO_COLOR || this._colorLevel === "UnknownEnv") {
355
+ return "";
356
+ }
269
357
  switch (this._colorLevel) {
270
358
  case ColorLevel.TRUECOLOR: {
271
359
  const [r, g, b] = colorConverter.hex.toRgb(hex);
272
- ansiCode = `\x1B[38;2;${r};${g};${b}m`;
273
- break;
360
+ return `\x1B[38;2;${r};${g};${b}m`;
274
361
  }
275
362
  case ColorLevel.ANSI256: {
276
363
  const code = colorConverter.hex.toAnsi256(hex);
277
- ansiCode = `\x1B[38;5;${code}m`;
278
- break;
364
+ return `\x1B[38;5;${code}m`;
279
365
  }
280
366
  case ColorLevel.ANSI:
281
367
  default: {
282
368
  const code = colorConverter.hex.toAnsi16(hex);
283
- ansiCode = `\x1B[${code}m`;
284
- break;
369
+ return `\x1B[${code}m`;
285
370
  }
286
371
  }
372
+ }
373
+ _applyNodeColors(consoleMethod, args) {
287
374
  const coloredArgs = [...args];
288
375
  const firstStringIndex = coloredArgs.findIndex(
289
376
  (arg) => typeof arg === "string"
290
377
  );
291
- if (firstStringIndex !== -1) {
292
- coloredArgs[firstStringIndex] = `${ansiCode}${coloredArgs[firstStringIndex]}\x1B[0m`;
378
+ if (firstStringIndex === -1) {
379
+ return coloredArgs;
380
+ }
381
+ const first = coloredArgs[firstStringIndex];
382
+ if (this._isAnsiColoredString(first)) {
383
+ return coloredArgs;
384
+ }
385
+ const hex = this._palette[consoleMethod];
386
+ const ansiPrefix = this._toAnsiPrefix(hex);
387
+ if (!ansiPrefix) {
388
+ return coloredArgs;
293
389
  }
390
+ coloredArgs[firstStringIndex] = `${ansiPrefix}${String(first)}\x1B[0m`;
294
391
  return coloredArgs;
295
392
  }
296
393
  _output(consoleMethod, args) {
297
- if (consoleMethod === "trace") {
298
- this._printCleanTrace(args);
299
- } else {
300
- console[consoleMethod](...args);
301
- }
394
+ if (consoleMethod === "trace") this._printCleanTrace(args);
395
+ else console[consoleMethod](...args);
302
396
  }
303
397
  _out(level, args) {
304
398
  const consoleMethod = isConsoleMethod(level) ? level : "log";
@@ -820,11 +914,11 @@ class Err {
820
914
  }
821
915
  Result.fromThrowable;
822
916
 
823
- class ColorinoError extends Error {
917
+ class InputValidationError extends Error {
824
918
  constructor(message) {
825
919
  super(message);
826
- this.name = "ColorinoError";
827
- Object.setPrototypeOf(this, ColorinoError.prototype);
920
+ this.name = "InputValidationError";
921
+ Object.setPrototypeOf(this, InputValidationError.prototype);
828
922
  }
829
923
  }
830
924
 
@@ -833,7 +927,7 @@ class InputValidator {
833
927
  const trimmedHex = hex.trim();
834
928
  const isHexValid = /^#[0-9A-F]{6}$/i.test(trimmedHex);
835
929
  if (!isHexValid) {
836
- return err(new ColorinoError(`Invalid hex color: '${hex}'`));
930
+ return err(new InputValidationError(`Invalid hex color: '${hex}'`));
837
931
  }
838
932
  return ok(true);
839
933
  }