bitburner-src/utils/helpers/createProgressBarText.ts

48 lines
1.5 KiB
TypeScript
Raw Normal View History

2018-07-08 07:11:34 +02:00
/**
* Represents the possible configuration values that can be provided when creating the progress bar text.
*/
interface IProgressBarConfiguration {
2021-09-05 01:09:30 +02:00
/**
* Current progress, taken as a decimal (i.e. '0.6' to represent '60%')
*/
progress?: number;
2021-09-05 01:09:30 +02:00
/**
* Total number of ticks in progress bar. Preferably a factor of 100.
*/
totalTicks?: number;
}
2018-07-08 07:11:34 +02:00
/**
* Represents concrete configuration values when creating the progress bar text.
*/
2021-09-09 05:47:34 +02:00
interface IProgressBarConfigurationMaterialized extends IProgressBarConfiguration {
2021-09-05 01:09:30 +02:00
progress: number;
totalTicks: number;
}
/**
* Creates a graphical "progress bar"
* e.g.: [||||---------------]
* @param params The configuration parameters for the progress bar
*/
2021-09-09 05:47:34 +02:00
export function createProgressBarText(params: IProgressBarConfiguration): string {
2021-09-05 01:09:30 +02:00
// Default values
const defaultParams: IProgressBarConfigurationMaterialized = {
progress: 0,
totalTicks: 20,
};
2021-09-05 01:09:30 +02:00
// tslint:disable-next-line:prefer-object-spread
2021-09-09 05:47:34 +02:00
const derived: IProgressBarConfigurationMaterialized = Object.assign({}, defaultParams, params);
2021-09-05 01:09:30 +02:00
// Ensure it is 0..1
derived.progress = Math.max(Math.min(derived.progress, 1), 0);
2021-09-05 01:09:30 +02:00
// This way there is always at least one bar filled in...
2021-09-09 05:47:34 +02:00
const bars: number = Math.max(Math.floor(derived.progress / (1 / derived.totalTicks)), 1);
2021-09-05 01:09:30 +02:00
const dashes: number = Math.max(derived.totalTicks - bars, 0);
2021-09-05 01:09:30 +02:00
// String.prototype.repeat isn't completley supported, but good enough for our purposes
return `[${"|".repeat(bars)}${"-".repeat(dashes)}]`;
}