mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-21 00:32:51 +02:00
[refactor] Moved "createProgressBarText" to its own TS file
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
interface IProgressBarConfiguration {
|
||||
/**
|
||||
* Current progress, taken as a decimal (i.e. '0.6' to represent '60%')
|
||||
*/
|
||||
progress?: number;
|
||||
|
||||
/**
|
||||
* Total number of ticks in progress bar. Preferably a factor of 100.
|
||||
*/
|
||||
totalTicks?: number;
|
||||
}
|
||||
|
||||
interface IProgressBarConfigurationMaterialized extends IProgressBarConfiguration {
|
||||
progress: number;
|
||||
totalTicks: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a graphical "progress bar"
|
||||
* e.g.: [||||---------------]
|
||||
* @param params The configuration parameters for the progress bar
|
||||
*/
|
||||
export function createProgressBarText(params: IProgressBarConfiguration) {
|
||||
// Default values
|
||||
const defaultParams: IProgressBarConfigurationMaterialized = {
|
||||
progress: 0,
|
||||
totalTicks: 20,
|
||||
};
|
||||
|
||||
// tslint:disable-next-line:prefer-object-spread
|
||||
const derivedParams: IProgressBarConfigurationMaterialized = Object.assign({}, params, defaultParams);
|
||||
|
||||
const bars: number = Math.floor(derivedParams.progress / (1 / derivedParams.totalTicks));
|
||||
const dashes: number = derivedParams.totalTicks - bars;
|
||||
|
||||
// String.prototype.repeat isn't completley supported, but good enough for our purposes
|
||||
return `[${"|".repeat(bars + 1)}${"-".repeat(dashes + 1)}]`;
|
||||
}
|
||||
Reference in New Issue
Block a user