This commit is contained in:
danielyxie
2019-04-19 22:27:33 -07:00
parent bf1af6a68c
commit bcb198220d
18 changed files with 384 additions and 186 deletions
@@ -1,3 +1,9 @@
import {
evaluateDirectoryPath,
getAllParentDirectories,
getSubdirectories,
} from "./DirectoryHelpers";
import {
Aliases,
GlobalAliases
@@ -44,12 +50,14 @@ const commands = [
"top"
];
export function determineAllPossibilitiesForTabCompletion(p: IPlayer, input: string, index: number=0): string[] {
export function determineAllPossibilitiesForTabCompletion(p: IPlayer, input: string, index: number, currPath: string=""): string[] {
let allPos: string[] = [];
allPos = allPos.concat(Object.keys(GlobalAliases));
const currServ = p.getCurrentServer();
const homeComputer = p.getHomeComputer();
input = input.toLowerCase();
let parentDirPath: string = "";
let evaledParentDirPath: string | null = null;
// Helper functions
function addAllCodingContracts() {
@@ -82,16 +90,72 @@ export function determineAllPossibilitiesForTabCompletion(p: IPlayer, input: str
function addAllScripts() {
for (const script of currServ.scripts) {
allPos.push(script.filename);
const res = processFilepath(script.filename);
if (res) {
allPos.push(res);
}
}
}
function addAllTextFiles() {
for (const txt of currServ.textFiles) {
allPos.push(txt.fn);
const res = processFilepath(txt.fn);
if (res) {
allPos.push(res);
}
}
}
function addAllDirectories() {
// Directories are based on the currently evaluated path
const subdirs = getSubdirectories(currServ, evaledParentDirPath == null ? "/" : evaledParentDirPath);
for (let i = 0; i < subdirs.length; ++i) {
const assembledDirPath = (evaledParentDirPath == null ? subdirs[i] : evaledParentDirPath + subdirs[i]);
const res = processFilepath(assembledDirPath);
if (res != null) {
subdirs[i] = res;
}
}
allPos = allPos.concat(subdirs);
}
// 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;
}
if (!filepath.startsWith(evaledParentDirPath)) {
console.warn(`convertParentPath() called for invalid path. (filepath=${filepath}) (evaledParentDirPath=${evaledParentDirPath})`);
return filepath;
}
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;
}
return null;
}
function isCommand(cmd: string) {
let t_cmd = cmd;
if (!t_cmd.endsWith(" ")) {
@@ -120,10 +184,28 @@ export function determineAllPossibilitiesForTabCompletion(p: IPlayer, input: str
}
// Autocomplete the command
if (index == -1) {
if (index === -1) {
return commands.concat(Object.keys(Aliases)).concat(Object.keys(GlobalAliases));
}
// 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")) {
let options = [];
for (const i in DarkWebItems) {
@@ -147,6 +229,7 @@ export function determineAllPossibilitiesForTabCompletion(p: IPlayer, input: str
addAllScripts();
addAllLitFiles();
addAllTextFiles();
addAllDirectories();
return allPos;
}
@@ -165,6 +248,7 @@ export function determineAllPossibilitiesForTabCompletion(p: IPlayer, input: str
if (isCommand("kill") || isCommand("tail") || isCommand("mem") || isCommand("check")) {
addAllScripts();
addAllDirectories();
return allPos;
}
@@ -173,6 +257,7 @@ export function determineAllPossibilitiesForTabCompletion(p: IPlayer, input: str
addAllScripts();
addAllTextFiles();
allPos.push(".fconf");
addAllDirectories();
return allPos;
}
@@ -183,6 +268,7 @@ export function determineAllPossibilitiesForTabCompletion(p: IPlayer, input: str
addAllLitFiles();
addAllTextFiles();
addAllCodingContracts();
addAllDirectories();
return allPos;
}
@@ -191,6 +277,7 @@ export function determineAllPossibilitiesForTabCompletion(p: IPlayer, input: str
addAllScripts();
addAllPrograms();
addAllCodingContracts();
addAllDirectories();
return allPos;
}
@@ -199,6 +286,7 @@ export function determineAllPossibilitiesForTabCompletion(p: IPlayer, input: str
addAllMessages();
addAllLitFiles();
addAllTextFiles();
addAllDirectories();
return allPos;
}
@@ -206,9 +294,20 @@ export function determineAllPossibilitiesForTabCompletion(p: IPlayer, input: str
if (isCommand("download") || isCommand("mv")) {
addAllScripts();
addAllTextFiles();
addAllDirectories();
return allPos;
}
if (isCommand("cd")) {
addAllDirectories();
return allPos;
}
if (isCommand("ls") && index === 0) {
addAllDirectories();
}
return allPos;
}