1'use strict' 2var validate = require('aproba') 3var renderTemplate = require('./render-template.js') 4var wideTruncate = require('./wide-truncate') 5var stringWidth = require('string-width') 6 7module.exports = function (theme, width, completed) { 8 validate('ONN', [theme, width, completed]) 9 if (completed < 0) { 10 completed = 0 11 } 12 if (completed > 1) { 13 completed = 1 14 } 15 if (width <= 0) { 16 return '' 17 } 18 var sofar = Math.round(width * completed) 19 var rest = width - sofar 20 var template = [ 21 { type: 'complete', value: repeat(theme.complete, sofar), length: sofar }, 22 { type: 'remaining', value: repeat(theme.remaining, rest), length: rest }, 23 ] 24 return renderTemplate(width, template, theme) 25} 26 27// lodash's way of repeating 28function repeat (string, width) { 29 var result = '' 30 var n = width 31 do { 32 if (n % 2) { 33 result += string 34 } 35 n = Math.floor(n / 2) 36 /* eslint no-self-assign: 0 */ 37 string += string 38 } while (n && stringWidth(result) < width) 39 40 return wideTruncate(result, width) 41} 42