UNPKG

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.

123 lines (119 loc) 4.45 kB
'use strict'; var base = require('./base.js'); /** * Default options for the ThemedStatusProgressBar * @hidden */ const DEFAULT_STATUS_PROGRESS_BAR_OPTIONS = { ...base.DEFAULT_PROGRESS_BAR_OPTIONS, showCompleted: false, showPercentage: false, showTimeElapsed: false, // Whether to show the current record showCurrentRecord: false, // Display options for the current record currentRecordDisplayOptions: {}, showSuccess: true, successDisplayOptions: 'success', showWarn: true, warnDisplayOptions: 'warn', showError: true, errorDisplayOptions: 'error', }; /** * A themed status progress bar that shows the progress of a task while also showing the current status of the task * * @class ThemedStatusProgressBar * @extends {ThemedProgressBar<ThemedStatusProgressBarOptions>} * @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 {ThemedStatusProgressBarOptions} [progressBarOptions=DEFAULT_STATUS_PROGRESS_BAR_OPTIONS] The options for the progress bar * @example * ```typescript * const theme = new EasyCLITheme(); * const progressBar = new ThemedStatusProgressBar(theme, 'Task', 'Task in progress', { * showCurrentRecord: true, * }); * * const bar = progressBar.start(0, 100); * * // Update the progress bar * progressBar.update(50, { current: 'Processing record XXX', success: 10, warn: 5, error: 0 }); * * // Stop the progress bar * bar.stop(); * ``` */ class ThemedStatusProgressBar extends base.ThemedProgressBar { constructor(theme, name, displayOptions, progressBarOptions = DEFAULT_STATUS_PROGRESS_BAR_OPTIONS) { super(theme, name, displayOptions, { ...DEFAULT_STATUS_PROGRESS_BAR_OPTIONS, ...progressBarOptions, }); } getOptions() { var _a, _b, _c; const options = super.getOptions(); const { showCurrentRecord, currentRecordDisplayOptions } = this.progressBarOptions; if (showCurrentRecord) { options.format += ` | ${this.theme.formattedString('{current}', currentRecordDisplayOptions !== null && currentRecordDisplayOptions !== undefined ? currentRecordDisplayOptions : {})}`; } if (this.progressBarOptions.showSuccess) { options.format += ` | ${this.theme.formattedString('success: {success}', (_a = this.progressBarOptions.successDisplayOptions) !== null && _a !== undefined ? _a : {})}`; } if (this.progressBarOptions.showWarn) { options.format += ` | ${this.theme.formattedString('warn: {warn}', (_b = this.progressBarOptions.warnDisplayOptions) !== null && _b !== undefined ? _b : {})}`; } if (this.progressBarOptions.showError) { options.format += ` | ${this.theme.formattedString('error: {error}', (_c = this.progressBarOptions.errorDisplayOptions) !== null && _c !== undefined ? _c : {})}`; } return options; } /** * Starts the progress bar * * @param initial The initial value for the progress bar * @param total The total value for the progress bar * * @returns an instance of 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 value * @param {StatusPayload} payload The payload for the status bar * * @example * ```typescript * progressBar.update(50, { current: 'Processing record XXX', success: 10, warn: 5, error: 0 }); * ``` */ update(progress, payload) { var _a; (_a = this.progressBar) === null || _a === undefined ? undefined : _a.update(progress, payload); } /** * Increments the progress bar * * @param {StatusPayload} payload The payload for the status bar * * @example * ```typescript * progressBar.increment({ current: 'Processing record XXX', success: 10, warn: 5, error: 0 }); * ``` */ increment(payload) { var _a; (_a = this.progressBar) === null || _a === undefined ? undefined : _a.increment(payload); } } exports.ThemedStatusProgressBar = ThemedStatusProgressBar;