1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace EddIriarte\Console\Handlers; |
|
|
|
|
3
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
5
|
|
|
use Symfony\Component\Console\Terminal; |
6
|
|
|
use EddIriarte\Console\Inputs\Interfaces\SelectInput; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class SelectHandler |
10
|
|
|
* @package Lazzier\Helpers |
|
|
|
|
11
|
|
|
* @author Eduardo Iriarte <eddiriarte[at]gmail[dot]com> |
|
|
|
|
12
|
|
|
*/ |
|
|
|
|
13
|
|
|
class SelectHandler |
14
|
|
|
{ |
|
|
|
|
15
|
|
|
/** |
|
|
|
|
16
|
|
|
* @var resource |
17
|
|
|
*/ |
18
|
|
|
protected $stream; |
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
|
|
|
|
21
|
|
|
* @var OutputInterface |
22
|
|
|
*/ |
23
|
|
|
protected $output; |
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
|
|
|
|
26
|
|
|
* @var SelectInput |
27
|
|
|
*/ |
28
|
|
|
protected $question; |
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
|
|
|
|
31
|
|
|
* @var int |
|
|
|
|
32
|
|
|
*/ |
33
|
|
|
protected $row; |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
|
|
|
|
36
|
|
|
* @var int |
|
|
|
|
37
|
|
|
*/ |
38
|
|
|
protected $column; |
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
|
|
|
|
41
|
|
|
* @var bool |
|
|
|
|
42
|
|
|
*/ |
43
|
|
|
protected $firstRun; |
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
|
|
|
|
46
|
|
|
* @var int |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
protected $terminalWidth; |
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
|
|
|
|
51
|
|
|
* SelectStreamHandler constructor. |
52
|
|
|
* @param SelectInput $question |
|
|
|
|
53
|
|
|
* @param OutputInterface $output |
|
|
|
|
54
|
|
|
* @param $stream |
|
|
|
|
55
|
|
|
*/ |
56
|
7 |
|
public function __construct(SelectInput $question, OutputInterface $output, $stream) |
|
|
|
|
57
|
|
|
{ |
|
|
|
|
58
|
7 |
|
$this->row = 0; |
|
|
|
|
59
|
7 |
|
$this->column = 0; |
|
|
|
|
60
|
7 |
|
$this->question = $question; |
61
|
7 |
|
$this->output = $output; |
|
|
|
|
62
|
7 |
|
$this->stream = $stream; |
|
|
|
|
63
|
7 |
|
} |
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Navigates through option items. |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
6 |
|
public function handle(): array |
71
|
|
|
{ |
|
|
|
|
72
|
6 |
|
$this->firstRun = true; |
|
|
|
|
73
|
6 |
|
$this->output->writeln( |
74
|
6 |
|
'<info>' . $this->question->getMessage() . '</info> [<comment>SPACE=select</>, <comment>ENTER=submit</>]' |
|
|
|
|
75
|
|
|
); |
76
|
6 |
|
$this->repaint(); |
77
|
|
|
|
78
|
6 |
|
$sttyMode = shell_exec('stty -g'); |
79
|
|
|
|
80
|
|
|
// Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead) |
|
|
|
|
81
|
6 |
|
shell_exec('stty -icanon -echo'); |
82
|
|
|
|
83
|
|
|
// Read a keypress |
|
|
|
|
84
|
6 |
|
while (!feof($this->stream)) { |
|
|
|
|
85
|
6 |
|
$char = fread($this->stream, 1); |
86
|
6 |
|
if (" " === $char) { |
|
|
|
|
87
|
6 |
|
$this->tryCellSelection(); |
88
|
6 |
|
} elseif ("\033" === $char) { |
|
|
|
|
89
|
6 |
|
$this->tryCellNavigation($char); |
90
|
6 |
|
} elseif (10 === ord($char)) { |
|
|
|
|
91
|
|
|
//TODO handle valid state... |
|
|
|
|
92
|
3 |
|
$this->clear(); |
93
|
3 |
|
$this->output->write('> ' . join(', ', $this->question->getSelections())); |
|
|
|
|
94
|
3 |
|
$this->output->write($char); |
95
|
3 |
|
break; |
96
|
|
|
} |
97
|
6 |
|
$this->repaint(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Reset stty so it behaves normally again |
|
|
|
|
101
|
6 |
|
shell_exec(sprintf('stty %s', $sttyMode)); |
102
|
|
|
|
103
|
6 |
|
$this->output->writeln(' '); |
104
|
|
|
|
105
|
6 |
|
return $this->question->getSelections(); |
106
|
|
|
} |
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
|
|
|
|
109
|
|
|
* @param $row |
|
|
|
|
110
|
|
|
* @param $column |
|
|
|
|
111
|
|
|
* @return bool |
|
|
|
|
112
|
|
|
*/ |
113
|
11 |
|
public function exists($row, $column): bool |
|
|
|
|
114
|
|
|
{ |
|
|
|
|
115
|
11 |
|
return $this->question->hasEntryAt($row, $column); |
|
|
|
|
116
|
|
|
} |
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
|
|
|
|
119
|
|
|
* |
120
|
|
|
*/ |
|
|
|
|
121
|
1 |
|
protected function up(): void |
122
|
|
|
{ |
|
|
|
|
123
|
1 |
|
if ($this->exists($this->row - 1, $this->column)) { |
|
|
|
|
124
|
1 |
|
$this->row -= 1; |
125
|
|
|
} |
126
|
1 |
|
} |
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
|
|
|
|
129
|
|
|
* |
130
|
|
|
*/ |
|
|
|
|
131
|
2 |
|
protected function down(): void |
132
|
|
|
{ |
|
|
|
|
133
|
2 |
|
if ($this->exists($this->row + 1, $this->column)) { |
|
|
|
|
134
|
2 |
|
$this->row += 1; |
135
|
|
|
} |
136
|
2 |
|
} |
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
|
|
|
|
139
|
|
|
* |
140
|
|
|
*/ |
|
|
|
|
141
|
3 |
|
protected function left(): void |
142
|
|
|
{ |
|
|
|
|
143
|
3 |
|
if ($this->exists($this->row, $this->column - 1)) { |
|
|
|
|
144
|
3 |
|
$this->column -= 1; |
145
|
|
|
} |
146
|
3 |
|
} |
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
|
|
|
|
149
|
|
|
* |
150
|
|
|
*/ |
|
|
|
|
151
|
5 |
|
protected function right(): void |
152
|
|
|
{ |
|
|
|
|
153
|
5 |
|
if ($this->exists($this->row, $this->column + 1)) { |
|
|
|
|
154
|
5 |
|
$this->column += 1; |
155
|
|
|
} |
156
|
5 |
|
} |
|
|
|
|
157
|
|
|
|
158
|
|
|
/** |
|
|
|
|
159
|
|
|
* |
160
|
|
|
*/ |
|
|
|
|
161
|
6 |
|
protected function tryCellSelection(): void |
162
|
|
|
{ |
|
|
|
|
163
|
|
|
// Try to select cell. |
164
|
6 |
|
if ($this->exists($this->row, $this->column)) { |
165
|
6 |
|
$option = $this->question->getEntryAt($this->row, $this->column); |
|
|
|
|
166
|
6 |
|
$this->question->select($option); |
167
|
|
|
} |
168
|
6 |
|
} |
|
|
|
|
169
|
|
|
|
170
|
|
|
/** |
|
|
|
|
171
|
|
|
* @param $char |
|
|
|
|
172
|
|
|
*/ |
|
|
|
|
173
|
6 |
|
protected function tryCellNavigation($char): void |
|
|
|
|
174
|
|
|
{ |
|
|
|
|
175
|
|
|
// Did we read an escape sequence? |
176
|
6 |
|
$char .= fread($this->stream, 2); |
177
|
6 |
|
if (empty($char[2]) || !in_array($char[2], ['A', 'B', 'C', 'D'])) { |
|
|
|
|
178
|
|
|
// Input stream was not an arrow key. |
179
|
|
|
return; |
180
|
|
|
} |
181
|
|
|
|
182
|
6 |
|
switch ($char[2]) { |
|
|
|
|
183
|
6 |
|
case 'A': // go up! |
|
|
|
|
184
|
1 |
|
$this->up(); |
185
|
1 |
|
break; |
|
|
|
|
186
|
6 |
|
case 'B': // go down! |
|
|
|
|
187
|
2 |
|
$this->down(); |
188
|
2 |
|
break; |
|
|
|
|
189
|
5 |
|
case 'C': // go right! |
|
|
|
|
190
|
5 |
|
$this->right(); |
191
|
5 |
|
break; |
|
|
|
|
192
|
3 |
|
case 'D': // go left! |
|
|
|
|
193
|
3 |
|
$this->left(); |
194
|
3 |
|
break; |
|
|
|
|
195
|
|
|
} |
196
|
6 |
|
} |
|
|
|
|
197
|
|
|
|
198
|
|
|
/** |
|
|
|
|
199
|
|
|
* |
200
|
|
|
*/ |
|
|
|
|
201
|
7 |
|
public function repaint(): void |
202
|
|
|
{ |
|
|
|
|
203
|
7 |
|
$message = $this->message(); |
204
|
7 |
|
if (!$this->firstRun) { |
|
|
|
|
205
|
7 |
|
$this->clear(); |
206
|
|
|
} |
207
|
|
|
|
208
|
7 |
|
$this->firstRun = false; |
|
|
|
|
209
|
7 |
|
$this->output->write($message); |
210
|
7 |
|
} |
|
|
|
|
211
|
|
|
|
212
|
|
|
/** |
|
|
|
|
213
|
|
|
* |
214
|
|
|
*/ |
|
|
|
|
215
|
7 |
|
public function clear(): void |
216
|
|
|
{ |
|
|
|
|
217
|
|
|
// Move the cursor to the beginning of the line |
|
|
|
|
218
|
7 |
|
$this->output->write("\x0D"); |
219
|
|
|
// Erase the line |
|
|
|
|
220
|
7 |
|
$this->output->write("\x1B[2K"); |
221
|
|
|
// Erase previous lines |
|
|
|
|
222
|
7 |
|
$lines = $this->question->getChunksCount() - 1; |
|
|
|
|
223
|
7 |
|
if ($lines > 0) { |
224
|
2 |
|
$this->output->write(str_repeat("\x1B[1A\x1B[2K", $lines)); |
225
|
|
|
} |
226
|
7 |
|
} |
|
|
|
|
227
|
|
|
|
228
|
|
|
/** |
|
|
|
|
229
|
|
|
* @return string |
230
|
|
|
*/ |
231
|
7 |
|
protected function message(): string |
232
|
|
|
{ |
|
|
|
|
233
|
7 |
|
$chunkSize = $this->chunkSize(); |
|
|
|
|
234
|
7 |
|
$chunks = $this->question->getChunks($chunkSize); |
|
|
|
|
235
|
7 |
|
$columnSpace = floor(($this->terminalWidth() - ($chunkSize * 5)) / $chunkSize); |
236
|
7 |
|
return join(PHP_EOL, array_map(function ($entries) use ($chunks, $columnSpace) { |
|
|
|
|
237
|
7 |
|
$hasCursor = (bool) ($this->row === array_search($entries, $chunks)); |
|
|
|
|
238
|
7 |
|
return $this->makeRow($entries, ($hasCursor ? $this->column : -10), $columnSpace); |
|
|
|
|
239
|
7 |
|
}, $chunks)); |
|
|
|
|
240
|
|
|
} |
|
|
|
|
241
|
|
|
|
242
|
|
|
/** |
|
|
|
|
243
|
|
|
* @param array $entries |
|
|
|
|
244
|
|
|
* @param int $activeColumn |
|
|
|
|
245
|
|
|
* @return mixed |
246
|
|
|
*/ |
247
|
|
|
protected function makeRow(array $entries, int $activeColumn, int $columnSpace) |
248
|
|
|
{ |
|
|
|
|
249
|
7 |
|
return array_reduce($entries, function ($carry, $item) use ($entries, $activeColumn, $columnSpace) { |
|
|
|
|
250
|
7 |
|
$isActive = ($activeColumn === array_search($item, $entries)); |
|
|
|
|
251
|
7 |
|
return $carry . $this->makeCell($item, $isActive, $columnSpace); |
|
|
|
|
252
|
7 |
|
}, ''); |
|
|
|
|
253
|
|
|
} |
|
|
|
|
254
|
|
|
|
255
|
|
|
/** |
|
|
|
|
256
|
|
|
* @param string $option |
|
|
|
|
257
|
|
|
* @param bool $hasCursor |
|
|
|
|
258
|
|
|
* @param int $maxWidth |
|
|
|
|
259
|
|
|
* @return string |
260
|
|
|
*/ |
261
|
7 |
|
protected function makeCell(string $option, bool $hasCursor = false, int $maxWidth = 20): string |
|
|
|
|
262
|
|
|
{ |
|
|
|
|
263
|
7 |
|
$selected = $this->question->isSelected($option); |
264
|
7 |
|
$name = substr($option, 0, ($maxWidth - 1)); |
|
|
|
|
265
|
|
|
|
266
|
7 |
|
return sprintf( |
267
|
7 |
|
$hasCursor ? ' <hl> %1$s %2$s </hl>' : ' <%3$s> %1$s %2$s </%3$s>', |
|
|
|
|
268
|
7 |
|
($selected ? '●' : '○'), |
|
|
|
|
269
|
7 |
|
$name . str_repeat(' ', $maxWidth - mb_strlen($name)), |
|
|
|
|
270
|
7 |
|
($selected ? 'info' : 'comment') |
|
|
|
|
271
|
|
|
); |
272
|
|
|
} |
|
|
|
|
273
|
|
|
|
274
|
7 |
|
public function terminalWidth() |
|
|
|
|
275
|
|
|
{ |
|
|
|
|
276
|
7 |
|
return (new Terminal)->getWidth(); |
277
|
|
|
} |
|
|
|
|
278
|
|
|
|
279
|
7 |
|
public function chunkSize() |
|
|
|
|
280
|
|
|
{ |
|
|
|
|
281
|
7 |
|
$max = $this->terminalWidth(); |
|
|
|
|
282
|
7 |
|
$largest = array_reduce($this->question->getOptions(), 'max', 0); |
283
|
|
|
|
284
|
7 |
|
if ($largest > ($max / 2)) { |
285
|
|
|
return 1; |
286
|
7 |
|
} elseif ($largest > ($max / 3)) { |
|
|
|
|
287
|
|
|
return 2; |
288
|
|
|
} else { |
289
|
7 |
|
return 3; |
290
|
|
|
} |
291
|
|
|
} |
|
|
|
|
292
|
|
|
} |
|
|
|
|
293
|
|
|
|