mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-08 08:43:53 +01:00
Refactor ls
This commit is contained in:
parent
0442f3d5e9
commit
c2cd5845b2
@ -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:**
|
||||
|
||||
|
@ -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. |
|
||||
|
@ -909,73 +909,21 @@ export const ns: InternalAPI<NSFull> = {
|
||||
|
||||
return noFailures;
|
||||
},
|
||||
ls:
|
||||
(ctx) =>
|
||||
(_hostname, _grep = ""): string[] => {
|
||||
ls: (ctx) => (_hostname, _substring) => {
|
||||
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);
|
||||
|
||||
// Get the grep filter, if one exists
|
||||
let filter = "";
|
||||
if (_grep !== undefined) {
|
||||
filter = grep.toString();
|
||||
}
|
||||
const allFilenames = [
|
||||
...server.contracts.map((contract) => contract.fn),
|
||||
...server.messages,
|
||||
...server.programs,
|
||||
...server.scripts.map((script) => script.filename),
|
||||
...server.textFiles.map((textFile) => textFile.filename),
|
||||
];
|
||||
|
||||
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) => ({
|
||||
|
4
src/ScriptEditor/NetscriptDefinitions.d.ts
vendored
4
src/ScriptEditor/NetscriptDefinitions.d.ts
vendored
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user