Vi Veri Veniversum Vivus Vici
/** * Returns ASCII-table in CLI-like style * @param {Array} data array of rows like key=>value * @param {Object} headers hash of table's headers like key=>title * @return {String} */ cliTable = function (data, headers) { var cellLengths = {}, result, cell, cellLength, bar = '+', header = '|', padRight = function (value, maxLen, character) { if (!Ext4.isString(value)) { value = value.toString(); } while (value.length < maxLen) { value += character; } return value; }; Ext4.each(data, function (row) { Ext4.Object.each(headers, function (key) { cellLength = row[key].toString().length; if (!cellLengths[key] || (cellLengths[key] < cellLength)) { cellLengths[key] = cellLength; } }); }); Ext4.Object.each(headers, function (key, value) { cellLength = cellLengths[key]; bar += Ext4.String.repeat('-', cellLength + 2) + '+'; if (value.length > cellLength) { value = value.substr(0, cellLength); } header += ' ' + padRight(value, cellLength, ' ') + ' |'; }); result = bar + 'n' + header + 'n' + bar + 'n'; Ext4.each(data, function (row) { result += '|'; Ext4.Object.each(row, function (key, value) { result += ' ' + padRight(value, cellLengths[key], ' ') + ' |'; }); result += 'n'; }); result += bar; return result; }