easy-cli-framework
Version:
A framework for building CLI applications that are robust and easy to maintain. Supports theming, configuration files, interactive prompts, and more.
103 lines (99 loc) • 3.17 kB
JavaScript
var base = require('./base.js');
/**
* Default options for the ThemedSimpleProgressBar
* @hidden
*/
const DEFAULT_SIMPLE_PROGRESS_BAR_OPTIONS = {
...base.DEFAULT_PROGRESS_BAR_OPTIONS,
// Whether to show the current record
showCurrentRecord: true,
// Display options for the current record
currentRecordDisplayOptions: {},
};
/**
* A themed simple progress bar that extends a ThemedProgressBar
*
* @template T
* @class ThemedSimpleProgressBar
* @extends {ThemedProgressBar<ThemedSimpleProgressBarOptions>}
*
* @param {EasyCLITheme} theme The theme to use
* @param {string} name The name of the progress bar
* @param {DisplayOptions} displayOptions The display options for the progress bar
* @param {ThemedSimpleProgressBarOptions} [progressBarOptions=DEFAULT_SIMPLE_PROGRESS_BAR_OPTIONS] The options for the progress bar
*
* @example
* ```typescript
* const progressBar = new ThemedSimpleProgressBar(theme, 'progress', displayOptions, {
* showCurrentRecord: true,
* currentRecordDisplayOptions: 'info',
* });
* ```
*/
class ThemedSimpleProgressBar extends base.ThemedProgressBar {
constructor(theme, name, displayOptions, progressBarOptions = DEFAULT_SIMPLE_PROGRESS_BAR_OPTIONS) {
super(theme, name, displayOptions, {
...DEFAULT_SIMPLE_PROGRESS_BAR_OPTIONS,
...progressBarOptions,
});
}
/**
* An internal method to get the options for the progress bar
*/
getOptions() {
const options = super.getOptions();
const { showCurrentRecord, currentRecordDisplayOptions } = this.progressBarOptions;
if (showCurrentRecord) {
options.format += ` ${this.theme.formattedString('{current}', currentRecordDisplayOptions !== null && currentRecordDisplayOptions !== undefined ? currentRecordDisplayOptions : {})}`;
}
return options;
}
/**
* Starts the progress bar
*
* @param {number} initial The initial value
* @param {number} total The total value
* @returns {SingleBar} The progress bar
*
* @example
* ```typescript
* progressBar.start(0, 100);
* ```
*/
start(initial, total) {
return super.start(initial, total, {}, this.getOptions());
}
/**
* Updates the progress bar
*
* @param {number} progress The current progress
* @param {string} [current=''] The current record
*
* @example
* ```typescript
* progressBar.update(50, 'Record 1');
* ```
*/
update(progress, current = '') {
var _a;
(_a = this.progressBar) === null || _a === undefined ? undefined : _a.update(progress, {
current,
});
}
/**
* Increments the progress bar by one.
*
* @param {string} [current=''] The current record
*
* @example
* ```typescript
* progressBar.increment('Record 1');
* ```
*/
increment(current = '') {
var _a;
(_a = this.progressBar) === null || _a === undefined ? undefined : _a.increment({ current });
}
}
exports.ThemedSimpleProgressBar = ThemedSimpleProgressBar;
;