From 006fbd528f8c2a15aed56754b3435d65347f4219 Mon Sep 17 00:00:00 2001 From: Snarling <84951833+Snarling@users.noreply.github.com> Date: Fri, 7 Jul 2023 18:23:26 -0400 Subject: [PATCH] DOCS: Add printRaw and tprintRaw docs (#663) --- markdown/bitburner.md | 3 +- markdown/bitburner.ns.md | 2 + markdown/bitburner.ns.printraw.md | 30 +++++++++++++++ markdown/bitburner.ns.settitle.md | 6 +-- markdown/bitburner.ns.tprintraw.md | 30 +++++++++++++++ markdown/bitburner.reactelement.md | 2 +- markdown/bitburner.reactnode.md | 17 +++++++++ src/NetscriptFunctions.ts | 6 +++ src/NetscriptFunctions/Extra.ts | 11 +----- src/ScriptEditor/NetscriptDefinitions.d.ts | 43 ++++++++++++++++++---- 10 files changed, 128 insertions(+), 22 deletions(-) create mode 100644 markdown/bitburner.ns.printraw.md create mode 100644 markdown/bitburner.ns.tprintraw.md create mode 100644 markdown/bitburner.reactnode.md diff --git a/markdown/bitburner.md b/markdown/bitburner.md index 5e29657ff..c655ca2a3 100644 --- a/markdown/bitburner.md +++ b/markdown/bitburner.md @@ -84,7 +84,7 @@ | [Player](./bitburner.player.md) | | | [ProcessInfo](./bitburner.processinfo.md) | A single process on a server. | | [Product](./bitburner.product.md) | Product in a warehouse | -| [ReactElement](./bitburner.reactelement.md) | A stand-in for the real React.ReactElement, which API-extractor doesn't know about. Don't try to create one of these by hand; use React.createElement(). | +| [ReactElement](./bitburner.reactelement.md) | A stand-in for the real React.ReactElement. Use React.createElement to make these. See [creating an element without jsx](https://react.dev/reference/react/createElement#creating-an-element-without-jsx) from the official React documentation. | | [RecentScript](./bitburner.recentscript.md) | | | [ReputationFormulas](./bitburner.reputationformulas.md) | Reputation formulas | | [ResetInfo](./bitburner.resetinfo.md) | Various info about resets | @@ -125,6 +125,7 @@ | [FilenameOrPID](./bitburner.filenameorpid.md) | | | [NSEnums](./bitburner.nsenums.md) | | | [PortData](./bitburner.portdata.md) | | +| [ReactNode](./bitburner.reactnode.md) |

A stand-in for the real React.ReactNode. A [ReactElement](./bitburner.reactelement.md) is rendered dynamically with React. number and string are displayed directly. boolean, null, and undefined are ignored and not rendered. An array of ReactNodes will display all members of that array sequentially.

Use React.createElement to make the ReactElement type, see [creating an element without jsx](https://react.dev/reference/react/createElement#creating-an-element-without-jsx) from the official React documentation.

| | [ScriptArg](./bitburner.scriptarg.md) | | | [SleeveBladeburnerTask](./bitburner.sleevebladeburnertask.md) | | | [SleeveClassTask](./bitburner.sleeveclasstask.md) | | diff --git a/markdown/bitburner.ns.md b/markdown/bitburner.ns.md index 1c50a938f..aea80c386 100644 --- a/markdown/bitburner.ns.md +++ b/markdown/bitburner.ns.md @@ -137,6 +137,7 @@ export async function main(ns) { | [peek(portNumber)](./bitburner.ns.peek.md) | Get a copy of the data from a port without popping it. | | [print(args)](./bitburner.ns.print.md) | Prints one or more values or variables to the script’s logs. | | [printf(format, args)](./bitburner.ns.printf.md) | Prints a formatted string to the script’s logs. | +| [printRaw(node)](./bitburner.ns.printraw.md) | Prints a ReactNode to the script logs. | | [prompt(txt, options)](./bitburner.ns.prompt.md) | Prompt the player with an input modal. | | [ps(host)](./bitburner.ns.ps.md) | List running scripts on a server. | | [purchaseServer(hostname, ram)](./bitburner.ns.purchaseserver.md) | Purchase a server. | @@ -163,6 +164,7 @@ export async function main(ns) { | [toast(msg, variant, duration)](./bitburner.ns.toast.md) | Queue a toast (bottom-right notification). | | [tprint(args)](./bitburner.ns.tprint.md) | Prints one or more values or variables to the Terminal. | | [tprintf(format, values)](./bitburner.ns.tprintf.md) | Prints a raw value or a variable to the Terminal. | +| [tprintRaw(node)](./bitburner.ns.tprintraw.md) | Prints a ReactNode to the terminal. | | [tryWritePort(portNumber, data)](./bitburner.ns.trywriteport.md) | Attempt to write to a port. | | [upgradePurchasedServer(hostname, ram)](./bitburner.ns.upgradepurchasedserver.md) | Upgrade a purchased server's RAM. | | [vsprintf(format, args)](./bitburner.ns.vsprintf.md) | Format a string with an array of arguments. | diff --git a/markdown/bitburner.ns.printraw.md b/markdown/bitburner.ns.printraw.md new file mode 100644 index 000000000..8e81a7d9c --- /dev/null +++ b/markdown/bitburner.ns.printraw.md @@ -0,0 +1,30 @@ + + +[Home](./index.md) > [bitburner](./bitburner.md) > [NS](./bitburner.ns.md) > [printRaw](./bitburner.ns.printraw.md) + +## NS.printRaw() method + +Prints a ReactNode to the script logs. + +**Signature:** + +```typescript +printRaw(node: ReactNode): void; +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| node | [ReactNode](./bitburner.reactnode.md) | The react node to be printed. | + +**Returns:** + +void + +## Remarks + +RAM cost: 0 GB + +See [ReactNode](./bitburner.reactnode.md) type for the acceptable values. + diff --git a/markdown/bitburner.ns.settitle.md b/markdown/bitburner.ns.settitle.md index cc777850e..e015a95c7 100644 --- a/markdown/bitburner.ns.settitle.md +++ b/markdown/bitburner.ns.settitle.md @@ -9,14 +9,14 @@ Set the title of the tail window of a script. **Signature:** ```typescript -setTitle(title: string | ReactElement, pid?: number): void; +setTitle(title: string | ReactNode, pid?: number): void; ``` ## Parameters | Parameter | Type | Description | | --- | --- | --- | -| title | string \| [ReactElement](./bitburner.reactelement.md) | | +| title | string \| [ReactNode](./bitburner.reactnode.md) | | | pid | number | _(Optional)_ Optional. PID of the script having its tail closed. If omitted, the current script is used. | **Returns:** @@ -35,5 +35,5 @@ If the pid is unspecified, it will modify the current script’s logs. Otherwise, the pid argument can be used to change the logs from another script. -It is possible to pass a React Element instead of a string. Get these by calling React.createElement() with appropriate parameters. You should either know or be willing to learn about the React UI library if you go down this route, and there will likely be rough edges. +It is possible to pass any React Node instead of a string. See [ReactElement](./bitburner.reactelement.md) and [ReactNode](./bitburner.reactnode.md) types for additional info. diff --git a/markdown/bitburner.ns.tprintraw.md b/markdown/bitburner.ns.tprintraw.md new file mode 100644 index 000000000..e39682e53 --- /dev/null +++ b/markdown/bitburner.ns.tprintraw.md @@ -0,0 +1,30 @@ + + +[Home](./index.md) > [bitburner](./bitburner.md) > [NS](./bitburner.ns.md) > [tprintRaw](./bitburner.ns.tprintraw.md) + +## NS.tprintRaw() method + +Prints a ReactNode to the terminal. + +**Signature:** + +```typescript +tprintRaw(node: ReactNode): void; +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| node | [ReactNode](./bitburner.reactnode.md) | The react node to be printed. | + +**Returns:** + +void + +## Remarks + +RAM cost: 0 GB + +See [ReactNode](./bitburner.reactnode.md) type for the acceptable values. + diff --git a/markdown/bitburner.reactelement.md b/markdown/bitburner.reactelement.md index 44141fc32..abb71adee 100644 --- a/markdown/bitburner.reactelement.md +++ b/markdown/bitburner.reactelement.md @@ -4,7 +4,7 @@ ## ReactElement interface -A stand-in for the real React.ReactElement, which API-extractor doesn't know about. Don't try to create one of these by hand; use React.createElement(). +A stand-in for the real React.ReactElement. Use React.createElement to make these. See [creating an element without jsx](https://react.dev/reference/react/createElement#creating-an-element-without-jsx) from the official React documentation. **Signature:** diff --git a/markdown/bitburner.reactnode.md b/markdown/bitburner.reactnode.md new file mode 100644 index 000000000..c75458052 --- /dev/null +++ b/markdown/bitburner.reactnode.md @@ -0,0 +1,17 @@ + + +[Home](./index.md) > [bitburner](./bitburner.md) > [ReactNode](./bitburner.reactnode.md) + +## ReactNode type + +A stand-in for the real React.ReactNode. A [ReactElement](./bitburner.reactelement.md) is rendered dynamically with React. number and string are displayed directly. boolean, null, and undefined are ignored and not rendered. An array of ReactNodes will display all members of that array sequentially. + +Use React.createElement to make the ReactElement type, see [creating an element without jsx](https://react.dev/reference/react/createElement#creating-an-element-without-jsx) from the official React documentation. + +**Signature:** + +```typescript +type ReactNode = ReactElement | string | number | null | undefined | boolean | ReactNode[]; +``` +**References:** [ReactElement](./bitburner.reactelement.md), [ReactNode](./bitburner.reactnode.md) + diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index ea84049c0..534b36664 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -1767,6 +1767,12 @@ export const ns: InternalAPI = { const name = helpers.string(ctx, "name", _name); return getRamCost(...name.split(".")); }, + tprintRaw: () => (value) => { + Terminal.printRaw(wrapUserNode(value)); + }, + printRaw: (ctx) => (value) => { + ctx.workerScript.print(wrapUserNode(value)); + }, flags: Flags, ...NetscriptExtra(), }; diff --git a/src/NetscriptFunctions/Extra.ts b/src/NetscriptFunctions/Extra.ts index cd11e30e1..3f73622f0 100644 --- a/src/NetscriptFunctions/Extra.ts +++ b/src/NetscriptFunctions/Extra.ts @@ -3,8 +3,7 @@ import { Exploit } from "../Exploits/Exploit"; import * as bcrypt from "bcryptjs"; import { Apr1Events as devMenu } from "../ui/Apr1"; import { InternalAPI } from "../Netscript/APIWrapper"; -import { helpers, wrapUserNode } from "../Netscript/NetscriptHelpers"; -import { Terminal } from "../Terminal"; +import { helpers } from "../Netscript/NetscriptHelpers"; import { RamCostConstants } from "../Netscript/RamCostGenerator"; export interface INetscriptExtra { @@ -16,8 +15,6 @@ export interface INetscriptExtra { bypass(doc: Document): void; alterReality(): void; rainbow(guess: string): void; - tprintRaw(value: React.ReactNode): void; - printRaw(value: React.ReactNode): void; } export function NetscriptExtra(): InternalAPI { @@ -66,11 +63,5 @@ export function NetscriptExtra(): InternalAPI { Player.giveExploit(Exploit.INeedARainbow); return true; }, - tprintRaw: () => (value) => { - Terminal.printRaw(wrapUserNode(value)); - }, - printRaw: (ctx) => (value) => { - ctx.workerScript.print(wrapUserNode(value)); - }, }; } diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index e4006ab5f..bbdb93c76 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -183,8 +183,21 @@ interface TailProperties { /** * @public - * A stand-in for the real React.ReactElement, which API-extractor doesn't know about. - * Don't try to create one of these by hand; use React.createElement(). + * A stand-in for the real React.ReactNode. + * A {@link ReactElement} is rendered dynamically with React. + * number and string are displayed directly. + * boolean, null, and undefined are ignored and not rendered. + * An array of ReactNodes will display all members of that array sequentially. + * + * Use React.createElement to make the ReactElement type, see {@link https://react.dev/reference/react/createElement#creating-an-element-without-jsx | creating an element without jsx} from the official React documentation. + */ +type ReactNode = ReactElement | string | number | null | undefined | boolean | ReactNode[]; + +/** + * @public + * A stand-in for the real React.ReactElement. + * Use React.createElement to make these. + * See {@link https://react.dev/reference/react/createElement#creating-an-element-without-jsx | creating an element without jsx} from the official React documentation. */ interface ReactElement { type: string | ((props: any) => ReactElement | null) | (new (props: any) => object); @@ -4919,6 +4932,15 @@ export interface NS { */ print(...args: any[]): void; + /** Prints a ReactNode to the script logs. + * @remarks + * RAM cost: 0 GB + * + * See {@link ReactNode} type for the acceptable values. + * + * @param node - The react node to be printed. */ + printRaw(node: ReactNode): void; + /** * Prints a formatted string to the script’s logs. * @remarks @@ -4957,6 +4979,15 @@ export interface NS { */ tprint(...args: any[]): void; + /** Prints a ReactNode to the terminal. + * @remarks + * RAM cost: 0 GB + * + * See {@link ReactNode} type for the acceptable values. + * + * @param node - The react node to be printed. */ + tprintRaw(node: ReactNode): void; + /** * Prints a raw value or a variable to the Terminal. * @remarks @@ -5149,14 +5180,12 @@ export interface NS { * * Otherwise, the pid argument can be used to change the logs from another script. * - * It is possible to pass a React Element instead of a string. Get these by calling - * React.createElement() with appropriate parameters. You should either know - * or be willing to learn about the React UI library if you go down this - * route, and there will likely be rough edges. + * It is possible to pass any React Node instead of a string. + * See {@link ReactElement} and {@link ReactNode} types for additional info. * * @param pid - Optional. PID of the script having its tail closed. If omitted, the current script is used. */ - setTitle(title: string | ReactElement, pid?: number): void; + setTitle(title: string | ReactNode, pid?: number): void; /** * Get the list of servers connected to a server.