Skip to content

Commit 44e79ea

Browse files
committed
refactor: code cleanup
1 parent 24a8a60 commit 44e79ea

2 files changed

Lines changed: 25 additions & 23 deletions

File tree

packages/pretty/src/formatter.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { assertEquals } from "@std/assert/equals";
55
import { assertMatch } from "@std/assert/match";
66
import { assertStringIncludes } from "@std/assert/string-includes";
77
import {
8-
type CategoryColorMap,
9-
getPrettyFormatter,
10-
prettyFormatter,
8+
type CategoryColorMap,
9+
getPrettyFormatter,
10+
prettyFormatter,
1111
} from "./formatter.ts";
1212

1313
const test = suite(import.meta);
@@ -830,7 +830,7 @@ test("properties set to true", () => {
830830
);
831831
});
832832

833-
test("newLine set to true", () => {
833+
test("messageNewLine set to true", () => {
834834
const formatter = getPrettyFormatter({
835835
properties: true,
836836
colors: false,
@@ -843,9 +843,9 @@ test("newLine set to true", () => {
843843
bar: "baz",
844844
});
845845
const result = formatter(record);
846-
// Should contain multiple lines due to wrapping
846+
// Should contain multiple lines due to message on new line + properties
847847
const lines = result.split("\n");
848-
assertEquals(lines.length, 5); // Normal log line + 1 for horizontal space + formatted properties + newline
848+
assertEquals(lines.length, 5); // Log prefix + message on new line + properties + final newline
849849
assertEquals(
850850
lines[2].trim(),
851851
"Deno" in globalThis ? 'foo: "bar"' : "foo: 'bar'",

packages/pretty/src/formatter.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { inspect, type InspectOptions } from "#util";
21
import {
3-
getLogLevels,
4-
type LogLevel,
5-
type LogRecord,
6-
type TextFormatter,
7-
type TextFormatterOptions,
2+
getLogLevels,
3+
type LogLevel,
4+
type LogRecord,
5+
type TextFormatter,
6+
type TextFormatterOptions,
87
} from "@logtape/logtape";
8+
import { inspect, type InspectOptions } from "#util";
99
import { getOptimalWordWrapWidth } from "./terminal.ts";
1010
import { truncateCategory, type TruncationStrategy } from "./truncate.ts";
1111
import { getDisplayWidth, stripAnsi } from "./wcwidth.ts";
@@ -477,10 +477,11 @@ export interface PrettyFormatterOptions
477477
*/
478478
readonly messageColor?: Color;
479479
/**
480-
* Visual style applied to save some horizontal space
480+
* Controls whether the message starts on a new line below the category.
481481
*
482-
* Controls whether the message will start after the category, or whether
483-
* it will wrap below the categories, where the categories appear as a heading
482+
* When `true`, the log message will be displayed on a new line below
483+
* the timestamp, level, and category, which can help save horizontal space
484+
* in narrow terminals or when dealing with long category names.
484485
*
485486
* @default false
486487
*/
@@ -832,8 +833,9 @@ export function getPrettyFormatter(
832833
const messageColorCode = useColors ? colorToAnsi(messageColor) : "";
833834
const messageStyleCode = useColors ? styleToAnsi(messageStyle) : "";
834835
const messageStart = messageNewLine ? "\n" : "";
835-
const messageNewLineIdentation = 4
836-
const messageNewLineIdentationProperties = messageNewLine ? messageNewLineIdentation + 2 :undefined
836+
// When message is on a new line, use consistent indentation
837+
const messageIndent = 4; // Standard indentation for new line messages
838+
const propertyIndent = messageNewLine ? messageIndent + 2 : undefined;
837839
const messagePrefix = useColors
838840
? `${messageStyleCode}${messageColorCode}`
839841
: "";
@@ -951,7 +953,7 @@ export function getPrettyFormatter(
951953
`${formattedTimestamp}${formattedIcon} ${paddedLevel} ${paddedCategory} `,
952954
),
953955
)
954-
: messageNewLineIdentation;
956+
: messageIndent;
955957

956958
// Apply word wrapping if enabled, or if there are multiline interpolated values
957959
if (wordWrapEnabled || message.includes("\n")) {
@@ -969,7 +971,7 @@ export function getPrettyFormatter(
969971
wordWrapEnabled ? wordWrapWidth : Infinity,
970972
useColors,
971973
inspectOptions,
972-
messageNewLineIdentationProperties
974+
propertyIndent,
973975
);
974976
}
975977

@@ -983,7 +985,7 @@ export function getPrettyFormatter(
983985
`${formattedTimestamp}${formattedIcon} ${formattedLevel} ${formattedCategory} `,
984986
),
985987
)
986-
: messageNewLineIdentation;
988+
: messageIndent;
987989

988990
// Apply word wrapping if enabled, or if there are multiline interpolated values
989991
if (wordWrapEnabled || message.includes("\n")) {
@@ -1001,7 +1003,7 @@ export function getPrettyFormatter(
10011003
wordWrapEnabled ? wordWrapWidth : Infinity,
10021004
useColors,
10031005
inspectOptions,
1004-
messageNewLineIdentationProperties
1006+
propertyIndent,
10051007
);
10061008
}
10071009

@@ -1016,12 +1018,12 @@ function formatProperties(
10161018
maxWidth: number,
10171019
useColors: boolean,
10181020
inspectOptions: InspectOptions,
1019-
messageNewLineIdentationProperties?: number
1021+
propertyIndent?: number,
10201022
): string {
10211023
let result = "";
10221024
for (const prop in record.properties) {
10231025
const propValue = record.properties[prop];
1024-
const pad = messageNewLineIdentationProperties ?? indentWidth - getDisplayWidth(prop);
1026+
const pad = propertyIndent ?? indentWidth - getDisplayWidth(prop) - 2;
10251027
result += "\n" + wrapText(
10261028
`${" ".repeat(pad)}${useColors ? DIM : ""}${prop}:${
10271029
useColors ? RESET : ""

0 commit comments

Comments
 (0)