11cb0ef41Sopenharmony_ci'use strict' 21cb0ef41Sopenharmony_civar validate = require('aproba') 31cb0ef41Sopenharmony_civar renderTemplate = require('./render-template.js') 41cb0ef41Sopenharmony_civar wideTruncate = require('./wide-truncate') 51cb0ef41Sopenharmony_civar stringWidth = require('string-width') 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_cimodule.exports = function (theme, width, completed) { 81cb0ef41Sopenharmony_ci validate('ONN', [theme, width, completed]) 91cb0ef41Sopenharmony_ci if (completed < 0) { 101cb0ef41Sopenharmony_ci completed = 0 111cb0ef41Sopenharmony_ci } 121cb0ef41Sopenharmony_ci if (completed > 1) { 131cb0ef41Sopenharmony_ci completed = 1 141cb0ef41Sopenharmony_ci } 151cb0ef41Sopenharmony_ci if (width <= 0) { 161cb0ef41Sopenharmony_ci return '' 171cb0ef41Sopenharmony_ci } 181cb0ef41Sopenharmony_ci var sofar = Math.round(width * completed) 191cb0ef41Sopenharmony_ci var rest = width - sofar 201cb0ef41Sopenharmony_ci var template = [ 211cb0ef41Sopenharmony_ci { type: 'complete', value: repeat(theme.complete, sofar), length: sofar }, 221cb0ef41Sopenharmony_ci { type: 'remaining', value: repeat(theme.remaining, rest), length: rest }, 231cb0ef41Sopenharmony_ci ] 241cb0ef41Sopenharmony_ci return renderTemplate(width, template, theme) 251cb0ef41Sopenharmony_ci} 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci// lodash's way of repeating 281cb0ef41Sopenharmony_cifunction repeat (string, width) { 291cb0ef41Sopenharmony_ci var result = '' 301cb0ef41Sopenharmony_ci var n = width 311cb0ef41Sopenharmony_ci do { 321cb0ef41Sopenharmony_ci if (n % 2) { 331cb0ef41Sopenharmony_ci result += string 341cb0ef41Sopenharmony_ci } 351cb0ef41Sopenharmony_ci n = Math.floor(n / 2) 361cb0ef41Sopenharmony_ci /* eslint no-self-assign: 0 */ 371cb0ef41Sopenharmony_ci string += string 381cb0ef41Sopenharmony_ci } while (n && stringWidth(result) < width) 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci return wideTruncate(result, width) 411cb0ef41Sopenharmony_ci} 42