bitburner/scripts/lib/utils.js
2022-03-11 12:05:34 +00:00

39 lines
1.3 KiB
JavaScript

/**
* 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)`);
}
/**
* 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);
}
/**
* 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: () => {} });
}