hadean-old/src/Progressbar.ts

26 lines
874 B
TypeScript
Raw Normal View History

2021-06-14 22:03:55 -04:00
import chalk from "chalk";
2021-06-15 15:02:47 -04:00
export enum ProgressbarStyle {
indicator = 'indicator',
progress = 'progress'
}
export function progressbar(completion, width, style: ProgressbarStyle = ProgressbarStyle.indicator) {
let chalkFn
if(style === ProgressbarStyle.indicator) {
if(completion > 0.8) chalkFn = chalk.bgBlue.cyan;
else if(completion > 0.5) chalkFn = chalk.bgBlue.green;
else if(completion > 0.2) chalkFn = chalk.bgBlue.yellow;
else chalkFn = chalk.bgBlue.red;
} else if(style === ProgressbarStyle.progress) {
chalkFn = chalk.bgBlue.cyan;
}
2021-06-14 22:03:55 -04:00
const chars = [' ', '▏', '▎', '▍', '▌', '▋', '▊', '▉', '█'];
let str = '';
for(let i = 0; i < width; i ++) {
const remainder = Math.floor(Math.min(Math.max(0, (completion * width) - i), 1) * 8);
const char = chars[remainder];
2021-06-15 15:02:47 -04:00
str += chalkFn(char);
2021-06-14 22:03:55 -04:00
}
return str;
}