colorino 0.13.2 → 0.14.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.
@@ -1,53 +1,5 @@
1
- function hexToRgb(hex) {
2
- const match = hex.toString().match(/[a-f0-9]{6}|[a-f0-9]{3}/i);
3
- if (!match) {
4
- return [0, 0, 0];
5
- }
6
- let colorString = match[0];
7
- if (match[0].length === 3) {
8
- colorString = [...colorString].map((char) => char + char).join("");
9
- }
10
- const integer = parseInt(colorString, 16);
11
- const r = integer >> 16 & 255;
12
- const g = integer >> 8 & 255;
13
- const b = integer & 255;
14
- return [r, g, b];
15
- }
16
- function rgbToAnsi256(rgb) {
17
- const [r, g, b] = rgb;
18
- if (r === g && g === b) {
19
- if (r < 8) return 16;
20
- if (r > 248) return 231;
21
- return Math.round((r - 8) / 247 * 24) + 232;
22
- }
23
- return 16 + 36 * Math.round(r / 255 * 5) + 6 * Math.round(g / 255 * 5) + Math.round(b / 255 * 5);
24
- }
25
- function rgbToHsvValue(rgb) {
26
- const r = rgb[0] / 255;
27
- const g = rgb[1] / 255;
28
- const b = rgb[2] / 255;
29
- const v = Math.max(r, g, b);
30
- return v * 100;
31
- }
32
- function rgbToAnsi16(rgb) {
33
- const [r, g, b] = rgb;
34
- const value = rgbToHsvValue(rgb);
35
- const roundedValue = Math.round(value / 50);
36
- if (roundedValue === 0) {
37
- return 30;
38
- }
39
- let ansi = 30 + (Math.round(b / 255) << 2 | Math.round(g / 255) << 1 | Math.round(r / 255));
40
- if (roundedValue === 2) {
41
- ansi += 60;
42
- }
43
- return ansi;
44
- }
45
- const colorConverter = {
46
- hex: {
47
- toRgb: hexToRgb,
48
- toAnsi16: (hex) => rgbToAnsi16(hexToRgb(hex)),
49
- toAnsi256: (hex) => rgbToAnsi256(hexToRgb(hex))
50
- }};
1
+ const ColorinoBrowserColorized = Symbol("colorino.browserColorized");
2
+ const ColorinoBrowserObject = Symbol("colorino.browserObject");
51
3
 
