@orkestrel/console 0.0.6 → 0.0.8

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,3 +1,4 @@
1
+ import { Attribute } from '@orkestrel/console';
1
2
  import { Color } from '@orkestrel/console';
2
3
  import { SinkInterface } from '@orkestrel/console';
3
4
 
@@ -19,10 +20,14 @@ import { SinkInterface } from '@orkestrel/console';
19
20
  * count always equals `styles.length`, and `console.log(format, ...styles)` lines up exactly.
20
21
  * - **Plain text short-circuits.** A string with NO SGR sequence yields `{ format: <escaped text>,
21
22
  * styles: [] }` — no `%c`, no styles (the text is still `%`-escaped).
23
+ * - **Partial palette.** A supplied palette overrides only its named colors and attributes. Every
24
+ * omitted entry resolves through {@link COLOR_HEX} or {@link ATTRIBUTE_CSS}, so defaults and
25
+ * unrelated entries stay byte-identical.
22
26
  * - **Pure + total.** Same input → same output; it never throws on any string (adversarial escapes,
23
27
  * lone `ESC`, unterminated sequences all fall through as literal text).
24
28
  *
25
29
  * @param text - Any string, ANSI-styled or plain
30
+ * @param palette - Optional partial browser CSS overrides
26
31
  * @returns The `%c` format string + parallel CSS array ({@link ConsoleOutput})
27
32
  *
28
33
  * @example
@@ -32,7 +37,7 @@ import { SinkInterface } from '@orkestrel/console';
32
37
  * ansiToConsole('50%') // { format: '50%%', styles: [] }
33
38
  * ```
34
39
  */
35
- export declare function ansiToConsole(text: string): ConsoleOutput;
40
+ export declare function ansiToConsole(text: string, palette?: BrowserPalette): ConsoleOutput;
36
41
 
37
42
  /**
38
43
  * Each text-{@link Attribute}'s SGR "on" number → its equivalent CSS declaration — the browser
@@ -50,15 +55,28 @@ export declare function ansiToConsole(text: string): ConsoleOutput;
50
55
  export declare const ATTRIBUTE_CSS: Readonly<Record<number, string>>;
51
56
 
52
57
  /**
53
- * Each SGR BACKGROUND parameter (40–47 / 100–107) its `background:<hex>` CSS. The sink reads this
54
- * while scanning a run to translate a background code to CSS.
58
+ * Partial browser CSS overrides for the core color and attribute axes. Omitted entries retain the
59
+ * built-in browser mappings, so one override changes only its named value.
55
60
  *
56
61
  * @remarks
57
- * Built the same way as {@link FOREGROUND_CSS} keys from core's {@link BACKGROUND_CODES}, values
58
- * from {@link COLOR_HEX} — and covered by the same exhaustive walk over core's {@link COLORS} in
59
- * `tests/src/browser/helpers.test.ts`. Deeply frozen.
62
+ * - `color` maps a named non-default {@link Color} to the CSS color value used for both foreground
63
+ * and background SGR codes.
64
+ * - `attribute` maps an {@link Attribute} to the CSS declaration used for its SGR code.
60
65
  */
61
- export declare const BACKGROUND_CSS: Readonly<Record<number, string>>;
66
+ export declare interface BrowserPalette {
67
+ readonly color?: Readonly<Partial<Record<Exclude<Color, 'default'>, string>>>;
68
+ readonly attribute?: Readonly<Partial<Record<Attribute, string>>>;
69
+ }
70
+
71
+ /**
72
+ * Options for {@link import('./factories.js').createBrowserSink}.
73
+ *
74
+ * @remarks
75
+ * `palette` partially overrides the browser's default color and attribute CSS mappings.
76
+ */
77
+ export declare interface BrowserSinkOptions {
78
+ readonly palette?: BrowserPalette;
79
+ }
62
80
 
63
81
  /**
64
82
  * Each named {@link Color}'s hex value — the 16 standard terminal colors a browser DevTools
@@ -100,12 +118,14 @@ export declare interface ConsoleOutput {
100
118
  * as a logger / reporter / spinner sink (`createLogger({ sink: createBrowserSink() })`) to retarget the
101
119
  * core output to the browser console with no change to the core.
102
120
  *
121
+ * @param options - See {@link BrowserSinkOptions}
103
122
  * @returns A browser `%c` {@link SinkInterface}
104
123
  *
105
124
  * @remarks
106
125
  * - **ANSI → `%c` at the sink.** The core produces ANSI strings; this sink parses the SGR runs and
107
126
  * re-emits them as a `console.log`-ready `%c` format string + parallel CSS array ({@link ansiToConsole}
108
127
  * — pure, total, and `%`-safe), so the styling survives the trip to a console that can't render ANSI.
128
+ * `options.palette` supplies partial named color and attribute overrides to that translation.
109
129
  * - **Routes by level.** `error` → `console.error`, `warn` → `console.warn`, every other level (and an
110
130
  * omitted level) → `console.log` — the SAME routing as core's `createConsoleSink`, so a logger's level
111
131
  * reaches the matching DevTools stream.
@@ -127,7 +147,7 @@ export declare interface ConsoleOutput {
127
147
  * logger.error('boom') // → console.error('%c…', 'color:#cd0000;…') in DevTools
128
148
  * ```
