forked from php-school/cli-menu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputIO.php
More file actions
258 lines (218 loc) · 7.7 KB
/
Copy pathInputIO.php
File metadata and controls
258 lines (218 loc) · 7.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
declare(strict_types=1);
namespace PhpSchool\CliMenu\Input;
use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\Util\StringUtil;
use PhpSchool\Terminal\InputCharacter;
use PhpSchool\Terminal\NonCanonicalReader;
use PhpSchool\Terminal\Terminal;
/**
* @author Aydin Hassan <[email protected]>
*/
class InputIO
{
/**
* @var CliMenu
*/
private $parentMenu;
/**
* @var Terminal
*/
private $terminal;
/**
* @var callable[][]
*/
private $callbacks = [];
public function __construct(CliMenu $parentMenu, Terminal $terminal)
{
$this->terminal = $terminal;
$this->parentMenu = $parentMenu;
}
public function collect(Input $input) : InputResult
{
$this->drawInput($input, $input->getPlaceholderText());
$inputValue = $input->getPlaceholderText();
$havePlaceHolderValue = !empty($inputValue);
$originalValue = $inputValue;
$reader = new NonCanonicalReader($this->terminal);
while ($char = $reader->readCharacter()) {
if ($char->isNotControl()) {
if ($havePlaceHolderValue) {
$inputValue = $char->get();
$havePlaceHolderValue = false;
} else {
$inputValue .= $char->get();
}
$this->parentMenu->redraw();
$this->drawInput($input, $inputValue);
continue;
}
if ($char->isHandledControl()) {
switch ($char->getControl()) {
case InputCharacter::ESC:
$this->parentMenu->redraw();
return new InputResult($originalValue);
case InputCharacter::ENTER:
if ($input->validate($inputValue)) {
$this->parentMenu->redraw();
return new InputResult($inputValue);
} else {
$this->drawInputWithError($input, $inputValue);
continue 2;
}
case InputCharacter::BACKSPACE:
if (!empty($inputValue)) {
$inputValue = mb_substr($inputValue, 0, -1);
$this->parentMenu->redraw();
$this->drawInput($input, $inputValue);
}
continue 2;
}
if (!empty($this->callbacks[$char->getControl()])) {
foreach ($this->callbacks[$char->getControl()] as $callback) {
$inputValue = $callback($inputValue);
$this->drawInput($input, $inputValue);
}
}
}
}
}
public function registerControlCallback(string $control, callable $callback) : void
{
if (!isset($this->callbacks[$control])) {
$this->callbacks[$control] = [];
}
$this->callbacks[$control][] = $callback;
}
private function getInputWidth(array $lines) : int
{
return max(
array_map(
function (string $line) {
return mb_strwidth($line);
},
$lines
)
) ? : 0;
}
private function calculateYPosition() : int
{
$lines = 5; //1. empty 2. prompt text 3. empty 4. input 5. empty
return (int) (ceil($this->parentMenu->getCurrentFrame()->count() / 2) - ceil($lines /2) + 1);
}
private function calculateYPositionWithError() : int
{
$lines = 7; //1. empty 2. prompt text 3. empty 4. input 5. empty 6. error 7. empty
return (int) (ceil($this->parentMenu->getCurrentFrame()->count() / 2) - ceil($lines /2) + 1);
}
private function calculateXPosition(Input $input, string $userInput) : int
{
$width = $this->getInputWidth(
[
$input->getPromptText(),
$input->getValidationFailedText(),
$userInput
]
);
$parentStyle = $this->parentMenu->getStyle();
$halfWidth = ($width + ($input->getStyle()->getPaddingLeftRight() * 2)) / 2;
$parentHalfWidth = ceil($parentStyle->getWidth() / 2 + $parentStyle->getMargin());
return (int) ($parentHalfWidth - $halfWidth);
}
private function drawLine(Input $input, string $userInput, string $text) : void
{
$this->terminal->moveCursorToColumn($this->calculateXPosition($input, $userInput));
$line = sprintf(
"%s%s%s%s%s\n",
$input->getStyle()->getColoursSetCode(),
str_repeat(' ', $input->getStyle()->getPaddingLeftRight()),
$text,
str_repeat(' ', $input->getStyle()->getPaddingLeftRight()),
$input->getStyle()->getColoursResetCode()
);
$this->terminal->write($line);
}
private function drawCenteredLine(Input $input, string $userInput, string $text) : void
{
$width = $this->getInputWidth(
[
$input->getPromptText(),
$input->getValidationFailedText(),
$userInput
]
);
$textLength = mb_strwidth(StringUtil::stripAnsiEscapeSequence($text));
$leftFill = (int) (($width / 2) - ($textLength / 2));
$rightFill = (int) ceil($width - $leftFill - $textLength);
$this->drawLine(
$input,
$userInput,
sprintf(
'%s%s%s',
str_repeat(' ', $leftFill),
$text,
str_repeat(' ', $rightFill)
)
);
}
private function drawEmptyLine(Input $input, string $userInput) : void
{
$width = $this->getInputWidth(
[
$input->getPromptText(),
$input->getValidationFailedText(),
$userInput
]
);
$this->drawLine(
$input,
$userInput,
str_repeat(' ', $width)
);
}
private function drawInput(Input $input, string $userInput) : void
{
$this->terminal->moveCursorToRow($this->calculateYPosition());
$this->drawEmptyLine($input, $userInput);
$this->drawTitle($input, $userInput);
$this->drawEmptyLine($input, $userInput);
$this->drawInputField($input, $input->filter($userInput));
$this->drawEmptyLine($input, $userInput);
}
private function drawInputWithError(Input $input, string $userInput) : void
{
$this->terminal->moveCursorToRow($this->calculateYPositionWithError());
$this->drawEmptyLine($input, $userInput);
$this->drawTitle($input, $userInput);
$this->drawEmptyLine($input, $userInput);
$this->drawInputField($input, $input->filter($userInput));
$this->drawEmptyLine($input, $userInput);
$this->drawCenteredLine(
$input,
$userInput,
$input->getValidationFailedText()
);
$this->drawEmptyLine($input, $userInput);
}
private function drawTitle(Input $input, string $userInput) : void
{
$this->drawCenteredLine(
$input,
$userInput,
$input->getPromptText()
);
}
private function drawInputField(Input $input, string $userInput) : void
{
$this->drawCenteredLine(
$input,
$userInput,
sprintf(
'%s%s%s',
$input->getStyle()->getInvertedColoursSetCode(),
$userInput,
$input->getStyle()->getInvertedColoursUnsetCode()
)
);
}
}