diff --git a/doc/source/basicgameplay/terminal.rst b/doc/source/basicgameplay/terminal.rst index e6aba8767..1ee640f24 100644 --- a/doc/source/basicgameplay/terminal.rst +++ b/doc/source/basicgameplay/terminal.rst @@ -432,7 +432,10 @@ empty file will be created. ps ^^ + $ ps [-g, --grep pattern] + Prints all scripts that are currently running on the current server. +The :code:`-g, --grep pattern` option will only output running scripts where the name matches the provided pattern. rm ^^ diff --git a/src/Terminal/commands/ps.ts b/src/Terminal/commands/ps.ts index e62f3fe3a..695d9a71d 100644 --- a/src/Terminal/commands/ps.ts +++ b/src/Terminal/commands/ps.ts @@ -2,6 +2,7 @@ import { ITerminal } from "../ITerminal"; import { IRouter } from "../../ui/Router"; import { IPlayer } from "../../PersonObjects/IPlayer"; import { BaseServer } from "../../Server/BaseServer"; +import * as libarg from "arg" export function ps( terminal: ITerminal, @@ -10,16 +11,40 @@ export function ps( server: BaseServer, args: (string | number | boolean)[], ): void { - if (args.length !== 0) { - terminal.error("Incorrect usage of ps command. Usage: ps"); + let flags; + try{ + flags = libarg({ + '--grep': String, + '-g': '--grep' + }, + { argv: args } + ) + }catch(e){ + // catch passing only -g / --grep with no string to use as the search + terminal.error("Incorrect usage of ps command. Usage: ps [-g, --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(); + const pattern = flags['--grep'] + if (pattern) { + const re = new RegExp(pattern.toString()) + const matching = server.runningScripts.filter((x) => re.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); + } + } + 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); } - terminal.print(res); } }