Skip to content

Commit 4a964b2

Browse files
committed
✨ 🔀 new options.wrap property that's more powerful with auto, single-line, multi-line or number for line breaking + old options.lineLength is now removed in favor of options.wrap + consoleTable() now shows as much of the object inline as it can + removed consoleGroup() usage when object is at root level
1 parent e115aa9 commit 4a964b2

File tree

10 files changed

+141
-93
lines changed

10 files changed

+141
-93
lines changed

src/extras/consoleTable.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ConsoleText } from "../core/consoleText";
22
import hasOnlyPrimitives from "../utils/hasOnlyPrimitives";
33
import consolePrint from "../core/consolePrint";
4-
import defaultLineLength from "../utils/defaultLineLength";
4+
import guessAvailableLength from "../utils/guessAvailableLength";
55
import arrayOfObjectsTable from "./consoleTable/arrayOfObjectsTable";
66
import flatObjectOrArrayTable from "./consoleTable/flatObjectOrArrayTable";
77

@@ -19,7 +19,7 @@ export default function consoleTable(
1919
Array.isArray(object) && !hasOnlyPrimitives(object);
2020
const optionsRequired = {
2121
print: true,
22-
lineLength: defaultLineLength(),
22+
lineLength: guessAvailableLength(),
2323
theme: matchMedia("(prefers-color-scheme: dark)").matches
2424
? "dark"
2525
: "light",
Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,66 @@
1-
import consoleInline from "../../utils/consoleInline";
1+
import inspectInline from "../../inspect/inspectors/inspectInline";
22
import inspectAny from "../../inspect/inspectors/inspectAny";
33
import { ConsoleText, consoleText } from "../../core/consoleText";
44
import createTableCell, { ConsoleTableCell } from "./createTableCell";
5+
import spansLength from "../../utils/spansLength";
56

67
export default function consoleTableCell(
78
value: unknown,
89
theme: "light" | "dark",
910
maxCellLength: number,
1011
): ConsoleTableCell {
11-
const spans = inspectAny(
12-
value,
13-
{
14-
theme,
15-
lineLength: maxCellLength,
16-
print: false,
17-
line: false,
18-
indent: 0,
19-
depth: 2,
20-
},
21-
{
22-
depth: 1,
23-
indent: 0,
24-
},
25-
).map((span) => {
26-
return typeof span === "string"
27-
? consoleText(span)
28-
: span.type === "object"
29-
? consoleInline(span, theme)
30-
: span;
31-
});
32-
return spans.every(
33-
(span): span is ConsoleText =>
34-
span.type === "text" && !span.text.includes("\n"),
35-
)
36-
? createTableCell(spans)
37-
: createTableCell(consoleInline(value, theme));
12+
const spans = findOptimalExpansion(value, theme, maxCellLength);
13+
return spans === undefined
14+
? createTableCell(inspectInline(value, theme))
15+
: createTableCell(spans);
16+
}
17+
18+
function findOptimalExpansion(
19+
value: unknown,
20+
theme: "light" | "dark",
21+
maxCellLength: number,
22+
): ConsoleText[] | undefined {
23+
let optimal: ConsoleText[] | undefined;
24+
let depth = 1;
25+
while (true) {
26+
let hasObject = false;
27+
const spans = inspectAny(
28+
value,
29+
{
30+
theme,
31+
wrap: "single-line",
32+
print: false,
33+
line: false,
34+
indent: 0,
35+
depth: depth + 1,
36+
},
37+
{
38+
depth: depth,
39+
indent: 0,
40+
wrap: Number.MAX_SAFE_INTEGER,
41+
},
42+
).map((span) => {
43+
return typeof span === "string"
44+
? consoleText(span)
45+
: span.type === "object"
46+
? ((hasObject = true), inspectInline(span, theme))
47+
: span;
48+
});
49+
if (
50+
spans.every((span): span is ConsoleText => span.type === "text") &&
51+
spansLength(spans) <= maxCellLength
52+
) {
53+
depth += 1;
54+
optimal = spans;
55+
if (!hasObject) {
56+
break;
57+
}
58+
if (depth > 6) {
59+
break;
60+
}
61+
} else {
62+
break;
63+
}
64+
}
65+
return optimal;
3866
}

src/extras/consoleTable/flatObjectOrArrayTable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default function flatObjectOrArrayTable(
1414
const spans: ConsoleText[] = [];
1515
const isArray = Array.isArray(object);
1616
const keys = Object.keys(object);
17-
const lengthPerColumn = Math.floor(options.lineLength / keys.length);
17+
const lengthPerColumn = Math.floor(options.lineLength / 2);
1818
const rows = keys.map((key) => {
1919
return [
2020
createTableCell(

src/inspect/consoleInspect.ts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import consolePrint from "../core/consolePrint";
22
import inspectAny from "./inspectors/inspectAny";
33
import consoleApply from "../core/consoleApply";
44
import ConsoleSpan from "../core/ConsoleSpan";
5-
import defaultLineLength from "../utils/defaultLineLength";
5+
import guessAvailableLength from "../utils/guessAvailableLength";
66

77
export interface ConsoleInspectOptions {
88
line?: boolean;
99
indent?: number;
1010
print?: boolean;
1111
depth?: number;
12-
lineLength?: number;
1312
theme?: "light" | "dark";
13+
wrap?: "auto" | "single-line" | "multi-line" | 100;
1414
// preferMultiLine?: boolean;
1515
// preferSingleLine?: boolean;
1616
// preferTables?: boolean;
@@ -19,31 +19,37 @@ export interface ConsoleInspectOptions {
1919
export interface ConsoleInspectContext {
2020
indent: number;
2121
depth: number;
22+
wrap: number;
2223
}
2324

2425
export default function consoleInspect(
2526
value: unknown,
2627
options?: ConsoleInspectOptions,
2728
): ConsoleSpan[] {
29+
const requiredOptions: Required<ConsoleInspectOptions> = {
30+
depth: 2,
31+
indent: 4,
32+
line: false,
33+
wrap: "auto",
34+
theme: matchMedia("(prefers-color-scheme: dark)").matches
35+
? "dark"
36+
: "light",
37+
print: true,
38+
...options,
39+
};
2840
const spans = consoleApply(
29-
inspectAny(
30-
value,
31-
{
32-
depth: 2,
33-
indent: 4,
34-
line: false,
35-
lineLength: defaultLineLength(),
36-
theme: matchMedia("(prefers-color-scheme: dark)").matches
37-
? "dark"
38-
: "light",
39-
print: true,
40-
...options,
41-
},
42-
{
43-
depth: 0,
44-
indent: 0,
45-
},
46-
),
41+
inspectAny(value, requiredOptions, {
42+
depth: 0,
43+
indent: 0,
44+
wrap:
45+
requiredOptions.wrap === "auto"
46+
? guessAvailableLength()
47+
: requiredOptions.wrap === "single-line"
48+
? Number.MAX_SAFE_INTEGER
49+
: requiredOptions.wrap === "multi-line"
50+
? 0
51+
: requiredOptions.wrap,
52+
}),
4753
{
4854
lineHeight: "1.6",
4955
},

src/inspect/inspectors/inspectAny.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ import inspectObject from "./inspectObject";
33
import isIterable from "../../utils/isIterable";
44
import isPrimitive from "../../utils/isPrimitive";
55
import inspectPrimitive from "./inspectPrimitive";
6-
import { consoleText } from "../../core/consoleText";
7-
import ConsoleSpan from "../../core/ConsoleSpan";
8-
import { consoleObject } from "../../core/consoleObject";
6+
import { ConsoleText, consoleText } from "../../core/consoleText";
7+
import { ConsoleObject, consoleObject } from "../../core/consoleObject";
98
import { ConsoleInspectContext, ConsoleInspectOptions } from "../consoleInspect";
109

1110
export default function inspectAny(
1211
value: unknown,
1312
options: Required<ConsoleInspectOptions>,
1413
context: ConsoleInspectContext,
15-
): ConsoleSpan[] {
14+
): (ConsoleText | ConsoleObject)[] {
1615
if (isPrimitive(value)) {
1716
return [inspectPrimitive(value, options.theme)];
1817
} else if (Array.isArray(value) || isIterable(value)) {

src/inspect/inspectors/inspectArray.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { Primitive } from "type-fest";
22
import { ConsoleText, consoleText } from "../../core/consoleText";
3-
import inspectPrimitive from "./inspectPrimitive";
4-
import ConsoleSpan from "../../core/ConsoleSpan";
53
import isPrimitive from "../../utils/isPrimitive";
64
import inspectAny from "./inspectAny";
75
import consoleStyles from "../utils/consoleStyles";
@@ -10,29 +8,29 @@ import {
108
ConsoleInspectOptions,
119
} from "../consoleInspect";
1210
import hasOnlyPrimitives from "../../utils/hasOnlyPrimitives";
13-
import { consoleObject } from "../../core/consoleObject";
11+
import { ConsoleObject, consoleObject } from "../../core/consoleObject";
1412
import createIndent from "../utils/createIndent";
1513
import spansLength from "../../utils/spansLength";
16-
import { consoleGroup } from "../../core/consoleGroup";
14+
import inspectInline from "./inspectInline";
1715

1816
export default function inspectArray(
1917
array: unknown[],
2018
options: Required<ConsoleInspectOptions>,
2119
context: ConsoleInspectContext,
22-
): ConsoleSpan[] {
23-
if (array.every(isPrimitive)) {
20+
): (ConsoleText | ConsoleObject)[] {
21+
if (options.wrap !== "auto" || array.every(isPrimitive)) {
2422
const singleLine = singleLineArray(array as Primitive[], options);
25-
if (spansLength(singleLine) + context.indent <= options.lineLength) {
23+
if (spansLength(singleLine) + context.indent <= context.wrap) {
2624
// special case: top-level array
2725
// we otherwise can't use groups because they call `consoleFlush()`
28-
if (context.depth === 0) {
29-
return [
30-
consoleGroup({
31-
header: singleLine,
32-
body: multiLineArray(array, options, context),
33-
}),
34-
];
35-
}
26+
// if (context.depth === 0) {
27+
// return [
28+
// consoleGroup({
29+
// header: singleLine,
30+
// body: multiLineArray(array, options, context),
31+
// }),
32+
// ];
33+
// }
3634
return singleLine;
3735
}
3836
}
@@ -47,13 +45,13 @@ export default function inspectArray(
4745
function singleLineArray(
4846
array: Primitive[],
4947
options: Required<ConsoleInspectOptions>,
50-
): ConsoleText[] {
48+
): (ConsoleText | ConsoleObject)[] {
5149
return [
5250
consoleText("["),
5351
...array.flatMap((value, i) => {
5452
return i === 0
55-
? [inspectPrimitive(value, options.theme)]
56-
: [consoleText(", "), inspectPrimitive(value, options.theme)];
53+
? [inspectInline(value, options.theme)]
54+
: [consoleText(", "), inspectInline(value, options.theme)];
5755
}),
5856
consoleText("]"),
5957
consoleText(` (${array.length})`, consoleStyles[options.theme].dimmed),
@@ -64,7 +62,7 @@ function multiLineArray(
6462
array: unknown[],
6563
options: Required<ConsoleInspectOptions>,
6664
context: ConsoleInspectContext,
67-
): ConsoleSpan[] {
65+
): (ConsoleText | ConsoleObject)[] {
6866
return array.flatMap((value, i) => {
6967
const indexText = `[${i}]: `;
7068
const valueSpans =
@@ -73,11 +71,13 @@ function multiLineArray(
7371
context.depth + 1 >= options.depth
7472
? inspectAny(value, options, {
7573
indent: 0,
74+
wrap: context.wrap,
7675
depth: context.depth + 1,
7776
})
7877
: [
7978
consoleText("\n"),
8079
...inspectAny(value, options, {
80+
wrap: context.wrap,
8181
indent: context.indent + options.indent,
8282
depth: context.depth + 1,
8383
}),

src/utils/consoleInline.ts renamed to src/inspect/inspectors/inspectInline.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { consoleText, ConsoleText } from "../core/consoleText";
2-
import isPrimitive from "./isPrimitive";
3-
import inspectPrimitive from "../inspect/inspectors/inspectPrimitive";
4-
import isIterable from "./isIterable";
1+
import { consoleText, ConsoleText } from "../../core/consoleText";
2+
import isPrimitive from "../../utils/isPrimitive";
3+
import inspectPrimitive from "./inspectPrimitive";
4+
import isIterable from "../../utils/isIterable";
55

6-
export default function consoleInline(
6+
export default function inspectInline(
77
value: unknown,
88
theme: "light" | "dark",
99
): ConsoleText {

0 commit comments

Comments
 (0)