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