colorino 0.14.1 → 0.15.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/README.md CHANGED
@@ -23,6 +23,7 @@ Colorino automatically adapts its palette to your terminal or browser DevTools t
23
23
  - [Customization](#5-4)
24
24
  - [Supported Environment Variables](#5-5)
25
25
  - [Colorize Helper (Manual Overrides)](#5-6)
26
+ - [Browser‑only CSS Helper (`css()`)](#5-7)
26
27
  - [Colorino vs. Chalk](#6)
27
28
  - [API Reference](#7)
28
29
  - [1. `colorino` (default instance)](#7-1)
@@ -45,6 +46,7 @@ Colorino is different: it’s a "batteries-included" logging facade with beautif
45
46
 
46
47
  - 🎨 **Smart Theming:** Automatically detects _dark/light_ mode and applies a high‑contrast base palette by default (Dracula for dark, GitHub Light for light); opt into a coordinated theme preset when you want richer colors.
47
48
  - 🤘 **Graceful Color Degradation**: Accepts rich colors (hex/RGB) and automatically down‑samples to the best ANSI‑16/ANSI‑256/Truecolor match for the current environment.​
49
+ - 🎯 **CSS styling in DevTools (browser only):** Use a dedicated helper to apply arbitrary CSS properties to specific console segments in Chrome, Firefox, and Safari DevTools, powered by the `%c` formatter.
48
50
  - 🤝 **Familiar API:** If you know `console.log`, you already know Colorino: all standard log levels are supported.
49
51
  - 🔀 **Environment-Aware:** Works in **Node.js** (ANSI color and truecolor) and all major **Browsers** (CSS styles).
50
52
  - ⚡️ **Fast, Lightweight:** Minimal dependencies, works great in modern frameworks and CLIs.
@@ -282,6 +284,25 @@ colorino.info(important, 'Something happened')
282
284
 
283
285
  When color is disabled (for example via `NO_COLOR=1` or lack of support), `colorize` returns the plain input string, so your logs stay readable.
284
286
 
287
+ ### <a id="5-7"></a>Browser‑only CSS Helper (`css()`)
288
+
289
+ In the browser, Colorino also exposes a `css(text, style)` helper that lets you apply arbitrary CSS to a single segment in DevTools using the `%c` formatter.
290
+
291
+ ```ts
292
+ import { colorino } from 'colorino'
293
+
294
+ // Object form: keys are CSS properties, values are strings
295
+ const badge = colorino.css('NEW', {
296
+ color: 'white',
297
+ 'background-color': '#e91e63',
298
+ 'font-weight': 'bold',
299
+ 'border-radius': '4px',
300
+ padding: '2px 6px',
301
+ })
302
+
303
+ colorino.info('Release status:', badge, 'shipped')
304
+ ```
305
+
285
306
  ## <a id="6"></a>Colorino vs. Chalk
286
307
 
287
308
  | Feature | 🎨 **Colorino** | 🖍️ **Chalk** |
@@ -355,11 +376,11 @@ function getCallerContext(): string {
355
376
  export function createContextLogger(
356
377
  palette?: Partial<Palette>,
357
378
  options?: ColorinoOptions
358
- ): Colorino {
379
+ ): ReturnType<typeof createColorino> {
359
380
  const base = createColorino(palette, options)
360
381
 
361
382
  // Inherit all default methods from the base logger...
362
- const logger = Object.create(base) as Colorino // Object.create uses `base` as the prototype.
383
+ const logger = Object.create(base) as ReturnType<typeof createColorino> // Object.create uses `base` as the prototype.
363
384
 
364
385
  // ...and override only what you need.
365
386
  Object.assign(logger, {
@@ -384,7 +405,7 @@ logger.error('Failed to load user', { id: 456 })
384
405
  ## <a id="9"></a>Internals & Dependencies
385
406
 
386
407
  - Colorino’s runtime logic depends on a single bundled library, `neverthrow`, which is MIT‑licensed and used for type‑safe Result handling.
387
- - `neverthrow` is bundled into the published artifacts, so you do not need to install it separately; your application sees a single runtime dependency: Colorino itself.
408
+ - `neverthrow` is bundled into the published artifacts, so you do not need to install it separately.
388
409
 
389
410
  ### <a id="9-1"></a>Why This Pattern?
390
411
 
@@ -3,6 +3,16 @@ type LogLevel = ConsoleMethod & string;
3
3
  type Palette = Record<LogLevel, string>;
4
4
  type TerminalTheme = 'dark' | 'light' | 'unknown';
5
5
  type ThemeName = 'catppuccin-mocha' | 'catppuccin-latte' | 'dracula' | 'github-light';
6
+ declare const ColorinoBrowserCss: unique symbol;
7
+ type CssConsoleStyle = string | Record<string, string>;
8
+ interface BrowserCssArg {
9
+ [ColorinoBrowserCss]: true;
10
+ text: string;
11
+ css: string;
12
+ }
13
+
14
+ declare const themePalettes: Record<ThemeName, Palette>;
15
+
6
16
  interface ColorinoOptions {
7
17
  disableWarnings?: boolean;
8
18
  theme?: TerminalTheme | ThemeName | 'auto';
@@ -18,12 +28,13 @@ interface Colorino {
18
28
  trace(...args: unknown[]): void;
19
29
  colorize(text: string, hex: string): void;
20
30
  }
31
+ interface ColorinoBrowserInterface extends Colorino {
32
+ css(text: string, style: CssConsoleStyle): string | BrowserCssArg;
33
+ }
21
34
 
22
- declare const themePalettes: Record<ThemeName, Palette>;
23
-
24
- declare function createColorino(userPalette?: Partial<Palette>, options?: ColorinoOptions): Colorino;
35
+ declare function createColorino(userPalette?: Partial<Palette>, options?: ColorinoOptions): ColorinoBrowserInterface;
25
36
 
26
- declare const colorino: Colorino;
37
+ declare const colorino: ColorinoBrowserInterface;
27
38
 
28
39
  export { colorino, createColorino, themePalettes };
29
- export type { Colorino, ColorinoOptions, LogLevel, Palette, ThemeName };
40
+ export type { ColorinoBrowserInterface, ColorinoOptions, LogLevel, Palette, ThemeName };
package/dist/browser.d.ts CHANGED
@@ -3,6 +3,16 @@ type LogLevel = ConsoleMethod & string;
3
3
  type Palette = Record<LogLevel, string>;
4
4
  type TerminalTheme = 'dark' | 'light' | 'unknown';
5
5
  type ThemeName = 'catppuccin-mocha' | 'catppuccin-latte' | 'dracula' | 'github-light';
6
+ declare const ColorinoBrowserCss: unique symbol;
7
+ type CssConsoleStyle = string | Record<string, string>;
8
+ interface BrowserCssArg {
9
+ [ColorinoBrowserCss]: true;
10
+ text: string;
11
+ css: string;
12
+ }
13
+
14
+ declare const themePalettes: Record<ThemeName, Palette>;
15
+
6
16
  interface ColorinoOptions {
7
17
  disableWarnings?: boolean;
8
18
  theme?: TerminalTheme | ThemeName | 'auto';
@@ -18,12 +28,13 @@ interface Colorino {
18
28
  trace(...args: unknown[]): void;
19
29
  colorize(text: string, hex: string): void;
20
30
  }
31
+ interface ColorinoBrowserInterface extends Colorino {
32
+ css(text: string, style: CssConsoleStyle): string | BrowserCssArg;
33
+ }
21
34
 
22
- declare const themePalettes: Record<ThemeName, Palette>;
23
-
24
- declare function createColorino(userPalette?: Partial<Palette>, options?: ColorinoOptions): Colorino;
35
+ declare function createColorino(userPalette?: Partial<Palette>, options?: ColorinoOptions): ColorinoBrowserInterface;
25
36
 
26
- declare const colorino: Colorino;
37
+ declare const colorino: ColorinoBrowserInterface;
27
38
 
28
39
  export { colorino, createColorino, themePalettes };
29
- export type { Colorino, ColorinoOptions, LogLevel, Palette, ThemeName };
40
+ export type { ColorinoBrowserInterface, ColorinoOptions, LogLevel, Palette, ThemeName };
package/dist/browser.mjs CHANGED
@@ -2,6 +2,7 @@ import { err, ok } from 'neverthrow';
2
2
 
3
3
  const ColorinoBrowserColorized = Symbol("colorino.browserColorized");
4
4
  const ColorinoBrowserObject = Symbol("colorino.browserObject");
5
+ const ColorinoBrowserCss = Symbol("colorino.browserCss");
5
6
 
6
7
  var ColorLevel = /* @__PURE__ */ ((ColorLevel2) => {
7
8
  ColorLevel2[ColorLevel2["NO_COLOR"] = 0] = "NO_COLOR";
@@ -36,6 +37,9 @@ class TypeValidator {
36
37
  static isBrowserColorizedArg(value) {
37
38
  return TypeValidator.isObject(value) && ColorinoBrowserColorized in value;
38
39
  }
40
+ static isBrowserCssArg(value) {
41
+ return typeof value === "object" && value !== null && ColorinoBrowserCss in value && value[ColorinoBrowserCss] === true;
42
+ }
39
43
  static isBrowserObjectArg(value) {
40
44
  return TypeValidator.isObject(value) && ColorinoBrowserObject in value;
41
45
  }
@@ -100,13 +104,13 @@ class AbstractColorino {
100
104
  }
101
105
  _out(level, args) {
102
106
  const consoleMethod = TypeValidator.isConsoleMethod(level) ? level : "log";
103
- const processedArgs = this.processArgs(args);
107
+ const processedArgs = this._processArgs(args);
104
108
  if (this._colorLevel === ColorLevel.NO_COLOR || this._colorLevel === "UnknownEnv") {
105
- this.output(consoleMethod, processedArgs);
109
+ this._output(consoleMethod, processedArgs);
106
110
  return;
107
111
  }
108
- const coloredArgs = this.applyColors(consoleMethod, processedArgs);
109
- this.output(consoleMethod, coloredArgs);
112
+ const coloredArgs = this._applyColors(consoleMethod, processedArgs);
113
+ this._output(consoleMethod, coloredArgs);
110
114
  }
111
115
  _toAnsiPrefix(_hex) {
112
116
  return "";
@@ -165,41 +169,69 @@ class ColorinoBrowser extends AbstractColorino {
165
169
  constructor(initialPalette, userPalette, validator, colorLevel, options = {}) {
166
170
  super(initialPalette, userPalette, validator, colorLevel, options);
167
171
  }
168
- applyColors(consoleMethod, args) {
172
+ css(text, style) {
173
+ if (this._colorLevel === ColorLevel.NO_COLOR || this._colorLevel === "UnknownEnv") {
174
+ return text;
175
+ }
176
+ const css = this._normalizeCssStyle(style);
177
+ return {
178
+ [ColorinoBrowserCss]: true,
179
+ text,
180
+ css
181
+ };
182
+ }
183
+ _normalizeCssStyle(style) {
184
+ if (TypeValidator.isString(style)) return style;
185
+ const parts = [];
186
+ for (const propertyName in style) {
187
+ if (!Object.prototype.hasOwnProperty.call(style, propertyName)) {
188
+ continue;
189
+ }
190
+ const value = style[propertyName];
191
+ if (!value) continue;
192
+ parts.push(`${propertyName}:${value}`);
193
+ }
194
+ return parts.join(";");
195
+ }
196
+ _applyColors(consoleMethod, args) {
169
197
  const formatParts = [];
170
- const cssArgs = [];
171
- const otherArgs = [];
198
+ const formatArgs = [];
172
199
  const paletteHex = this._palette[consoleMethod];
173
200
  for (const arg of args) {
174
201
  if (TypeValidator.isBrowserColorizedArg(arg)) {
175
202
  formatParts.push(`%c${arg.text}`);
176
- cssArgs.push(`color:${arg.hex}`);
203
+ formatArgs.push(`color:${arg.hex}`);
204
+ continue;
205
+ }
206
+ if (TypeValidator.isBrowserCssArg(arg)) {
207
+ formatParts.push(`%c${arg.text}`);
208
+ formatArgs.push(arg.css);
177
209
  continue;
178
210
  }
179
211
  if (TypeValidator.isBrowserObjectArg(arg)) {
180
212
  formatParts.push("%o");
181
- otherArgs.push(arg.value);
213
+ formatArgs.push(arg.value);
182
214
  continue;
183
215
  }
184
216
  if (TypeValidator.isString(arg)) {
185
217
  formatParts.push(`%c${arg}`);
186
- cssArgs.push(`color:${paletteHex}`);
218
+ formatArgs.push(`color:${paletteHex}`);
187
219
  continue;
188
220
  }
189
221
  formatParts.push("%o");
190
- otherArgs.push(arg);
222
+ formatArgs.push(arg);
191
223
  }
192
224
  if (formatParts.length === 0) return args;
193
- return [formatParts.join(""), ...cssArgs, ...otherArgs];
225
+ return [formatParts.join(""), ...formatArgs];
194
226
  }
195
- output(consoleMethod, args) {
227
+ _output(consoleMethod, args) {
196
228
  console[consoleMethod](...args);
197
229
  }
198
- processArgs(args) {
230
+ _processArgs(args) {
199
231
  const processedArgs = [];
200
232
  let previousWasObject = false;
201
233
  for (const arg of args) {
202
- if (TypeValidator.isBrowserColorizedArg(arg)) {
234
+ if (TypeValidator.isBrowserColorizedArg(arg) || TypeValidator.isBrowserCssArg(arg)) {
203
235
  processedArgs.push(arg);
204
236
  previousWasObject = false;
205
237
  continue;
package/dist/cdn.js CHANGED
@@ -1,11 +1,12 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
- typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.colorino = {}));
5
- })(this, (function (exports) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
+ typeof define === 'function' && define.amd ? define(factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.colorino = factory());
5
+ })(this, (function () { 'use strict';
6
6
 
7
7
  const ColorinoBrowserColorized = Symbol("colorino.browserColorized");
8
8
  const ColorinoBrowserObject = Symbol("colorino.browserObject");
9
+ const ColorinoBrowserCss = Symbol("colorino.browserCss");
9
10
 
10
11
  var ColorLevel = /* @__PURE__ */ ((ColorLevel2) => {
11
12
  ColorLevel2[ColorLevel2["NO_COLOR"] = 0] = "NO_COLOR";
@@ -40,6 +41,9 @@
40
41
  static isBrowserColorizedArg(value) {
41
42
  return TypeValidator.isObject(value) && ColorinoBrowserColorized in value;
42
43
  }
44
+ static isBrowserCssArg(value) {
45
+ return typeof value === "object" && value !== null && ColorinoBrowserCss in value && value[ColorinoBrowserCss] === true;
46
+ }
43
47
  static isBrowserObjectArg(value) {
44
48
  return TypeValidator.isObject(value) && ColorinoBrowserObject in value;
45
49
  }
@@ -104,13 +108,13 @@
104
108
  }
105
109
  _out(level, args) {
106
110
  const consoleMethod = TypeValidator.isConsoleMethod(level) ? level : "log";
107
- const processedArgs = this.processArgs(args);
111
+ const processedArgs = this._processArgs(args);
108
112
  if (this._colorLevel === ColorLevel.NO_COLOR || this._colorLevel === "UnknownEnv") {
109
- this.output(consoleMethod, processedArgs);
113
+ this._output(consoleMethod, processedArgs);
110
114
  return;
111
115
  }
112
- const coloredArgs = this.applyColors(consoleMethod, processedArgs);
113
- this.output(consoleMethod, coloredArgs);
116
+ const coloredArgs = this._applyColors(consoleMethod, processedArgs);
117
+ this._output(consoleMethod, coloredArgs);
114
118
  }
115
119
  _toAnsiPrefix(_hex) {
116
120
  return "";
@@ -169,41 +173,69 @@ ${cleanStack}`);
169
173
  constructor(initialPalette, userPalette, validator, colorLevel, options = {}) {
170
174
  super(initialPalette, userPalette, validator, colorLevel, options);
171
175
  }
172
- applyColors(consoleMethod, args) {
176
+ css(text, style) {
177
+ if (this._colorLevel === ColorLevel.NO_COLOR || this._colorLevel === "UnknownEnv") {
178
+ return text;
179
+ }
180
+ const css = this._normalizeCssStyle(style);
181
+ return {
182
+ [ColorinoBrowserCss]: true,
183
+ text,
184
+ css
185
+ };
186
+ }
187
+ _normalizeCssStyle(style) {
188
+ if (TypeValidator.isString(style)) return style;
189
+ const parts = [];
190
+ for (const propertyName in style) {
191
+ if (!Object.prototype.hasOwnProperty.call(style, propertyName)) {
192
+ continue;
193
+ }
194
+ const value = style[propertyName];
195
+ if (!value) continue;
196
+ parts.push(`${propertyName}:${value}`);
197
+ }
198
+ return parts.join(";");
199
+ }
200
+ _applyColors(consoleMethod, args) {
173
201
  const formatParts = [];
174
- const cssArgs = [];
175
- const otherArgs = [];
202
+ const formatArgs = [];
176
203
  const paletteHex = this._palette[consoleMethod];
177
204
  for (const arg of args) {
178
205
  if (TypeValidator.isBrowserColorizedArg(arg)) {
179
206
  formatParts.push(`%c${arg.text}`);
180
- cssArgs.push(`color:${arg.hex}`);
207
+ formatArgs.push(`color:${arg.hex}`);
208
+ continue;
209
+ }
210
+ if (TypeValidator.isBrowserCssArg(arg)) {
211
+ formatParts.push(`%c${arg.text}`);
212
+ formatArgs.push(arg.css);
181
213
  continue;
182
214
  }
183
215
  if (TypeValidator.isBrowserObjectArg(arg)) {
184
216
  formatParts.push("%o");
185
- otherArgs.push(arg.value);
217
+ formatArgs.push(arg.value);
186
218
  continue;
187
219
  }
188
220
  if (TypeValidator.isString(arg)) {
189
221
  formatParts.push(`%c${arg}`);
190
- cssArgs.push(`color:${paletteHex}`);
222
+ formatArgs.push(`color:${paletteHex}`);
191
223
  continue;
192
224
  }
193
225
  formatParts.push("%o");
194
- otherArgs.push(arg);
226
+ formatArgs.push(arg);
195
227
  }
196
228
  if (formatParts.length === 0) return args;
197
- return [formatParts.join(""), ...cssArgs, ...otherArgs];
229
+ return [formatParts.join(""), ...formatArgs];
198
230
  }
199
- output(consoleMethod, args) {
231
+ _output(consoleMethod, args) {
200
232
  console[consoleMethod](...args);
201
233
  }
202
- processArgs(args) {
234
+ _processArgs(args) {
203
235
  const processedArgs = [];
204
236
  let previousWasObject = false;
205
237
  for (const arg of args) {
206
- if (TypeValidator.isBrowserColorizedArg(arg)) {
238
+ if (TypeValidator.isBrowserColorizedArg(arg) || TypeValidator.isBrowserCssArg(arg)) {
207
239
  processedArgs.push(arg);
208
240
  previousWasObject = false;
209
241
  continue;
@@ -894,9 +926,11 @@ ${arg}` : arg
894
926
  }
895
927
  const colorino = createColorino();
896
928
 
897
- exports.colorino = colorino;
898
- exports.createColorino = createColorino;
899
- exports.themePalettes = themePalettes;
929
+ const colorinoUmdGlobal = colorino;
930
+ colorinoUmdGlobal.createColorino = createColorino;
931
+ colorinoUmdGlobal.themePalettes = themePalettes;
932
+
933
+ return colorinoUmdGlobal;
900
934
 
901
935
  }));
902
936
  //# sourceMappingURL=cdn.js.map