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.
@@ -62,6 +62,8 @@ var ColorLevel = /* @__PURE__ */ ((ColorLevel2) => {
62
62
  function isConsoleMethod(level) {
63
63
  return ["log", "info", "warn", "error", "trace", "debug"].includes(level);
64
64
  }
65
+ const ColorinoBrowserColorized = Symbol("colorino.browserColorized");
66
+ const ColorinoBrowserObject = Symbol("colorino.browserObject");
65
67
 
66
68
  const catppuccinMochaPalette = {
67
69
  log: "#cdd6f4",
@@ -146,6 +148,33 @@ function determineBaseTheme(themeOpt, detectedBrowserTheme) {
146
148
  return baseThemeName;
147
149
  }
148
150
 
151
+ class TypeValidator {
152
+ static isNull(value) {
153
+ return value === null;
154
+ }
155
+ static isObject(value) {
156
+ return typeof value === "object" && value !== null;
157
+ }
158
+ static isString(value) {
159
+ return typeof value === "string";
160
+ }
161
+ static isError(value) {
162
+ return value instanceof Error;
163
+ }
164
+ static isBrowserColorizedArg(value) {
165
+ return typeof value === "object" && value !== null && ColorinoBrowserColorized in value;
166
+ }
167
+ static isBrowserObjectArg(value) {
168
+ return typeof value === "object" && value !== null && ColorinoBrowserObject in value;
169
+ }
170
+ static isAnsiColoredString(value) {
171
+ return TypeValidator.isString(value) && /\x1b\[[0-9;]*m/.test(value);
172
+ }
173
+ static isFormattableObject(value) {
174
+ return TypeValidator.isObject(value) && !TypeValidator.isError(value) && !TypeValidator.isBrowserColorizedArg(value);
175
+ }
176
+ }
177
+
149
178
  class MyColorino {
150
179
  constructor(initialPalette, _userPalette, _validator, _browserColorSupportDetector, _nodeColorSupportDetector, _options = {}) {
151
180
  this._userPalette = _userPalette;
@@ -164,7 +193,7 @@ class MyColorino {
164
193
  const themeOpt = this._options.theme ?? "auto";
165
194
  if (themeOpt === "auto" && this._nodeColorSupportDetector) {
166
195
  this._nodeColorSupportDetector.onTheme((resolvedTheme) => {
167
- this._appllyResolvedTheme(resolvedTheme);
196
+ this._applyResolvedTheme(resolvedTheme);
168
197
  });
169
198
  }
170
199
  }
@@ -172,12 +201,6 @@ class MyColorino {
172
201
  _colorLevel;
173
202
  isBrowser;
174
203
  _palette;
175
- _appllyResolvedTheme(resolvedTheme) {
176
- const themeOpt = this._options.theme ?? "auto";
177
- const baseThemeName = determineBaseTheme(themeOpt, resolvedTheme);
178
- const basePalette = themePalettes[baseThemeName];
179
- this._palette = { ...basePalette, ...this._userPalette };
180
- }
181
204
  log(...args) {
182
205
  this._out("log", args);
183
206
  }
@@ -196,6 +219,32 @@ class MyColorino {
196
219
  debug(...args) {
197
220
  this._out("debug", args);
198
221
  }
222
+ colorize(text, hex) {
223
+ if (this._colorLevel === ColorLevel.NO_COLOR || this._colorLevel === "UnknownEnv") {
224
+ return text;
225
+ }
226
+ if (this.isBrowser) {
227
+ return {
228
+ [ColorinoBrowserColorized]: true,
229
+ text,
230
+ hex
231
+ };
232
+ }
233
+ const ansiPrefix = this._toAnsiPrefix(hex);
234
+ if (!ansiPrefix) {
235
+ return text;
236
+ }
237
+ return `${ansiPrefix}${text}\x1B[0m`;
238
+ }
239
+ _isAnsiColoredString(value) {
240
+ return typeof value === "string" && /\x1b\[[0-9;]*m/.test(value);
241
+ }
242
+ _applyResolvedTheme(resolvedTheme) {
243
+ const themeOpt = this._options.theme ?? "auto";
244
+ const baseThemeName = determineBaseTheme(themeOpt, resolvedTheme);
245
+ const basePalette = themePalettes[baseThemeName];
246
+ this._palette = { ...basePalette, ...this._userPalette };
247
+ }
199
248
  _detectColorSupport() {
200
249
  if (this.isBrowser) {
201
250
  return this._browserColorSupportDetector?.getColorLevel() ?? "UnknownEnv";
@@ -215,7 +264,7 @@ class MyColorino {
215
264
  _formatValue(value, maxDepth = this._options.maxDepth ?? 5) {
216
265
  const seen = /* @__PURE__ */ new WeakSet();
217
266
  const sanitize = (val, currentDepth) => {
218
- if (val === null || typeof val !== "object") return val;
267
+ if (val == null || typeof val !== "object") return val;
219
268
  if (seen.has(val)) return "[Circular]";
220
269
  seen.add(val);
221
270
  if (currentDepth >= maxDepth) return "[Object]";
@@ -233,21 +282,36 @@ class MyColorino {
233
282
  };
234
283
  return JSON.stringify(sanitize(value, 0), null, 2);
235
284
  }
285
+ _normalizeString(value) {
286
+ if (value instanceof String) return value.valueOf();
287
+ return value;
288
+ }
236
289
  _processArgs(args) {
237
290
  const processedArgs = [];
238
291
  let previousWasObject = false;
239
- for (const arg of args) {
240
- const isFormattableObject = arg !== null && typeof arg === "object" && typeof arg !== "string" && !(arg instanceof Error);
241
- const isError = arg instanceof Error;
242
- if (isFormattableObject) {
243
- processedArgs.push(`
292
+ for (const rawArg of args) {
293
+ const arg = this._normalizeString(rawArg);
294
+ if (TypeValidator.isBrowserColorizedArg(arg)) {
295
+ processedArgs.push(arg);
296
+ previousWasObject = false;
297
+ continue;
298
+ }
299
+ if (TypeValidator.isFormattableObject(arg)) {
300
+ if (this.isBrowser) {
301
+ processedArgs.push({
302
+ [ColorinoBrowserObject]: true,
303
+ value: arg
304
+ });
305
+ } else {
306
+ processedArgs.push(`
244
307
  ${this._formatValue(arg)}`);
308
+ }
245
309
  previousWasObject = true;
246
- } else if (isError) {
310
+ } else if (TypeValidator.isError(arg)) {
247
311
  processedArgs.push("\n", this._cleanErrorStack(arg));
248
312
  previousWasObject = true;
249
313
  } else {
250
- if (typeof arg === "string" && previousWasObject) {
314
+ if (TypeValidator.isString(arg) && previousWasObject) {
251
315
  processedArgs.push(`
252
316
  ${arg}`);
253
317
  } else {
@@ -259,48 +323,78 @@ ${arg}`);
259
323
  return processedArgs;
260
324
  }
261
325
  _applyBrowserColors(consoleMethod, args) {
262
- const hex = this._palette[consoleMethod];
263
- if (typeof args[0] === "string") {
264
- return [`%c${args[0]}`, `color:${hex}`, ...args.slice(1)];
326
+ const formatParts = [];
327
+ const cssArgs = [];
328
+ const otherArgs = [];
329
+ const paletteHex = this._palette[consoleMethod];
330
+ for (const rawArg of args) {
331
+ const arg = this._normalizeString(rawArg);
332
+ if (TypeValidator.isBrowserColorizedArg(arg)) {
333
+ formatParts.push(`%c${arg.text}`);
334
+ cssArgs.push(`color:${arg.hex}`);
335
+ continue;
336
+ }
337
+ if (TypeValidator.isBrowserObjectArg(arg)) {
338
+ formatParts.push("%o");
339
+ otherArgs.push(arg.value);
340
+ continue;
341
+ }
342
+ if (TypeValidator.isString(arg)) {
343
+ formatParts.push(`%c${arg}`);
344
+ cssArgs.push(`color:${paletteHex}`);
345
+ continue;
346
+ }
347
+ formatParts.push("%o");
348
+ otherArgs.push(arg);
265
349
  }
266
- return args;
350
+ if (formatParts.length === 0) {
351
+ return args;
352
+ }
353
+ return [formatParts.join(""), ...cssArgs, ...otherArgs];
267
354
  }
268
- _applyNodeColors(consoleMethod, args) {
269
- const hex = this._palette[consoleMethod];
270
- let ansiCode;
355
+ _toAnsiPrefix(hex) {
356
+ if (this._colorLevel === ColorLevel.NO_COLOR || this._colorLevel === "UnknownEnv") {
357
+ return "";
358
+ }
271
359
  switch (this._colorLevel) {
272
360
  case ColorLevel.TRUECOLOR: {
273
361
  const [r, g, b] = colorConverter.hex.toRgb(hex);
274
- ansiCode = `\x1B[38;2;${r};${g};${b}m`;
275
- break;
362
+ return `\x1B[38;2;${r};${g};${b}m`;
276
363
  }
277
364
  case ColorLevel.ANSI256: {
278
365
  const code = colorConverter.hex.toAnsi256(hex);
279
- ansiCode = `\x1B[38;5;${code}m`;
280
- break;
366
+ return `\x1B[38;5;${code}m`;
281
367
  }
282
368
  case ColorLevel.ANSI:
283
369
  default: {
284
370
  const code = colorConverter.hex.toAnsi16(hex);
285
- ansiCode = `\x1B[${code}m`;
286
- break;
371
+ return `\x1B[${code}m`;
287
372
  }
288
373
  }
374
+ }
375
+ _applyNodeColors(consoleMethod, args) {
289
376
  const coloredArgs = [...args];
290
377
  const firstStringIndex = coloredArgs.findIndex(
291
378
  (arg) => typeof arg === "string"
292
379
  );
293
- if (firstStringIndex !== -1) {
294
- coloredArgs[firstStringIndex] = `${ansiCode}${coloredArgs[firstStringIndex]}\x1B[0m`;
380
+ if (firstStringIndex === -1) {
381
+ return coloredArgs;
382
+ }
383
+ const first = coloredArgs[firstStringIndex];
384
+ if (this._isAnsiColoredString(first)) {
385
+ return coloredArgs;
386
+ }
387
+ const hex = this._palette[consoleMethod];
388
+ const ansiPrefix = this._toAnsiPrefix(hex);
389
+ if (!ansiPrefix) {
390
+ return coloredArgs;
295
391
  }
392
+ coloredArgs[firstStringIndex] = `${ansiPrefix}${String(first)}\x1B[0m`;
296
393
  return coloredArgs;
297
394
  }
298
395
  _output(consoleMethod, args) {
299
- if (consoleMethod === "trace") {
300
- this._printCleanTrace(args);
301
- } else {
302
- console[consoleMethod](...args);
303
- }
396
+ if (consoleMethod === "trace") this._printCleanTrace(args);
397
+ else console[consoleMethod](...args);
304
398
  }
305
399
  _out(level, args) {
306
400
  const consoleMethod = isConsoleMethod(level) ? level : "log";
@@ -822,11 +916,11 @@ class Err {
822
916
  }
823
917
  Result.fromThrowable;
824
918
 
825
- class ColorinoError extends Error {
919
+ class InputValidationError extends Error {
826
920
  constructor(message) {
827
921
  super(message);
828
- this.name = "ColorinoError";
829
- Object.setPrototypeOf(this, ColorinoError.prototype);
922
+ this.name = "InputValidationError";
923
+ Object.setPrototypeOf(this, InputValidationError.prototype);
830
924
  }
831
925
  }
832
926
 
@@ -835,7 +929,7 @@ class InputValidator {
835
929
  const trimmedHex = hex.trim();
836
930
  const isHexValid = /^#[0-9A-F]{6}$/i.test(trimmedHex);
837
931
  if (!isHexValid) {
838
- return err(new ColorinoError(`Invalid hex color: '${hex}'`));
932
+ return err(new InputValidationError(`Invalid hex color: '${hex}'`));
839
933
  }
840
934
  return ok(true);
841
935
  }
@@ -16,6 +16,7 @@ interface Colorino {
16
16
  error(...args: unknown[]): void;
17
17
  debug(...args: unknown[]): void;
18
18
  trace(...args: unknown[]): void;
19
+ colorize(text: string, hex: string): void;
19
20
  }
20
21
 
21
22
  declare const themePalettes: Record<ThemeName, Palette>;
@@ -16,6 +16,7 @@ interface Colorino {
16
16
  error(...args: unknown[]): void;
17
17
  debug(...args: unknown[]): void;
18
18
  trace(...args: unknown[]): void;
19
+ colorize(text: string, hex: string): void;
19
20
  }
20
21
 
21
22
  declare const themePalettes: Record<ThemeName, Palette>;
@@ -16,6 +16,7 @@ interface Colorino {
16
16
  error(...args: unknown[]): void;
17
17
  debug(...args: unknown[]): void;
18
18
  trace(...args: unknown[]): void;
19
+ colorize(text: string, hex: string): void;
19
20
  }
20
21
 
21
22
  declare const themePalettes: Record<ThemeName, Palette>;
package/package.json CHANGED
@@ -1,25 +1,33 @@
1
1
  {
2
2
  "name": "colorino",
3
- "version": "0.12.7",
3
+ "version": "0.13.1",
4
4
  "description": "A super simple colorized logger that gets the most out of your terminal",
5
- "type": "module",
6
- "license": "MIT",
7
- "author": "simwai",
8
5
  "keywords": [
6
+ "ansi",
9
7
  "color",
8
+ "console",
10
9
  "logger",
11
10
  "terminal",
12
- "ansi",
13
- "console"
11
+ "chalk",
12
+ "kuler"
14
13
  ],
15
14
  "homepage": "https://github.com/simwai/colorino",
15
+ "bugs": {
16
+ "url": "https://github.com/simwai/colorino/issues"
17
+ },
18
+ "license": "MIT",
19
+ "author": "simwai",
16
20
  "repository": {
17
21
  "type": "git",
18
22
  "url": "git+https://github.com/simwai/colorino.git"
19
23
  },
20
- "bugs": {
21
- "url": "https://github.com/simwai/colorino/issues"
22
- },
24
+ "files": [
25
+ "dist",
26
+ "LICENSE.md",
27
+ "package.json",
28
+ "README.md"
29
+ ],
30
+ "type": "module",
23
31
  "main": "./dist/node.cjs",
24
32
  "module": "./dist/browser.mjs",
25
33
  "types": "./dist/browser.d.ts",
@@ -35,12 +43,9 @@
35
43
  "require": "./dist/browser.cjs"
36
44
  }
37
45
  },
38
- "files": [
39
- "dist",
40
- "README.md",
41
- "package.json",
42
- "LICENSE.md"
43
- ],
46
+ "publishConfig": {
47
+ "access": "public"
48
+ },
44
49
  "scripts": {
45
50
  "bundle:browser": "node --loader ts-node/esm node_modules/rollup/dist/bin/rollup -c rollup.browser.config.ts",
46
51
  "build": "unbuild && npm run bundle:browser",
@@ -76,7 +81,7 @@
76
81
  "lint-staged": "^15.2.0",
77
82
  "md-toc-cli": "^3.1.1",
78
83
  "node-pty": "^1.1.0",
79
- "oxfmt": "^0.9.0",
84
+ "oxfmt": "^0.23.0",
80
85
  "oxlint": "^0.2.0",
81
86
  "playwright": "^1.57.0",
82
87
  "rollup": "^4.54.0",
@@ -96,8 +101,5 @@
96
101
  "engines": {
97
102
  "node": ">=17.0.0",
98
103
  "npm": ">=8.0.0"
99
- },
100
- "publishConfig": {
101
- "access": "public"
102
104
  }
103
105
  }