|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\Dialogue; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @author Aydin Hassan <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
class Confirm extends Dialogue |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Display confirmation with a button with the given text |
|
13
|
|
|
* |
|
14
|
|
|
* @param string $confirmText |
|
15
|
|
|
*/ |
|
16
|
|
|
public function display($confirmText = 'OK') |
|
17
|
|
|
{ |
|
18
|
|
|
$this->assertMenuOpen(); |
|
19
|
|
|
|
|
20
|
|
|
$this->terminal->moveCursorToRow($this->y); |
|
21
|
|
|
|
|
22
|
|
|
$promptWidth = mb_strlen($this->text) + 4; |
|
23
|
|
|
|
|
24
|
|
|
$this->emptyRow(); |
|
25
|
|
|
|
|
26
|
|
|
$this->write(sprintf( |
|
27
|
|
|
"%s%s%s%s%s\n", |
|
28
|
|
|
$this->style->getUnselectedSetCode(), |
|
29
|
|
|
str_repeat(' ', $this->style->getPadding()), |
|
30
|
|
|
$this->text, |
|
31
|
|
|
str_repeat(' ', $this->style->getPadding()), |
|
32
|
|
|
$this->style->getUnselectedUnsetCode() |
|
33
|
|
|
)); |
|
34
|
|
|
|
|
35
|
|
|
$this->emptyRow(); |
|
36
|
|
|
|
|
37
|
|
|
$confirmText = sprintf(' < %s > ', $confirmText); |
|
38
|
|
|
$leftFill = ($promptWidth / 2) - (mb_strlen($confirmText) / 2); |
|
39
|
|
|
|
|
40
|
|
|
$this->write(sprintf( |
|
41
|
|
|
'%s%s%s', |
|
42
|
|
|
$this->style->getUnselectedSetCode(), |
|
43
|
|
|
str_repeat(' ', $leftFill), |
|
44
|
|
|
$this->style->getUnselectedSetCode() |
|
45
|
|
|
)); |
|
46
|
|
|
|
|
47
|
|
|
$this->write( |
|
48
|
|
|
sprintf( |
|
49
|
|
|
'%s%s%s', |
|
50
|
|
|
$this->style->getSelectedSetCode(), |
|
51
|
|
|
$confirmText, |
|
52
|
|
|
$this->style->getSelectedUnsetCode() |
|
53
|
|
|
), |
|
54
|
|
|
-1 |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
$this->write( |
|
58
|
|
|
sprintf( |
|
59
|
|
|
"%s%s%s\n", |
|
60
|
|
|
$this->style->getUnselectedSetCode(), |
|
61
|
|
|
str_repeat(' ', ceil($promptWidth - $leftFill - mb_strlen($confirmText))), |
|
62
|
|
|
$this->style->getSelectedUnsetCode() |
|
63
|
|
|
), |
|
64
|
|
|
-1 |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
$this->write(sprintf( |
|
68
|
|
|
"%s%s%s%s%s\n", |
|
69
|
|
|
$this->style->getUnselectedSetCode(), |
|
70
|
|
|
str_repeat(' ', $this->style->getPadding()), |
|
71
|
|
|
str_repeat(' ', mb_strlen($this->text)), |
|
72
|
|
|
str_repeat(' ', $this->style->getPadding()), |
|
73
|
|
|
$this->style->getUnselectedUnsetCode() |
|
74
|
|
|
)); |
|
75
|
|
|
|
|
76
|
|
|
$this->terminal->moveCursorToTop(); |
|
77
|
|
|
$input = $this->terminal->getKeyedInput(); |
|
78
|
|
|
|
|
79
|
|
|
while ($input !== 'enter') { |
|
80
|
|
|
$input = $this->terminal->getKeyedInput(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$this->parentMenu->redraw(); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|