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