terminal parses boolean exactly.

This commit is contained in:
Olivier Gagnon 2021-12-03 14:44:32 -05:00
parent 99ed9b25a3
commit 13b08d7cc8
40 changed files with 67 additions and 55 deletions

@ -87,7 +87,7 @@ export function scriptCalculateOfflineProduction(runningScript: RunningScript):
//designated server, and false otherwise //designated server, and false otherwise
export function findRunningScript( export function findRunningScript(
filename: string, filename: string,
args: (string | number)[], args: (string | number | boolean)[],
server: BaseServer, server: BaseServer,
): RunningScript | null { ): RunningScript | null {
for (let i = 0; i < server.runningScripts.length; ++i) { for (let i = 0; i < server.runningScripts.length; ++i) {

@ -31,7 +31,7 @@ export function ParseCommands(commands: string): string[] {
return out; return out;
} }
export function ParseCommand(command: string): (string | number)[] { export function ParseCommand(command: string): (string | number | boolean)[] {
// This will be used to keep track of whether we're in a quote. This is for situations // This will be used to keep track of whether we're in a quote. This is for situations
// like the alias command: // like the alias command:
// alias run="run NUKE.exe" // alias run="run NUKE.exe"
@ -101,6 +101,10 @@ export function ParseCommand(command: string): (string | number)[] {
// If this is a number, convert it from a string to number // If this is a number, convert it from a string to number
if (isNumber(arg)) { if (isNumber(arg)) {
args.push(parseFloat(arg)); args.push(parseFloat(arg));
} else if (arg === "true") {
args.push(true);
} else if (arg === "false") {
args.push(false);
} else { } else {
args.push(arg); args.push(arg);
} }

@ -718,7 +718,7 @@ export class Terminal implements ITerminal {
/****************** END INTERACTIVE TUTORIAL ******************/ /****************** END INTERACTIVE TUTORIAL ******************/
/* Command parser */ /* Command parser */
const commandName = commandArray[0]; const commandName = commandArray[0];
if (typeof commandName === "number") { if (typeof commandName === "number" || typeof commandName === "boolean") {
this.error(`Command ${commandArray[0]} not found`); this.error(`Command ${commandArray[0]} not found`);
return; return;
} }
@ -729,7 +729,7 @@ export class Terminal implements ITerminal {
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
) => void; ) => void;
} = { } = {
"scan-analyze": scananalyze, "scan-analyze": scananalyze,

@ -9,7 +9,7 @@ export function alias(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length === 0) { if (args.length === 0) {
printAliases(); printAliases();

@ -8,7 +8,7 @@ export function analyze(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length !== 0) { if (args.length !== 0) {
terminal.error("Incorrect usage of analyze command. Usage: analyze"); terminal.error("Incorrect usage of analyze command. Usage: analyze");

@ -10,7 +10,7 @@ export function backdoor(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length !== 0) { if (args.length !== 0) {
terminal.error("Incorrect usage of backdoor command. Usage: backdoor"); terminal.error("Incorrect usage of backdoor command. Usage: backdoor");

@ -11,7 +11,7 @@ export function buy(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (!GetServer(SpecialServers.DarkWeb)) { if (!GetServer(SpecialServers.DarkWeb)) {
terminal.error( terminal.error(

@ -10,7 +10,7 @@ export function cat(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length !== 1) { if (args.length !== 1) {
terminal.error("Incorrect usage of cat command. Usage: cat [file]"); terminal.error("Incorrect usage of cat command. Usage: cat [file]");

@ -10,7 +10,7 @@ export function cd(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length > 1) { if (args.length > 1) {
terminal.error("Incorrect number of arguments. Usage: cd [dir]"); terminal.error("Incorrect number of arguments. Usage: cd [dir]");

@ -10,7 +10,7 @@ export function check(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length < 1) { if (args.length < 1) {
terminal.error("Incorrect number of arguments. Usage: check [script] [arg1] [arg2]..."); terminal.error("Incorrect number of arguments. Usage: check [script] [arg1] [arg2]...");

@ -9,7 +9,7 @@ export function connect(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
// Disconnect from current server in terminal and connect to new one // Disconnect from current server in terminal and connect to new one
if (args.length !== 1) { if (args.length !== 1) {

@ -9,7 +9,7 @@ export function cp(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
try { try {
if (args.length !== 2) { if (args.length !== 2) {

@ -11,7 +11,7 @@ export function download(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
try { try {
if (args.length !== 1) { if (args.length !== 1) {
@ -24,7 +24,7 @@ export function download(
const matchEnding = fn.length == 1 || fn === "*.*" ? null : fn.slice(1); // Treat *.* the same as * const matchEnding = fn.length == 1 || fn === "*.*" ? null : fn.slice(1); // Treat *.* the same as *
const zip = new JSZip(); const zip = new JSZip();
// Helper function to zip any file contents whose name matches the pattern // Helper function to zip any file contents whose name matches the pattern
let zipFiles = (fileNames: string[], fileContents: string[]) => { const zipFiles = (fileNames: string[], fileContents: string[]) => {
for (let i = 0; i < fileContents.length; ++i) { for (let i = 0; i < fileContents.length; ++i) {
let name = fileNames[i]; let name = fileNames[i];
if (name.startsWith("/")) name = name.slice(1); if (name.startsWith("/")) name = name.slice(1);
@ -34,13 +34,18 @@ export function download(
}; };
// In the case of script files, we pull from the server.scripts array // In the case of script files, we pull from the server.scripts array
if (!matchEnding || isScriptFilename(matchEnding)) if (!matchEnding || isScriptFilename(matchEnding))
zipFiles(server.scripts.map(s => s.filename), server.scripts.map(s => s.code)); zipFiles(
server.scripts.map((s) => s.filename),
server.scripts.map((s) => s.code),
);
// In the case of text files, we pull from the server.scripts array // In the case of text files, we pull from the server.scripts array
if (!matchEnding || matchEnding.endsWith(".txt")) if (!matchEnding || matchEnding.endsWith(".txt"))
zipFiles(server.textFiles.map(s => s.fn), server.textFiles.map(s => s.text)); zipFiles(
server.textFiles.map((s) => s.fn),
server.textFiles.map((s) => s.text),
);
// Return an error if no files matched, rather than an empty zip folder // Return an error if no files matched, rather than an empty zip folder
if (Object.keys(zip.files).length == 0) if (Object.keys(zip.files).length == 0) return terminal.error(`No files match the pattern ${fn}`);
return terminal.error(`No files match the pattern ${fn}`);
const zipFn = `bitburner${isScriptFilename(fn) ? "Scripts" : fn === "*.txt" ? "Texts" : "Files"}.zip`; const zipFn = `bitburner${isScriptFilename(fn) ? "Scripts" : fn === "*.txt" ? "Texts" : "Files"}.zip`;
zip.generateAsync({ type: "blob" }).then((content: any) => FileSaver.saveAs(content, zipFn)); zip.generateAsync({ type: "blob" }).then((content: any) => FileSaver.saveAs(content, zipFn));
return; return;

@ -8,7 +8,7 @@ export function expr(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length === 0) { if (args.length === 0) {
terminal.error("Incorrect usage of expr command. Usage: expr [math expression]"); terminal.error("Incorrect usage of expr command. Usage: expr [math expression]");

@ -9,7 +9,7 @@ export function free(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length !== 0) { if (args.length !== 0) {
terminal.error("Incorrect usage of free command. Usage: free"); terminal.error("Incorrect usage of free command. Usage: free");

@ -9,7 +9,7 @@ export function grow(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length !== 0) { if (args.length !== 0) {
terminal.error("Incorrect usage of grow command. Usage: grow"); terminal.error("Incorrect usage of grow command. Usage: grow");

@ -9,7 +9,7 @@ export function hack(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length !== 0) { if (args.length !== 0) {
terminal.error("Incorrect usage of hack command. Usage: hack"); terminal.error("Incorrect usage of hack command. Usage: hack");

@ -9,7 +9,7 @@ export function help(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length !== 0 && args.length !== 1) { if (args.length !== 0 && args.length !== 1) {
terminal.error("Incorrect usage of help command. Usage: help"); terminal.error("Incorrect usage of help command. Usage: help");
@ -18,7 +18,7 @@ export function help(
if (args.length === 0) { if (args.length === 0) {
TerminalHelpText.forEach((line) => terminal.print(line)); TerminalHelpText.forEach((line) => terminal.print(line));
} else { } else {
const cmd = args[0]; const cmd = args[0] + "";
const txt = HelpTexts[cmd]; const txt = HelpTexts[cmd];
if (txt == null) { if (txt == null) {
terminal.error("No help topics match '" + cmd + "'"); terminal.error("No help topics match '" + cmd + "'");

@ -8,7 +8,7 @@ export function home(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length !== 0) { if (args.length !== 0) {
terminal.error("Incorrect usage of home command. Usage: home"); terminal.error("Incorrect usage of home command. Usage: home");

@ -8,7 +8,7 @@ export function hostname(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length !== 0) { if (args.length !== 0) {
terminal.error("Incorrect usage of hostname command. Usage: hostname"); terminal.error("Incorrect usage of hostname command. Usage: hostname");

@ -9,13 +9,16 @@ export function kill(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
try { try {
if (args.length < 1) { if (args.length < 1) {
terminal.error("Incorrect usage of kill command. Usage: kill [scriptname] [arg1] [arg2]..."); terminal.error("Incorrect usage of kill command. Usage: kill [scriptname] [arg1] [arg2]...");
return; return;
} }
if (typeof args[0] === "boolean") {
return;
}
// Kill by PID // Kill by PID
if (typeof args[0] === "number") { if (typeof args[0] === "number") {

@ -10,7 +10,7 @@ export function ls(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
const numArgs = args.length; const numArgs = args.length;
function incorrectUsage(): void { function incorrectUsage(): void {

@ -9,7 +9,7 @@ export function mem(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
try { try {
if (args.length !== 1 && args.length !== 3) { if (args.length !== 1 && args.length !== 3) {

@ -11,7 +11,7 @@ export function mv(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length !== 2) { if (args.length !== 2) {
terminal.error(`Incorrect number of arguments. Usage: mv [src] [dest]`); terminal.error(`Incorrect number of arguments. Usage: mv [src] [dest]`);

@ -10,7 +10,7 @@ export function nano(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length !== 1) { if (args.length !== 1) {
terminal.error("Incorrect usage of nano command. Usage: nano [scriptname]"); terminal.error("Incorrect usage of nano command. Usage: nano [scriptname]");

@ -8,7 +8,7 @@ export function ps(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length !== 0) { if (args.length !== 0) {
terminal.error("Incorrect usage of ps command. Usage: ps"); terminal.error("Incorrect usage of ps command. Usage: ps");

@ -9,7 +9,7 @@ export function rm(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length !== 1) { if (args.length !== 1) {
terminal.error("Incorrect number of arguments. Usage: rm [program/script]"); terminal.error("Incorrect number of arguments. Usage: rm [program/script]");

@ -11,7 +11,7 @@ export function run(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
// Run a program or a script // Run a program or a script
if (args.length < 1) { if (args.length < 1) {

@ -9,7 +9,7 @@ export function runProgram(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length < 1) { if (args.length < 1) {
return; return;

@ -14,7 +14,7 @@ export function runScript(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
commandArgs: (string | number)[], commandArgs: (string | number | boolean)[],
): void { ): void {
if (commandArgs.length < 1) { if (commandArgs.length < 1) {
terminal.error( terminal.error(

@ -9,7 +9,7 @@ export function scan(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length !== 0) { if (args.length !== 0) {
terminal.error("Incorrect usage of netstat/scan command. Usage: netstat/scan"); terminal.error("Incorrect usage of netstat/scan command. Usage: netstat/scan");

@ -9,7 +9,7 @@ export function scananalyze(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length === 0) { if (args.length === 0) {
terminal.executeScanAnalyzeCommand(player, 1); terminal.executeScanAnalyzeCommand(player, 1);

@ -10,7 +10,7 @@ export function scp(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
try { try {
if (args.length !== 2) { if (args.length !== 2) {

@ -8,7 +8,7 @@ export function sudov(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length !== 0) { if (args.length !== 0) {
terminal.error("Incorrect number of arguments. Usage: sudov"); terminal.error("Incorrect number of arguments. Usage: sudov");

@ -12,7 +12,7 @@ export function tail(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
commandArray: (string | number)[], commandArray: (string | number | boolean)[],
): void { ): void {
try { try {
if (commandArray.length < 1) { if (commandArray.length < 1) {
@ -67,7 +67,7 @@ export function tail(
// if there's no candidate then we just don't know. // if there's no candidate then we just don't know.
terminal.error("No such script exists."); terminal.error("No such script exists.");
} else { } else if (typeof commandArray[0] === "number") {
const runningScript = findRunningScriptByPid(commandArray[0], server); const runningScript = findRunningScriptByPid(commandArray[0], server);
if (runningScript == null) { if (runningScript == null) {
terminal.error("No such script exists"); terminal.error("No such script exists");

@ -10,7 +10,7 @@ export function top(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length !== 0) { if (args.length !== 0) {
terminal.error("Incorrect usage of top command. Usage: top"); terminal.error("Incorrect usage of top command. Usage: top");

@ -9,7 +9,7 @@ export function unalias(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length !== 1) { if (args.length !== 1) {
terminal.error("Incorrect usage of unalias name. Usage: unalias [alias]"); terminal.error("Incorrect usage of unalias name. Usage: unalias [alias]");

@ -9,7 +9,7 @@ export function weaken(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length !== 0) { if (args.length !== 0) {
terminal.error("Incorrect usage of weaken command. Usage: weaken"); terminal.error("Incorrect usage of weaken command. Usage: weaken");

@ -9,7 +9,7 @@ export function wget(
router: IRouter, router: IRouter,
player: IPlayer, player: IPlayer,
server: BaseServer, server: BaseServer,
args: (string | number)[], args: (string | number | boolean)[],
): void { ): void {
if (args.length !== 2) { if (args.length !== 2) {
terminal.error("Incorrect usage of wget command. Usage: wget [url] [target file]"); terminal.error("Incorrect usage of wget command. Usage: wget [url] [target file]");

@ -265,13 +265,6 @@ export async function determineAllPossibilitiesForTabCompletion(
return allPos; return allPos;
} }
if (isCommand("kill") || isCommand("tail") || isCommand("mem") || isCommand("check")) {
addAllScripts();
addAllDirectories();
return allPos;
}
if (isCommand("nano")) { if (isCommand("nano")) {
addAllScripts(); addAllScripts();
addAllTextFiles(); addAllTextFiles();
@ -292,7 +285,7 @@ export async function determineAllPossibilitiesForTabCompletion(
} }
async function scriptAutocomplete(): Promise<string[] | undefined> { async function scriptAutocomplete(): Promise<string[] | undefined> {
if (!isCommand("run")) return; if (!isCommand("run") && !isCommand("tail") && !isCommand("kill")) return;
const commands = ParseCommands(input); const commands = ParseCommands(input);
if (commands.length === 0) return; if (commands.length === 0) return;
const command = ParseCommand(commands[commands.length - 1]); const command = ParseCommand(commands[commands.length - 1]);
@ -348,6 +341,13 @@ export async function determineAllPossibilitiesForTabCompletion(
addAllDirectories(); addAllDirectories();
} }
if (isCommand("kill") || isCommand("tail") || isCommand("mem") || isCommand("check")) {
addAllScripts();
addAllDirectories();
return allPos;
}
if (isCommand("cat")) { if (isCommand("cat")) {
addAllMessages(); addAllMessages();
addAllLitFiles(); addAllLitFiles();