tty-table 6.0.0 → 6.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -512,28 +512,28 @@ var import_wcwidth = __toESM(require_wcwidth());
512
512
  var addPadding = (config, width) => {
513
513
  return width + config.paddingLeft + config.paddingRight;
514
514
  };
515
+ var getDisplayWidth = (value) => {
516
+ const lines = (0, import_strip_ansi2.default)(value.toString()).split(/[\n\r]/);
517
+ let widest = 0;
518
+ for (const line of lines) {
519
+ const width = (0, import_wcwidth.default)(line);
520
+ if (width > widest) widest = width;
521
+ }
522
+ return widest;
523
+ };
515
524
  var getMaxLength = (columnOptions, rows, columnIndex) => {
516
- let iterable;
525
+ let widest = 0;
517
526
  if (columnOptions && (columnOptions.value || columnOptions.alias)) {
518
- let val = columnOptions.alias || columnOptions.value;
519
- val = val.toString();
520
- const headerRow = Array(rows[0].length);
521
- headerRow[columnIndex] = val;
522
- iterable = rows.slice();
523
- iterable.push(headerRow);
524
- } else {
525
- iterable = rows;
527
+ const value = columnOptions.alias || columnOptions.value;
528
+ widest = getDisplayWidth(value);
526
529
  }
527
- const widest = iterable.reduce((prev, row) => {
530
+ for (const row of rows) {
528
531
  if (row[columnIndex]) {
529
532
  const value = row[columnIndex].value ? row[columnIndex].value : row[columnIndex];
530
- const width = Math.max(
531
- ...(0, import_strip_ansi2.default)(value.toString()).split(/[\n\r]/).map((s) => (0, import_wcwidth.default)(s))
532
- );
533
- return width > prev ? width : prev;
533
+ const width = getDisplayWidth(value);
534
+ if (width > widest) widest = width;
534
535
  }
535
- return prev;
536
- }, 0);
536
+ }
537
537
  return widest;
538
538
  };
539
539
  var getAvailableWidth = (config) => {
@@ -598,13 +598,13 @@ var wrapCellText = (config, cellValue, columnIndex, cellOptions, rowType) => {
598
598
  emptySpace--;
599
599
  const padBoth = Math.floor(emptySpace / 2);
600
600
  const padRemainder = emptySpace % 2;
601
- line = Array(padBoth + 1).join(" ") + line + Array(padBoth + 1 + padRemainder).join(" ");
601
+ line = " ".repeat(padBoth) + line + " ".repeat(padBoth + padRemainder);
602
602
  break;
603
603
  case cellOptions[alignTgt] === "right":
604
- line = Array(emptySpace - cellOptions.paddingRight).join(" ") + line + Array(cellOptions.paddingRight + 1).join(" ");
604
+ line = " ".repeat(emptySpace - cellOptions.paddingRight - 1) + line + " ".repeat(cellOptions.paddingRight);
605
605
  break;
606
606
  default:
607
- line = Array(cellOptions.paddingLeft + 1).join(" ") + line + Array(emptySpace - cellOptions.paddingLeft).join(" ");
607
+ line = " ".repeat(cellOptions.paddingLeft) + line + " ".repeat(emptySpace - cellOptions.paddingLeft - 1);
608
608
  }
609
609
  }
610
610
  return startMatches[0] + line + endMatches[0];
