@@ -17,14 +17,14 @@ without requiring any extensions or special installation.
17
17
* [ Stdio] ( #stdio )
18
18
* [ Output] ( #output )
19
19
* [ Input] ( #input )
20
- * [ Readline] ( #readline )
21
20
* [ Prompt] ( #prompt )
22
21
* [ Echo] ( #echo )
23
22
* [ Input buffer] ( #input-buffer )
24
23
* [ Cursor] ( #cursor )
25
24
* [ History] ( #history )
26
25
* [ Autocomplete] ( #autocomplete )
27
26
* [ Keys] ( #keys )
27
+ * [ Readline] ( #readline )
28
28
* [ Pitfalls] ( #pitfalls )
29
29
* [ Install] ( #install )
30
30
* [ Tests] ( #tests )
@@ -39,7 +39,7 @@ Once [installed](#install), you can use the following code to present a prompt i
39
39
$loop = React\EventLoop\Factory::create();
40
40
$stdio = new Stdio($loop);
41
41
42
- $stdio->getReadline()-> setPrompt('Input > ');
42
+ $stdio->setPrompt('Input > ');
43
43
44
44
$stdio->on('data', function ($line) use ($stdio) {
45
45
$line = rtrim($line, "\r\n");
@@ -135,34 +135,13 @@ Because the `Stdio` is a well-behaving readable stream that will emit incoming
135
135
data as-is, you can also use this to ` pipe() ` this stream into other writable
136
136
streams.
137
137
138
- ```
138
+ ``` php
139
139
$stdio->pipe($logger);
140
140
```
141
141
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 ,
143
143
so read on..
144
144
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
-
166
145
#### Prompt
167
146
168
147
The * 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.
171
150
The prompt will be printed to the * user input line* as-is, so you will likely want to end this with a space:
172
151
173
152
``` php
174
- $readline ->setPrompt('Input: ');
153
+ $stdio ->setPrompt('Input: ');
175
154
```
176
155
177
156
The default input prompt is empty, i.e. the * user input line* contains only the actual * user input buffer* .
178
157
You can restore this behavior by passing an empty prompt:
179
158
180
159
``` php
181
- $readline ->setPrompt('');
160
+ $stdio ->setPrompt('');
182
161
```
183
162
184
163
The ` getPrompt() ` method can be used to get the current input prompt.
185
164
It will return an empty string unless you've set anything else:
186
165
187
166
``` php
188
- assert($readline ->getPrompt() === '');
167
+ assert($stdio ->getPrompt() === '');
189
168
```
190
169
191
170
#### Echo
@@ -201,7 +180,7 @@ Please note that this often leads to a bad user experience as users will not eve
201
180
Simply pass a boolean ` false ` like this:
202
181
203
182
``` php
204
- $readline ->setEcho(false);
183
+ $stdio ->setEcho(false);
205
184
```
206
185
207
186
Alternatively, 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
211
190
Simply pass a string replacement character likes this:
212
191
213
192
``` php
214
- $readline ->setEcho('*');
193
+ $stdio ->setEcho('*');
215
194
```
216
195
217
196
To restore the original behavior where every character appears as-is, simply pass a boolean ` true ` :
218
197
219
198
``` php
220
- $readline ->setEcho(true);
199
+ $stdio ->setEcho(true);
221
200
```
222
201
223
202
#### Input buffer
@@ -235,7 +214,7 @@ the user (like the last password attempt).
235
214
Simply pass an input string like this:
236
215
237
216
``` php
238
- $readline ->addInput('hello');
217
+ $stdio ->addInput('hello');
239
218
```
240
219
241
220
The ` setInput($buffer) ` method can be used to control the * user input buffer* .
@@ -247,15 +226,15 @@ the user (like the last password attempt).
247
226
Simply pass an input string like this:
248
227
249
228
``` php
250
- $readline ->setInput('lastpass');
229
+ $stdio ->setInput('lastpass');
251
230
```
252
231
253
232
The ` getInput() ` method can be used to access the current * user input buffer* .
254
233
This can be useful if you want to append some input behind the current * user input buffer* .
255
234
You can simply access the buffer like this:
256
235
257
236
``` php
258
- $buffer = $readline ->getInput();
237
+ $buffer = $stdio ->getInput();
259
238
```
260
239
261
240
#### Cursor
@@ -267,14 +246,14 @@ The `setMove($toggle)` method can be used to control whether users are allowed t
267
246
To disable the left and right arrow keys, simply pass a boolean ` false ` like this:
268
247
269
248
``` php
270
- $readline ->setMove(false);
249
+ $stdio ->setMove(false);
271
250
```
272
251
273
252
To restore the default behavior where the user can use the left and right arrow keys,
274
253
simply pass a boolean ` true ` like this:
275
254
276
255
``` php
277
- $readline ->setMove(true);
256
+ $stdio ->setMove(true);
278
257
```
279
258
280
259
The ` 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
283
262
Simply invoke it like this:
284
263
285
264
``` php
286
- $position = $readline ->getCursorPosition();
265
+ $position = $stdio ->getCursorPosition();
287
266
```
288
267
289
268
The ` 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
296
275
but you may also invoke it like this:
297
276
298
277
``` php
299
- $cell = $readline ->getCursorCell();
278
+ $cell = $stdio ->getCursorCell();
300
279
```
301
280
302
281
The ` moveCursorTo($position) ` method can be used to set the current cursor position to the given absolute character position.
303
282
For example, to move the cursor to the beginning of the * user input buffer* , simply call:
304
283
305
284
``` php
306
- $readline ->moveCursorTo(0);
285
+ $stdio ->moveCursorTo(0);
307
286
```
308
287
309
288
The ` 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
312
291
For example, to move the cursor one character to the left, simply call:
313
292
314
293
``` php
315
- $readline ->moveCursorBy(-1);
294
+ $stdio ->moveCursorBy(-1);
316
295
```
317
296
318
297
#### History
@@ -331,13 +310,13 @@ If you want to automatically add everything from the user input to the history,
331
310
you may want to use something like this:
332
311
333
312
``` php
334
- $stdio->on('data', function ($line) use ($readline ) {
313
+ $stdio->on('data', function ($line) use ($stdio ) {
335
314
$line = rtrim($line);
336
- $all = $readline ->listHistory();
315
+ $all = $stdio ->listHistory();
337
316
338
317
// skip empty line and duplicate of previous line
339
318
if ($line !== '' && $line !== end($all)) {
340
- $readline ->addHistory($line);
319
+ $stdio ->addHistory($line);
341
320
}
342
321
});
343
322
```
@@ -347,38 +326,38 @@ return an array with all lines in the history.
347
326
This will be an empty array until you add new entries via ` addHistory() ` .
348
327
349
328
``` php
350
- $list = $readline ->listHistory();
329
+ $list = $stdio ->listHistory();
351
330
352
331
assert(count($list) === 0);
353
332
```
354
333
355
- The ` addHistory(string $line): Readline ` method can be used to
334
+ The ` addHistory(string $line): void ` method can be used to
356
335
add a new line to the (bottom position of the) history list.
357
336
A following ` listHistory() ` call will return this line as the last element.
358
337
359
338
``` php
360
- $readline ->addHistory('a');
361
- $readline ->addHistory('b');
339
+ $stdio ->addHistory('a');
340
+ $stdio ->addHistory('b');
362
341
363
- $list = $readline ->listHistory();
342
+ $list = $stdio ->listHistory();
364
343
assert($list === array('a', 'b'));
365
344
```
366
345
367
- The ` clearHistory(): Readline ` method can be used to
346
+ The ` clearHistory(): void ` method can be used to
368
347
clear the complete history list.
369
348
A following ` listHistory() ` call will return an empty array until you add new
370
349
entries via ` addHistory() ` again.
371
350
Note that the history feature will effectively be disabled if the history is
372
351
empty, as the UP and DOWN cursor keys have no function then.
373
352
374
353
``` php
375
- $readline ->clearHistory();
354
+ $stdio ->clearHistory();
376
355
377
- $list = $readline ->listHistory();
356
+ $list = $stdio ->listHistory();
378
357
assert(count($list) === 0);
379
358
```
380
359
381
- The ` limitHistory(?int $limit): Readline ` method can be used to
360
+ The ` limitHistory(?int $limit): void ` method can be used to
382
361
set a limit of history lines to keep in memory.
383
362
By default, only the last 500 lines will be kept in memory and everything else
384
363
will be discarded.
@@ -394,10 +373,10 @@ this to obey the `HISTSIZE` environment variable:
394
373
$limit = getenv('HISTSIZE');
395
374
if ($limit === '' || $limit < 0) {
396
375
// empty string or negative value means unlimited
397
- $readline ->limitHistory(null);
376
+ $stdio ->limitHistory(null);
398
377
} elseif ($limit !== false) {
399
378
// apply any other value if given
400
- $readline ->limitHistory($limit);
379
+ $stdio ->limitHistory($limit);
401
380
}
402
381
```
403
382
@@ -415,13 +394,13 @@ By default, users can use autocompletion by using their TAB keys on the keyboard
415
394
The autocomplete function is not registered by default, thus this feature is
416
395
effectively disabled, as the TAB key has no function then.
417
396
418
- The ` setAutocomplete(?callable $autocomplete): Readline ` method can be used to
397
+ The ` setAutocomplete(?callable $autocomplete): void ` method can be used to
419
398
register a new autocomplete handler.
420
399
In its most simple form, you won't need to assign any arguments and can simply
421
400
return an array of possible word matches from a callable like this:
422
401
423
402
``` php
424
- $readline ->setAutocomplete(function () {
403
+ $stdio ->setAutocomplete(function () {
425
404
return array(
426
405
'exit',
427
406
'echo',
@@ -464,7 +443,7 @@ is an argument or a root command and the `$word` argument to autocomplete
464
443
partial filename matches like this:
465
444
466
445
``` php
467
- $readline ->setAutocomplete(function ($word, $offset) {
446
+ $stdio ->setAutocomplete(function ($word, $offset) {
468
447
if ($offset <= 1) {
469
448
// autocomplete root commands at offset=0/1 only
470
449
return array('cat', 'rm', 'stat');
@@ -486,10 +465,10 @@ and/or manipulate the [input buffer](#input-buffer) and [cursor](#cursor)
486
465
directly like this:
487
466
488
467
``` 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);
493
472
}
494
473
495
474
// 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
501
480
disable the autocomplete function:
502
481
503
482
``` php
504
- $readline ->setAutocomplete(null);
483
+ $stdio ->setAutocomplete(null);
505
484
```
506
485
507
486
#### Keys
@@ -527,7 +506,7 @@ For example, you can use the following code to print some help text when the
527
506
user hits a certain key:
528
507
529
508
``` php
530
- $readline ->on('?', function () use ($stdio) {
509
+ $stdio ->on('?', function () use ($stdio) {
531
510
$stdio->write('Here\'s some help: …' . PHP_EOL);
532
511
});
533
512
```
@@ -536,8 +515,8 @@ Similarly, this can be used to manipulate the user input and replace some of the
536
515
input when the user hits a certain key:
537
516
538
517
``` php
539
- $readline ->on('ä', function () use ($readline ) {
540
- $readline ->addInput('a');
518
+ $stdio ->on('ä', function () use ($stdio ) {
519
+ $stdio ->addInput('a');
541
520
});
542
521
```
543
522
@@ -550,14 +529,47 @@ For example, the following code can be used to register a custom function to the
550
529
UP arrow cursor key:
551
530
552
531
``` 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()));
555
534
});
556
535
```
557
536
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
+
558
570
## Pitfalls
559
571
560
- The [ ` Readline ` ] ( #readline ) has to redraw the current user
572
+ The [ ` Stdio ` ] ( #stdio ) has to redraw the current user
561
573
input line whenever output is written to the ` STDOUT ` .
562
574
Because of this, it is important to make sure any output is always
563
575
written like this instead of using ` echo ` statements:
0 commit comments