Add ns.printf function

Behaviour is consistent with that of tprintf versus tprint and
should be equivalent to ns.print(ns.sprintf(...))
This commit is contained in:
Terrana 2022-01-21 11:34:10 +00:00
parent 07fe3c1906
commit f2c4109211
No known key found for this signature in database
GPG Key ID: 12F172F4671A4334
3 changed files with 18 additions and 0 deletions

@ -114,6 +114,7 @@ export const RamCosts: IMap<any> = {
weaken: RamCostConstants.ScriptWeakenRamCost, weaken: RamCostConstants.ScriptWeakenRamCost,
weakenAnalyze: RamCostConstants.ScriptWeakenAnalyzeRamCost, weakenAnalyze: RamCostConstants.ScriptWeakenAnalyzeRamCost,
print: 0, print: 0,
printf: 0,
tprint: 0, tprint: 0,
clearLog: 0, clearLog: 0,
disableLog: 0, disableLog: 0,

@ -722,6 +722,12 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
} }
workerScript.print(argsToString(args)); 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 { tprint: function (...args: any[]): void {
if (args.length === 0) { if (args.length === 0) {
throw makeRuntimeErrorMsg("tprint", "Takes at least 1 argument."); throw makeRuntimeErrorMsg("tprint", "Takes at least 1 argument.");

@ -4460,6 +4460,17 @@ export interface NS extends Singularity {
*/ */
print(...args: any[]): void; print(...args: any[]): void;
/**
* Prints a formatted string to the scripts 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. * Prints one or more values or variables to the Terminal.
* @remarks * @remarks