@@ -223,6 +223,7 @@ <h3>Table Of Contents</h3>
223
223
< li > < a href ="#docs.4.2.2 "> Executing Commands</ a > </ li >
224
224
< li > < a href ="#docs.4.2.3 "> Handling User Input</ a > </ li >
225
225
< li > < a href ="#docs.4.2.4 "> Printing</ a > </ li >
226
+ < li > < a href ="#docs.4.2.5 "> Handling Output</ a > </ li >
226
227
</ ol >
227
228
</ li >
228
229
</ ol >
@@ -1229,6 +1230,35 @@ <h3 id="docs.4.2.4">4.2.4. Printing</h3>
1229
1230
</ p >
1230
1231
< code class ="pre "> function myInitHandler (term) {
1231
1232
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
+ });
1232
1262
}</ code >
1233
1263
</ div >
1234
1264
< div class ="segment ">
0 commit comments