import { ProgressBarState } from "./ProgressBar" ; import { formatNumber , NumberFormat } from "./utils" ; export type Token = ( bar : ProgressBarState ) => string ; export type TokenDict = { [ _ ] : Token ; } ; export const bar = ( { length , fill = '=' , head = fill , empty = ' ' } ) : Token => bar => { const fillLength = Math . round ( length * bar . progress ( ) ) ; return new Array ( length ) . fill ( 1 ) . map ( ( _ , idx ) => { if ( idx < fillLength - 1 ) { return fill ; } else if ( idx === fillLength - 1 ) { return head ; } else { return empty ; } } ) . join ( "" ) ; } ; export const elapsedTime = ( { interval = 1 , until = Date . now , ... other } = { } ) : Token => bar => formatNumber ( bar . elapsedTime ( until ( ) ) / interval , other ) ; export const progress = ( options = { } ) : Token => bar => formatNumber ( bar . progress ( ) * 100 , options ) ; export const current = ( options = { } ) : Token => bar => formatNumber ( bar . current , options ) ; export const total = ( options = { } ) : Token => bar => formatNumber ( bar . total , options ) ; export const title = ( ) : Token => bar => bar . title ; export const rate = ( { interval = 1 , ... other } = { } ) : Token => bar => formatNumber ( bar . rate ( ) / interval , other ) ; export const eta = ( { interval = 1 , ... other } = { } ) : Token => bar => formatNumber ( bar . eta ( ) / interval , other ) ; param initialValue export const mutable = ( initialValue ) : ( Token & { getValue : ( ) => string ; setValue : ( value : string ) => void ; } ) => { const func = ( ) => initialValue ; func . setValue = ( value ) => { initialValue = value ; } ; func . getValue = ( ) => initialValue ; return func ; } ;