console-progress-bar
Version:
Console progress bar
136 lines (119 loc) • 3.9 kB
JavaScript
// Import.
const cc = require('ccolor');
// Constants.
const MAX_PERCENT_VALUE = 100;
const PROGRESS_BAR_MAX_VALUE = 100;
const PROGRESS_BAR_START_CHARS = ' ';
const PROGRESS_BAR_END_CHARS = ' ';
const PROGRESS_BAR_NOT_FILLED_PART_CHARS = '░';
const PROGRESS_BAR_FILLED_PART_CHARS = ' ';
const PROGRESS_BAR_CHARS_LENGTH = 50;
/**
* Console progress bar.
*/
class ConsoleProgressBar {
/**
* Console progress bar constructor.
* @param {object} [options] Options.
* @param {number} [options.maxValue] Max value. Default is 100.
* @param {string} [options.startChars] Start chars.
* @param {string} [options.endChars] End chars.
* @param {string} [options.notFilledPartChars] Not filled part chars.
* @param {string} [options.filledPartChars] Filled part chars.
* @param {number} [options.charsLength] Chars length.
*/
constructor(options = {}) {
// Define options.
const defaultOptions = {
maxValue: PROGRESS_BAR_MAX_VALUE,
startChars: PROGRESS_BAR_START_CHARS,
endChars: PROGRESS_BAR_END_CHARS,
notFilledPartChars: PROGRESS_BAR_NOT_FILLED_PART_CHARS,
filledPartChars: PROGRESS_BAR_FILLED_PART_CHARS,
charsLength: PROGRESS_BAR_CHARS_LENGTH
};
const progressBarOptions = Object.assign({}, defaultOptions, options);
// Set options.
this.value = 0;
this.percent = -1;
this.filledParts = -1;
this.maxValue = progressBarOptions.maxValue;
this.startChars = progressBarOptions.startChars;
this.endChars = progressBarOptions.endChars;
this.notFilledPartChars = progressBarOptions.notFilledPartChars;
this.filledPartChars = progressBarOptions.filledPartChars;
this.charsLength = progressBarOptions.charsLength;
// Show progress bar.
this.show();
}
/**
* Show.
*/
show() {
// Define params.
this.percent = Math.floor((this.value / this.maxValue) * MAX_PERCENT_VALUE);
const filledParts = Math.floor((this.value / this.maxValue) * this.charsLength);
const notFilledParts = this.charsLength - filledParts;
const filledPartsCharsString = ''.padStart(filledParts, this.filledPartChars);
const notFilledPartCharsString = ''.padStart(notFilledParts, this.notFilledPartChars);
// Check if filled parts amount chnaged.
if (filledParts === this.filledParts) {
return;
}
this.filledParts = filledParts;
// Show progress bar.
const progressBarString = `${cc.whiteBackground(this.startChars)}${addColor(filledPartsCharsString, this.percent)}${
notFilledPartCharsString}${cc.whiteBackground(this.endChars)}`;
const newLineIfLast = this.percent === MAX_PERCENT_VALUE ? '\n' : '';
process.stdout.write(`\r${progressBarString}${newLineIfLast}`);
}
/**
* Set value.
* @param {number} [value] Value.
*/
setValue(value = this.value) {
// Check if out of range.
if (value > this.maxValue) {
this.value = this.maxValue;
return;
}
// Set as defined.
this.value = value;
// Show progress bar.
this.show();
}
/**
* Add value.
* @param {number} [value] Value.
*/
addValue(value = 1) {
// Define next value.
const nextValue = this.value + value;
// Check if out of range.
if (nextValue > this.maxValue) {
this.value = this.maxValue;
return;
}
// Set as defined.
this.value = nextValue;
// Show progress bar.
this.show();
}
}
/**
* Add color.
* @param {string} progressBarString Progress bar string.
* @param {number} percent Percent.
* @returns {string} Progress bar string with color.
*/
function addColor(progressBarString, percent) {
// Set color accordance to percent value.
switch (percent) {
case 100:
return cc.greenBackground(progressBarString);
default:
return cc.cyanBackground(progressBarString);
}
}
// Export.
module.exports = ConsoleProgressBar;