129
149
  */
130
- export declare function createBrowserSink(): SinkInterface;
150
+ export declare function createBrowserSink(options?: BrowserSinkOptions): SinkInterface;
131
151
 
132
152
  /**
133
153
  * The browser console directive that switches the active style — one `%c` prefixes every styled run
@@ -152,19 +172,6 @@ export declare const DIRECTIVE = "%c";
152
172
  */
153
173
  export declare function escapePercent(text: string): string;
154
174
 
155
- /**
156
- * Each SGR FOREGROUND parameter (30–37 / 90–97) → its `color:<hex>` CSS. The sink reads this while
157
- * scanning a run to translate a foreground code to CSS.
158
- *
159
- * @remarks
160
- * Every key is core's {@link FOREGROUND_CODES} entry and every value reads {@link COLOR_HEX}, so
161
- * neither the number↔name mapping nor the palette is duplicated here — this is a table of
162
- * references, not of literals. `tests/src/browser/helpers.test.ts` walks core's {@link COLORS} and
163
- * asserts this record covers every one of them, so a color added to core fails there rather than
164
- * going silently untranslated. Deeply frozen.
165
- */
166
- export declare const FOREGROUND_CSS: Readonly<Record<number, string>>;
167
-
168
175
  /**
169
176
  * Parse an SGR parameter list (the `;`-separated numeric string captured by {@link SGR_PATTERN})
170
177
  * into its numeric codes — `'1;31'` → `[1, 31]`. An EMPTY list (a bare `ESC[m`) yields `[0]`, since
@@ -1,4 +1,4 @@
1
- import { ATTRIBUTE_CODES, BACKGROUND_CODES, ESC, FOREGROUND_CODES, RESET_CODE } from "../core/index.js";
1
+ import { ATTRIBUTES, ATTRIBUTE_CODES, BACKGROUND_CODES, COLORS, ESC, FOREGROUND_CODES, RESET_CODE } from "../core/index.js";
2
2
  //#region src/browser/constants.ts
3
3
  /**
4
4
  * Each named {@link Color}'s hex value — the 16 standard terminal colors a browser DevTools
@@ -50,62 +50,6 @@ var ATTRIBUTE_CSS = Object.freeze({
50
50
  [ATTRIBUTE_CODES.strikethrough]: "text-decoration:line-through"
51
51
  });
52
52
  /**
53
- * Each SGR FOREGROUND parameter (30–37 / 90–97) → its `color:<hex>` CSS. The sink reads this while
54
- * scanning a run to translate a foreground code to CSS.
55
- *
56
- * @remarks
57
- * Every key is core's {@link FOREGROUND_CODES} entry and every value reads {@link COLOR_HEX}, so
58
- * neither the number↔name mapping nor the palette is duplicated here — this is a table of
59
- * references, not of literals. `tests/src/browser/helpers.test.ts` walks core's {@link COLORS} and
60
- * asserts this record covers every one of them, so a color added to core fails there rather than
61
- * going silently untranslated. Deeply frozen.
62
- */
63
- var FOREGROUND_CSS = Object.freeze({
64
- [FOREGROUND_CODES.black]: `color:${COLOR_HEX.black}`,
65
- [FOREGROUND_CODES.red]: `color:${COLOR_HEX.red}`,
66
- [FOREGROUND_CODES.green]: `color:${COLOR_HEX.green}`,
67
- [FOREGROUND_CODES.yellow]: `color:${COLOR_HEX.yellow}`,
68
- [FOREGROUND_CODES.blue]: `color:${COLOR_HEX.blue}`,
69
- [FOREGROUND_CODES.magenta]: `color:${COLOR_HEX.magenta}`,
70
- [FOREGROUND_CODES.cyan]: `color:${COLOR_HEX.cyan}`,
71
- [FOREGROUND_CODES.white]: `color:${COLOR_HEX.white}`,
72
- [FOREGROUND_CODES.brightBlack]: `color:${COLOR_HEX.brightBlack}`,
73
- [FOREGROUND_CODES.brightRed]: `color:${COLOR_HEX.brightRed}`,
74
- [FOREGROUND_CODES.brightGreen]: `color:${COLOR_HEX.brightGreen}`,
75
- [FOREGROUND_CODES.brightYellow]: `color:${COLOR_HEX.brightYellow}`,
76
- [FOREGROUND_CODES.brightBlue]: `color:${COLOR_HEX.brightBlue}`,
77
- [FOREGROUND_CODES.brightMagenta]: `color:${COLOR_HEX.brightMagenta}`,
78
- [FOREGROUND_CODES.brightCyan]: `color:${COLOR_HEX.brightCyan}`,
79
- [FOREGROUND_CODES.brightWhite]: `color:${COLOR_HEX.brightWhite}`
80
- });
81
- /**
82
- * Each SGR BACKGROUND parameter (40–47 / 100–107) → its `background:<hex>` CSS. The sink reads this
83
- * while scanning a run to translate a background code to CSS.
84
- *
85
- * @remarks
86
- * Built the same way as {@link FOREGROUND_CSS} — keys from core's {@link BACKGROUND_CODES}, values
87
- * from {@link COLOR_HEX} — and covered by the same exhaustive walk over core's {@link COLORS} in
88
- * `tests/src/browser/helpers.test.ts`. Deeply frozen.
89
- */
90
- var BACKGROUND_CSS = Object.freeze({
91
- [BACKGROUND_CODES.black]: `background:${COLOR_HEX.black}`,
92
- [BACKGROUND_CODES.red]: `background:${COLOR_HEX.red}`,
93
- [BACKGROUND_CODES.green]: `background:${COLOR_HEX.green}`,
94
- [BACKGROUND_CODES.yellow]: `background:${COLOR_HEX.yellow}`,
95
- [BACKGROUND_CODES.blue]: `background:${COLOR_HEX.blue}`,
96
- [BACKGROUND_CODES.magenta]: `background:${COLOR_HEX.magenta}`,
97
- [BACKGROUND_CODES.cyan]: `background:${COLOR_HEX.cyan}`,
98
- [BACKGROUND_CODES.white]: `background:${COLOR_HEX.white}`,
99
- [BACKGROUND_CODES.brightBlack]: `background:${COLOR_HEX.brightBlack}`,
100
- [BACKGROUND_CODES.brightRed]: `background:${COLOR_HEX.brightRed}`,
101
- [BACKGROUND_CODES.brightGreen]: `background:${COLOR_HEX.brightGreen}`,
102
- [BACKGROUND_CODES.brightYellow]: `background:${COLOR_HEX.brightYellow}`,
103
- [BACKGROUND_CODES.brightBlue]: `background:${COLOR_HEX.brightBlue}`,
104
- [BACKGROUND_CODES.brightMagenta]: `background:${COLOR_HEX.brightMagenta}`,
105
- [BACKGROUND_CODES.brightCyan]: `background:${COLOR_HEX.brightCyan}`,
106
- [BACKGROUND_CODES.brightWhite]: `background:${COLOR_HEX.brightWhite}`
107
- });
108
- /**
109
53
  * The browser console directive that switches the active style — one `%c` prefixes every styled run
110
54
  * in the {@link import('./types.js').ConsoleOutput} format string, consuming the next entry of the
111
55
  * parallel CSS array. The single source of truth for the directive token.
@@ -145,10 +89,14 @@ var SGR_PATTERN = new RegExp(`${ESC}\\[([0-9;]*)m`, "g");
145
89
  * count always equals `styles.length`, and `console.log(format, ...styles)` lines up exactly.
146
90
  * - **Plain text short-circuits.** A string with NO SGR sequence yields `{ format: <escaped text>,
147
91
  * styles: [] }` — no `%c`, no styles (the text is still `%`-escaped).
92
+ * - **Partial palette.** A supplied palette overrides only its named colors and attributes. Every
93
+ * omitted entry resolves through {@link COLOR_HEX} or {@link ATTRIBUTE_CSS}, so defaults and
94
+ * unrelated entries stay byte-identical.
148
95
  * - **Pure + total.** Same input → same output; it never throws on any string (adversarial escapes,
149
96
  * lone `ESC`, unterminated sequences all fall through as literal text).
150
97
  *
151
98
  * @param text - Any string, ANSI-styled or plain
99
+ * @param palette - Optional partial browser CSS overrides
152
100
  * @returns The `%c` format string + parallel CSS array ({@link ConsoleOutput})
153
101
  *
154
102
  * @example
@@ -158,7 +106,7 @@ var SGR_PATTERN = new RegExp(`${ESC}\\[([0-9;]*)m`, "g");
158
106
  * ansiToConsole('50%') // { format: '50%%', styles: [] }
159
107
  * ```
160
108
  */
