createStream.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.createStream = void 0;
  4. const alignTableData_1 = require("./alignTableData");
  5. const calculateRowHeights_1 = require("./calculateRowHeights");
  6. const drawBorder_1 = require("./drawBorder");
  7. const drawRow_1 = require("./drawRow");
  8. const makeStreamConfig_1 = require("./makeStreamConfig");
  9. const mapDataUsingRowHeights_1 = require("./mapDataUsingRowHeights");
  10. const padTableData_1 = require("./padTableData");
  11. const stringifyTableData_1 = require("./stringifyTableData");
  12. const truncateTableData_1 = require("./truncateTableData");
  13. const prepareData = (data, config) => {
  14. let rows = stringifyTableData_1.stringifyTableData(data);
  15. rows = truncateTableData_1.truncateTableData(rows, config);
  16. const rowHeights = calculateRowHeights_1.calculateRowHeights(rows, config);
  17. rows = mapDataUsingRowHeights_1.mapDataUsingRowHeights(rows, rowHeights, config);
  18. rows = alignTableData_1.alignTableData(rows, config);
  19. rows = padTableData_1.padTableData(rows, config);
  20. return rows;
  21. };
  22. const create = (row, columnWidths, config) => {
  23. const rows = prepareData([row], config);
  24. const body = rows.map((literalRow) => {
  25. return drawRow_1.drawRow(literalRow, config);
  26. }).join('');
  27. let output;
  28. output = '';
  29. output += drawBorder_1.drawBorderTop(columnWidths, config);
  30. output += body;
  31. output += drawBorder_1.drawBorderBottom(columnWidths, config);
  32. output = output.trimEnd();
  33. process.stdout.write(output);
  34. };
  35. const append = (row, columnWidths, config) => {
  36. const rows = prepareData([row], config);
  37. const body = rows.map((literalRow) => {
  38. return drawRow_1.drawRow(literalRow, config);
  39. }).join('');
  40. let output = '';
  41. const bottom = drawBorder_1.drawBorderBottom(columnWidths, config);
  42. if (bottom !== '\n') {
  43. output = '\r\u001B[K';
  44. }
  45. output += drawBorder_1.drawBorderJoin(columnWidths, config);
  46. output += body;
  47. output += bottom;
  48. output = output.trimEnd();
  49. process.stdout.write(output);
  50. };
  51. const createStream = (userConfig) => {
  52. const config = makeStreamConfig_1.makeStreamConfig(userConfig);
  53. const columnWidths = Object.values(config.columns).map((column) => {
  54. return column.width + column.paddingLeft + column.paddingRight;
  55. });
  56. let empty = true;
  57. return {
  58. write: (row) => {
  59. if (row.length !== config.columnCount) {
  60. throw new Error('Row cell count does not match the config.columnCount.');
  61. }
  62. if (empty) {
  63. empty = false;
  64. create(row, columnWidths, config);
  65. }
  66. else {
  67. append(row, columnWidths, config);
  68. }
  69. },
  70. };
  71. };
  72. exports.createStream = createStream;