@logtape/pretty 1.0.0-dev.231

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.
@@ -0,0 +1,488 @@
1
+ import { TruncationStrategy } from "./truncate.js";
2
+ import { LogLevel, TextFormatter, TextFormatterOptions } from "@logtape/logtape";
3
+
4
+ //#region formatter.d.ts
5
+
6
+ /**
7
+ * ANSI style codes
8
+ */
9
+ declare const styles: {
10
+ readonly reset: "\u001B[0m";
11
+ readonly bold: "\u001B[1m";
12
+ readonly dim: "\u001B[2m";
13
+ readonly italic: "\u001B[3m";
14
+ readonly underline: "\u001B[4m";
15
+ readonly strikethrough: "\u001B[9m";
16
+ };
17
+ /**
18
+ * Standard ANSI colors (16-color)
19
+ */
20
+ declare const ansiColors: {
21
+ readonly black: "\u001B[30m";
22
+ readonly red: "\u001B[31m";
23
+ readonly green: "\u001B[32m";
24
+ readonly yellow: "\u001B[33m";
25
+ readonly blue: "\u001B[34m";
26
+ readonly magenta: "\u001B[35m";
27
+ readonly cyan: "\u001B[36m";
28
+ readonly white: "\u001B[37m";
29
+ };
30
+ /**
31
+ * Color type definition
32
+ */
33
+ type Color = keyof typeof ansiColors | `rgb(${number},${number},${number})` | `#${string}` | null;
34
+ /**
35
+ * Category color mapping for prefix-based coloring.
36
+ *
37
+ * Maps category prefixes (as arrays) to colors. The formatter will match
38
+ * categories against these prefixes and use the corresponding color.
39
+ * Longer/more specific prefixes take precedence over shorter ones.
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * new Map([
44
+ * [["app", "auth"], "#ff6b6b"], // app.auth.* -> red
45
+ * [["app", "db"], "#4ecdc4"], // app.db.* -> teal
46
+ * [["app"], "#45b7d1"], // app.* (fallback) -> blue
47
+ * [["lib"], "#96ceb4"], // lib.* -> green
48
+ * ])
49
+ * ```
50
+ */
51
+ type CategoryColorMap = Map<readonly string[], Color>;
52
+ /**
53
+ * Style type definition - supports single styles, arrays of styles, or null
54
+ */
55
+ type Style = keyof typeof styles | (keyof typeof styles)[] | null;
56
+ /**
57
+ * Configuration options for the pretty formatter.
58
+ *
59
+ * This interface extends the base text formatter options while providing
60
+ * extensive customization options for visual styling, layout control, and
61
+ * development-focused features. It offers granular control over colors,
62
+ * styles, and formatting similar to the ANSI color formatter.
63
+ *
64
+ * @since 1.0.0
65
+ */
66
+ interface PrettyFormatterOptions extends Omit<TextFormatterOptions, "category" | "value" | "format"> {
67
+ /**
68
+ * Color for timestamp display when timestamps are enabled.
69
+ *
70
+ * Supports true color RGB values, hex colors, or ANSI color names.
71
+ * Set to `null` to disable timestamp coloring.
72
+ *
73
+ * @example
74
+ * ```typescript
75
+ * timestampColor: "#888888" // Hex color
76
+ * timestampColor: "rgb(128,128,128)" // RGB color
77
+ * timestampColor: "cyan" // ANSI color name
78
+ * timestampColor: null // No color
79
+ * ```
80
+ *
81
+ * @default `"rgb(100,116,139)"` (slate gray)
82
+ */
83
+ timestampColor?: Color;
84
+ /**
85
+ * Visual style applied to timestamp text.
86
+ *
87
+ * Controls text appearance like boldness, dimming, etc.
88
+ * Supports single styles, multiple styles combined, or no styling.
89
+ * Combines with `timestampColor` for full styling control.
90
+ *
91
+ * @example
92
+ * ```typescript
93
+ * timestampStyle: "dim" // Single style: dimmed text
94
+ * timestampStyle: "bold" // Single style: bold text
95
+ * timestampStyle: ["bold", "underline"] // Multiple styles: bold + underlined
96
+ * timestampStyle: ["dim", "italic"] // Multiple styles: dimmed + italic
97
+ * timestampStyle: null // No styling
98
+ * ```
99
+ *
100
+ * @default `"dim"`
101
+ */
102
+ timestampStyle?: Style;
103
+ /**
104
+ * Custom colors for each log level.
105
+ *
106
+ * Allows fine-grained control over level appearance. Each level can have
107
+ * its own color scheme. Unspecified levels use built-in defaults.
108
+ * Set individual levels to `null` to disable coloring for that level.
109
+ *
110
+ * @example
111
+ * ```typescript
112
+ * levelColors: {
113
+ * info: "#00ff00", // Bright green for info
114
+ * error: "#ff0000", // Bright red for errors
115
+ * warning: "orange", // ANSI orange for warnings
116
+ * debug: null, // No color for debug
117
+ * }
118
+ * ```
119
+ *
120
+ * @default Built-in color scheme (purple trace, blue debug, green info, amber warning, red error, dark red fatal)
121
+ */
122
+ levelColors?: Partial<Record<LogLevel, Color>>;
123
+ /**
124
+ * Visual style applied to log level text.
125
+ *
126
+ * Controls the appearance of the level indicator (e.g., "info", "error").
127
+ * Supports single styles, multiple styles combined, or no styling.
128
+ * Applied in addition to level-specific colors.
129
+ *
130
+ * @example
131
+ * ```typescript
132
+ * levelStyle: "bold" // Single style: bold level text
133
+ * levelStyle: "underline" // Single style: underlined level text
134
+ * levelStyle: ["bold", "underline"] // Multiple styles: bold + underlined
135
+ * levelStyle: ["dim", "italic"] // Multiple styles: dimmed + italic
136
+ * levelStyle: null // No additional styling
137
+ * ```
138
+ *
139
+ * @default `null` (no additional styling)
140
+ */
141
+ levelStyle?: Style;
142
+ /**
143
+ * Icon configuration for each log level.
144
+ *
145
+ * Controls the emoji/symbol displayed before each log entry.
146
+ * Provides visual quick-identification of log severity.
147
+ *
148
+ * - `true`: Use built-in emoji set (🔍 trace, 🐛 debug, ✨ info, ⚠️ warning, ❌ error, 💀 fatal)
149
+ * - `false`: Disable all icons for clean text-only output
150
+ * - Object: Custom icon mapping, falls back to defaults for unspecified levels
151
+ *
152
+ * @example
153
+ * ```typescript
154
+ * icons: true // Use default emoji set
155
+ * icons: false // No icons
156
+ * icons: {
157
+ * info: "ℹ️", // Custom info icon
158
+ * error: "🔥", // Custom error icon
159
+ * warning: "⚡", // Custom warning icon
160
+ * }
161
+ * ```
162
+ *
163
+ * @default `true` (use default emoji icons)
164
+ */
165
+ icons?: boolean | Partial<Record<LogLevel, string>>;
166
+ /**
167
+ * Character(s) used to separate category hierarchy levels.
168
+ *
169
+ * Categories are hierarchical (e.g., ["app", "auth", "jwt"]) and this
170
+ * separator joins them for display (e.g., "app.auth.jwt").
171
+ *
172
+ * @example
173
+ * ```typescript
174
+ * categorySeparator: "·" // app·auth·jwt
175
+ * categorySeparator: "." // app.auth.jwt
176
+ * categorySeparator: ":" // app:auth:jwt
177
+ * categorySeparator: " > " // app > auth > jwt
178
+ * categorySeparator: "::" // app::auth::jwt
179
+ * ```
180
+ *
181
+ * @default `"·"` (interpunct)
182
+ */
183
+ categorySeparator?: string;
184
+ /**
185
+ * Default color for category display.
186
+ *
187
+ * Used as fallback when no specific color is found in `categoryColorMap`.
188
+ * Controls the visual appearance of the category hierarchy display.
189
+ *
190
+ * @example
191
+ * ```typescript
192
+ * categoryColor: "#666666" // Gray categories
193
+ * categoryColor: "blue" // Blue categories
194
+ * categoryColor: "rgb(100,150,200)" // Light blue categories
195
+ * categoryColor: null // No coloring
196
+ * ```
197
+ *
198
+ * @default `"rgb(100,116,139)"` (slate gray)
199
+ */
200
+ categoryColor?: Color;
201
+ /**
202
+ * Category-specific color mapping based on prefixes.
203
+ *
204
+ * Maps category prefixes (as arrays) to colors for visual grouping.
205
+ * More specific (longer) prefixes take precedence over shorter ones.
206
+ * If no prefix matches, falls back to the default `categoryColor`.
207
+ *
208
+ * @example
209
+ * ```typescript
210
+ * new Map([
211
+ * [["app", "auth"], "#ff6b6b"], // app.auth.* -> red
212
+ * [["app", "db"], "#4ecdc4"], // app.db.* -> teal
213
+ * [["app"], "#45b7d1"], // app.* (fallback) -> blue
214
+ * [["lib"], "#96ceb4"], // lib.* -> green
215
+ * ])
216
+ * ```
217
+ */
218
+ categoryColorMap?: CategoryColorMap;
219
+ /**
220
+ * Visual style applied to category text.
221
+ *
222
+ * Controls the appearance of the category hierarchy display.
223
+ * Supports single styles, multiple styles combined, or no styling.
224
+ * Applied in addition to category colors from `categoryColor` or `categoryColorMap`.
225
+ *
226
+ * @example
227
+ * ```typescript
228
+ * categoryStyle: "dim" // Single style: dimmed category text
229
+ * categoryStyle: "italic" // Single style: italic category text
230
+ * categoryStyle: ["dim", "italic"] // Multiple styles: dimmed + italic
231
+ * categoryStyle: ["bold", "underline"] // Multiple styles: bold + underlined
232
+ * categoryStyle: null // No additional styling
233
+ * ```
234
+ *
235
+ * @default `["dim", "italic"]` (dimmed for subtle appearance)
236
+ */
237
+ categoryStyle?: Style;
238
+ /**
239
+ * Maximum display width for category names.
240
+ *
241
+ * Controls layout consistency by limiting category width.
242
+ * Long categories are truncated according to `categoryTruncate` strategy.
243
+ *
244
+ * - Number: Fixed character width limit
245
+ * - `"auto"`: No width limit, categories display at full length
246
+ *
247
+ * @example
248
+ * ```typescript
249
+ * categoryWidth: 20 // Limit to 20 characters
250
+ * categoryWidth: 30 // Limit to 30 characters
251
+ * categoryWidth: "auto" // No limit
252
+ * ```
253
+ *
254
+ * @default `20` (20 character limit)
255
+ */
256
+ categoryWidth?: number | "auto";
257
+ /**
258
+ * Strategy for truncating long category names.
259
+ *
260
+ * When categories exceed `categoryWidth`, this controls how truncation works.
261
+ * Smart truncation preserves important context while maintaining layout.
262
+ *
263
+ * - `"middle"`: Keep first and last parts (e.g., "app.server…auth.jwt")
264
+ * - `"end"`: Truncate at the end (e.g., "app.server.middleware…")
265
+ * - `false`: No truncation (ignores `categoryWidth`)
266
+ *
267
+ * @example
268
+ * ```typescript
269
+ * categoryTruncate: "middle" // app.server…jwt (preserves context)
270
+ * categoryTruncate: "end" // app.server.midd… (linear truncation)
271
+ * categoryTruncate: false // app.server.middleware.auth.jwt (full)
272
+ * ```
273
+ *
274
+ * @default `"middle"` (smart context-preserving truncation)
275
+ */
276
+ categoryTruncate?: TruncationStrategy;
277
+ /**
278
+ * Color for log message text content.
279
+ *
280
+ * Controls the visual appearance of the actual log message content.
281
+ * Does not affect structured values, which use syntax highlighting.
282
+ *
283
+ * @example
284
+ * ```typescript
285
+ * messageColor: "#ffffff" // White message text
286
+ * messageColor: "green" // Green message text
287
+ * messageColor: "rgb(200,200,200)" // Light gray message text
288
+ * messageColor: null // No coloring
289
+ * ```
290
+ *
291
+ * @default `"rgb(148,163,184)"` (light slate gray)
292
+ */
293
+ messageColor?: Color;
294
+ /**
295
+ * Visual style applied to log message text.
296
+ *
297
+ * Controls the appearance of the log message content.
298
+ * Supports single styles, multiple styles combined, or no styling.
299
+ * Applied in addition to `messageColor`.
300
+ *
301
+ * @example
302
+ * ```typescript
303
+ * messageStyle: "dim" // Single style: dimmed message text
304
+ * messageStyle: "italic" // Single style: italic message text
305
+ * messageStyle: ["dim", "italic"] // Multiple styles: dimmed + italic
306
+ * messageStyle: ["bold", "underline"] // Multiple styles: bold + underlined
307
+ * messageStyle: null // No additional styling
308
+ * ```
309
+ *
310
+ * @default `"dim"` (dimmed for subtle readability)
311
+ */
312
+ messageStyle?: Style;
313
+ /**
314
+ * Global color control for the entire formatter.
315
+ *
316
+ * Master switch to enable/disable all color output.
317
+ * When disabled, produces clean monochrome output suitable for
318
+ * non-color terminals or when colors are not desired.
319
+ *
320
+ * @example
321
+ * ```typescript
322
+ * colors: true // Full color output (default)
323
+ * colors: false // Monochrome output only
324
+ * ```
325
+ *
326
+ * @default `true` (colors enabled)
327
+ */
328
+ colors?: boolean;
329
+ /**
330
+ * Column alignment for consistent visual layout.
331
+ *
332
+ * When enabled, ensures all log components (icons, levels, categories)
333
+ * align consistently across multiple log entries, creating a clean
334
+ * tabular appearance.
335
+ *
336
+ * @example
337
+ * ```typescript
338
+ * align: true // Aligned columns (default)
339
+ * align: false // Compact, non-aligned output
340
+ * ```
341
+ *
342
+ * @default `true` (alignment enabled)
343
+ */
344
+ align?: boolean;
345
+ /**
346
+ * Configuration for structured value inspection and rendering.
347
+ *
348
+ * Controls how objects, arrays, and other complex values are displayed
349
+ * within log messages. Uses Node.js `util.inspect()` style options.
350
+ *
351
+ * @example
352
+ * ```typescript
353
+ * inspectOptions: {
354
+ * depth: 3, // Show 3 levels of nesting
355
+ * colors: false, // Disable value syntax highlighting
356
+ * compact: true, // Use compact object display
357
+ * }
358
+ * ```
359
+ *
360
+ * @default `{}` (use built-in defaults: depth=unlimited, colors=auto, compact=true)
361
+ */
362
+ inspectOptions?: {
363
+ /**
364
+ * Maximum depth to traverse when inspecting nested objects.
365
+ * @default Infinity (no depth limit)
366
+ */
367
+ depth?: number;
368
+ /**
369
+ * Whether to use syntax highlighting colors for inspected values.
370
+ * @default Inherited from global `colors` setting
371
+ */
372
+ colors?: boolean;
373
+ /**
374
+ * Whether to use compact formatting for objects and arrays.
375
+ * @default `true` (compact formatting)
376
+ */
377
+ compact?: boolean;
378
+ };
379
+ /**
380
+ * Enable word wrapping for long messages.
381
+ *
382
+ * When enabled, long messages will be wrapped at the specified width,
383
+ * with continuation lines aligned to the message column position.
384
+ *
385
+ * - `true`: Auto-detect terminal width when attached to a terminal,
386
+ * fallback to 80 columns when not in a terminal or detection fails
387
+ * - `number`: Use the specified width in columns
388
+ * - `false`: Disable word wrapping
389
+ *
390
+ * @example
391
+ * ```typescript
392
+ * // Auto-detect terminal width (recommended)
393
+ * wordWrap: true
394
+ *
395
+ * // Custom wrap width
396
+ * wordWrap: 120
397
+ *
398
+ * // Disable word wrapping (default)
399
+ * wordWrap: false
400
+ * ```
401
+ *
402
+ * @default `false` (no word wrapping)
403
+ * @since 1.0.0
404
+ */
405
+ wordWrap?: boolean | number;
406
+ }
407
+ /**
408
+ * Creates a beautiful console formatter optimized for local development.
409
+ *
410
+ * This formatter provides a Signale-inspired visual design with colorful icons,
411
+ * smart category truncation, dimmed styling, and perfect column alignment.
412
+ * It's specifically designed for development environments that support true colors
413
+ * and Unicode characters.
414
+ *
415
+ * The formatter features:
416
+ * - Emoji icons for each log level (🔍 trace, 🐛 debug, ✨ info, etc.)
417
+ * - True color support with rich color schemes
418
+ * - Intelligent category truncation for long hierarchical categories
419
+ * - Optional timestamp display with multiple formats
420
+ * - Configurable alignment and styling options
421
+ * - Enhanced value rendering with syntax highlighting
422
+ *
423
+ * @param options Configuration options for customizing the formatter behavior.
424
+ * @returns A text formatter function that can be used with LogTape sinks.
425
+ *
426
+ * @example
427
+ * ```typescript
428
+ * import { configure } from "@logtape/logtape";
429
+ * import { getConsoleSink } from "@logtape/logtape";
430
+ * import { getPrettyFormatter } from "@logtape/pretty";
431
+ *
432
+ * await configure({
433
+ * sinks: {
434
+ * console: getConsoleSink({
435
+ * formatter: getPrettyFormatter({
436
+ * timestamp: "time",
437
+ * categoryWidth: 25,
438
+ * icons: {
439
+ * info: "📘",
440
+ * error: "🔥"
441
+ * }
442
+ * })
443
+ * })
444
+ * }
445
+ * });
446
+ * ```
447
+ *
448
+ * @since 1.0.0
449
+ */
450
+ declare function getPrettyFormatter(options?: PrettyFormatterOptions): TextFormatter;
451
+ /**
452
+ * A pre-configured beautiful console formatter for local development.
453
+ *
454
+ * This is a ready-to-use instance of the pretty formatter with sensible defaults
455
+ * for most development scenarios. It provides immediate visual enhancement to
456
+ * your logs without requiring any configuration.
457
+ *
458
+ * Features enabled by default:
459
+ * - Emoji icons for all log levels
460
+ * - True color support with rich color schemes
461
+ * - Dimmed text styling for better readability
462
+ * - Smart category truncation (20 characters max)
463
+ * - Perfect column alignment
464
+ * - No timestamp display (cleaner for development)
465
+ *
466
+ * For custom configuration, use {@link getPrettyFormatter} instead.
467
+ *
468
+ * @example
469
+ * ```typescript
470
+ * import { configure } from "@logtape/logtape";
471
+ * import { getConsoleSink } from "@logtape/logtape";
472
+ * import { prettyFormatter } from "@logtape/pretty";
473
+ *
474
+ * await configure({
475
+ * sinks: {
476
+ * console: getConsoleSink({
477
+ * formatter: prettyFormatter
478
+ * })
479
+ * }
480
+ * });
481
+ * ```
482
+ *
483
+ * @since 1.0.0
484
+ */
485
+ declare const prettyFormatter: TextFormatter;
486
+ //#endregion
487
+ export { PrettyFormatterOptions, getPrettyFormatter, prettyFormatter };
488
+ //# sourceMappingURL=formatter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formatter.d.ts","names":[],"sources":["../formatter.ts"],"sourcesContent":[],"mappings":";;;;;;;AAQ0E;AAiChE,cAPJ,MAqBI,EAAA;EAKE,SAAK,KAAA,EAAA,WACA;EAsBL,SAAA,IAAA,EAAA,WAAgB;EAAA,SAAA,GAAA,EAAA,WAAA;EAAA,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