2020-08-26 01:57:08 +02:00
|
|
|
import _ from 'lodash';
|
2021-02-26 04:58:33 +01:00
|
|
|
import alignTableData from './alignTableData';
|
|
|
|
import calculateRowHeightIndex from './calculateRowHeightIndex';
|
2020-08-26 01:57:08 +02:00
|
|
|
import {
|
|
|
|
drawBorderBottom,
|
|
|
|
drawBorderJoin,
|
2021-02-26 04:58:33 +01:00
|
|
|
drawBorderTop,
|
2020-08-26 01:57:08 +02:00
|
|
|
} from './drawBorder';
|
2021-02-26 04:58:33 +01:00
|
|
|
import drawRow from './drawRow';
|
|
|
|
import makeStreamConfig from './makeStreamConfig';
|
2020-08-26 01:57:08 +02:00
|
|
|
import mapDataUsingRowHeightIndex from './mapDataUsingRowHeightIndex';
|
|
|
|
import padTableData from './padTableData';
|
2021-02-26 04:58:33 +01:00
|
|
|
import stringifyTableData from './stringifyTableData';
|
|
|
|
import truncateTableData from './truncateTableData';
|
2020-08-26 01:57:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Array} data
|
2021-02-26 04:58:33 +01:00
|
|
|
* @param {object} config
|
2020-08-26 01:57:08 +02:00
|
|
|
* @returns {Array}
|
|
|
|
*/
|
|
|
|
const prepareData = (data, config) => {
|
|
|
|
let rows;
|
|
|
|
|
|
|
|
rows = stringifyTableData(data);
|
|
|
|
|
|
|
|
rows = truncateTableData(data, config);
|
|
|
|
|
|
|
|
const rowHeightIndex = calculateRowHeightIndex(rows, config);
|
|
|
|
|
|
|
|
rows = mapDataUsingRowHeightIndex(rows, rowHeightIndex, config);
|
|
|
|
rows = alignTableData(rows, config);
|
|
|
|
rows = padTableData(rows, config);
|
|
|
|
|
|
|
|
return rows;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string[]} row
|
|
|
|
* @param {number[]} columnWidthIndex
|
2021-02-26 04:58:33 +01:00
|
|
|
* @param {object} config
|
2020-08-26 01:57:08 +02:00
|
|
|
* @returns {undefined}
|
|
|
|
*/
|
|
|
|
const create = (row, columnWidthIndex, config) => {
|
|
|
|
const rows = prepareData([row], config);
|
|
|
|
|
|
|
|
const body = rows.map((literalRow) => {
|
|
|
|
return drawRow(literalRow, config.border);
|
|
|
|
}).join('');
|
|
|
|
|
|
|
|
let output;
|
|
|
|
|
|
|
|
output = '';
|
|
|
|
|
|
|
|
output += drawBorderTop(columnWidthIndex, config.border);
|
|
|
|
output += body;
|
|
|
|
output += drawBorderBottom(columnWidthIndex, config.border);
|
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
output = output.trimEnd();
|
2020-08-26 01:57:08 +02:00
|
|
|
|
|
|
|
process.stdout.write(output);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string[]} row
|
|
|
|
* @param {number[]} columnWidthIndex
|
2021-02-26 04:58:33 +01:00
|
|
|
* @param {object} config
|
2020-08-26 01:57:08 +02:00
|
|
|
* @returns {undefined}
|
|
|
|
*/
|
|
|
|
const append = (row, columnWidthIndex, config) => {
|
|
|
|
const rows = prepareData([row], config);
|
|
|
|
|
|
|
|
const body = rows.map((literalRow) => {
|
|
|
|
return drawRow(literalRow, config.border);
|
|
|
|
}).join('');
|
|
|
|
|
|
|
|
let output = '';
|
|
|
|
const bottom = drawBorderBottom(columnWidthIndex, config.border);
|
|
|
|
|
|
|
|
if (bottom !== '\n') {
|
|
|
|
output = '\r\u001B[K';
|
|
|
|
}
|
|
|
|
|
|
|
|
output += drawBorderJoin(columnWidthIndex, config.border);
|
|
|
|
output += body;
|
|
|
|
output += bottom;
|
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
output = output.trimEnd();
|
2020-08-26 01:57:08 +02:00
|
|
|
|
|
|
|
process.stdout.write(output);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-02-26 04:58:33 +01:00
|
|
|
* @param {object} userConfig
|
|
|
|
* @returns {object}
|
2020-08-26 01:57:08 +02:00
|
|
|
*/
|
|
|
|
export default (userConfig = {}) => {
|
|
|
|
const config = makeStreamConfig(userConfig);
|
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
const columnWidthIndex = Object.values(_.mapValues(config.columns, (column) => {
|
2020-08-26 01:57:08 +02:00
|
|
|
return column.width + column.paddingLeft + column.paddingRight;
|
|
|
|
}));
|
|
|
|
|
|
|
|
let empty;
|
|
|
|
|
|
|
|
empty = true;
|
|
|
|
|
|
|
|
return {
|
|
|
|
/**
|
|
|
|
* @param {string[]} row
|
|
|
|
* @returns {undefined}
|
|
|
|
*/
|
|
|
|
write: (row) => {
|
|
|
|
if (row.length !== config.columnCount) {
|
|
|
|
throw new Error('Row cell count does not match the config.columnCount.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty) {
|
|
|
|
empty = false;
|
|
|
|
|
|
|
|
return create(row, columnWidthIndex, config);
|
|
|
|
} else {
|
|
|
|
return append(row, columnWidthIndex, config);
|
|
|
|
}
|
2021-02-26 04:58:33 +01:00
|
|
|
},
|
2020-08-26 01:57:08 +02:00
|
|
|
};
|
|
|
|
};
|