bitburner-src/src/Terminal/tabCompletion.ts

94 lines
2.9 KiB
TypeScript
Raw Normal View History

2021-09-25 20:42:57 +02:00
import { containsAllStrings, longestCommonStart } from "../utils/StringHelperFunctions";
/**
* Implements tab completion for the Terminal
*
* @param command {string} Terminal command, excluding the last incomplete argument
* @param arg {string} Last argument that is being completed
* @param allPossibilities {string[]} All values that `arg` can complete to
*/
2021-09-16 08:52:45 +02:00
export function tabCompletion(
command: string,
arg: string,
allPossibilities: string[],
oldValue: string,
): string[] | string | undefined {
2021-09-05 01:09:30 +02:00
if (!(allPossibilities.constructor === Array)) {
return;
}
if (!containsAllStrings(allPossibilities)) {
return;
}
// Remove all options in allPossibilities that do not match the current string
// that we are attempting to autocomplete
if (arg === "") {
for (let i = allPossibilities.length - 1; i >= 0; --i) {
2021-09-09 05:47:34 +02:00
if (!allPossibilities[i].toLowerCase().startsWith(command.toLowerCase())) {
2021-09-05 01:09:30 +02:00
allPossibilities.splice(i, 1);
}
}
} else {
for (let i = allPossibilities.length - 1; i >= 0; --i) {
if (!allPossibilities[i].toLowerCase().startsWith(arg.toLowerCase())) {
allPossibilities.splice(i, 1);
}
}
}
const semiColonIndex = oldValue.lastIndexOf(";");
let val = "";
if (allPossibilities.length === 0) {
return;
} else if (allPossibilities.length === 1) {
if (arg === "") {
2021-09-05 01:09:30 +02:00
//Autocomplete command
2021-10-15 04:36:28 +02:00
val = allPossibilities[0];
} else {
2021-09-05 01:09:30 +02:00
val = command + " " + allPossibilities[0];
}
2021-09-05 01:09:30 +02:00
if (semiColonIndex === -1) {
// No semicolon, so replace the whole command
2021-09-16 08:52:45 +02:00
return val;
2021-09-05 01:09:30 +02:00
} else {
// Replace only after the last semicolon
2021-09-16 08:52:45 +02:00
return oldValue.slice(0, semiColonIndex + 1) + " " + val;
}
2021-09-05 01:09:30 +02:00
} else {
const longestStartSubstr = longestCommonStart(allPossibilities);
/**
* If the longest common starting substring of remaining possibilities is the same
* as whatevers already in terminal, just list all possible options. Otherwise,
* change the input in the terminal to the longest common starting substr
*/
if (arg === "") {
if (longestStartSubstr === command) {
2021-09-16 08:52:45 +02:00
return allPossibilities;
2021-09-05 01:09:30 +02:00
} else {
if (semiColonIndex === -1) {
2021-09-05 01:09:30 +02:00
// No semicolon, so replace the whole command
2021-09-16 08:52:45 +02:00
return longestStartSubstr;
} else {
2021-09-05 01:09:30 +02:00
// Replace only after the last semicolon
2021-09-16 08:52:45 +02:00
return `${oldValue.slice(0, semiColonIndex + 1)} ${longestStartSubstr}`;
}
2021-09-05 01:09:30 +02:00
}
} else {
2021-09-05 01:09:30 +02:00
if (longestStartSubstr === arg) {
// List all possible options
2021-09-16 08:52:45 +02:00
return allPossibilities;
2021-09-05 01:09:30 +02:00
} else {
if (semiColonIndex == -1) {
// No semicolon, so replace the whole command
2021-09-16 08:52:45 +02:00
return `${command} ${longestStartSubstr}`;
} else {
2021-09-05 01:09:30 +02:00
// Replace only after the last semicolon
2021-09-16 08:52:45 +02:00
return `${oldValue.slice(0, semiColonIndex + 1)} ${command} ${longestStartSubstr}`;
}
2021-09-05 01:09:30 +02:00
}
}
2021-09-05 01:09:30 +02:00
}
}