bitburner-src/src/Terminal/determineAllPossibilitiesForTabCompletion.ts

328 lines
7.6 KiB
TypeScript
Raw Normal View History

2021-09-09 05:47:34 +02:00
import { evaluateDirectoryPath, getAllParentDirectories } from "./DirectoryHelpers";
import { getSubdirectories } from "./DirectoryServerHelpers";
2019-04-20 07:27:33 +02:00
2021-09-05 01:09:30 +02:00
import { Aliases, GlobalAliases } from "../Alias";
import { DarkWebItems } from "../DarkWeb/DarkWebItems";
import { Message } from "../Message/Message";
2021-09-05 01:09:30 +02:00
import { IPlayer } from "../PersonObjects/IPlayer";
2021-10-07 22:56:01 +02:00
import { GetServer, GetAllServers } from "../Server/AllServers";
// An array of all Terminal commands
const commands = [
2021-09-05 01:09:30 +02:00
"alias",
"analyze",
"backdoor",
"cat",
"cd",
"check",
"clear",
"cls",
"connect",
2021-10-12 04:35:00 +02:00
"cp",
2021-09-05 01:09:30 +02:00
"download",
"expr",
"free",
"grow",
2021-09-05 01:09:30 +02:00
"hack",
"help",
"home",
"hostname",
"ifconfig",
"kill",
"killall",
"ls",
"lscpu",
"mem",
"mv",
"nano",
"ps",
"rm",
"run",
"scan-analyze",
"scan",
2021-09-05 01:09:30 +02:00
"scp",
"sudov",
"tail",
"theme",
"top",
"weaken",
];
2021-09-05 01:09:30 +02:00
export function determineAllPossibilitiesForTabCompletion(
p: IPlayer,
input: string,
index: number,
currPath = "",
): string[] {
let allPos: string[] = [];
allPos = allPos.concat(Object.keys(GlobalAliases));
const currServ = p.getCurrentServer();
const homeComputer = p.getHomeComputer();
let parentDirPath = "";
let evaledParentDirPath: string | null = null;
// Helper functions
function addAllCodingContracts(): void {
for (const cct of currServ.contracts) {
allPos.push(cct.fn);
}
2021-09-05 01:09:30 +02:00
}
2021-09-05 01:09:30 +02:00
function addAllLitFiles(): void {
for (const file of currServ.messages) {
2021-10-14 08:07:05 +02:00
if (!file.endsWith(".msg")) {
2021-09-05 01:09:30 +02:00
allPos.push(file);
}
}
2021-09-05 01:09:30 +02:00
}
2021-09-05 01:09:30 +02:00
function addAllMessages(): void {
for (const file of currServ.messages) {
2021-10-14 08:07:05 +02:00
if (file.endsWith(".msg")) {
allPos.push(file);
2021-09-05 01:09:30 +02:00
}
}
2021-09-05 01:09:30 +02:00
}
2021-09-05 01:09:30 +02:00
function addAllPrograms(): void {
for (const program of homeComputer.programs) {
allPos.push(program);
}
2021-09-05 01:09:30 +02:00
}
function addAllScripts(): void {
for (const script of currServ.scripts) {
const res = processFilepath(script.filename);
if (res) {
allPos.push(res);
}
}
2021-09-05 01:09:30 +02:00
}
function addAllTextFiles(): void {
for (const txt of currServ.textFiles) {
const res = processFilepath(txt.fn);
if (res) {
allPos.push(res);
}
2019-04-20 07:27:33 +02:00
}
2021-09-05 01:09:30 +02:00
}
function addAllDirectories(): void {
// Directories are based on the currently evaluated path
2021-09-09 05:47:34 +02:00
const subdirs = getSubdirectories(currServ, evaledParentDirPath == null ? "/" : evaledParentDirPath);
2021-09-05 01:09:30 +02:00
for (let i = 0; i < subdirs.length; ++i) {
2021-09-09 05:47:34 +02:00
const assembledDirPath = evaledParentDirPath == null ? subdirs[i] : evaledParentDirPath + subdirs[i];
2021-09-05 01:09:30 +02:00
const res = processFilepath(assembledDirPath);
if (res != null) {
subdirs[i] = res;
}
2019-04-20 07:27:33 +02:00
}
2021-09-05 01:09:30 +02:00
allPos = allPos.concat(subdirs);
}
2019-04-20 07:27:33 +02:00
2021-09-05 01:09:30 +02:00
// Convert from the real absolute path back to the original path used in the input
function convertParentPath(filepath: string): string {
if (parentDirPath == null || evaledParentDirPath == null) {
console.warn(`convertParentPath() called when paths are null`);
return filepath;
2019-04-20 07:27:33 +02:00
}
2021-09-05 01:09:30 +02:00
if (!filepath.startsWith(evaledParentDirPath)) {
console.warn(
`convertParentPath() called for invalid path. (filepath=${filepath}) (evaledParentDirPath=${evaledParentDirPath})`,
);
return filepath;
}
2021-09-05 01:09:30 +02:00
return parentDirPath + filepath.slice(evaledParentDirPath.length);
}
// Given an a full, absolute filepath, converts it to the proper value
// for autocompletion purposes
function processFilepath(filepath: string): string | null {
if (evaledParentDirPath) {
if (filepath.startsWith(evaledParentDirPath)) {
return convertParentPath(filepath);
}
} else if (parentDirPath !== "") {
// If the parent directory is the root directory, but we're not searching
// it from the root directory, we have to add the original path
let t_parentDirPath = parentDirPath;
if (!t_parentDirPath.endsWith("/")) {
t_parentDirPath += "/";
}
return parentDirPath + filepath;
} else {
return filepath;
}
2021-09-05 01:09:30 +02:00
return null;
}
function isCommand(cmd: string): boolean {
let t_cmd = cmd;
if (!t_cmd.endsWith(" ")) {
t_cmd += " ";
}
2021-09-05 01:09:30 +02:00
return input.startsWith(t_cmd);
}
/**
* If the command starts with './' and the index == -1, then the user
* has input ./partialexecutablename so autocomplete the script or program.
* Put './' in front of each script/executable
*/
if (isCommand("./") && index == -1) {
//All programs and scripts
for (let i = 0; i < currServ.scripts.length; ++i) {
allPos.push("./" + currServ.scripts[i].filename);
}
2021-09-05 01:09:30 +02:00
//Programs are on home computer
for (let i = 0; i < homeComputer.programs.length; ++i) {
allPos.push("./" + homeComputer.programs[i]);
2019-04-20 07:27:33 +02:00
}
2021-09-05 01:09:30 +02:00
return allPos;
}
// Autocomplete the command
if (index === -1) {
2021-09-09 05:47:34 +02:00
return commands.concat(Object.keys(Aliases)).concat(Object.keys(GlobalAliases));
2021-09-05 01:09:30 +02:00
}
// Since we're autocompleting an argument and not a command, the argument might
// be a file/directory path. We have to account for that when autocompleting
const commandArray = input.split(" ");
if (commandArray.length === 0) {
console.warn(`Tab autocompletion logic reached invalid branch`);
return allPos;
}
const arg = commandArray[commandArray.length - 1];
parentDirPath = getAllParentDirectories(arg);
evaledParentDirPath = evaluateDirectoryPath(parentDirPath, currPath);
if (evaledParentDirPath === "/") {
evaledParentDirPath = null;
} else if (evaledParentDirPath == null) {
return allPos; // Invalid path
} else {
evaledParentDirPath += "/";
}
if (isCommand("buy")) {
const options = [];
for (const i in DarkWebItems) {
const item = DarkWebItems[i];
options.push(item.program);
2019-04-20 07:27:33 +02:00
}
2021-09-05 01:09:30 +02:00
return options.concat(Object.keys(GlobalAliases));
}
2021-09-05 01:09:30 +02:00
if (isCommand("scp") && index === 1) {
2021-10-07 22:04:04 +02:00
for (const server of GetAllServers()) {
allPos.push(server.hostname);
}
2021-09-05 01:09:30 +02:00
return allPos;
}
2021-09-05 01:09:30 +02:00
if (isCommand("scp") && index === 0) {
addAllScripts();
addAllLitFiles();
addAllTextFiles();
addAllDirectories();
2021-09-05 01:09:30 +02:00
return allPos;
}
2021-10-12 04:35:00 +02:00
if (isCommand("cp") && index === 0) {
addAllScripts();
addAllTextFiles();
addAllDirectories();
return allPos;
}
2021-09-05 01:09:30 +02:00
if (isCommand("connect")) {
// All network connections
for (let i = 0; i < currServ.serversOnNetwork.length; ++i) {
2021-10-07 22:56:01 +02:00
const serv = GetServer(currServ.serversOnNetwork[i]);
2021-09-05 01:09:30 +02:00
if (serv == null) {
continue;
}
allPos.push(serv.hostname);
}
2021-09-05 01:09:30 +02:00
return allPos;
}
2021-09-09 05:47:34 +02:00
if (isCommand("kill") || isCommand("tail") || isCommand("mem") || isCommand("check")) {
2021-09-05 01:09:30 +02:00
addAllScripts();
addAllDirectories();
2021-09-05 01:09:30 +02:00
return allPos;
}
2021-09-05 01:09:30 +02:00
if (isCommand("nano")) {
addAllScripts();
addAllTextFiles();
allPos.push(".fconf");
addAllDirectories();
2021-09-05 01:09:30 +02:00
return allPos;
}
2021-09-05 01:09:30 +02:00
if (isCommand("rm")) {
addAllScripts();
addAllPrograms();
addAllLitFiles();
addAllTextFiles();
addAllCodingContracts();
addAllDirectories();
2021-09-05 01:09:30 +02:00
return allPos;
}
2021-09-05 01:09:30 +02:00
if (isCommand("run")) {
addAllScripts();
addAllPrograms();
addAllCodingContracts();
addAllDirectories();
2021-09-05 01:09:30 +02:00
return allPos;
}
2021-09-05 01:09:30 +02:00
if (isCommand("cat")) {
addAllMessages();
addAllLitFiles();
addAllTextFiles();
addAllDirectories();
2021-09-05 01:09:30 +02:00
return allPos;
}
2021-09-05 01:09:30 +02:00
if (isCommand("download") || isCommand("mv")) {
addAllScripts();
addAllTextFiles();
addAllDirectories();
2019-04-20 07:27:33 +02:00
2021-09-05 01:09:30 +02:00
return allPos;
}
2019-04-20 07:27:33 +02:00
2021-09-05 01:09:30 +02:00
if (isCommand("cd")) {
addAllDirectories();
2021-09-05 01:09:30 +02:00
return allPos;
}
2021-09-05 01:09:30 +02:00
if (isCommand("ls") && index === 0) {
addAllDirectories();
}
2019-04-20 07:27:33 +02:00
2021-09-05 01:09:30 +02:00
return allPos;
}