2019-02-25 18:28:22 -05:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors. MIT license.
|
|
|
|
// Forked from Node's lib/internal/cli_table.js
|
2019-01-28 16:41:29 -05:00
|
|
|
|
2020-03-11 05:53:06 -04:00
|
|
|
import { TextEncoder } from "./text_encoding.ts";
|
2020-03-11 10:49:53 -04:00
|
|
|
import { hasOwnProperty } from "./util.ts";
|
2019-01-28 16:41:29 -05:00
|
|
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
|
|
|
const tableChars = {
|
|
|
|
middleMiddle: "─",
|
|
|
|
rowMiddle: "┼",
|
|
|
|
topRight: "┐",
|
|
|
|
topLeft: "┌",
|
|
|
|
leftMiddle: "├",
|
|
|
|
topMiddle: "┬",
|
|
|
|
bottomRight: "┘",
|
|
|
|
bottomLeft: "└",
|
|
|
|
bottomMiddle: "┴",
|
|
|
|
rightMiddle: "┤",
|
|
|
|
left: "│ ",
|
|
|
|
right: " │",
|
2020-03-28 13:03:49 -04:00
|
|
|
middle: " │ ",
|
2019-01-28 16:41:29 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
const colorRegExp = /\u001b\[\d\d?m/g;
|
|
|
|
|
|
|
|
function removeColors(str: string): string {
|
|
|
|
return str.replace(colorRegExp, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
function countBytes(str: string): number {
|
|
|
|
const normalized = removeColors(String(str)).normalize("NFC");
|
|
|
|
|
|
|
|
return encoder.encode(normalized).byteLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderRow(row: string[], columnWidths: number[]): string {
|
|
|
|
let out = tableChars.left;
|
|
|
|
for (let i = 0; i < row.length; i++) {
|
|
|
|
const cell = row[i];
|
|
|
|
const len = countBytes(cell);
|
|
|
|
const needed = (columnWidths[i] - len) / 2;
|
|
|
|
// round(needed) + ceil(needed) will always add up to the amount
|
|
|
|
// of spaces we need while also left justifying the output.
|
|
|
|
out += `${" ".repeat(needed)}${cell}${" ".repeat(Math.ceil(needed))}`;
|
|
|
|
if (i !== row.length - 1) {
|
|
|
|
out += tableChars.middle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out += tableChars.right;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function cliTable(head: string[], columns: string[][]): string {
|
2019-03-09 12:30:38 -05:00
|
|
|
const rows: string[][] = [];
|
2019-04-21 16:40:10 -04:00
|
|
|
const columnWidths = head.map((h: string): number => countBytes(h));
|
2019-01-28 16:41:29 -05:00
|
|
|
const longestColumn = columns.reduce(
|
2019-04-21 16:40:10 -04:00
|
|
|
(n: number, a: string[]): number => Math.max(n, a.length),
|
2019-01-28 16:41:29 -05:00
|
|
|
0
|
|
|
|
);
|
|
|
|
|
|
|
|
for (let i = 0; i < head.length; i++) {
|
|
|
|
const column = columns[i];
|
|
|
|
for (let j = 0; j < longestColumn; j++) {
|
|
|
|
if (rows[j] === undefined) {
|
|
|
|
rows[j] = [];
|
|
|
|
}
|
2019-04-18 21:56:33 -04:00
|
|
|
const value = (rows[j][i] = hasOwnProperty(column, j) ? column[j] : "");
|
2019-01-28 16:41:29 -05:00
|
|
|
const width = columnWidths[i] || 0;
|
|
|
|
const counted = countBytes(value);
|
|
|
|
columnWidths[i] = Math.max(width, counted);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
const divider = columnWidths.map((i: number): string =>
|
|
|
|
tableChars.middleMiddle.repeat(i + 2)
|
2019-01-28 16:41:29 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
let result =
|
|
|
|
`${tableChars.topLeft}${divider.join(tableChars.topMiddle)}` +
|
|
|
|
`${tableChars.topRight}\n${renderRow(head, columnWidths)}\n` +
|
|
|
|
`${tableChars.leftMiddle}${divider.join(tableChars.rowMiddle)}` +
|
|
|
|
`${tableChars.rightMiddle}\n`;
|
|
|
|
|
|
|
|
for (const row of rows) {
|
|
|
|
result += `${renderRow(row, columnWidths)}\n`;
|
|
|
|
}
|
|
|
|
|
|
|
|
result +=
|
|
|
|
`${tableChars.bottomLeft}${divider.join(tableChars.bottomMiddle)}` +
|
|
|
|
tableChars.bottomRight;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|