UNPKG

cli-infinity-progress

Version:

Use infinity progress-bar for command-line/terminal

1 lines 4.39 kB
"use strict";var _barColor,_backgroundColor,_size,_barSize,_refreshRate,_currentIndex,_intervalId,_barChar,_backgroundChar,_direction,_header,_footer,Direction,__classPrivateFieldSet=this&&this.__classPrivateFieldSet||function(e,t,i){if(!t.has(e))throw new TypeError("attempted to set private field on non-instance");return t.set(e,i),i},__classPrivateFieldGet=this&&this.__classPrivateFieldGet||function(e,t){if(!t.has(e))throw new TypeError("attempted to get private field on non-instance");return t.get(e)};const colors=require("colors/safe"),logUpdate=require("log-update");!function(e){e[e.LeftToRight=0]="LeftToRight",e[e.RightToLeft=1]="RightToLeft"}(Direction=Direction||{});const log=logUpdate.create(process.stdout,{showCursor:!1}),NEW_LINE="\n";class CLIInfinityProgress{constructor(){_barColor.set(this,colors.green),_backgroundColor.set(this,colors.gray),_size.set(this,60),_barSize.set(this,20),_refreshRate.set(this,40),_currentIndex.set(this,0),_intervalId.set(this,void 0),_barChar.set(this,"🀫"),_backgroundChar.set(this,"🀆"),_direction.set(this,Direction.LeftToRight),_header.set(this,void 0),_footer.set(this,void 0)}setBarColor(e=colors.green){return __classPrivateFieldSet(this,_barColor,e),this}setBackgroundColor(e=colors.gray){return __classPrivateFieldSet(this,_backgroundColor,e),this}setHeader(e){return __classPrivateFieldSet(this,_header,e),this}setFooter(e){return __classPrivateFieldSet(this,_footer,e),this}setBarChar(e=__classPrivateFieldGet(this,_barChar)){return __classPrivateFieldSet(this,_barChar,e),this}setBackgroundChar(e=__classPrivateFieldGet(this,_backgroundChar)){return __classPrivateFieldSet(this,_backgroundChar,e),this}setDirectionRightToLeft(){return __classPrivateFieldSet(this,_direction,Direction.RightToLeft),this}setDirectionLeftToRight(){return __classPrivateFieldSet(this,_direction,Direction.LeftToRight),this}setSize(e=60){if(e<1)throw"setSize: size must be greater than 0";return __classPrivateFieldSet(this,_size,e),this}setBarSize(e=20){if(e<1)throw"setBarSize: size must be greater than 0";return __classPrivateFieldSet(this,_barSize,e),this}setRefreshRate(e=40){if(e<1e3/60)throw`setRefreshRate: rate must be greater than ${1e3/60}`;return __classPrivateFieldSet(this,_refreshRate,e),this}start(){return __classPrivateFieldSet(this,_currentIndex,0),this.stopAnimate(),this.startAnimate(),this}remove(){return this.stopAnimate(),log.clear(),this}stop(){return this.stopAnimate(),log.done(),this}pause(){return this.stopAnimate(),this}resume(){return this.startAnimate(),this}startAnimate(){__classPrivateFieldSet(this,_intervalId,setInterval(this.render.bind(this),__classPrivateFieldGet(this,_refreshRate)))}stopAnimate(){clearInterval(__classPrivateFieldGet(this,_intervalId))}render(){let e=__classPrivateFieldGet(this,_direction)===Direction.LeftToRight?__classPrivateFieldGet(this,_currentIndex)-__classPrivateFieldGet(this,_barSize):__classPrivateFieldGet(this,_size)-__classPrivateFieldGet(this,_currentIndex),t="";0<e?t=__classPrivateFieldGet(this,_backgroundChar).repeat(e):e=0;let i=__classPrivateFieldGet(this,_direction)===Direction.LeftToRight?__classPrivateFieldGet(this,_size)-__classPrivateFieldGet(this,_currentIndex):__classPrivateFieldGet(this,_currentIndex)-__classPrivateFieldGet(this,_barSize),r="";0<i?r=__classPrivateFieldGet(this,_backgroundChar).repeat(i):i=0;var s=__classPrivateFieldGet(this,_barChar).repeat(__classPrivateFieldGet(this,_size)-(e+i));__classPrivateFieldSet(this,_currentIndex,+__classPrivateFieldGet(this,_currentIndex)+1),__classPrivateFieldGet(this,_currentIndex)>__classPrivateFieldGet(this,_size)+__classPrivateFieldGet(this,_barSize)&&__classPrivateFieldSet(this,_currentIndex,0);let a="";__classPrivateFieldGet(this,_header)&&(a+=__classPrivateFieldGet(this,_header)+NEW_LINE),a+=`${__classPrivateFieldGet(this,_backgroundColor).call(this,t)}${__classPrivateFieldGet(this,_barColor).call(this,s)}${__classPrivateFieldGet(this,_backgroundColor).call(this,r)}`,__classPrivateFieldGet(this,_footer)&&(a+=NEW_LINE+__classPrivateFieldGet(this,_footer)),log(a)}}_barColor=new WeakMap,_backgroundColor=new WeakMap,_size=new WeakMap,_barSize=new WeakMap,_refreshRate=new WeakMap,_currentIndex=new WeakMap,_intervalId=new WeakMap,_barChar=new WeakMap,_backgroundChar=new WeakMap,_direction=new WeakMap,_header=new WeakMap,_footer=new WeakMap,module.exports=CLIInfinityProgress;