react / wstein / node_modules / jest-cli / node_modules / cover / contrib / cli-table / lib / cli-table / index.js
80687 views1/**2* Module dependencies.3*/45var utils = require('./utils')6, repeat = utils.repeat7, truncate = utils.truncate8, pad = utils.pad;910require('../../../colors');1112/**13* Table constructor14*15* @param {Object} options16* @api public17*/1819function Table (options){20this.options = utils.options({21chars: {22'top': '━'23, 'top-mid': '┳'24, 'top-left': '┏'25, 'top-right': '┓'26, 'bottom': '━'27, 'bottom-mid': '┻'28, 'bottom-left': '┗'29, 'bottom-right': '┛'30, 'left': '┃'31, 'left-mid': '┣'32, 'mid': '━'33, 'mid-mid': '╋'34, 'right': '┃'35, 'right-mid': '┫'36}37, truncate: '…'38, colWidths: []39, colAligns: []40, style: {41'padding-left': 142, 'padding-right': 143, head: ['cyan']44}45, head: []46}, options);47};4849/**50* Inherit from Array.51*/5253Table.prototype.__proto__ = Array.prototype;5455/**56* Width getter57*58* @return {Number} width59* @api public60*/6162Table.prototype.__defineGetter__('width', function (){63var str = this.toString().split("\n");64if (str.length) return str[0].length;65return 0;66});6768/**69* Render to a string.70*71* @return {String} table representation72* @api public73*/7475Table.prototype.render76Table.prototype.toString = function (){77var ret = ''78, options = this.options79, style = options.style80, head = options.head81, chars = options.chars82, truncater = options.truncate83, colWidths = options.colWidths || new Array(this.head.length)84, totalWidth = 0;8586if (!head.length && !this.length) return '';8788if (!colWidths.length){89this.slice(0).concat([head]).forEach(function(cells){90cells.forEach(function(cell, i){91var width = typeof cell == 'object' && cell.width != undefined92? cell.width93: ((typeof cell == 'object' ? utils.strlen(cell.text) : utils.strlen(cell)) + (style['padding-left'] || 0) + (style['padding-right'] || 0))94colWidths[i] = Math.max(colWidths[i] || 0, width || 0);95});96});97};9899totalWidth = (colWidths.length == 1 ? colWidths[0] : colWidths.reduce(100function (a, b){101return a + b102})) + colWidths.length + 1;103104// draws a line105function line (line, left, right, intersection){106var width = 0107, line =108left109+ repeat(line, totalWidth - 2)110+ right;111112colWidths.forEach(function (w, i){113if (i == colWidths.length - 1) return;114width += w + 1;115line = line.substr(0, width) + intersection + line.substr(width + 1);116});117118ret += line;119};120121// draws the top line122function lineTop (){123line(chars.top124, chars['top-left'] || chars.top125, chars['top-right'] || chars.top126, chars['top-mid']);127ret += "\n";128};129130// renders a string, by padding it or truncating it131function string (str, index){132var str = String(typeof str == 'object' && str.text ? str.text : str)133, length = utils.strlen(str)134, width = colWidths[index]135- (style['padding-left'] || 0)136- (style['padding-right'] || 0)137, align = options.colAligns[index] || 'left';138139return repeat(' ', style['padding-left'] || 0)140+ (length == width ? str :141(length < width142? pad(str, ( width + (str.length - length) ), ' ', align == 'left' ? 'right' :143(align == 'middle' ? 'both' : 'left'))144: (truncater ? truncate(str, width, truncater) : str))145)146+ repeat(' ', style['padding-right'] || 0);147};148149if (head.length){150lineTop();151152ret += chars.left;153154head.forEach(function (th, index){155var text = string(th, index);156if (style.head){157style.head.forEach(function(style){158text = text[style];159});160}161162ret += text;163ret += chars.right;164});165166ret += "\n";167}168169if (this.length)170this.forEach(function (cells, i){171if (!head.length && i == 0)172lineTop();173else {174line(chars.mid175, chars['left-mid']176, chars['right-mid']177, chars['mid-mid']);178179ret += "\n" + chars.left;180181cells.forEach(function(cell, i){182ret += string(cell, i);183ret += chars.right;184});185186ret += "\n";187}188});189190line(chars.bottom191, chars['bottom-left'] || chars.bottom192, chars['bottom-right'] || chars.bottom193, chars['bottom-mid']);194195return ret;196};197198/**199* Module exports.200*/201202module.exports = Table;203204module.exports.version = '0.0.1';205206