Added copy.js

This commit is contained in:
Zakary Timson 2022-03-16 19:07:36 -04:00
parent 3753ae02db
commit 82c8f4006b
3 changed files with 128 additions and 8 deletions

View File

@ -7,6 +7,7 @@ These scripts are for playing the [open source](https://github.com/danielyxie/bi
- [Quick Start](#quick-start)
- [Scripts](#scripts)
- [connect.js](#connectjs)
- [copy.js](#copyjs)
- [crawler.js](#crawlerjs)
- [hacknet-manager.js](#hacknet-managerjs)
- [miner.js](#minerjs)
@ -61,6 +62,29 @@ Options:
-h --help Display this help message
```
### [copy.js](./scripts/copy.js)
**RAM:** 3.50 GB
Copy a file/script to a device along with any dependencies.
```
[home ~/]> run scripts/copy.js --help
Running script with 1 thread(s), pid 1 and args: ["--help"].
/scripts/copy.js:
Copy a file/script to a device along with any dependencies.
Usage: run copy.js [OPTIONS] FILE DEVICE
run copy.js --help
FILE File to copy
DEVICE Device to copy file(s) to
Options:
-d --no-deps Skip copying dependencies
-s --silent Surpress program output
-h --help Display this help message
```
### [crawler.js](./scripts/crawler.js)
**RAM:** 4.05 GB

61
scripts/copy.js Normal file
View File

@ -0,0 +1,61 @@
import {ArgError, ArgParser} from '/scripts/lib/arg-parser';
import {copyWithDependencies, percentageBar} from '/scripts/lib/utils';
/**
* BitBurner autocomplete
* @param data {server: string[], txts: string[], scripts: string[], flags: string[]} - Contextual information
* @returns {string[]} - Pool of autocomplete options
*/
export function autocomplete(data) {
return [...data.servers, ...data.scripts];
}
/** @param {NS} ns **/
export async function main(ns) {
// Setup
ns.disableLog('ALL');
const argParser = new ArgParser('copy.js', 'Copy a file/script to a device along with any dependencies.', null, [
{name: 'file', desc: 'File to copy', type: 'string'},
{name: 'device', desc: 'Device to copy file(s) to', type: 'string'},
{name: 'noDeps', desc: 'Skip copying dependencies', flags: ['-d', '--no-deps'], type: 'bool'},
{name: 'silent', desc: 'Surpress program output', flags: ['-s', '--silent'], type: 'bool'}
], true);
try {
// Run
const args = argParser.parse(ns.args);
// Banner
if(!args['silent']) {
ns.tprint('===================================================');
ns.tprint(`Copying: ${args['device']}`);
ns.tprint('===================================================');
ns.tprint('');
ns.tprint('Copying Files:');
await ns.sleep(500);
}
// Copy files & create download bar
if(args['noDeps']) {
await ns.scp(args['file'], args['device']);
if(!args['silent']) await percentageBar(ns, args['file']);
} else {
const files = await copyWithDependencies(ns, args['file'], args['device']);
if(!args['silent']) {
for(let file of files) {
await percentageBar(ns, file, 1);
}
}
}
// Done message
if(!args['silent']) {
ns.tprint('');
ns.tprint('Done!');
ns.tprint('');
}
} catch(err) {
if(err instanceof ArgError) return ns.tprint(argParser.help(err.message));
throw err;
}
}

View File

@ -19,7 +19,42 @@ export async function copyWithDependencies(ns, src, device) {
}
}
await ns.scp(found, device);
return found;
return found.reverse();
}
/**
* Display a percentage 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 time {number} - Time it takes for bar to fill
*/
export async function percentageBar(ns, name, time = 1) {
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, p == 0 ? 0 : speed)}`;
}
return;
}
/**