@@ -64,7 +64,7 @@ $stdio = new Stdio($loop);
6464See below for waiting for user input and writing output.
6565The ` Stdio ` class is a well-behaving duplex stream
6666(implementing ReactPHP's ` DuplexStreamInterface ` ) that emits each complete
67- line as a ` data ` event ( including the trailing newline) .
67+ line as a ` data ` event, including the trailing newline.
6868
6969#### Output
7070
@@ -79,8 +79,12 @@ $stdio->write('hello');
7979$stdio->write(" world\n");
8080```
8181
82- Alternatively, you can also use the ` Stdio ` as a writable stream.
83- You can ` pipe() ` any readable stream into this stream.
82+ Because the ` Stdio ` is a well-behaving writable stream,
83+ you can also ` pipe() ` any readable stream into this stream.
84+
85+ ``` php
86+ $logger->pipe($stdio);
87+ ```
8488
8589#### Input
8690
@@ -99,10 +103,34 @@ $stdio->on('data', function ($line) {
99103});
100104```
101105
102- Because the ` Stdio ` is a well-behaving redable stream that will emit incoming
106+ Note that this class takes care of buffering incomplete lines and will only emit
107+ complete lines.
108+ This means that the line will usually end with the trailing newline character.
109+ If the stream ends without a trailing newline character, it will not be present
110+ in the ` data ` event.
111+ As such, it's usually recommended to remove the trailing newline character
112+ before processing command line input like this:
113+
114+ ``` php
115+ $stdio->on('data', function ($line) {
116+ $line = rtrim($line, "\r\n");
117+ if ($line === "start") {
118+ doSomething();
119+ }
120+ });
121+ ```
122+
123+ Similarly, if you copy and paste a larger chunk of text, it will properly emit
124+ multiple complete lines with a separate ` data ` event for each line.
125+
126+ Because the ` Stdio ` is a well-behaving readable stream that will emit incoming
103127data as-is, you can also use this to ` pipe() ` this stream into other writable
104128streams.
105129
130+ ```
131+ $stdio->pipe($logger);
132+ ```
133+
106134You can control various aspects of the console input through the [ ` Readline ` ] ( #readline ) ,
107135so read on..
108136
@@ -124,7 +152,7 @@ See above for waiting for user input.
124152
125153Alternatively, the ` Readline ` is also a well-behaving readable stream
126154(implementing ReactPHP's ` ReadableStreamInterface ` ) that emits each complete
127- line as a ` data ` event (without the trailing newline) .
155+ line as a ` data ` event, including the trailing newline.
128156This is considered advanced usage.
129157
130158#### Prompt
0 commit comments