updated update.js to use progressBar

This commit is contained in:
Zakary Timson 2022-03-19 14:00:28 -04:00
parent 88ff84c082
commit 276c452f1c

View File

@ -1,4 +1,4 @@
export class ArgError extends Error {}
class ArgError extends Error {}
class ArgParser {
/**
@ -95,14 +95,39 @@ class ArgParser {
}
/**
* Print a download bar to the terminal.
* @params ns {NS} - Bitburner API
* @params file - Filename to display with progress bar
* Display a progress bar in the terminal which updates in real time.
*
* **Example:**
*
* `/script/test.js [||||||||||----------] 50% (24.2 MB/s)`
*
* @param ns {NS} - BitBurner API
* @param name {string} - Name to display at the begging of bar
* @param showSpeed {boolean} - Show the speed in the progress bar
* @param time {number} - Time it takes for bar to fill
*/
async function downloadPrint(ns, file) {
const speed = ~~(Math.random() * 100) / 10;
const spacing = Array((40 - file.length) || 1).fill(' ').join('');
await slowPrint(ns, `${file}${spacing}[==================>] 100%\t(${speed} MB/s)`);
export async function progressBar(ns, name, showSpeed = true, time = Math.random() + 0.5) {
const text = (percentage, speed) => {
const p = percentage > 1 ? 1 : percentage < 0 ? 0 : percentage;
const spacer = Array(30 - name.length).fill(' ').join('');
const bar = `[${Array(Math.round(20 * p)).fill('|').join('')}${Array(Math.round(20 * (1 - p))).fill('-').join('')}]`;
const percent = `${Math.round(p * 100)}`;
const percentSpacer = Array(3 - percent.length).fill(' ').join('');
return `${name}${spacer}${bar} ${percentSpacer}${percent}%${speed != null ? ` (${speed} MB/s)` : ''}`;
}
let speed = Math.round((20 + Math.random() * 10) * 10) / 10;
ns.tprint(text(1, speed)); // Display the complete bar (This is the one that will be shown on redraws)
await ns.sleep(25); // Wait for the new line to display
const terminalOutput = eval('document').querySelectorAll('[class*="jss"].MuiTypography-body1');
const updateLine = terminalOutput[terminalOutput.length - 1];
const script = updateLine.innerText.split(': ')[0];
for(let p = 0; p <= 100; p++) {
await ns.sleep((time * 1000) / 100);
if(p % 5 == 0) speed = Math.round((speed + (Math.random() > 0.5 ? 1 : -1) * Math.random()) * 10) / 10;
updateLine.innerText = `${script}: ${text(p / 100, showSpeed ? p == 0 ? 0 : speed : null)}`;
}
return;
}
/**
@ -162,7 +187,7 @@ export async function main(ns) {
if(!args['skip-self']) { // Update self & restart
await slowPrint(ns, 'Updating self:');
await ns.wget(`${src}${updateFile}`, `${dest}${updateFile}`, args['device']);
await downloadPrint(ns, `${dest}${updateFile}`);
await progressBar(ns, `${dest}${updateFile}`);
ns.tprint('');
await slowPrint(ns, 'Restarting...');
const pid = ns.run(`${dest}${updateFile}`, 1, args['device'], '--skip-self', '--no-banner');
@ -173,14 +198,14 @@ export async function main(ns) {
await slowPrint(ns, 'Downloading scripts:');
for(let file of fileList) {
await ns.wget(`${src}${file}`, `${dest}${file}`, args['device']);
await downloadPrint(ns, `${dest}${file}`);
await progressBar(ns, `${dest}${file}`);
}
ns.tprint('');
ns.tprint('Done!');
ns.tprint('');
}
} catch(err) {
if(err instanceof ArgParser.ArgError) return ns.tprint(argParser.help(err.message));
if(err instanceof ArgError) return ns.tprint(argParser.help(err.message));
throw err;
}
}