Skip to content

Commit ee2f14a

Browse files
authored
Merge pull request #715 from KxSystems/line-col-spacing
Align columns for nested data
2 parents 1b80e25 + 8f386a0 commit ee2f14a

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

src/utils/queryUtils.ts

Lines changed: 25 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,29 @@ 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+
} else {
383+
row[j] += "\n";
384+
}
377385
}
378-
}
386+
387+
const diff = counter - line.length;
388+
if (diff > 0) {
389+
if (!haveHeader && j !== columnCounters.length - 1) {
390+
row[j] += line + "|" + " ".repeat(diff > 1 ? diff - 1 : diff);
391+
} else {
392+
row[j] += line + " ".repeat(diff);
393+
}
394+
}
395+
});
379396
});
380397
});
381398

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
`,

test/suite/utils/queryUtils.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,19 @@ describe("queryUtils", () => {
284284

285285
assert.deepEqual(result, expectedRes);
286286
});
287+
288+
it("should work with rows with newlines", () => {
289+
const rows = ["a#$#;header;#$#b", "a1\na2#$#;#$#b1\nb2", "3#$#;#$#4"];
290+
const expectedRes = [
291+
"a b ",
292+
"--------",
293+
"a1 \na2 b1 \n b2 ",
294+
"3 4 ",
295+
];
296+
const result = queryUtils.convertRowsToConsole(rows);
297+
298+
assert.deepEqual(result, expectedRes);
299+
});
287300
});
288301

289302
it("getConnectionType", () => {

0 commit comments

Comments
 (0)