From c2cd5845b2fe81b37ed26ff28f8ff28078abfc61 Mon Sep 17 00:00:00 2001 From: omuretsu <84951833+Snarling@users.noreply.github.com> Date: Tue, 7 Mar 2023 08:04:08 -0500 Subject: [PATCH] Refactor ls --- markdown/bitburner.ns.ls.md | 4 +- markdown/bitburner.ns.md | 2 +- src/NetscriptFunctions.ts | 80 ++++------------------ src/ScriptEditor/NetscriptDefinitions.d.ts | 4 +- 4 files changed, 19 insertions(+), 71 deletions(-) diff --git a/markdown/bitburner.ns.ls.md b/markdown/bitburner.ns.ls.md index 16b2c4cc9..673138917 100644 --- a/markdown/bitburner.ns.ls.md +++ b/markdown/bitburner.ns.ls.md @@ -9,7 +9,7 @@ List files on a server. **Signature:** ```typescript -ls(host: string, grep?: string): string[]; +ls(host: string, substring?: string): string[]; ``` ## Parameters @@ -17,7 +17,7 @@ ls(host: string, grep?: string): string[]; | Parameter | Type | Description | | --- | --- | --- | | host | string | Hostname of the target server. | -| grep | string | _(Optional)_ A substring to search for in the filename. | +| substring | string | _(Optional)_ A substring to search for in the filename. | **Returns:** diff --git a/markdown/bitburner.ns.md b/markdown/bitburner.ns.md index 2da1b8ad3..c8e8c4362 100644 --- a/markdown/bitburner.ns.md +++ b/markdown/bitburner.ns.md @@ -135,7 +135,7 @@ export async function main(ns) { | [kill(pid)](./bitburner.ns.kill.md) | Terminate the script with the provided PID. | | [kill(filename, hostname, args)](./bitburner.ns.kill_1.md) | Terminate the script with the provided filename, hostname, and script arguments. | | [killall(host, safetyguard)](./bitburner.ns.killall.md) | Terminate all scripts on a server. | -| [ls(host, grep)](./bitburner.ns.ls.md) | List files on a server. | +| [ls(host, substring)](./bitburner.ns.ls.md) | List files on a server. | | [moveTail(x, y, pid)](./bitburner.ns.movetail.md) | Move a tail window. | | [mv(host, source, destination)](./bitburner.ns.mv.md) | Move a file on the target server. | | [nFormat(n, format)](./bitburner.ns.nformat.md) | Format a number using the numeral library. This function is deprecated and will be removed in 2.3. | diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 0794f74ee..ab92731cd 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -909,74 +909,22 @@ export const ns: InternalAPI = { return noFailures; }, - ls: - (ctx) => - (_hostname, _grep = ""): string[] => { - const hostname = helpers.string(ctx, "hostname", _hostname); - const grep = helpers.string(ctx, "grep", _grep); + ls: (ctx) => (_hostname, _substring) => { + const hostname = helpers.string(ctx, "hostname", _hostname); + const substring = helpers.string(ctx, "substring", _substring ?? ""); + const server = helpers.getServer(ctx, hostname); - const server = helpers.getServer(ctx, hostname); + const allFilenames = [ + ...server.contracts.map((contract) => contract.fn), + ...server.messages, + ...server.programs, + ...server.scripts.map((script) => script.filename), + ...server.textFiles.map((textFile) => textFile.filename), + ]; - // Get the grep filter, if one exists - let filter = ""; - if (_grep !== undefined) { - filter = grep.toString(); - } - - const allFiles = []; - for (let i = 0; i < server.programs.length; i++) { - if (filter) { - if (server.programs[i].includes(filter)) { - allFiles.push(server.programs[i]); - } - } else { - allFiles.push(server.programs[i]); - } - } - for (let i = 0; i < server.scripts.length; i++) { - if (filter) { - if (server.scripts[i].filename.includes(filter)) { - allFiles.push(server.scripts[i].filename); - } - } else { - allFiles.push(server.scripts[i].filename); - } - } - for (let i = 0; i < server.messages.length; i++) { - if (filter) { - const msg = server.messages[i]; - if (msg.includes(filter)) { - allFiles.push(msg); - } - } else { - allFiles.push(server.messages[i]); - } - } - - for (let i = 0; i < server.textFiles.length; i++) { - if (filter) { - if (server.textFiles[i].fn.includes(filter)) { - allFiles.push(server.textFiles[i].fn); - } - } else { - allFiles.push(server.textFiles[i].fn); - } - } - - for (let i = 0; i < server.contracts.length; ++i) { - if (filter) { - if (server.contracts[i].fn.includes(filter)) { - allFiles.push(server.contracts[i].fn); - } - } else { - allFiles.push(server.contracts[i].fn); - } - } - - // Sort the files alphabetically then print each - allFiles.sort(); - return allFiles; - }, + if (!substring) return allFilenames.sort(); + return allFilenames.filter((filename) => filename.includes(substring)).sort(); + }, getRecentScripts: () => (): RecentScript[] => { return recentScripts.map((rs) => ({ timeOfDeath: rs.timeOfDeath, diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index b1378e821..887d0d2f9 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -5592,10 +5592,10 @@ export interface NS { * (as strings). The returned array is sorted in alphabetic order. * * @param host - Hostname of the target server. - * @param grep - A substring to search for in the filename. + * @param substring - A substring to search for in the filename. * @returns Array with the filenames of all files on the specified server. */ - ls(host: string, grep?: string): string[]; + ls(host: string, substring?: string): string[]; /** * List running scripts on a server.