import * as ProgressBar from 'node-progress-bars' ; import { Dialogue } from './spec/dialogue' ; import { TurnType } from './spec/turn' ; export function createBar ( dialogue , minWidth ) { const title = padString ( dialogue . title , minWidth ) ; return new ProgressBar ( { schema : `${ title }: [:bar] :current/:total :percent :elapseds.yellow` , current : 0 , total : dialogue . turns . filter ( t => t . turnType === TurnType . Bot ) . length , } ) ; } export function tickProgress ( progressBar , dialogue , minWidth ) { const title = padString ( dialogue . title , minWidth ) ; progressBar . tick ( ) ; if ( progressBar . completed && progressBar . schema ) { progressBar . setSchema ( `${ title }: [:bar] :current/:total :percent :elapseds.green` , true ) ; } } export function failProgress ( progressBar , dialogue , minWidth ) { const title = padString ( dialogue . title , minWidth ) ; progressBar . setSchema ( `${ title }: [:bar] :current/:total :percent :elapseds.red` , true ) ; } export function updateSchema ( progressBar , dialogue , minWidth ) { progressBar . setSchema ( { schema : `${ padString ( dialogue . title , minWidth ) }: [:bar] :current/:total :percent :elapseds` , } , true ) ; } function padString ( text , width ) { const widthDiff = width - text . length ; if ( widthDiff < 1 ) { return text ; } else { return text + Array ( widthDiff ) . fill ( ' ' ) . join ( '' ) ; } }