Refactor ls

This commit is contained in:
omuretsu 2023-03-07 08:04:08 -05:00
parent 0442f3d5e9
commit c2cd5845b2
4 changed files with 19 additions and 71 deletions

@ -9,7 +9,7 @@ List files on a server.
**Signature:** **Signature:**
```typescript ```typescript
ls(host: string, grep?: string): string[]; ls(host: string, substring?: string): string[];
``` ```
## Parameters ## Parameters
@ -17,7 +17,7 @@ ls(host: string, grep?: string): string[];
| Parameter | Type | Description | | Parameter | Type | Description |
| --- | --- | --- | | --- | --- | --- |
| host | string | Hostname of the target server. | | 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:** **Returns:**

@ -135,7 +135,7 @@ export async function main(ns) {
| [kill(pid)](./bitburner.ns.kill.md) | Terminate the script with the provided PID. | | [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. | | [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. | | [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. | | [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. | | [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. | | [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. |

@ -909,73 +909,21 @@ export const ns: InternalAPI<NSFull> = {
return noFailures; return noFailures;
}, },
ls: ls: (ctx) => (_hostname, _substring) => {
(ctx) =>
(_hostname, _grep = ""): string[] => {
const hostname = helpers.string(ctx, "hostname", _hostname); const hostname = helpers.string(ctx, "hostname", _hostname);
const grep = helpers.string(ctx, "grep", _grep); const substring = helpers.string(ctx, "substring", _substring ?? "");
const server = helpers.getServer(ctx, hostname); const server = helpers.getServer(ctx, hostname);
// Get the grep filter, if one exists const allFilenames = [
let filter = ""; ...server.contracts.map((contract) => contract.fn),
if (_grep !== undefined) { ...server.messages,
filter = grep.toString(); ...server.programs,
} ...server.scripts.map((script) => script.filename),
...server.textFiles.map((textFile) => textFile.filename),
];
const allFiles = []; if (!substring) return allFilenames.sort();
for (let i = 0; i < server.programs.length; i++) { return allFilenames.filter((filename) => filename.includes(substring)).sort();
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;
}, },
getRecentScripts: () => (): RecentScript[] => { getRecentScripts: () => (): RecentScript[] => {
return recentScripts.map((rs) => ({ return recentScripts.map((rs) => ({

@ -5592,10 +5592,10 @@ export interface NS {
* (as strings). The returned array is sorted in alphabetic order. * (as strings). The returned array is sorted in alphabetic order.
* *
* @param host - Hostname of the target server. * @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. * @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. * List running scripts on a server.