
有时候项目中需要一个进度条插件,关注执行进度,心里有底。
忘了从哪里看的代码,贴出来一起学习下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| import { stdout } from 'single-line-log'
export default function ProgressBar(description, bar_length) { this.description = description || 'Progress' this.length = bar_length || 25
this.render = function (opts) { let percent = (opts.completed / opts.total).toFixed(4) let cell_num = Math.floor(percent * this.length)
let cell = '' for (let i = 0; i < cell_num; i++) { cell += '█' }
let empty = ''; for (let i = 0; i < this.length - cell_num; i++) { empty += '░' }
let cmdText = this.description + ': ' + (100 * percent).toFixed(2) + '% ' + cell + empty + ' ' + opts.completed + '/' + opts.total
stdout(cmdText) } }
|