@@ -17,14 +17,14 @@ without requiring any extensions or special installation.
1717 * [ Stdio] ( #stdio )
1818 * [ Output] ( #output )
1919 * [ Input] ( #input )
20- * [ Readline] ( #readline )
2120 * [ Prompt] ( #prompt )
2221 * [ Echo] ( #echo )
2322 * [ Input buffer] ( #input-buffer )
2423 * [ Cursor] ( #cursor )
2524 * [ History] ( #history )
2625 * [ Autocomplete] ( #autocomplete )
2726 * [ Keys] ( #keys )
27+ * [ Readline] ( #readline )
2828* [ Pitfalls] ( #pitfalls )
2929* [ Install] ( #install )
3030* [ Tests] ( #tests )
@@ -39,7 +39,7 @@ Once [installed](#install), you can use the following code to present a prompt i
3939$loop = React\EventLoop\Factory::create();
4040$stdio = new Stdio($loop);
4141
42- $stdio->getReadline()-> setPrompt('Input > ');
42+ $stdio->setPrompt('Input > ');
4343
4444$stdio->on('data', function ($line) use ($stdio) {
4545 $line = rtrim($line, "\r\n");
@@ -135,34 +135,13 @@ Because the `Stdio` is a well-behaving readable stream that will emit incoming
135135data as-is, you can also use this to ` pipe() ` this stream into other writable
136136streams.
137137
138- ```
138+ ``` php
139139$stdio->pipe($logger);
140140```
141141
142- You can control various aspects of the console input through the [ ` Readline ` ] ( #readline ) ,
142+ You can control various aspects of the console input through this interface ,
143143so read on..
144144
145- ### Readline
146-
147- The [ ` Readline ` ] ( #readline ) class is responsible for reacting to user input and presenting a prompt to the user.
148- It does so by reading individual bytes from the input stream and writing the current * user input line* to the output stream.
149-
150- The * user input line* consists of a * prompt* , following by the current * user input buffer* .
151- The ` Readline ` allows you to control various aspects of this * user input line* .
152-
153- You can access the current instance through the [ ` Stdio ` ] ( #stdio ) :
154-
155- ``` php
156- $readline = $stdio->getReadline();
157- ```
158-
159- See above for waiting for user input.
160-
161- Alternatively, the ` Readline ` is also a well-behaving readable stream
162- (implementing ReactPHP's ` ReadableStreamInterface ` ) that emits each complete
163- line as a ` data ` event, including the trailing newline.
164- This is considered advanced usage.
165-
166145#### Prompt
167146
168147The * prompt* will be written at the beginning of the * user input line* , right before the * user input buffer* .
@@ -171,21 +150,21 @@ The `setPrompt($prompt)` method can be used to change the input prompt.
171150The prompt will be printed to the * user input line* as-is, so you will likely want to end this with a space:
172151
173152``` php
174- $readline ->setPrompt('Input: ');
153+ $stdio ->setPrompt('Input: ');
175154```
176155
177156The default input prompt is empty, i.e. the * user input line* contains only the actual * user input buffer* .
178157You can restore this behavior by passing an empty prompt:
179158
180159``` php
181- $readline ->setPrompt('');
160+ $stdio ->setPrompt('');
182161```
183162
184163The ` getPrompt() ` method can be used to get the current input prompt.
185164It will return an empty string unless you've set anything else:
186165
187166``` php
188- assert($readline ->getPrompt() === '');
167+ assert($stdio ->getPrompt() === '');
189168```
190169
191170#### Echo
@@ -201,7 +180,7 @@ Please note that this often leads to a bad user experience as users will not eve
201180Simply pass a boolean ` false ` like this:
202181
203182``` php
204- $readline ->setEcho(false);
183+ $stdio ->setEcho(false);
205184```
206185
207186Alternatively, you can also * hide* the * user input buffer* by using a replacement character.
@@ -211,13 +190,13 @@ This often provides a better user experience and allows users to still control t
211190Simply pass a string replacement character likes this:
212191
213192``` php
214- $readline ->setEcho('*');
193+ $stdio ->setEcho('*');
215194```
216195
217196To restore the original behavior where every character appears as-is, simply pass a boolean ` true ` :
218197
219198``` php
220- $readline ->setEcho(true);
199+ $stdio ->setEcho(true);
221200```
222201
223202#### Input buffer
@@ -235,7 +214,7 @@ the user (like the last password attempt).
235214Simply pass an input string like this:
236215
237216``` php
238- $readline ->addInput('hello');
217+ $stdio ->addInput('hello');
239218```
240219
241220The ` setInput($buffer) ` method can be used to control the * user input buffer* .
@@ -247,15 +226,15 @@ the user (like the last password attempt).
247226Simply pass an input string like this:
248227
249228``` php
250- $readline ->setInput('lastpass');
229+ $stdio ->setInput('lastpass');
251230```
252231
253232The ` getInput() ` method can be used to access the current * user input buffer* .
254233This can be useful if you want to append some input behind the current * user input buffer* .
255234You can simply access the buffer like this:
256235
257236``` php
258- $buffer = $readline ->getInput();
237+ $buffer = $stdio ->getInput();
259238```
260239
261240#### Cursor
@@ -267,14 +246,14 @@ The `setMove($toggle)` method can be used to control whether users are allowed t
267246To disable the left and right arrow keys, simply pass a boolean ` false ` like this:
268247
269248``` php
270- $readline ->setMove(false);
249+ $stdio ->setMove(false);
271250```
272251
273252To restore the default behavior where the user can use the left and right arrow keys,
274253simply pass a boolean ` true ` like this:
275254
276255``` php
277- $readline ->setMove(true);
256+ $stdio ->setMove(true);
278257```
279258
280259The ` getCursorPosition() ` method can be used to access the current cursor position,
@@ -283,7 +262,7 @@ This can be useful if you want to get a substring of the current *user input buf
283262Simply invoke it like this:
284263
285264``` php
286- $position = $readline ->getCursorPosition();
265+ $position = $stdio ->getCursorPosition();
287266```
288267
289268The ` getCursorCell() ` method can be used to get the current cursor position,
@@ -296,14 +275,14 @@ This method is mostly useful for calculating the visual cursor position on scree
296275but you may also invoke it like this:
297276
298277``` php
299- $cell = $readline ->getCursorCell();
278+ $cell = $stdio ->getCursorCell();
300279```
301280
302281The ` moveCursorTo($position) ` method can be used to set the current cursor position to the given absolute character position.
303282For example, to move the cursor to the beginning of the * user input buffer* , simply call:
304283
305284``` php
306- $readline ->moveCursorTo(0);
285+ $stdio ->moveCursorTo(0);
307286```
308287
309288The ` moveCursorBy($offset) ` method can be used to change the cursor position
@@ -312,7 +291,7 @@ A positive number will move the cursor to the right - a negative number will mov
312291For example, to move the cursor one character to the left, simply call:
313292
314293``` php
315- $readline ->moveCursorBy(-1);
294+ $stdio ->moveCursorBy(-1);
316295```
317296
318297#### History
@@ -331,13 +310,13 @@ If you want to automatically add everything from the user input to the history,
331310you may want to use something like this:
332311
333312``` php
334- $stdio->on('data', function ($line) use ($readline ) {
313+ $stdio->on('data', function ($line) use ($stdio ) {
335314 $line = rtrim($line);
336- $all = $readline ->listHistory();
315+ $all = $stdio ->listHistory();
337316
338317 // skip empty line and duplicate of previous line
339318 if ($line !== '' && $line !== end($all)) {
340- $readline ->addHistory($line);
319+ $stdio ->addHistory($line);
341320 }
342321});
343322```
@@ -347,38 +326,38 @@ return an array with all lines in the history.
347326This will be an empty array until you add new entries via ` addHistory() ` .
348327
349328``` php
350- $list = $readline ->listHistory();
329+ $list = $stdio ->listHistory();
351330
352331assert(count($list) === 0);
353332```
354333
355- The ` addHistory(string $line): Readline ` method can be used to
334+ The ` addHistory(string $line): void ` method can be used to
356335add a new line to the (bottom position of the) history list.
357336A following ` listHistory() ` call will return this line as the last element.
358337
359338``` php
360- $readline ->addHistory('a');
361- $readline ->addHistory('b');
339+ $stdio ->addHistory('a');
340+ $stdio ->addHistory('b');
362341
363- $list = $readline ->listHistory();
342+ $list = $stdio ->listHistory();
364343assert($list === array('a', 'b'));
365344```
366345
367- The ` clearHistory(): Readline ` method can be used to
346+ The ` clearHistory(): void ` method can be used to
368347clear the complete history list.
369348A following ` listHistory() ` call will return an empty array until you add new
370349entries via ` addHistory() ` again.
371350Note that the history feature will effectively be disabled if the history is
372351empty, as the UP and DOWN cursor keys have no function then.
373352
374353``` php
375- $readline ->clearHistory();
354+ $stdio ->clearHistory();
376355
377- $list = $readline ->listHistory();
356+ $list = $stdio ->listHistory();
378357assert(count($list) === 0);
379358```
380359
381- The ` limitHistory(?int $limit): Readline ` method can be used to
360+ The ` limitHistory(?int $limit): void ` method can be used to
382361set a limit of history lines to keep in memory.
383362By default, only the last 500 lines will be kept in memory and everything else
384363will be discarded.
@@ -394,10 +373,10 @@ this to obey the `HISTSIZE` environment variable:
394373$limit = getenv('HISTSIZE');
395374if ($limit === '' || $limit < 0) {
396375 // empty string or negative value means unlimited
397- $readline ->limitHistory(null);
376+ $stdio ->limitHistory(null);
398377} elseif ($limit !== false) {
399378 // apply any other value if given
400- $readline ->limitHistory($limit);
379+ $stdio ->limitHistory($limit);
401380}
402381```
403382
@@ -415,13 +394,13 @@ By default, users can use autocompletion by using their TAB keys on the keyboard
415394The autocomplete function is not registered by default, thus this feature is
416395effectively disabled, as the TAB key has no function then.
417396
418- The ` setAutocomplete(?callable $autocomplete): Readline ` method can be used to
397+ The ` setAutocomplete(?callable $autocomplete): void ` method can be used to
419398register a new autocomplete handler.
420399In its most simple form, you won't need to assign any arguments and can simply
421400return an array of possible word matches from a callable like this:
422401
423402``` php
424- $readline ->setAutocomplete(function () {
403+ $stdio ->setAutocomplete(function () {
425404 return array(
426405 'exit',
427406 'echo',
@@ -464,7 +443,7 @@ is an argument or a root command and the `$word` argument to autocomplete
464443partial filename matches like this:
465444
466445``` php
467- $readline ->setAutocomplete(function ($word, $offset) {
446+ $stdio ->setAutocomplete(function ($word, $offset) {
468447 if ($offset <= 1) {
469448 // autocomplete root commands at offset=0/1 only
470449 return array('cat', 'rm', 'stat');
@@ -486,10 +465,10 @@ and/or manipulate the [input buffer](#input-buffer) and [cursor](#cursor)
486465directly like this:
487466
488467``` php
489- $readline ->setAutocomplete(function () use ($readline ) {
490- if ($readline ->getInput() === 'run') {
491- $readline ->setInput('run --test --value=42');
492- $readline ->moveCursorBy(-2);
468+ $stdio ->setAutocomplete(function () use ($stdio ) {
469+ if ($stdio ->getInput() === 'run') {
470+ $stdio ->setInput('run --test --value=42');
471+ $stdio ->moveCursorBy(-2);
493472 }
494473
495474 // return empty array so normal autocompletion doesn't kick in
@@ -501,7 +480,7 @@ You can use a `null` value to remove the autocomplete function again and thus
501480disable the autocomplete function:
502481
503482``` php
504- $readline ->setAutocomplete(null);
483+ $stdio ->setAutocomplete(null);
505484```
506485
507486#### Keys
@@ -527,7 +506,7 @@ For example, you can use the following code to print some help text when the
527506user hits a certain key:
528507
529508``` php
530- $readline ->on('?', function () use ($stdio) {
509+ $stdio ->on('?', function () use ($stdio) {
531510 $stdio->write('Here\'s some help: …' . PHP_EOL);
532511});
533512```
@@ -536,8 +515,8 @@ Similarly, this can be used to manipulate the user input and replace some of the
536515input when the user hits a certain key:
537516
538517``` php
539- $readline ->on('ä', function () use ($readline ) {
540- $readline ->addInput('a');
518+ $stdio ->on('ä', function () use ($stdio ) {
519+ $stdio ->addInput('a');
541520});
542521```
543522
@@ -550,14 +529,47 @@ For example, the following code can be used to register a custom function to the
550529UP arrow cursor key:
551530
552531``` php
553- $readline ->on("\033[A", function () use ($readline ) {
554- $readline ->setInput(strtoupper($readline ->getInput()));
532+ $stdio ->on("\033[A", function () use ($stdio ) {
533+ $stdio ->setInput(strtoupper($stdio ->getInput()));
555534});
556535```
557536
537+ ### Readline
538+
539+ The deprecated ` Readline ` class is responsible for reacting to user input and
540+ presenting a prompt to the user. It does so by reading individual bytes from the
541+ input stream and writing the current * user input line* to the output stream.
542+
543+ The deprecated ` Readline ` class is only used internally and should no longer be
544+ referenced from consuming projects.
545+
546+ You can access the current instance through the [ ` Stdio ` ] ( #stdio ) :
547+
548+ ``` php
549+ // deprecated
550+ $readline = $stdio->getReadline();
551+ ```
552+
553+ All methods that are available on the ` Readline ` instance are now available on
554+ the ` Stdio ` class. For BC reasons, they remain available on the ` Readline ` class
555+ until the next major release, see also above for more details.
556+
557+ ``` php
558+ // deprecated
559+ $readline->setPrompt('> ');
560+
561+ // new
562+ $stdio->setPrompt('> ');
563+ ```
564+
565+ Internally, the ` Readline ` is also a well-behaving readable stream
566+ (implementing ReactPHP's ` ReadableStreamInterface ` ) that emits each complete
567+ line as a ` data ` event, including the trailing newline.
568+ This is considered advanced usage.
569+
558570## Pitfalls
559571
560- The [ ` Readline ` ] ( #readline ) has to redraw the current user
572+ The [ ` Stdio ` ] ( #stdio ) has to redraw the current user
561573input line whenever output is written to the ` STDOUT ` .
562574Because of this, it is important to make sure any output is always
563575written like this instead of using ` echo ` statements:
0 commit comments