normalize.js 843 B

12345678910111213141516171819202122232425262728293031323334
  1. const stripAnsi = require('strip-ansi')
  2. module.exports = function normalizeOutput (rows, max) {
  3. function padEnd (string, maxLength, fillString) {
  4. while (stripAnsi(string).length < maxLength) {
  5. string = string + fillString
  6. }
  7. return string
  8. }
  9. var widths = []
  10. max = max || rows[0].length
  11. for (var i = 0; i < rows.length; i++) {
  12. for (var j = 0; j < rows[i].length && j < max; j++) {
  13. if (!widths[j] || widths[j] < stripAnsi(rows[i][j]).length) {
  14. widths[j] = stripAnsi(rows[i][j]).length
  15. }
  16. }
  17. }
  18. for (var i = 0; i < rows.length; i++) {
  19. for (var j = 0; j < rows[i].length && j < max; j++) {
  20. if (rows[i][j] == '-') {
  21. rows[i][j] = padEnd(rows[i][j], widths[j], '-')
  22. } else {
  23. rows[i][j] = padEnd(rows[i][j], widths[j], ' ')
  24. }
  25. }
  26. }
  27. return rows
  28. }