|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\Dialogue; |
|
4
|
|
|
|
|
5
|
|
|
use PhpSchool\Terminal\InputCharacter; |
|
6
|
|
|
use PhpSchool\Terminal\NonCanonicalReader; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @author Aydin Hassan <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class Confirm extends Dialogue |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Display confirmation with a button with the given text |
|
16
|
|
|
*/ |
|
17
|
|
|
public function display(string $confirmText = 'OK') : void |
|
18
|
|
|
{ |
|
19
|
|
|
$this->assertMenuOpen(); |
|
20
|
|
|
|
|
21
|
|
|
$this->terminal->moveCursorToRow($this->y); |
|
22
|
|
|
|
|
23
|
|
|
$promptWidth = mb_strlen($this->text) + 4; |
|
24
|
|
|
|
|
25
|
|
|
$this->emptyRow(); |
|
26
|
|
|
|
|
27
|
|
|
$this->write(sprintf( |
|
28
|
|
|
"%s%s%s%s%s\n", |
|
29
|
|
|
$this->style->getColoursSetCode(), |
|
30
|
|
|
str_repeat(' ', $this->style->getPaddingLeftRight()), |
|
31
|
|
|
$this->text, |
|
32
|
|
|
str_repeat(' ', $this->style->getPaddingLeftRight()), |
|
33
|
|
|
$this->style->getColoursResetCode() |
|
34
|
|
|
)); |
|
35
|
|
|
|
|
36
|
|
|
$this->emptyRow(); |
|
37
|
|
|
|
|
38
|
|
|
$confirmText = sprintf(' < %s > ', $confirmText); |
|
39
|
|
|
$leftFill = (int) (($promptWidth / 2) - (mb_strlen($confirmText) / 2)); |
|
40
|
|
|
|
|
41
|
|
|
$this->write(sprintf( |
|
42
|
|
|
"%s%s%s%s%s%s%s\n", |
|
43
|
|
|
$this->style->getColoursSetCode(), |
|
44
|
|
|
str_repeat(' ', $leftFill), |
|
45
|
|
|
$this->style->getInvertedColoursSetCode(), |
|
46
|
|
|
$confirmText, |
|
47
|
|
|
$this->style->getInvertedColoursUnsetCode(), |
|
48
|
|
|
str_repeat(' ', (int) ceil($promptWidth - $leftFill - mb_strlen($confirmText))), |
|
49
|
|
|
$this->style->getColoursResetCode() |
|
50
|
|
|
)); |
|
51
|
|
|
|
|
52
|
|
|
$this->write(sprintf( |
|
53
|
|
|
"%s%s%s%s%s\n", |
|
54
|
|
|
$this->style->getColoursSetCode(), |
|
55
|
|
|
str_repeat(' ', $this->style->getPaddingLeftRight()), |
|
56
|
|
|
str_repeat(' ', mb_strlen($this->text)), |
|
57
|
|
|
str_repeat(' ', $this->style->getPaddingLeftRight()), |
|
58
|
|
|
$this->style->getColoursResetCode() |
|
59
|
|
|
)); |
|
60
|
|
|
|
|
61
|
|
|
$this->terminal->moveCursorToTop(); |
|
62
|
|
|
|
|
63
|
|
|
$reader = new NonCanonicalReader($this->terminal); |
|
64
|
|
|
|
|
65
|
|
|
while ($char = $reader->readCharacter()) { |
|
66
|
|
|
if ($char->isControl() && $char->getControl() === InputCharacter::ENTER) { |
|
67
|
|
|
$this->parentMenu->redraw(); |
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|