Skip to content

Commit e15e60a

Browse files
committed
Pad mutiple values
1 parent 2431357 commit e15e60a

File tree

7 files changed

+63
-5
lines changed

7 files changed

+63
-5
lines changed

runtime/index.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {transpileJavaScript} from "@observablehq/notebook-kit";
22
import {Runtime} from "@observablehq/runtime";
33
import {parse} from "acorn";
4-
import {group} from "d3-array";
4+
import {group, max} from "d3-array";
55
import {dispatch as d3Dispatch} from "d3-dispatch";
66
import * as stdlib from "./stdlib.js";
77
import {OUTPUT_MARK, ERROR_MARK} from "./constant.js";
@@ -37,8 +37,34 @@ function debounce(fn, delay = 0) {
3737
};
3838
}
3939

40+
function padStringWidth(string) {
41+
const lines = string.split("\n");
42+
const maxLength = max(lines, (line) => line.length);
43+
return lines.map((line) => line.padEnd(maxLength)).join("\n");
44+
}
45+
46+
function padStringHeight(string, height) {
47+
const lines = string.split("\n");
48+
const diff = height - lines.length;
49+
return lines.join("\n") + "\n".repeat(diff);
50+
}
51+
4052
function merge(...strings) {
41-
return strings.join("\n");
53+
const maxHeight = max(strings, (string) => string.split("\n").length);
54+
const aligned = strings
55+
.map((string) => padStringHeight(string, maxHeight))
56+
.map((string) => padStringWidth(string))
57+
.map((string) => string.split("\n"));
58+
let output = "";
59+
for (let i = 0; i < maxHeight; i++) {
60+
for (let j = 0; j < aligned.length; j++) {
61+
const line = aligned[j][i];
62+
output += line;
63+
output += j < aligned.length - 1 ? " " : "";
64+
}
65+
output += i < maxHeight - 1 ? "\n" : "";
66+
}
67+
return output;
4268
}
4369

4470
function addPrefix(string, prefix) {
@@ -69,9 +95,13 @@ export function createRuntime(initialCode) {
6995
let error = false;
7096
for (let i = 0; i < values.length; i++) {
7197
const line = values[i];
98+
const n = values[i].length;
7299
const formatted = line.map((v) => {
73100
if (isError(v)) error = true;
74-
const inspector = v instanceof Inspector ? v : new Inspector(v);
101+
// If there are multiple values, we don't want to quote the string values.
102+
// such as echo("a =", 1) results in "a = 1" instead of "a = "1"".
103+
const options = n === 1 ? {} : {quote: false};
104+
const inspector = v instanceof Inspector ? v : new Inspector(v, options);
75105
return inspector.format();
76106
});
77107
const merged = merge(...formatted);

runtime/inspect.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function isMultiline(value) {
1010
function inspector(value, {limit = 200, quote = "double", indent = null} = {}) {
1111
if (isMultiline(value)) return value;
1212
if (typeof value === "string" && !quote) return value;
13+
if (!quote) quote = "double";
1314
const string = objectInspector(value, {indent, quoteStyle: quote});
1415
if (string.length > limit) return string.slice(0, limit) + "…";
1516
return string;

test/js/echo-multiple-values.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const echoMultipleValues = `echo(1, 2, 3);
2+
3+
echo(1, [1, 2, 3], 3);
4+
5+
echo("a =", 1);
6+
7+
echo("a =", [1, 2, 3]);
8+
9+
echo(1, recho.inspect({key: [1, 2, 3]}, {indent: 2}), 2);`;

test/js/index-tests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export {syntaxError2} from "./syntax-error2.js";
88
export {syntaxError3} from "./syntax-error3.js";
99
export {syntaxError4} from "./syntax-error4.js";
1010
export {nonCallEcho} from "./non-call-echo.js";
11+
export {echoMultipleValues} from "./echo-multiple-values.js";

test/js/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ export {commentLink} from "./comment-link.js";
1717
export {syntaxError3} from "./syntax-error3.js";
1818
export {syntaxError4} from "./syntax-error4.js";
1919
export {nonCallEcho} from "./non-call-echo.js";
20+
export {echoMultipleValues} from "./echo-multiple-values.js";

test/output/echoMultipleValues.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//➜ 1 2 3
2+
echo(1, 2, 3);
3+
4+
//➜ 1 [ 1, 2, 3 ] 3
5+
echo(1, [1, 2, 3], 3);
6+
7+
//➜ a = 1
8+
echo("a =", 1);
9+
10+
//➜ a = [ 1, 2, 3 ]
11+
echo("a =", [1, 2, 3]);
12+
13+
//➜ 1 { 2
14+
//➜ key: [ 1, 2, 3 ]
15+
//➜ }
16+
echo(1, recho.inspect({key: [1, 2, 3]}, {indent: 2}), 2);

test/output/inspector.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ echo(new Date('2025-09-23T12:00:00Z'));
2929

3030
//➜ Map (2) {
3131
//➜ 1 => 2,
32-
//➜ 3 => 4
33-
//➜ }
32+
//➜ 3 => 4
33+
//➜ }
3434
echo(recho.inspect(new Map([[1, 2], [3, 4]]), {indent: 2}));
3535

3636
//➜ [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …

0 commit comments

Comments
 (0)