diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 14d953f1f..6674d6944 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -55,7 +55,7 @@ import { makeRuntimeRejectMsg, netscriptDelay, resolveNetscriptRequestedThreads import { numeralWrapper } from "./ui/numeralFormat"; import { convertTimeMsToTimeElapsedString } from "./utils/StringHelperFunctions"; -import { LogBoxEvents } from "./ui/React/LogBoxManager"; +import { LogBoxEvents, LogBoxCloserEvents } from "./ui/React/LogBoxManager"; import { arrayToString } from "./utils/helpers/arrayToString"; import { isString } from "./utils/helpers/isString"; @@ -988,6 +988,12 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { LogBoxEvents.emit(runningScriptObj); }, + closeTail: function (_pid: unknown = workerScript.scriptRef.pid): void { + updateDynamicRam("closeTail", getRamCost(Player, "closeTail")); + const pid = helper.number("closeTail", "pid", _pid); + //Emit an event to tell the game to close the tail window if it exists + LogBoxCloserEvents.emit(pid); + }, nuke: function (_hostname: unknown): boolean { updateDynamicRam("nuke", getRamCost(Player, "nuke")); const hostname = helper.string("tail", "hostname", _hostname); diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 63f6d2250..dfc63eee6 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -4981,6 +4981,21 @@ export interface NS { */ tail(fn?: FilenameOrPID, host?: string, ...args: any[]): void; + /** + * Close the tail window of a script. + * @remarks + * RAM cost: 0 GB + * + * Closes a script’s logs. This is functionally the same pressing the "Close" button on the tail window. + * + * If the function is called with no arguments, it will close the current script’s logs. + * + * Otherwise, the pid argument can be used to close the logs from another script. + * + * @param pid - Optional. PID of the script having its tail closed. If omitted, the current script is used. + */ + closeTail(pid?: number): void; + /** * Get the list of servers connected to a server. * @remarks diff --git a/src/ui/React/LogBoxManager.tsx b/src/ui/React/LogBoxManager.tsx index 21839a23b..8da57a924 100644 --- a/src/ui/React/LogBoxManager.tsx +++ b/src/ui/React/LogBoxManager.tsx @@ -23,6 +23,7 @@ import { Settings } from "../../Settings/Settings"; let layerCounter = 0; export const LogBoxEvents = new EventEmitter<[RunningScript]>(); +export const LogBoxCloserEvents = new EventEmitter<[number]>(); export const LogBoxClearEvents = new EventEmitter<[]>(); interface Log { @@ -51,6 +52,15 @@ export function LogBoxManager(): React.ReactElement { [], ); + //Event used by ns.closeTail to close tail windows + useEffect( + () => + LogBoxCloserEvents.subscribe((pid: number) => { + closePid(pid); + }), + [], + ); + useEffect(() => LogBoxClearEvents.subscribe(() => { logs = []; @@ -58,11 +68,18 @@ export function LogBoxManager(): React.ReactElement { }), ); + //Close tail windows by their id function close(id: string): void { logs = logs.filter((l) => l.id !== id); rerender(); } + //Close tail windows by their pid + function closePid(pid: number): void { + logs = logs.filter((log) => log.script.pid != pid); + rerender(); + } + return ( <> {logs.map((log) => (