add option to pass grep pattern to ps

This commit is contained in:
maxtimum 2021-12-23 17:39:45 -08:00
parent ed86577d6c
commit deb6d6cc0a

@ -10,16 +10,32 @@ export function ps(
server: BaseServer,
args: (string | number | boolean)[],
): void {
if (args.length !== 0) {
terminal.error("Incorrect usage of ps command. Usage: ps");
if (args.length === 1 || args.length === 2 || args.length > 3) {
terminal.error("Incorrect usage of ps command. Usage: ps [| grep <pattern>]");
return;
}
for (let i = 0; i < server.runningScripts.length; i++) {
const rsObj = server.runningScripts[i];
let res = `(PID - ${rsObj.pid}) ${rsObj.filename}`;
for (let j = 0; j < rsObj.args.length; ++j) {
res += " " + rsObj.args[j].toString();
if(args[0] === '|' && args[1] === 'grep'){
const pattern = new RegExp(`${args[2].toString()}`)
const matching = server.runningScripts.filter((x) => pattern.test(x.filename))
for(let i = 0; i < matching.length; i++){
const rsObj = matching[i];
let res = `(PID - ${rsObj.pid}) ${rsObj.filename}`;
for (let j = 0; j < rsObj.args.length; ++j) {
res += " " + rsObj.args[j].toString();
}
terminal.print(res);
}
terminal.print(res);
}else if(args.length === 0){
for (let i = 0; i < server.runningScripts.length; i++) {
const rsObj = server.runningScripts[i];
let res = `(PID - ${rsObj.pid}) ${rsObj.filename}`;
for (let j = 0; j < rsObj.args.length; ++j) {
res += " " + rsObj.args[j].toString();
}
terminal.print(res);
}
}else{
terminal.error("Incorrect usage of ps command. Usage: ps [| grep <pattern>]");
return;
}
}