161
- function ansiToConsole(text) {
109
+ function ansiToConsole(text, palette) {
162
110
  const scanner = new RegExp(SGR_PATTERN.source, SGR_PATTERN.flags);
163
111
  let active = Object.freeze({
164
112
  foreground: "",
@@ -195,23 +143,26 @@ function ansiToConsole(text) {
195
143
  });
196
144
  continue;
197
145
  }
198
- const foreground = FOREGROUND_CSS[code];
146
+ const foreground = COLORS.find((color) => FOREGROUND_CODES[color] === code);
199
147
  if (foreground !== void 0) {
148
+ const color = palette?.color?.[foreground] ?? COLOR_HEX[foreground];
200
149
  active = Object.freeze({
201
150
  ...active,
202
- foreground
151
+ foreground: `color:${color}`
203
152
  });
204
153
  continue;
205
154
  }
206
- const background = BACKGROUND_CSS[code];
155
+ const background = COLORS.find((color) => BACKGROUND_CODES[color] === code);
207
156
  if (background !== void 0) {
157
+ const color = palette?.color?.[background] ?? COLOR_HEX[background];
208
158
  active = Object.freeze({
209
159
  ...active,
210
- background
160
+ background: `background:${color}`
211
161
  });
212
162
  continue;
213
163
  }
214
- const attribute = ATTRIBUTE_CSS[code];
164
+ const name = ATTRIBUTES.find((attribute) => ATTRIBUTE_CODES[attribute] === code);
165
+ const attribute = name === void 0 ? void 0 : palette?.attribute?.[name] ?? ATTRIBUTE_CSS[code];
215
166
  if (attribute !== void 0 && !active.attributes.includes(attribute)) active = Object.freeze({
216
167
  ...active,
217
168
  attributes: Object.freeze([...active.attributes, attribute])
@@ -270,12 +221,14 @@ function parseParameters(parameters) {
270
221
  * as a logger / reporter / spinner sink (`createLogger({ sink: createBrowserSink() })`) to retarget the
271
222
  * core output to the browser console with no change to the core.
272
223
  *
224
+ * @param options - See {@link BrowserSinkOptions}
273
225
  * @returns A browser `%c` {@link SinkInterface}
274
226
  *
275
227
  * @remarks
276
228
  * - **ANSI → `%c` at the sink.** The core produces ANSI strings; this sink parses the SGR runs and
277
229
  * re-emits them as a `console.log`-ready `%c` format string + parallel CSS array ({@link ansiToConsole}
278
230
  * — pure, total, and `%`-safe), so the styling survives the trip to a console that can't render ANSI.
231
+ * `options.palette` supplies partial named color and attribute overrides to that translation.
279
232
  * - **Routes by level.** `error` → `console.error`, `warn` → `console.warn`, every other level (and an
280
233
  * omitted level) → `console.log` — the SAME routing as core's `createConsoleSink`, so a logger's level
281
234
  * reaches the matching DevTools stream.
@@ -297,12 +250,12 @@ function parseParameters(parameters) {
297
250
  * logger.error('boom') // → console.error('%c…', 'color:#cd0000;…') in DevTools
298
251
  * ```
299
252
  */
300
- function createBrowserSink() {
253
+ function createBrowserSink(options) {
301
254
  const log = console.log.bind(console);
302
255
  const warn = console.warn.bind(console);
303
256
  const error = console.error.bind(console);
304
257
  return { write(text, level) {
305
- const { format, styles } = ansiToConsole(text.startsWith("\r") ? text.slice(1) : text);
258
+ const { format, styles } = ansiToConsole(text.startsWith("\r") ? text.slice(1) : text, options?.palette);
306
259
  if (level === "error") {
307
260
  error(format, ...styles);
308
261
  return;
@@ -315,6 +268,6 @@ function createBrowserSink() {
315
268
  } };
316
269
  }
317
270
  //#endregion
318
- export { ATTRIBUTE_CSS, BACKGROUND_CSS, COLOR_HEX, DIRECTIVE, FOREGROUND_CSS, SGR_PATTERN, ansiToConsole, createBrowserSink, escapePercent, parseParameters };
271
+ export { ATTRIBUTE_CSS, COLOR_HEX, DIRECTIVE, SGR_PATTERN, ansiToConsole, createBrowserSink, escapePercent, parseParameters };
319
272
 
320
273
  //# sourceMappingURL=index.js.map