Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/utils/queryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,10 @@ export function convertRowsToConsole(rows: string[]): string[] {
}

const columnCounters = vector[0].reduce((counters: number[], _, j) => {
// get max width of column, splitting values by new line
const maxLength = vector.reduce(
(max, row) => Math.max(max, row[j].length),
(max, row) =>
Math.max(max, Math.max(...row[j].split("\n").map((l) => l.length))),
0,
);
counters.push(maxLength + 2);
Expand All @@ -368,14 +370,29 @@ export function convertRowsToConsole(rows: string[]): string[] {
vector.forEach((row) => {
row.forEach((value, j) => {
const counter = columnCounters[j];
const diff = counter - value.length;
if (diff > 0) {
if (!haveHeader && j !== columnCounters.length - 1) {
row[j] = value + "|" + " ".repeat(diff > 1 ? diff - 1 : diff);
} else {
row[j] = value + " ".repeat(diff);
const lines = value.split("\n");
row[j] = "";

lines.forEach((line, lineIndex) => {
if (lineIndex > 0) {
// prepend spacing to align lines within the same cell
const prevCol = columnCounters[j - 1];
if (prevCol) {
row[j] += "\n" + " ".repeat(prevCol);
} else {
row[j] += "\n";
}
}
}

const diff = counter - line.length;
if (diff > 0) {
if (!haveHeader && j !== columnCounters.length - 1) {
row[j] += line + "|" + " ".repeat(diff > 1 ? diff - 1 : diff);
} else {
row[j] += line + " ".repeat(diff);
}
}
});
});
});

Expand Down
7 changes: 6 additions & 1 deletion src/webview/components/kdbResultsView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,12 @@ export class KdbResultsView extends LitElement {
(row: any) => html`
<tr class="rows">
${this.columnDefs.map(
(col: any) => html`<td nowrap>${row[col.field]}</td>`,
(col: any) =>
html`<td nowrap>
<span style="white-space: pre-line"
>${row[col.field]}</span
>
</td>`,
)}
</tr>
`,
Expand Down
13 changes: 13 additions & 0 deletions test/suite/utils/queryUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,19 @@ describe("queryUtils", () => {

assert.deepEqual(result, expectedRes);
});

it("should work with rows with newlines", () => {
const rows = ["a#$#;header;#$#b", "a1\na2#$#;#$#b1\nb2", "3#$#;#$#4"];
const expectedRes = [
"a b ",
"--------",
"a1 \na2 b1 \n b2 ",
"3 4 ",
];
const result = queryUtils.convertRowsToConsole(rows);

assert.deepEqual(result, expectedRes);
});
});

it("getConnectionType", () => {
Expand Down