Skip to content

Commit 31ade79

Browse files
committed
Align columns for nested data
1 parent 1b80e25 commit 31ade79

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/utils/queryUtils.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,10 @@ export function convertRowsToConsole(rows: string[]): string[] {
357357
}
358358

359359
const columnCounters = vector[0].reduce((counters: number[], _, j) => {
360+
// get max width of column, splitting values by new line
360361
const maxLength = vector.reduce(
361-
(max, row) => Math.max(max, row[j].length),
362+
(max, row) =>
363+
Math.max(max, Math.max(...row[j].split("\n").map((l) => l.length))),
362364
0,
363365
);
364366
counters.push(maxLength + 2);
@@ -368,14 +370,27 @@ export function convertRowsToConsole(rows: string[]): string[] {
368370
vector.forEach((row) => {
369371
row.forEach((value, j) => {
370372
const counter = columnCounters[j];
371-
const diff = counter - value.length;
372-
if (diff > 0) {
373-
if (!haveHeader && j !== columnCounters.length - 1) {
374-
row[j] = value + "|" + " ".repeat(diff > 1 ? diff - 1 : diff);
375-
} else {
376-
row[j] = value + " ".repeat(diff);
373+
const lines = value.split("\n");
374+
row[j] = "";
375+
376+
lines.forEach((line, lineIndex) => {
377+
if (lineIndex > 0) {
378+
// prepend spacing to align lines within the same cell
379+
const prevCol = columnCounters[j - 1];
380+
if (prevCol) {
381+
row[j] += "\n" + " ".repeat(prevCol);
382+
}
377383
}
378-
}
384+
385+
const diff = counter - line.length;
386+
if (diff > 0) {
387+
if (!haveHeader && j !== columnCounters.length - 1) {
388+
row[j] += line + "|" + " ".repeat(diff > 1 ? diff - 1 : diff);
389+
} else {
390+
row[j] += line + " ".repeat(diff);
391+
}
392+
}
393+
});
379394
});
380395
});
381396

src/webview/components/kdbResultsView.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,12 @@ export class KdbResultsView extends LitElement {
189189
(row: any) => html`
190190
<tr class="rows">
191191
${this.columnDefs.map(
192-
(col: any) => html`<td nowrap>${row[col.field]}</td>`,
192+
(col: any) =>
193+
html`<td nowrap>
194+
<span style="white-space: pre-line"
195+
>${row[col.field]}</span
196+
>
197+
</td>`,
193198
)}
194199
</tr>
195200
`,

0 commit comments

Comments
 (0)