From f2c4109211448038c59bbe0eb988bb88f1994150 Mon Sep 17 00:00:00 2001 From: Terrana Date: Fri, 21 Jan 2022 11:34:10 +0000 Subject: [PATCH] Add ns.printf function Behaviour is consistent with that of tprintf versus tprint and should be equivalent to ns.print(ns.sprintf(...)) --- src/Netscript/RamCostGenerator.ts | 1 + src/NetscriptFunctions.ts | 6 ++++++ src/ScriptEditor/NetscriptDefinitions.d.ts | 11 +++++++++++ 3 files changed, 18 insertions(+) diff --git a/src/Netscript/RamCostGenerator.ts b/src/Netscript/RamCostGenerator.ts index 81625b6c5..8aae3ee2c 100644 --- a/src/Netscript/RamCostGenerator.ts +++ b/src/Netscript/RamCostGenerator.ts @@ -114,6 +114,7 @@ export const RamCosts: IMap = { weaken: RamCostConstants.ScriptWeakenRamCost, weakenAnalyze: RamCostConstants.ScriptWeakenAnalyzeRamCost, print: 0, + printf: 0, tprint: 0, clearLog: 0, disableLog: 0, diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 5d43cd1e9..799db37ca 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -722,6 +722,12 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { } workerScript.print(argsToString(args)); }, + printf: function (format: string, ...args: any[]): void { + if (typeof format !== "string") { + throw makeRuntimeErrorMsg("printf", "First argument must be string for the format."); + } + workerScript.print(vsprintf(format, args)); + }, tprint: function (...args: any[]): void { if (args.length === 0) { throw makeRuntimeErrorMsg("tprint", "Takes at least 1 argument."); diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 6021a88ef..37db134d8 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -4460,6 +4460,17 @@ export interface NS extends Singularity { */ print(...args: any[]): void; + /** + * Prints a formatted string to the script’s logs. + * @remarks + * RAM cost: 0 GB + * + * see: https://github.com/alexei/sprintf.js + * @param format - format of the message + * @param args - Value(s) to be printed. + */ + printf(format: string, ...args: any[]): void; + /** * Prints one or more values or variables to the Terminal. * @remarks