Skip to content

Commit 758d62f

Browse files
committed
Update README.md
1 parent 4338708 commit 758d62f

File tree

1 file changed

+220
-0
lines changed

1 file changed

+220
-0
lines changed

README.md

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,226 @@ $loop->run();
2727

2828
See also the [examples](examples).
2929

30+
## Usage
31+
32+
### Stdio
33+
34+
The `Stdio` is the main interface to this library.
35+
It is responsible for orchestrating the input and output streams
36+
by registering and forwarding the corresponding events.
37+
It also registers everything with the main [EventLoop](https://github.com/reactphp/event-loop#usage).
38+
39+
```php
40+
$loop = React\EventLoop\Factory::create();
41+
$stdio = new Stdio($loop);
42+
```
43+
44+
#### Output
45+
46+
The `writeln($line)` method can be used to print a line to console output.
47+
A trailing newline will be added automatically.
48+
49+
```php
50+
$stdio->writeln('hello world');
51+
```
52+
53+
The `write($text)` method can be used to print the given text characters to console output.
54+
This is useful if you need more control or want to output individual bytes or binary output:
55+
56+
```php
57+
$stdio->write('hello');
58+
$stdio->write(" world\n");
59+
```
60+
61+
#### Input
62+
63+
The `Stdio` will emit a `line` event for every line read from console input.
64+
The event will contain the input buffer as-is, without the trailing newline.
65+
You can register any number of event handlers like this:
66+
67+
```php
68+
$stdio->on('line', function ($line) {
69+
if ($line === 'start') {
70+
doSomething();
71+
}
72+
});
73+
```
74+
75+
You can control various aspects of the console input through the [`Readline`](#readline),
76+
so read on..
77+
78+
### Readline
79+
80+
The [`Readline`](#readline) class is responsible for reacting to user input and presenting a prompt to the user.
81+
It does so by reading individual bytes from the input stream and writing the current *user input line* to the output stream.
82+
83+
The *user input line* consists of a *prompt*, following by the current *user input buffer*.
84+
The `Readline` allows you to control various aspects of this *user input line*.
85+
86+
You can access the current instance through the [`Stdio`](#stdio):
87+
88+
```php
89+
$readline = $stdio->getReadline();
90+
```
91+
92+
#### Prompt
93+
94+
The *prompt* will be written at the beginning of the *user input line*, right before the *user input buffer*.
95+
96+
The `setPrompt($prompt)` method can be used to change the input prompt.
97+
The prompt will be printed to the *user input line* as-is, so you will likely want to end this with a space:
98+
99+
```php
100+
$readline->setPrompt('Input: ');
101+
```
102+
103+
The default input prompt is empty, i.e. the *user input line* contains only the actual *user input buffer*.
104+
You can restore this behavior by passing an empty prompt:
105+
106+
```php
107+
$readline->setPrompt('');
108+
```
109+
110+
#### Echo
111+
112+
The *echo mode* controls how the actual *user input buffer* will be presented in the *user input line*.
113+
114+
The `setEcho($echo)` method can be used to control the echo mode.
115+
The default is to print the *user input buffer* as-is.
116+
117+
You can disable printing the *user input buffer*, e.g. for password prompts.
118+
The user will still be able to type, but will not receive any indication of the current *user input buffer*.
119+
Please note that this often leads to a bad user experience as users will not even see their cursor position.
120+
Simply pass a boolean `false` like this:
121+
122+
```php
123+
$readline->setEcho(false);
124+
```
125+
126+
Alternatively, you can also *hide* the *user input buffer* by using a replacement character.
127+
One replacement character will be printed for each character in the *user input buffer*.
128+
This is useful for password prompts to give users an indicatation that their key presses are registered.
129+
This often provides a better user experience and allows users to still control their cursor position.
130+
Simply pass a string replacement character likes this:
131+
132+
```php
133+
$readline->setEcho('*');
134+
```
135+
136+
To restore the original behavior where every character appears as-is, simply pass a boolean `true`:
137+
138+
```php
139+
$readline->setEcho(true);
140+
```
141+
142+
#### Input buffer
143+
144+
Everything the user types will be buffered in the current *user input buffer*.
145+
Once the user hits enter, the *user input buffer* will be processed and cleared.
146+
147+
The `setInput($buffer)` method can be used to control the *user input buffer*.
148+
The user will be able to delete and/or rewrite the buffer at any time.
149+
Changing the *user input buffer* can be useful for presenting a preset input to the user
150+
(like the last password attempt).
151+
Simple pass an input string like this:
152+
153+
```php
154+
$readline->setInput('lastpass');
155+
```
156+
157+
The `getInput()` method can be used to access the current *user input buffer*.
158+
This can be useful if you want to append some input behind the current *user input buffer*.
159+
You can simply access the buffer like this:
160+
161+
```php
162+
$buffer = $readline->getInput();
163+
```
164+
165+
#### Cursor
166+
167+
By default, users can control their (horizontal) cursor position by using their arrow keys on the keyboard.
168+
Also, every character pressed on the keyboard advances the cursor position.
169+
170+
The `setMove($toggle)` method can be used to control whether users are allowed to use their arrow keys.
171+
To disable the left and right arrow keys, simply pass a boolean `false` like this:
172+
173+
```php
174+
$readline->setMove(false);
175+
```
176+
177+
To restore the default behavior where the user can use the left and right arrow keys,
178+
simply pass a boolean `true` like this:
179+
180+
```php
181+
$readline->setMove(true);
182+
```
183+
184+
The `getCursorPosition()` method can be used to access the current cursor position.
185+
This can be useful if you want to get a substring of the current *user input buffer*.
186+
Simply invoke it like this:
187+
188+
```php
189+
$position = $readline->getCursorPosition();
190+
```
191+
192+
The `moveCursorTo($position)` method can be used to set the current cursor position to the given absolute character position.
193+
For example, to move the cursor to the beginning of the *user input buffer*, simply call:
194+
195+
```php
196+
$readline->moveCursorTo(0);
197+
```
198+
199+
The `moveCursorBy($offset)` method can be used to change the cursor position
200+
by the given number of characters relative to the current position.
201+
A positive number will move the cursor to the right - a negative number will move the cursor to the left.
202+
For example, to move the cursor one character to the left, simply call:
203+
204+
```php
205+
$readline->moveCursorBy(-1);
206+
```
207+
208+
### Advanced
209+
210+
#### Stdout
211+
212+
The `Stdout` represents a `WritableStream` and is responsible for handling console output.
213+
214+
Interfacing with it directly is *not recommended* and considered *advanced usage*.
215+
216+
If you want to print some text to console output, use the [`Stdio::write()`](#output) instead:
217+
218+
```php
219+
$stdio->write('hello');
220+
```
221+
222+
Should you need to interface with the `Stdout`, you can access the current instance through the [`Stdio`](#stdio):
223+
224+
```php
225+
$stdout = $stdio->getOutput();
226+
```
227+
228+
#### Stdin
229+
230+
The `Stdin` represents a `ReadableStream` and is responsible for handling console input.
231+
232+
Interfacing with it directly is *not recommended* and considered *advanced usage*.
233+
234+
If you want to read a line from console input, use the [`Stdio::on()`](#input) instead:
235+
236+
```php
237+
$stdio->on('line', function ($line) use ($stdio) {
238+
$stdio->writeln('You said "' . $line . '"');
239+
});
240+
```
241+
242+
Should you need to interface with the `Stdin`, you can access the current instance through the [`Stdio`](#stdio):
243+
244+
You can access the current instance through the [`Stdio`](#stdio):
245+
246+
```php
247+
$stdin = $stdio->getInput();
248+
```
249+
30250
## Install
31251

32252
The recommended way to install this library is [through composer](https://getcomposer.org).

0 commit comments

Comments
 (0)