[refactor] Moved "createProgressBarText" to its own TS file

This commit is contained in:
Steven Evans 2018-07-04 00:23:59 -04:00
parent b84e9749f6
commit 187b5051b9
3 changed files with 40 additions and 20 deletions

@ -9,12 +9,12 @@ import {Player} from "./Player";
import {hackWorldDaemon, redPillFlag} from "./RedPill";
import {KEY} from "./Terminal";
import {createProgressBarText} from "../utils/helpers/createProgressBarText";
import {dialogBoxCreate} from "../utils/DialogBox";
import {getRandomInt, addOffset, clearObject,
createElement, removeChildrenFromElement,
exceptionAlert, createPopup, appendLineBreaks,
removeElementById, removeElement,
createProgressBarText} from "../utils/HelperFunctions";
removeElementById, removeElement} from "../utils/HelperFunctions";
import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver";
import numeral from "numeral/min/numeral.min";

@ -258,23 +258,6 @@ function exceptionAlert(e) {
"safe doesn't get corrupted");
}
/*Creates a graphical "progress bar"
* e.g.: [||||---------------]
* params:
* @totalTicks - Total number of ticks in progress bar. Preferably a factor of 100
* @progress - Current progress, taken as a decimal (i.e. 0.6 to represent 60%)
*/
function createProgressBarText(params={}) {
//Default values
var totalTicks = (params.totalTicks == null ? 20 : params.totalTicks);
var progress = (params.progress == null ? 0 : params.progress);
var percentPerTick = 1 / totalTicks;
var numTicks = Math.floor(progress / percentPerTick);
var numDashes = totalTicks - numTicks;
return "[" + Array(numTicks+1).join("|") + Array(numDashes+1).join("-") + "]";
}
export {sizeOfObject,
clearObject,
addOffset,
@ -294,5 +277,4 @@ export {sizeOfObject,
createPopup,
clearSelector,
exceptionAlert,
createProgressBarText,
getElementById};

@ -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)}]`;
}