@@ -675,26 +675,18 @@ var getColumnWidths = (config, rows) => {
675
675
  // src/render.ts
676
676
  var import_strip_ansi3 = __toESM(require("strip-ansi"));
677
677
  var stringifyData = (config, inputData) => {
678
- const sections = {
679
- header: [],
680
- body: [],
681
- footer: []
682
- };
683
- const marginLeft = Array(config.marginLeft + 1).join(" ");
678
+ const sections = { header: [], body: [], footer: [] };
679
+ const marginLeft = " ".repeat(config.marginLeft);
684
680
  const borderStyle = config.borderCharacters[config.borderStyle];
685
681
  const borders = [];
686
682
  const constructorType = getConstructorGeometry(inputData[0] || [], config);
687
683
  const rows = coerceConstructorGeometry(config, inputData, constructorType);
688
- if (!global.columnWidths) {
689
- global.columnWidths = {};
690
- }
684
+ if (!global.columnWidths) global.columnWidths = {};
691
685
  if (global.columnWidths[config.tableId]) {
692
686
  config.table.columnWidths = global.columnWidths[config.tableId];
693
687
  } else {
694
688
  const formattedRows = rows.map((row, rowIndex) => {
695
- return row.map((cell, cellIndex) => {
696
- return buildCell(config, cell, cellIndex, "body", rowIndex, rows, inputData, true);
697
- });
689
+ return row.map((cell, cellIndex) => buildCell(config, cell, cellIndex, "body", rowIndex, rows, inputData, true));
698
690
  });
699
691
  global.columnWidths[config.tableId] = config.table.columnWidths = getColumnWidths(config, formattedRows);
700
692
  }
@@ -703,60 +695,49 @@ var stringifyData = (config, inputData) => {
703
695
  sections.header = [];
704
696
  break;
705
697
  case config.showHeader === true:
706
- // explicitly true, show
707
698
  case !!config.table.header[0].find((obj) => obj.value || obj.alias):
708
- sections.header = config.table.header.map((row) => {
709
- return buildRow(config, row, "header", null, rows, inputData);
710
- });
699
+ sections.header = config.table.header.map((row) => buildRow(config, row, "header", null, rows, inputData));
711
700
  break;
712
701
  default:
713
702
  sections.header = [];
714
703
  }
715
- sections.body = rows.map((row, rowIndex) => {
716
- return buildRow(config, row, "body", rowIndex, rows, inputData);
717
- });
704
+ sections.body = rows.map((row, rowIndex) => buildRow(config, row, "body", rowIndex, rows, inputData));
718
705
  sections.footer = config.table.footer instanceof Array && config.table.footer.length > 0 ? [config.table.footer] : [];
719
- sections.footer = sections.footer.map((row) => {
720
- return buildRow(config, row, "footer", null, rows, inputData);
721
- });
706
+ sections.footer = sections.footer.map((row) => buildRow(config, row, "footer", null, rows, inputData));
722
707
  for (let a = 0; a < 3; a++) {
723
708
  borders[a] = borderStyle[a].l;
724
709
  config.table.columnWidths.forEach((columnWidth, index, arr) => {
725
- borders[a] += Array(Math.max(columnWidth, 2)).join(borderStyle[a].h);
710
+ borders[a] += borderStyle[a].h.repeat(Math.max(columnWidth, 2) - 1);
726
711
  borders[a] += index + 1 < arr.length ? borderStyle[a].j : "";
727
712
  });
728
713
  borders[a] += borderStyle[a].r;
729
714
  borders[a] = a < 2 ? `${marginLeft + borders[a]}
730
715
  ` : marginLeft + borders[a];
731
716
  }
732
- let output = borders[0];
733
- Object.keys(sections).forEach((p, i) => {
734
- while (sections[p].length) {
735
- const row = sections[p].shift();
717
+ const output = [borders[0]];
718
+ for (const [sectionIndex, sectionName] of Object.keys(sections).entries()) {
719
+ const section = sections[sectionName];
720
+ for (let rowIndex = 0; rowIndex < section.length; rowIndex++) {
721
+ const row = section[rowIndex];
736
722
  row.forEach((line) => {
737
- output = `${output + marginLeft + borderStyle[1].v + line.join(borderStyle[1].v) + borderStyle[1].v}
738
- `;
723
+ output.push(marginLeft + borderStyle[1].v + line.join(borderStyle[1].v) + borderStyle[1].v + "\n");
739
724
  });
740
725
  switch (true) {
741
- // skip if end of body and no footer
742
- case (sections[p].length === 0 && i === 1 && sections.footer.length === 0):
726
+ case (rowIndex === section.length - 1 && sectionIndex === 1 && sections.footer.length === 0):
743
727
  break;
744
- // skip if end of footer
745
- case (sections[p].length === 0 && i === 2):
728
+ case (rowIndex === section.length - 1 && sectionIndex === 2):
746
729
  break;
747
- // skip if compact
748
- case (config.compact && p === "body" && !row.empty):
730
+ case (config.compact && sectionName === "body" && !row.empty):
749
731
  break;
750
- // skip if border style is "none"
751
732
  case (config.borderStyle === "none" && config.compact):
752
733
  break;
753
734
  default:
754
- output += borders[1];
735
+ output.push(borders[1]);
755
736
  }
756
737
  }
757
- });
758
- output += borders[2];
759
- const finalOutput = Array(config.marginTop + 1).join("\n") + output;
738
+ }
739
+ output.push(borders[2]);
740
+ const finalOutput = "\n".repeat(config.marginTop) + output.join("");
760
741
  config.height = finalOutput.split(/\r\n|\r|\n/).length;
761
742
  return finalOutput;
762
743
  };
@@ -767,11 +748,8 @@ var buildRow = (config, row, rowType, rowIndex, rowData, inputData) => {
767
748
  return row;
768
749
  }
769
750
  const lengthDifference = config.table.columnWidths.length - row.length;
770
- if (lengthDifference > 0) {
771
- row = row.concat(Array.apply(null, new Array(lengthDifference)).map(() => null));
772
- } else if (lengthDifference < 0) {
773
- row.length = config.table.columnWidths.length;
774
- }
751
+ if (lengthDifference > 0) row = row.concat(Array.apply(null, new Array(lengthDifference)).map(() => null));
752
+ else if (lengthDifference < 0) row.length = config.table.columnWidths.length;
775
753
  row = row.map((elem, elemIndex) => {
776
754
  const cell = buildCell(config, elem, elemIndex, rowType, rowIndex, rowData, inputData);
777
755
  minRowHeight = minRowHeight < cell.length ? cell.length : minRowHeight;
@@ -780,18 +758,12 @@ var buildRow = (config, row, rowType, rowIndex, rowData, inputData) => {
780
758
  minRowHeight = rowType === "header" ? minRowHeight : minRowHeight + (config.paddingBottom + config.paddingTop);
781
759
  const linedRow = Array.apply(null, { length: minRowHeight }).map(Function.call, () => []);
782
760
  row.forEach(function(cell, a) {
783
- const whitespace = Array(config.table.columnWidths[a]).join(" ");
761
+ const whitespace = " ".repeat(Math.max(config.table.columnWidths[a] - 1, 0));
784
762
  if (rowType === "body") {
785
- for (let i = 0; i < config.paddingTop; i++) {
786
- cell.unshift(whitespace);
787
- }
788
- for (let i = 0; i < config.paddingBottom; i++) {
789
- cell.push(whitespace);
790
- }
791
- }
792
- for (let i = 0; i < minRowHeight; i++) {
793
- linedRow[i].push(typeof cell[i] !== "undefined" ? cell[i] : whitespace);
763
+ for (let i = 0; i < config.paddingTop; i++) cell.unshift(whitespace);
764
+ for (let i = 0; i < config.paddingBottom; i++) cell.push(whitespace);
794
765
  }
766
+ for (let i = 0; i < minRowHeight; i++) linedRow[i].push(typeof cell[i] !== "undefined" ? cell[i] : whitespace);
795
767
  });
796
768
  return linedRow;
797
769
  };
@@ -810,9 +782,7 @@ var buildCell = (config, elem, columnIndex, rowType, rowIndex, rowData, inputDat
810
782
  switch (true) {
811
783
  case (typeof elem === "undefined" || elem === null):
812
784
  cellValue = config.errorOnNull ? config.defaultErrorValue : config.defaultValue;
813
- if (!isColorEnabled()) {
814
- cellValue = (0, import_strip_ansi3.default)(cellValue);
815
- }
785
+ if (!isColorEnabled()) cellValue = (0, import_strip_ansi3.default)(cellValue);
816
786
  cellOptions.isNull = true;
817
787
  break;
818
788
  case (typeof elem === "object" && elem !== null && typeof elem.value !== "undefined"):
@@ -825,13 +795,7 @@ var buildCell = (config, elem, columnIndex, rowType, rowIndex, rowData, inputDat
825
795
  },
826
796
  style,
827
797
  resetStyle
828
- })(
829
- cellValue,
830
- columnIndex,
831
- rowIndex,
832
- rowData,
833
- inputData
834
- );
798
+ })(cellValue, columnIndex, rowIndex, rowData, inputData);
835
799
  break;
836
800
  default:
837
801
  cellValue = elem;
@@ -843,25 +807,13 @@ var buildCell = (config, elem, columnIndex, rowType, rowIndex, rowData, inputDat
843
807
  },
844
808
  style,
845
809
  resetStyle
846
- })(
847
- cellValue,
848
- columnIndex,
849
- rowIndex,
850
- rowData,
851
- inputData
852
- );
810
+ })(cellValue, columnIndex, rowIndex, rowData, inputData);
853
811
  }
