@@ -18,51 +18,151 @@ yarn add react-stream-renderer
1818
1919``` js
2020import React from ' react' ;
21- import { render , Text } from ' react-stream-renderer' ;
21+ import { render , View , makeTTYAdapter } from ' react-stream-renderer' ;
2222
2323class App extends React .Component {
2424 render () {
2525 return (
26- < Text style= {{ color: ' green' }}>
26+ < View style= {{ color: ' green' }}>
2727 Hello world!
28- < / Text >
28+ < / View >
2929 );
3030 }
3131}
3232
33- render (< App / > , process .stdout );
33+ render (< App / > , makeTTYAdapter ( process .stdout ). makeEffects () );
3434```
3535
3636## API
3737---
3838
3939## Functions
4040
41- ### ` render(element, writableStream, options): void `
41+ ### ` render(element: React.Element, adapter: BaseAdapter): () => void `
42+
43+ Render given element using a given adapter (` makeTTYAdapter ` for TTY streams or ` makeTestAdapter ` for testing purposes).
44+
45+ Returns ` forceRender ` function which triggers full re-render.
46+
47+ ### ` makeTTYAdapter(ttyStream: NodeTTYStream): TTYAdapter `
48+
49+ Creates an adapter a TTY Node stream.
50+ Use chainable API from ` TTYAdapter ` for configuring the behavior:
51+
52+ #### ` withCustomConsole(options: OverwriteConsoleOptions): TTYAdapter `
53+
54+ Redirect console output to specified streams or files.
55+
56+ This method won't have any effect unless ` makeEffects ` is called.
57+
58+ ` options: OverwriteConsoleOptions ` :
59+
60+ * ` exitOnWarning: boolean ` - Exit on first call to ` console.warning ` .
61+ * ` exitOnError: boolean ` - Exit on first call to ` console.error ` .
62+ * ` outStream: boolean | string | NodeWritableStream ` - Redirect console output to:
63+ * ` stdout.log ` if ` true `
64+ * custom file if ` string ` with path is supplied
65+ * custom Node stream if writable node stream is supplied
66+ * ` errStream: boolean | string | NodeWritableStream ` - Redirect console error output to:
67+ * ` stderr.log ` if ` true `
68+ * custom file if ` string ` with path is supplied
69+ * custom Node stream if writable node stream is supplied
70+
71+ #### ` hideCursor(): TTYAdapter `
72+
73+ Hides cursor.
74+
75+ This method won't have any effect unless ` makeEffects ` is called.
76+
77+ #### ` clearOnExit(shouldClearScrollback: boolean = false): TTYAdapter `
78+
79+ Clear screen or scrollback (if ` shouldClearScrollback ` is ` true ` ) when process is about to exit.
80+
81+ This method won't have any effect unless ` makeEffects ` is called.
82+
83+ #### ` clearOnError(): TTYAdapter `
84+
85+ Clear screen (scrollback will be untouched) when process is about to exit due to an error.
86+
87+ This method won't have any effect unless ` makeEffects ` is called.
88+
89+ #### ` makeEffects(): TTYAdapter `
90+
91+ Perform accumulated side effects.
92+ __ This method must always be called!__
93+
94+ __ Example__
95+
96+ ``` js
97+ render (
98+ < View> Hello world< / View> ,
99+ makeTTYAdapter (process .stdout )
100+ .withCustomConsole ({ outStream: true , errStream: true })
101+ .hideCursor ()
102+ .clearOnExit (true )
103+ .makeEffects ()
104+ );
105+ ```
106+
107+ ### ` makeTestAdapter(options: Options): TestAdapter `
108+
109+ Creates an adapter for testing. You can provide hooks to assert the rendered content.
110+
111+ #### ` options: Options `
112+
113+ * ` height: number = 40 ` - Canvas height (default: ` 40 ` )
114+ * ` width: number = 80 ` - Canvas width (default: ` 80 ` )
115+ * ` onPrint?: (data: string) => void ` - Testing hook executed on each render, ` data ` will be a string with full rendered content.
116+ * ` onClear: () => void ` - Testing hook executed when canvas should be cleared.
117+ * ` onSetCursorPosition: (x: number, y: number) => void ` - Testing hook executed when cursor should be changed.
42118
43- Render given element to writable (Node) stream.
119+ #### Example
120+
121+ ``` js
122+ test (' render should draw content' , () => {
123+ const onDraw = jest .fn ();
124+ const adapter = makeTestAdapter ({ onDraw });
44125
45- #### ` options `
126+ render ( < View > Test < / View > , adapter);
46127
47- * ` hideCursor?: boolean ` - Hide cursor if true.
48- * ` clearOnError?: boolean ` - Clear screen when process exits due to error being thrown.
49- * ` clearScreenOnExit?: boolean ` - Clear screen when process is about to exit.
50- * ` clearScrollbackOnExit?: boolean ` - Clear scrollback when process is about to exit (__ clearing scrollback also clears the whole screen__ ).
51- * ` exitOnWarning?: boolean ` - Exit when there's a call to ` console.warn ` .
52- * ` exitOnError?: boolean ` - Exit when there's a call to ` console.error ` .
53- * ` outStream?: any ` - Custom writable stream or file path for output from ` console ` .
54- * ` errStream?: any ` - Custom writable stream or file path for errors logged with ` console.error ` .
128+ expect (onDraw).toHaveBeenCalledWith (' Test' );
129+ });
130+ ```
55131
56132## Components
57133
58- ### ` Text `
134+ ### ` View ` (aka ` Text ` )
135+
136+ Basic building block. Can render text (strings), arrays and other nested components.
59137
60- Basic building block. Can render text (strings) or other nested components .
138+ ` Text ` component is exposed for compatibility and it's the same as ` View ` .
61139
62140#### Props
63141
64142* ` style?: Style ` - Object with [ Style properties] ( #style-properties )
65143
144+ #### Example
145+
146+ ``` js
147+ class App extends React .Component {
148+ render () {
149+ return (
150+ < View>
151+ < View style= {styles .title }> Hello world! < / View>
152+ Today is {new Date ().toLocaleString ()}
153+ < / View>
154+ );
155+ }
156+ }
157+
158+ const styles = {
159+ title: {
160+ margin: ' 0 1' ,
161+ color: ' green' ,
162+ },
163+ };
164+ ```
165+
66166
67167### ` KeyPress `
68168
0 commit comments