11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst stringWidth = require('string-width'); 41cb0ef41Sopenharmony_ciconst stripAnsi = require('strip-ansi'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst concat = Array.prototype.concat; 71cb0ef41Sopenharmony_ciconst defaults = { 81cb0ef41Sopenharmony_ci character: ' ', 91cb0ef41Sopenharmony_ci newline: '\n', 101cb0ef41Sopenharmony_ci padding: 2, 111cb0ef41Sopenharmony_ci sort: true, 121cb0ef41Sopenharmony_ci width: 0, 131cb0ef41Sopenharmony_ci}; 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_cifunction byPlainText(a, b) { 161cb0ef41Sopenharmony_ci const plainA = stripAnsi(a); 171cb0ef41Sopenharmony_ci const plainB = stripAnsi(b); 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ci if (plainA === plainB) { 201cb0ef41Sopenharmony_ci return 0; 211cb0ef41Sopenharmony_ci } 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci if (plainA > plainB) { 241cb0ef41Sopenharmony_ci return 1; 251cb0ef41Sopenharmony_ci } 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci return -1; 281cb0ef41Sopenharmony_ci} 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_cifunction makeArray() { 311cb0ef41Sopenharmony_ci return []; 321cb0ef41Sopenharmony_ci} 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_cifunction makeList(count) { 351cb0ef41Sopenharmony_ci return Array.apply(null, Array(count)); 361cb0ef41Sopenharmony_ci} 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_cifunction padCell(fullWidth, character, value) { 391cb0ef41Sopenharmony_ci const valueWidth = stringWidth(value); 401cb0ef41Sopenharmony_ci const filler = makeList(fullWidth - valueWidth + 1); 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci return value + filler.join(character); 431cb0ef41Sopenharmony_ci} 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_cifunction toRows(rows, cell, i) { 461cb0ef41Sopenharmony_ci rows[i % rows.length].push(cell); 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci return rows; 491cb0ef41Sopenharmony_ci} 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_cifunction toString(arr) { 521cb0ef41Sopenharmony_ci return arr.join(''); 531cb0ef41Sopenharmony_ci} 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_cifunction columns(values, options) { 561cb0ef41Sopenharmony_ci values = concat.apply([], values); 571cb0ef41Sopenharmony_ci options = Object.assign({}, defaults, options); 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci let cells = values.filter(Boolean).map(String); 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci if (options.sort !== false) { 621cb0ef41Sopenharmony_ci cells = cells.sort(byPlainText); 631cb0ef41Sopenharmony_ci } 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci const termWidth = options.width || process.stdout.columns; 661cb0ef41Sopenharmony_ci const cellWidth = 671cb0ef41Sopenharmony_ci Math.max.apply(null, cells.map(stringWidth)) + options.padding; 681cb0ef41Sopenharmony_ci const columnCount = Math.floor(termWidth / cellWidth) || 1; 691cb0ef41Sopenharmony_ci const rowCount = Math.ceil(cells.length / columnCount) || 1; 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci if (columnCount === 1) { 721cb0ef41Sopenharmony_ci return cells.join(options.newline); 731cb0ef41Sopenharmony_ci } 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci return cells 761cb0ef41Sopenharmony_ci .map(padCell.bind(null, cellWidth, options.character)) 771cb0ef41Sopenharmony_ci .reduce(toRows, makeList(rowCount).map(makeArray)) 781cb0ef41Sopenharmony_ci .map(toString) 791cb0ef41Sopenharmony_ci .join(options.newline); 801cb0ef41Sopenharmony_ci} 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_cimodule.exports = columns; 83