tty-table 6.0.0 → 6.1.0

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