Merge pull request #2735 from Ninetailed/ns_printf

Add ns.printf function
This commit is contained in:
hydroflame 2022-01-26 00:45:18 -05:00 committed by GitHub
commit 77ba45df40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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.");

@ -4463,6 +4463,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