854
- if (dryRun) {
855
- return cellValue;
856
- }
857
- }
858
- if (!cellOptions.reset) {
859
- cellValue = colorizeCell(cellValue, cellOptions, rowType);
812
+ if (dryRun) return cellValue;
860
813
  }
814
+ if (!cellOptions.reset) cellValue = colorizeCell(cellValue, cellOptions, rowType);
861
815
  const { cell, innerWidth } = wrapCellText(cellOptions, cellValue, columnIndex, cellOptions, rowType);
862
- if (rowType === "header") {
863
- config.table.columnInnerWidths.push(innerWidth);
864
- }
816
+ if (rowType === "header") config.table.columnInnerWidths.push(innerWidth);
865
817
  return cell;
866
818
  };
867
819
  var getConstructorGeometry = (row, config) => {
@@ -870,17 +822,9 @@ var getConstructorGeometry = (row, config) => {
870
822
  const keys = Object.keys(row);
871
823
  if (config.adapter === "automattic") {
872
824
  const key = keys[0];
873
- if (row[key] instanceof Array) {
874
- type = "automattic-cross";
875
- } else {
876
- type = "automattic-vertical";
877
- }
878
- } else {
879
- type = "o-horizontal";
880
- }
881
- } else {
882
- type = "a-horizontal";
883
- }
825
+ type = row[key] instanceof Array ? "automattic-cross" : "automattic-vertical";
826
+ } else type = "o-horizontal";
827
+ } else type = "a-horizontal";
884
828
  return type;
