Fixed terminal utility after 1.6.0 update

This commit is contained in:
Zakary Timson 2022-04-03 11:20:36 -04:00
parent aa26f3db93
commit 886900de5d
2 changed files with 20 additions and 10 deletions

View File

@ -14,7 +14,7 @@ export function autocomplete(data) {
* Search the network for a device and connect to it.
* @param ns {NS} - BitBurner API
*/
export function main(ns) {
export async function main(ns) {
// Setup
ns.disableLog('ALL');
const argParser = new ArgParser('connect.js', 'Search the network for a device and connect to it.', null, [
@ -26,11 +26,12 @@ export function main(ns) {
const args = argParser.parse(ns.args);
const [devices, network] = scanNetwork(ns);
pruneTree(network, d => d == args['device']);
let current = network, name;
let current = network, name, path = [];
while(name = Object.keys(current)[0]) {
terminal(`connect ${name}`);
current = current[name];
path.push(name);
}
await terminal('home; ' + path.map(p => `connect ${p}`).join('; '));
} catch(err) {
if(err instanceof ArgError) return ns.tprint(argParser.help(err.message));
throw err;

View File

@ -125,14 +125,23 @@ export async function slowPrint(ns, message, min = 0.5, max = 1.5) {
/**
* Write a command to the terminal.
* @param command {string} - Command that will be run
* @returns {string} - Response
* @returns {Promise<string>} - Command line response
*/
export async function terminal(command) {
// Get Terminal
const cli = eval('document').querySelector("#terminal-input"); // Terminal
const key = Object.keys(cli)[1];
export function terminal(command) {
// Get the terminal
const terminalInput = document.getElementById("terminal-input");
const handler = Object.keys(terminalInput)[1];
// Send command
cli[key].onChange({ target: {value: command} });
cli[key].onKeyDown({ keyCode: 13, preventDefault: () => {} });
terminalInput.value = command; // Enter the command
terminalInput[handler].onChange({target:terminalInput}); // React on change
terminalInput[handler].onKeyDown({key: 'Enter', preventDefault: () => null}); // Enter 'keystroke'
// Return any new terminal output
return new Promise(res => setTimeout(() => {
const terminalOutput = Array.from(eval('document')
.querySelectorAll('#terminal li p')).map(out => out.innerText);
const i = terminalOutput.length - terminalOutput.reverse().findIndex(o => o.indexOf(command) != -1);
res(terminalOutput.reverse().slice(i));
}, 25));
}