Skip to content

Commit

Permalink
Replace text table (#4373)
Browse files Browse the repository at this point in the history
  • Loading branch information
soulgalore authored Dec 29, 2024
1 parent ebae0bf commit c2f44d2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
28 changes: 25 additions & 3 deletions lib/plugins/text/textBuilder.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import table from 'text-table';
import clic from 'cli-color';

import { plural, short, cap } from '../../support/helpers/index.js';
Expand All @@ -10,6 +9,30 @@ const tableOptions = {
};
const drab = blackBright;

function table(rows, options = {}) {
const { stringLength = str => str.length } = options;

const colWidths = [];
for (const row of rows) {
for (const [i, cell] of row.entries()) {
const len = stringLength(String(cell));
colWidths[i] = Math.max(colWidths[i] || 0, len);
}
}

return rows
.map(row =>
row
.map((cell, i) => {
const cellStr = String(cell);
const padding = colWidths[i] - stringLength(cellStr);
return cellStr + ' '.repeat(padding);
})
.join(' ')
)
.join('\n');
}

function chunk(array, size) {
const out = [];
for (let i = 0; i < array.length; i += size) {
Expand Down Expand Up @@ -46,7 +69,6 @@ function getBoxes(metrics, html) {
return chunk(summaryBoxesSetup(metrics, html).filter(Boolean), 3).flat();
}

// foo bar -> fb
function abbr(string_) {
if (/total|overall/i.test(string_)) return string_.trim();
return string_.replaceAll(/\w+\s?/g, a => a[0]);
Expand All @@ -61,7 +83,7 @@ export function renderSummary(metrics, context, options) {
let rows = getBoxes(metrics, options.html).map(b => {
const marker = getMarker(b.label),
c = getColor(b.label);
// color.xterm(ansi)(label),

return [clic[c](marker), clic[c](b.name), bold(b.median)];
});
rows.unshift(
Expand Down
4 changes: 2 additions & 2 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@
"simplecrawler": "1.1.9",
"@sitespeed.io/plugin": "0.0.6",
"tape": "5.8.1",
"text-table": "0.2.0",
"yargs": "17.7.2"
},
"overrides": {
Expand Down

0 comments on commit c2f44d2

Please sign in to comment.