autocomplete

This commit is contained in:
Olivier Gagnon
2021-10-14 22:36:28 -04:00
parent 3fddb3c9f2
commit 4603216aa0
5 changed files with 32 additions and 19 deletions

View File

@ -543,7 +543,6 @@ export class Terminal implements ITerminal {
}
clear(): void {
// TODO: remove this once we figure out the height issue.
this.outputHistory = [new Output(`Bitburner v${CONSTANTS.Version}`, "primary")];
TerminalEvents.emit();
TerminalClearEvents.emit();

View File

@ -19,15 +19,6 @@ export function run(
} else {
const executableName = args[0] + "";
// Secret Music player!
// if (executableName === "musicplayer") {
// post(
// '<iframe src="https://open.spotify.com/embed/user/danielyxie/playlist/1ORnnL6YNvXOracUaUV2kh" width="300" height="380" frameborder="0" allowtransparency="true"></iframe>',
// false,
// );
// return;
// }
// Check if its a script or just a program/executable
if (isScriptFilename(executableName)) {
runScript(terminal, router, player, server, args);

View File

@ -3,9 +3,10 @@ import { getSubdirectories } from "./DirectoryServerHelpers";
import { Aliases, GlobalAliases } from "../Alias";
import { DarkWebItems } from "../DarkWeb/DarkWebItems";
import { Message } from "../Message/Message";
import { IPlayer } from "../PersonObjects/IPlayer";
import { GetServer, GetAllServers } from "../Server/AllServers";
import { ParseCommand, ParseCommands } from "./Parser";
import { isScriptFilename } from "../Script/isScriptFilename";
// An array of all Terminal commands
const commands = [
@ -48,12 +49,12 @@ const commands = [
"weaken",
];
export function determineAllPossibilitiesForTabCompletion(
export async function determineAllPossibilitiesForTabCompletion(
p: IPlayer,
input: string,
index: number,
currPath = "",
): string[] {
): Promise<string[]> {
let allPos: string[] = [];
allPos = allPos.concat(Object.keys(GlobalAliases));
const currServ = p.getCurrentServer();
@ -287,13 +288,34 @@ export function determineAllPossibilitiesForTabCompletion(
return allPos;
}
async function scriptAutocomplete(): Promise<string[] | undefined> {
if (!isCommand("run")) return;
const commands = ParseCommands(input);
if (commands.length === 0) return;
const command = ParseCommand(commands[commands.length - 1]);
const filename = command[1] + "";
if (!isScriptFilename(filename)) return; // Not a script.
if (filename.endsWith(".script")) return; // Doesn't work with ns1.
const script = currServ.scripts.find((script) => script.filename === filename);
if (!script) return; // Doesn't exist.
// TODO compile if needs be.
console.log("asd1");
if (!script.module) return;
const loadedModule = await script.module;
console.log("asd2");
if (!loadedModule.autocomplete) return; // Doesn't have an autocomplete function.
console.log(loadedModule.autocomplete());
return loadedModule.autocomplete();
}
const pos = await scriptAutocomplete();
console.log(pos);
if (pos) return pos;
if (isCommand("run")) {
addAllScripts();
addAllPrograms();
addAllCodingContracts();
addAllDirectories();
return allPos;
}
if (isCommand("cat")) {

View File

@ -44,7 +44,7 @@ export function tabCompletion(
} else if (allPossibilities.length === 1) {
if (arg === "") {
//Autocomplete command
val = allPossibilities[0] + " ";
val = allPossibilities[0];
} else {
val = command + " " + allPossibilities[0];
}

View File

@ -162,7 +162,7 @@ export function TerminalInput({ terminal, router, player }: IProps): React.React
return () => document.removeEventListener("keydown", keyDown);
});
function onKeyDown(event: React.KeyboardEvent<HTMLInputElement>): void {
async function onKeyDown(event: React.KeyboardEvent<HTMLInputElement>): Promise<void> {
// Run command.
if (event.keyCode === KEY.ENTER && value !== "") {
event.preventDefault();
@ -190,7 +190,7 @@ export function TerminalInput({ terminal, router, player }: IProps): React.React
if (index < -1) {
index = 0;
}
const allPos = determineAllPossibilitiesForTabCompletion(player, copy, index, terminal.cwd());
const allPos = await determineAllPossibilitiesForTabCompletion(player, copy, index, terminal.cwd());
if (allPos.length == 0) {
return;
}
@ -213,8 +213,9 @@ export function TerminalInput({ terminal, router, player }: IProps): React.React
command = commandArray.join(" ");
}
const newValue = tabCompletion(command, arg, allPos, value);
let newValue = tabCompletion(command, arg, allPos, value);
if (typeof newValue === "string" && newValue !== "") {
if (!newValue.endsWith(" ") && allPos.length === 1) newValue += " ";
saveValue(newValue);
}
if (Array.isArray(newValue)) {