bitburner-src/src/Terminal/commands/ps.ts

34 lines
1003 B
TypeScript
Raw Normal View History

2022-09-06 15:07:12 +02:00
import { Terminal } from "../../Terminal";
2021-09-16 01:50:44 +02:00
import { BaseServer } from "../../Server/BaseServer";
import { matchScriptPathUnanchored } from "../../utils/helpers/scriptKey";
import libarg from "arg";
2021-09-16 01:50:44 +02:00
2022-09-06 15:07:12 +02:00
export function ps(args: (string | number | boolean)[], server: BaseServer): void {
let flags;
2022-04-07 01:30:08 +02:00
try {
flags = libarg(
{
"--grep": String,
"-g": "--grep",
},
{ argv: args },
);
} catch (e) {
// catch passing only -g / --grep with no string to use as the search
2022-09-06 15:07:12 +02:00
Terminal.error("Incorrect usage of ps command. Usage: ps [-g, --grep pattern]");
2021-09-16 01:50:44 +02:00
return;
}
let pattern = flags["--grep"];
if (!pattern) {
pattern = ".*"; // Match anything
}
const re = matchScriptPathUnanchored(pattern);
for (const [k, byPid] of server.runningScriptMap) {
if (!re.test(k)) continue;
for (const rsObj of byPid.values()) {
const res = `(PID - ${rsObj.pid}) ${rsObj.filename} ${rsObj.args.join(" ")}`;
2022-09-06 15:07:12 +02:00
Terminal.print(res);
2021-12-24 02:39:45 +01:00
}
2021-09-16 01:50:44 +02:00
}
}