Skip to content

Commit 3dff973

Browse files
Docs update
1 parent 673eb63 commit 3dff973

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

index.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ <h3>Table Of Contents</h3>
223223
<li><a href="#docs.4.2.2">Executing Commands</a></li>
224224
<li><a href="#docs.4.2.3">Handling User Input</a></li>
225225
<li><a href="#docs.4.2.4">Printing</a></li>
226+
<li><a href="#docs.4.2.5">Handling Output</a></li>
226227
</ol>
227228
</li>
228229
</ol>
@@ -1229,6 +1230,35 @@ <h3 id="docs.4.2.4">4.2.4. Printing</h3>
12291230
</p>
12301231
<code class="pre">function myInitHandler (term) {
12311232
term.print("\r\nWe're ready!\r\n\n");
1233+
}</code>
1234+
<h3 id="docs.4.2.5">4.2.5. Handling Output</h3>
1235+
<p>
1236+
You can handle terminal output by using
1237+
<code class="inline">term.onOutput([ options ], callback)</code>. By default,
1238+
<b>callback</b>(<u>strings</u>) will be called before the user is prompted for
1239+
input, and <code class="inline">strings</code> array will always contain an array of chunks
1240+
of all the text printed between the prompts. For example, if user writes
1241+
<code class="inline">write 123</code> and presses "Enter", the <code class="inline">strings</code> will
1242+
contain this array: <code class="inline">["\r\n", "123", "\r\n"]</code>. However, when user
1243+
enters <code class="inline">write 1, 2, 3</code>, <code class="inline">strings</code> will result with
1244+
<code class="inline">["\r\n", "1", "2", "3", "\r\n"]</code>. You can join this array with
1245+
<code class="inline">join("")</code> array method to get the full output.<br/>
1246+
Optional <code class="inline">options</code> object may include <code class="inline">stream</code> property,
1247+
which is <code class="inline">false</code> by default. When set to <code class="inline">true</code>,
1248+
<b>callback</b> will be fired every time something is printed to the terminal
1249+
simultaneously.
1250+
</p>
1251+
<p>
1252+
Example:
1253+
</p>
1254+
<code class="pre">function myInitHandler (term) {
1255+
term.onOutput((chunks) => {
1256+
// If you "write 12", chunks are ["\r\n", "12", "\r\n"].
1257+
// If you "write 1, 2", chunks are ["\r\n", "1", "2", "\r\n"].
1258+
if (chunks.slice(1, -1).join("") === "duck") { // if user do enters: write "duck"
1259+
alert(`You've found a secret phrase!`);
1260+
}
1261+
});
12321262
}</code>
12331263
</div>
12341264
<div class="segment">

0 commit comments

Comments
 (0)