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