885
829
  };
886
830
  var coerceConstructorGeometry = (config, rows, constructorType) => {
@@ -890,16 +834,14 @@ var coerceConstructorGeometry = (config, rows, constructorType) => {
890
834
  config.columnSettings[0] = config.columnSettings[0] || {};
891
835
  config.columnSettings[0].color = config.headerColor;
892
836
  output = rows.map((obj) => {
893
- const arr = [];
894
837
  const key = Object.keys(obj)[0];
895
- arr.push(key);
896
- return arr.concat(obj[key]);
838
+ return [key].concat(obj[key]);
897
839
  });
898
840
  break;
899
841
  case "automattic-vertical":
900
842
  config.columnSettings[0] = config.columnSettings[0] || {};
901
843
  config.columnSettings[0].color = config.headerColor;
902
- output = rows.map(function(value) {
844
+ output = rows.map((value) => {
903
845
  const key = Object.keys(value)[0];
904
846
  return [key, value[key]];
905
847
  });
@@ -907,14 +849,11 @@ var coerceConstructorGeometry = (config, rows, constructorType) => {
907
849
  case "o-horizontal":
908
850
  if (config.table.header[0].length && config.table.header[0].every((obj) => obj.value)) {
909
851
  output = rows.map((row) => config.table.header[0].map((obj) => row[obj.value]));
910
- } else {
911
- output = rows.map((obj) => Object.values(obj));
912
- }
852
+ } else output = rows.map((obj) => Object.values(obj));
913
853
  break;
914
854
  case "a-horizontal":
915
855
  output = rows;
916
856
  break;
917
- default:
918
857
  }
919
858
  return output;
920
859
  };