react / wstein / node_modules / jest-cli / node_modules / cover / node_modules / 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, compact : false45}46, head: []47}, options);48};4950/**51* Inherit from Array.52*/5354Table.prototype.__proto__ = Array.prototype;5556/**57* Width getter58*59* @return {Number} width60* @api public61*/6263Table.prototype.__defineGetter__('width', function (){64var str = this.toString().split("\n");65if (str.length) return str[0].length;66return 0;67});6869/**70* Render to a string.71*72* @return {String} table representation73* @api public74*/7576Table.prototype.render77Table.prototype.toString = function (){78var ret = ''79, options = this.options80, style = options.style81, head = options.head82, chars = options.chars83, truncater = options.truncate84, colWidths = options.colWidths || new Array(this.head.length)85, totalWidth = 0;8687if (!head.length && !this.length) return '';8889if (!colWidths.length){90this.slice(0).concat([head]).forEach(function(cells){91cells.forEach(function(cell, i){92var width = typeof cell == 'object' && cell.width != undefined93? cell.width94: ((typeof cell == 'object' ? utils.strlen(cell.text) : utils.strlen(cell)) + (style['padding-left'] || 0) + (style['padding-right'] || 0))95colWidths[i] = Math.max(colWidths[i] || 0, width || 0);96});97});98};99100totalWidth = (colWidths.length == 1 ? colWidths[0] : colWidths.reduce(101function (a, b){102return a + b103})) + colWidths.length + 1;104105// draws a line106function line (line, left, right, intersection){107var width = 0108, line =109left110+ repeat(line, totalWidth - 2)111+ right;112113colWidths.forEach(function (w, i){114if (i == colWidths.length - 1) return;115width += w + 1;116line = line.substr(0, width) + intersection + line.substr(width + 1);117});118119ret += line;120};121122// draws the top line123function lineTop (){124line(chars.top125, chars['top-left'] || chars.top126, chars['top-right'] || chars.top127, chars['top-mid']);128ret += "\n";129};130131// renders a string, by padding it or truncating it132function string (str, index){133var str = String(typeof str == 'object' && str.text ? str.text : str)134, length = utils.strlen(str)135, width = colWidths[index]136- (style['padding-left'] || 0)137- (style['padding-right'] || 0)138, align = options.colAligns[index] || 'left';139140return repeat(' ', style['padding-left'] || 0)141+ (length == width ? str :142(length < width143? pad(str, ( width + (str.length - length) ), ' ', align == 'left' ? 'right' :144(align == 'middle' ? 'both' : 'left'))145: (truncater ? truncate(str, width, truncater) : str))146)147+ repeat(' ', style['padding-right'] || 0);148};149150if (head.length){151lineTop();152153ret += chars.left;154155head.forEach(function (th, index){156var text = string(th, index);157if (style.head){158style.head.forEach(function(style){159text = text[style];160});161}162163ret += text;164ret += chars.right;165});166167ret += "\n";168}169170if (this.length)171this.forEach(function (cells, i){172if (!head.length && i == 0)173lineTop();174else {175if (!style.compact || i<(!!head.length) ?1:0 || cells.length == 0){176line(chars.mid177, chars['left-mid']178, chars['right-mid']179, chars['mid-mid']);180181ret += "\n"182}183if (!cells.length)184return;185186ret += chars.left;187188cells.forEach(function(cell, i){189ret += string(cell, i);190ret += chars.right;191});192193ret += "\n";194}195});196197line(chars.bottom198, chars['bottom-left'] || chars.bottom199, chars['bottom-right'] || chars.bottom200, chars['bottom-mid']);201202return ret;203};204205/**206* Module exports.207*/208209module.exports = Table;210211module.exports.version = '0.0.1';212213214