bitburner/scripts/lib/utils.js

39 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-03-09 14:06:14 -05:00
/**
* Print a download bar to the terminal.
* @params ns {NS} - Bitburner API
* @params file - Filename to display with progress bar
*/
export 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)`);
2022-03-09 14:06:14 -05:00
}
/**
* Print text to the terminal & then delay for a random amount of time to emulate execution time.
* @params ns {NS} - Bitburner API
* @params message {string} - Text to display
* @params min {number} - minimum amount of time to wait after printing text
* @params max {number} - maximum amount of time to wait after printing text
*/
export async function slowPrint(ns, message, min = 0.5, max = 1.5) {
const time = ~~(Math.random() * (max * 1000 - min * 1000)) + min * 1000;
ns.tprint(message);
await ns.sleep(time);
}
2022-03-11 07:05:34 -05:00
/**
* Write a command to the terminal.
* @params command {string} - Command that will be run
* @returns {string} - Response
*/
export async function terminal(command) {
// Get Terminal
const cli = eval('document').querySelector("#terminal-input"); // Terminal
const key = Object.keys(cli)[1];
// Send command
cli[key].onChange({ target: {value: command} });
cli[key].onKeyDown({ keyCode: 13, preventDefault: () => {} });
}