52
4
  var ColorLevel = /* @__PURE__ */ ((ColorLevel2) => {
53
5
  ColorLevel2[ColorLevel2["NO_COLOR"] = 0] = "NO_COLOR";
@@ -57,92 +9,6 @@ var ColorLevel = /* @__PURE__ */ ((ColorLevel2) => {
57
9
  return ColorLevel2;
58
10
  })(ColorLevel || {});
59
11
 
60
- const ColorinoBrowserColorized = Symbol("colorino.browserColorized");
61
- const ColorinoBrowserObject = Symbol("colorino.browserObject");
62
-
63
- const catppuccinMochaPalette = {
64
- log: "#cdd6f4",
65
- // Text
66
- info: "#89b4fa",
67
- // Blue
68
- warn: "#f9e2af",
69
- // Yellow
70
- error: "#f38ba8",
71
- // Red
72
- debug: "#a6adc8",
73
- // Subtext0
74
- trace: "#9399b2"
75
- // Subtext1
76
- };
77
- const catppuccinLattePalette = {
78
- log: "#4c4f69",
79
- // Text
80
- info: "#1e66f5",
81
- // Blue
82
- warn: "#df8e1d",
83
- // Yellow
84
- error: "#d20f39",
85
- // Red
86
- debug: "#7c7f93",
87
- // Subtext0
88
- trace: "#8c8fa1"
89
- // Subtext1
90
- };
91
- const draculaPalette = {
92
- log: "#f8f8f2",
93
- // Foreground
94
- info: "#8be9fd",
95
- // Cyan
96
- warn: "#f1fa8c",
97
- // Yellow
98
- error: "#ff5555",
99
- // Red
100
- debug: "#bd93f9",
101
- // Purple
102
- trace: "#6272a4"
103
- // Comment
104
- };
105
- const githubLightPalette = {
106
- log: "#24292e",
107
- // Text
108
- info: "#0366d6",
109
- // Blue
110
- warn: "#f9a002",
111
- // Yellow
112
- error: "#d73a49",
113
- // Red
114
- debug: "#586069",
115
- // Gray
116
- trace: "#6a737d"
117
- // Gray-light
118
- };
119
-
120
- const themePalettes = {
121
- "catppuccin-mocha": catppuccinMochaPalette,
122
- "catppuccin-latte": catppuccinLattePalette,
123
- dracula: draculaPalette,
124
- "github-light": githubLightPalette
125
- };
126
- const defaultDarkTheme = "dracula";
127
- const defaultLightTheme = "github-light";
128
- function isThemeName(theme) {
129
- return theme in themePalettes;
130
- }
131
-
132
- function determineBaseTheme(themeOpt, detectedBrowserTheme) {
133
- let baseThemeName;
134
- if (isThemeName(themeOpt)) {
135
- baseThemeName = themeOpt;
136
- } else if (themeOpt === "light") {
137
- baseThemeName = defaultLightTheme;
138
- } else if (themeOpt === "dark") {
139
- baseThemeName = defaultDarkTheme;
140
- } else {
141
- baseThemeName = detectedBrowserTheme === "light" ? defaultLightTheme : defaultDarkTheme;
142
- }
143
- return baseThemeName;
144
- }
145
-
146
12
  class TypeValidator {
147
13
  static isNull(value) {
148
14
  return value === null;
@@ -182,31 +48,18 @@ class TypeValidator {
182
48
  }
183
49
  }
184
50
 
185
- class MyColorino {
186
- constructor(initialPalette, _userPalette, _validator, _browserColorSupportDetector, _nodeColorSupportDetector, _options = {}) {
51
+ class AbstractColorino {
52
+ constructor(initialPalette, _userPalette, _validator, colorLevel, _options = {}) {
187
53
  this._userPalette = _userPalette;
188
54
  this._validator = _validator;
189
- this._browserColorSupportDetector = _browserColorSupportDetector;
190
- this._nodeColorSupportDetector = _nodeColorSupportDetector;
191
55
  this._options = _options;
192
56
  this._palette = initialPalette;
193
- this.isBrowser = !!this._browserColorSupportDetector;
194
- this._colorLevel = this._detectColorSupport();
195
57
  const validatePaletteResult = this._validator.validatePalette(this._palette);
196
58
  if (validatePaletteResult.isErr()) throw validatePaletteResult.error;
197
- if (this._colorLevel !== ColorLevel.NO_COLOR && !this._options.disableWarnings && this._colorLevel === "UnknownEnv") {
198
- this._maybeWarnUser();
199
- }
200
- const themeOpt = this._options.theme ?? "auto";
201
- if (themeOpt === "auto" && this._nodeColorSupportDetector) {
202
- this._nodeColorSupportDetector.onTheme((resolvedTheme) => {
203
- this._applyResolvedTheme(resolvedTheme);
204
- });
205
- }
59
+ this._colorLevel = colorLevel;
206
60
  }
207
61
  _alreadyWarned = false;
208
62
  _colorLevel;
209
- isBrowser;
210
63
  _palette;
211
64
  log(...args) {
212
65
  this._out("log", args);
@@ -230,7 +83,7 @@ class MyColorino {
230
83
  if (this._colorLevel === ColorLevel.NO_COLOR || this._colorLevel === "UnknownEnv") {
231
84
  return text;
232
85
  }
233
- if (this.isBrowser) {
86
+ if (this.isBrowser()) {
234
87
  return {
235
88
  [ColorinoBrowserColorized]: true,
236
89
  text,
@@ -243,37 +96,28 @@ class MyColorino {
243
96
  }
244
97
  return `${ansiPrefix}${text}\x1B[0m`;
245
98
  }
246
- _applyResolvedTheme(resolvedTheme) {
247
- const themeOpt = this._options.theme ?? "auto";
248
- const baseThemeName = determineBaseTheme(themeOpt, resolvedTheme);
249
- const basePalette = themePalettes[baseThemeName];
250
- this._palette = { ...basePalette, ...this._userPalette };
251
- }
252
- _detectColorSupport() {
253
- if (this.isBrowser) {
254
- return this._browserColorSupportDetector?.getColorLevel() ?? "UnknownEnv";
255
- }
256
- if (this._nodeColorSupportDetector?.isNodeEnv()) {
257
- return this._nodeColorSupportDetector?.getColorLevel() ?? "UnknownEnv";
99
+ _out(level, args) {
100
+ const consoleMethod = TypeValidator.isConsoleMethod(level) ? level : "log";
101
+ const processedArgs = this.processArgs(args);
102
+ if (this._colorLevel === ColorLevel.NO_COLOR || this._colorLevel === "UnknownEnv") {
103
+ this.output(consoleMethod, processedArgs);
104
+ return;
258
105
  }
259
- return "UnknownEnv";
106
+ const coloredArgs = this.applyColors(consoleMethod, processedArgs);
107
+ this.output(consoleMethod, coloredArgs);
260
108
  }
261
- _maybeWarnUser() {
262
- if (this._alreadyWarned) return;
263
- this._alreadyWarned = true;
264
- console.warn(
265
- "No ANSI color support detected in this terminal. See [https://github.com/chalk/supports-color#support-matrix](https://github.com/chalk/supports-color#support-matrix) to learn how to enable terminal color."
266
- );
109
+ _toAnsiPrefix(_hex) {
110
+ return "";
267
111
  }
268
112
  _formatValue(value, maxDepth = this._options.maxDepth ?? 5) {
269
113
  const seen = /* @__PURE__ */ new WeakSet();
270
- const sanitizeArray = (items, currentDepth) => {
271
- return items.map((item) => sanitize(item, currentDepth));
114
+ const sanitizeArray = (items, depth) => {
115
+ return items.map((item) => sanitize(item, depth));
272
116
  };
273
- const sanitizeObject = (obj, currentDepth) => {
117
+ const sanitizeObject = (obj, depth) => {
274
118
  const result = {};
275
119
  for (const key in obj) {
276
- result[key] = sanitize(obj[key], currentDepth);
120
+ result[key] = sanitize(obj[key], depth);
277
121
  }
278
122
  return result;
279
123
  };
@@ -292,42 +136,34 @@ class MyColorino {
292
136
  };
293
137
  return JSON.stringify(sanitize(value, 0), null, 2);
294
138
  }
295
- _processArgs(args) {
296
- const processedArgs = [];
297
- let previousWasObject = false;
298
- for (const arg of args) {
299
- if (TypeValidator.isBrowserColorizedArg(arg)) {
300
- processedArgs.push(arg);
301
- previousWasObject = false;
302
- continue;
303
- }
304
- if (TypeValidator.isFormattableObject(arg)) {
305
- if (this.isBrowser) {
306
- processedArgs.push({
307
- [ColorinoBrowserObject]: true,
308
- value: arg
309
- });
310
- } else {
311
- processedArgs.push(`
312
- ${this._formatValue(arg)}`);
313
- }
314
- previousWasObject = true;
315
- } else if (TypeValidator.isError(arg)) {
316
- processedArgs.push("\n", this._cleanErrorStack(arg));
317
- previousWasObject = true;
318
- } else {
319
- if (TypeValidator.isString(arg) && previousWasObject) {
320
- processedArgs.push(`
321
- ${arg}`);
322
- } else {
323
- processedArgs.push(arg);
324
- }
325
- previousWasObject = false;
326
- }
139
+ _filterStack(stack) {
140
+ return stack.split("\n").filter((line) => !line.match(/.*colorino.*/gi)).join("\n");
141
+ }
142
+ _cleanErrorStack(error) {
143
+ if (!error.stack) return error;
144
+ const cleanStack = this._filterStack(error.stack);
145
+ const cloned = Object.create(Object.getPrototypeOf(error));
146
+ Object.assign(cloned, error);
147
+ cloned.stack = cleanStack;
148
+ return cloned;
149
+ }
150
+ _printCleanTrace(args) {
151
+ const error = new Error();
152
+ if (error.stack) {
153
+ const cleanStack = this._filterStack(error.stack);
154
+ console.log(...args, `
155
+ ${cleanStack}`);
156
+ } else {
157
+ console.log(...args);
327
158
  }
328
- return processedArgs;
329
159
  }
330
- _applyBrowserColors(consoleMethod, args) {
160
+ }
161
+
162
+ class ColorinoBrowser extends AbstractColorino {
163
+ constructor(initialPalette, userPalette, validator, colorLevel, options = {}) {
164
+ super(initialPalette, userPalette, validator, colorLevel, options);
165
+ }
166
+ applyColors(consoleMethod, args) {
331
167
  const formatParts = [];
332
168
  const cssArgs = [];
333
169
  const otherArgs = [];
@@ -351,80 +187,83 @@ ${arg}`);
351
187
  formatParts.push("%o");
352
188
  otherArgs.push(arg);
353
189
  }
354
- if (formatParts.length === 0) {
355
- return args;
356
- }
190
+ if (formatParts.length === 0) return args;
357
191
  return [formatParts.join(""), ...cssArgs, ...otherArgs];
358
192
  }
359
- _toAnsiPrefix(hex) {
360
- if (this._colorLevel === ColorLevel.NO_COLOR || this._colorLevel === "UnknownEnv") {
361
- return "";
362
- }
363
- switch (this._colorLevel) {
364
- case ColorLevel.TRUECOLOR: {
365
- const [r, g, b] = colorConverter.hex.toRgb(hex);
366
- return `\x1B[38;2;${r};${g};${b}m`;
367
- }
368
- case ColorLevel.ANSI256: {
369
- const code = colorConverter.hex.toAnsi256(hex);
370
- return `\x1B[38;5;${code}m`;
193
+ output(consoleMethod, args) {
194
+ console[consoleMethod](...args);
195
+ }
196
+ processArgs(args) {
197
+ const processedArgs = [];
198
+ let previousWasObject = false;
199
+ for (const arg of args) {
200
+ if (TypeValidator.isBrowserColorizedArg(arg)) {
201
+ processedArgs.push(arg);
202
+ previousWasObject = false;
203
+ continue;
371
204
  }
372
- case ColorLevel.ANSI:
373
- default: {
374
- const code = colorConverter.hex.toAnsi16(hex);
375
- return `\x1B[${code}m`;
205
+ if (TypeValidator.isFormattableObject(arg)) {
206
+ processedArgs.push({
207
+ [ColorinoBrowserObject]: true,
208
+ value: arg
209
+ });
210
+ previousWasObject = true;
211
+ } else if (TypeValidator.isError(arg)) {
212
+ processedArgs.push("\n", this._cleanErrorStack(arg));
213
+ previousWasObject = true;
214
+ } else {
215
+ processedArgs.push(
216
+ TypeValidator.isString(arg) && previousWasObject ? `
217
+ ${arg}` : arg
218
+ );
219
+ previousWasObject = false;
376
220
  }
377
221
  }
222
+ return processedArgs;
378
223
  }
379
- _applyNodeColors(consoleMethod, args) {
380
- const paletteHex = this._palette[consoleMethod];
381
- const paletteAnsiPrefix = this._toAnsiPrefix(paletteHex);
382
- if (!paletteAnsiPrefix) return args;
383
- return args.map((arg) => {
384
- if (!TypeValidator.isString(arg) || TypeValidator.isAnsiColoredString(arg))
385
- return arg;
386
- return `${paletteAnsiPrefix}${String(arg)}\x1B[0m`;
387
- });
388
- }
389
- _output(consoleMethod, args) {
390
- if (consoleMethod === "trace") this._printCleanTrace(args);
391
- else console[consoleMethod](...args);
224
+ isBrowser() {
225
+ return true;
392
226
  }
393
- _out(level, args) {
394
- const consoleMethod = TypeValidator.isConsoleMethod(level) ? level : "log";
395
- const processedArgs = this._processArgs(args);
396
- if (this._colorLevel === ColorLevel.NO_COLOR || this._colorLevel === "UnknownEnv") {
397
- this._output(consoleMethod, processedArgs);
398
- return;
227
+ }
228
+
229
+ class BrowserColorSupportDetector {
230
+ constructor(_window, _navigator, _overrideTheme) {
231
+ this._window = _window;
232
+ this._navigator = _navigator;
233
+ this._overrideTheme = _overrideTheme;
234
+ }
235
+ isBrowserEnv() {
236
+ return !!this._window && !!this._navigator?.userAgent;
237
+ }
238
+ getColorLevel() {
239
+ const isTruecolor = window.matchMedia("(color-gamut: p3)").matches;
240
+ if (isTruecolor) {
241
+ return ColorLevel.TRUECOLOR;
399
242
  }
400
- if (this.isBrowser) {
401
- const coloredArgs2 = this._applyBrowserColors(consoleMethod, processedArgs);
402
- this._output(consoleMethod, coloredArgs2);
403
- return;
243
+ const isAnsi256 = window.matchMedia("(monochrome: 0)").matches;
244
+ if (isAnsi256) {
245
+ return ColorLevel.ANSI256;
404
246
  }
405
- const coloredArgs = this._applyNodeColors(consoleMethod, processedArgs);
406
- this._output(consoleMethod, coloredArgs);
407
- }
408
- _filterStack(stack) {
409
- return stack.split("\n").filter((line) => !line.match(/.*colorino.*/gi)).join("\n");
247
+ return ColorLevel.ANSI;
410
248
  }
411
- _cleanErrorStack(error) {
412
- if (!error.stack) return error;
413
- const cleanStack = this._filterStack(error.stack);
414
- const cloned = Object.create(Object.getPrototypeOf(error));
415
- Object.assign(cloned, error);
416
- cloned.stack = cleanStack;
417
- return cloned;
418
- }
419
- _printCleanTrace(args) {
420
- const error = new Error();
421
- if (error.stack) {
422
- const cleanStack = this._filterStack(error.stack);
423
- console.log(...args, `
424
- ${cleanStack}`);
425
- } else {
426
- console.log(...args);
249
+ getTheme() {
250
+ if (this._overrideTheme) {
251
+ return this._overrideTheme;
252
+ }
253
+ if (!this.isBrowserEnv() || typeof this._window.matchMedia !== "function") {
254
+ return "unknown";
255
+ }
256
+ const isDarkTheme = this._window.matchMedia(
257
+ "(prefers-color-scheme: dark)"
258
+ ).matches;
259
+ if (isDarkTheme) {
260
+ return "dark";
261
+ }
262
+ const isLightTheme = "(prefers-color-scheme: light)";
263
+ if (this._window.matchMedia(isLightTheme).matches) {
264
+ return "light";
427
265
  }
266
+ return "unknown";
428
267
  }
429
268
  }
430
269
 
@@ -939,4 +778,115 @@ class InputValidator {
939
778
  }
940
779
  }
941
780
 
942
- export { ColorLevel as C, InputValidator as I, MyColorino as M, determineBaseTheme as d, themePalettes as t };
781
+ const catppuccinMochaPalette = {
782
+ log: "#cdd6f4",
783
+ // Text
784
+ info: "#89b4fa",
785
+ // Blue
786
+ warn: "#f9e2af",
787
+ // Yellow
788
+ error: "#f38ba8",
789
+ // Red
790
+ debug: "#a6adc8",
791
+ // Subtext0
792
+ trace: "#9399b2"
793
+ // Subtext1
794
+ };
795
+ const catppuccinLattePalette = {
796
+ log: "#4c4f69",
797
+ // Text
798
+ info: "#1e66f5",
799
+ // Blue
800
+ warn: "#df8e1d",
801
+ // Yellow
802
+ error: "#d20f39",
803
+ // Red
804
+ debug: "#7c7f93",
805
+ // Subtext0
806
+ trace: "#8c8fa1"
807
+ // Subtext1
808
+ };
809
+ const draculaPalette = {
810
+ log: "#f8f8f2",
811
+ // Foreground
812
+ info: "#8be9fd",
813
+ // Cyan
814
+ warn: "#f1fa8c",
815
+ // Yellow
816
+ error: "#ff5555",
817
+ // Red
818
+ debug: "#bd93f9",
819
+ // Purple
820
+ trace: "#6272a4"
821
+ // Comment
822
+ };
823
+ const githubLightPalette = {
824
+ log: "#24292e",
825
+ // Text
826
+ info: "#0366d6",
827
+ // Blue
828
+ warn: "#f9a002",
829
+ // Yellow
830
+ error: "#d73a49",
831
+ // Red
832
+ debug: "#586069",
833
+ // Gray
834
+ trace: "#6a737d"
835
+ // Gray-light
836
+ };
837
+
838
+ const themePalettes = {
839
+ "catppuccin-mocha": catppuccinMochaPalette,
840
+ "catppuccin-latte": catppuccinLattePalette,
841
+ dracula: draculaPalette,
842
+ "github-light": githubLightPalette
843
+ };
844
+ const defaultDarkTheme = "dracula";
845
+ const defaultLightTheme = "github-light";
846
+ function isThemeName(theme) {
847
+ return theme in themePalettes;
848
+ }
849
+
850
+ function determineBaseTheme(themeOpt, detectedBrowserTheme) {
851
+ let baseThemeName;
852
+ if (isThemeName(themeOpt)) {
853
+ baseThemeName = themeOpt;
854
+ } else if (themeOpt === "light") {
855
+ baseThemeName = defaultLightTheme;
856
+ } else if (themeOpt === "dark") {
857
+ baseThemeName = defaultDarkTheme;
858
+ } else {
859
+ baseThemeName = detectedBrowserTheme === "light" ? defaultLightTheme : defaultDarkTheme;
860
+ }
861
+ return baseThemeName;
862
+ }
863
+
864
+ function createColorino(userPalette = {}, options = {}) {
865
+ const validator = new InputValidator();
866
+ let detectorThemeOverride;
867
+ if (options.theme === "dark" || options.theme === "light") {
868
+ detectorThemeOverride = options.theme;
869
+ }
870
+ const browserDetector = new BrowserColorSupportDetector(
871
+ window,
872
+ navigator,
873
+ detectorThemeOverride
874
+ );
875
+ const detectedBrowserTheme = browserDetector.getTheme();
876
+ const themeOpt = options.theme ?? "auto";
877
+ const baseThemeName = determineBaseTheme(themeOpt, detectedBrowserTheme);
878
+ const basePalette = themePalettes[baseThemeName];
879
+ const finalPalette = { ...basePalette, ...userPalette };
880
+ const colorLevel = browserDetector.isBrowserEnv() ? browserDetector.getColorLevel() ?? "UnknownEnv" : "UnknownEnv";
881
+ return new ColorinoBrowser(
882
+ finalPalette,
883
+ userPalette,
884
+ validator,
885
+ colorLevel,
886
+ options
887
+ );
888
+ }
889
+ const colorino = createColorino();
890
+
891
+ export { colorino, createColorino, themePalettes };
892
+ //# sourceMappingURL=cdn.mjs.map