1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\Dialogue; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @author Aydin Hassan <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
class YesNo extends Dialogue |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
private $yesText = 'Yes'; |
12
|
|
|
|
13
|
|
|
private $noText = 'No'; |
14
|
|
|
|
15
|
|
|
private $optionValue = false; |
16
|
|
|
|
17
|
|
|
public function setYesText($text) |
18
|
|
|
{ |
19
|
|
|
$this->yesText = $text; |
20
|
|
|
|
21
|
|
|
return $this; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function setNoText($text) |
25
|
|
|
{ |
26
|
|
|
$this->noText = $text; |
27
|
|
|
|
28
|
|
|
return $this; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getYesText() |
32
|
|
|
{ |
33
|
|
|
return sprintf(' <%s> ', $this->yesText); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getNoText() |
37
|
|
|
{ |
38
|
|
|
return sprintf(' <%s> ', $this->noText); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function setOptionValue(bool $value) |
42
|
|
|
{ |
43
|
|
|
$this->optionValue = $value; |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function getOptionValue() |
49
|
|
|
{ |
50
|
|
|
return $this->optionValue; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function displayBody() |
54
|
|
|
{ |
55
|
|
|
$this->terminal->moveCursorToRow($this->y); |
56
|
|
|
$this->emptyRow(); |
57
|
|
|
$this->write(sprintf( |
58
|
|
|
"%s%s%s%s%s\n", |
59
|
|
|
$this->style->getUnselectedSetCode(), |
60
|
|
|
str_repeat(' ', $this->style->getPadding()), |
61
|
|
|
$this->text, |
62
|
|
|
str_repeat(' ', $this->style->getPadding()), |
63
|
|
|
$this->style->getUnselectedUnsetCode() |
64
|
|
|
)); |
65
|
|
|
$this->emptyRow(); |
66
|
|
|
|
67
|
|
|
$promptWidth = mb_strlen($this->text) + 4; |
68
|
|
|
$fillWidth = $promptWidth - (mb_strlen($this->getYesText()) + mb_strlen($this->getNoText())); |
69
|
|
|
$placeHolderWidth = 0 == ($fillWidth % 2) ? 2 : 1; |
70
|
|
|
$fillWidth = ($fillWidth - $placeHolderWidth) / 2; |
71
|
|
|
|
72
|
|
|
$this->write(sprintf( |
73
|
|
|
'%s%s%s', |
74
|
|
|
$this->style->getUnselectedSetCode(), |
75
|
|
|
str_repeat(' ', $fillWidth), |
76
|
|
|
$this->style->getUnselectedSetCode() |
77
|
|
|
)); |
78
|
|
|
$this->write( |
79
|
|
|
sprintf( |
80
|
|
|
'%s%s%s', |
81
|
|
|
$this->getOptionValue() ? $this->style->getSelectedSetCode() : $this->style->getUnselectedSetCode(), |
82
|
|
|
$this->getYesText(), |
83
|
|
|
$this->getOptionValue() ? $this->style->getSelectedSetCode() : $this->style->getUnselectedSetCode() |
84
|
|
|
), |
85
|
|
|
-1 |
86
|
|
|
); |
87
|
|
|
$this->write( |
88
|
|
|
sprintf( |
89
|
|
|
'%s%s%s', |
90
|
|
|
$this->style->getUnselectedSetCode(), |
91
|
|
|
str_repeat(' ', $placeHolderWidth), |
92
|
|
|
$this->style->getUnselectedSetCode() |
93
|
|
|
), |
94
|
|
|
-1 |
95
|
|
|
); |
96
|
|
|
$this->write( |
97
|
|
|
sprintf( |
98
|
|
|
'%s%s%s', |
99
|
|
|
$this->getOptionValue() ? $this->style->getUnselectedSetCode() : $this->style->getSelectedSetCode(), |
100
|
|
|
$this->getNoText(), |
101
|
|
|
$this->getOptionValue() ? $this->style->getUnselectedSetCode() : $this->style->getSelectedSetCode() |
102
|
|
|
), |
103
|
|
|
-1 |
104
|
|
|
); |
105
|
|
|
$this->write(sprintf( |
106
|
|
|
"%s%s%s\n", |
107
|
|
|
$this->style->getUnselectedSetCode(), |
108
|
|
|
str_repeat(' ', $fillWidth), |
109
|
|
|
$this->style->getUnselectedSetCode() |
110
|
|
|
), -1); |
111
|
|
|
|
112
|
|
|
$this->write(sprintf( |
113
|
|
|
"%s%s%s%s%s\n", |
114
|
|
|
$this->style->getUnselectedSetCode(), |
115
|
|
|
str_repeat(' ', $this->style->getPadding()), |
116
|
|
|
str_repeat(' ', mb_strlen($this->text)), |
117
|
|
|
str_repeat(' ', $this->style->getPadding()), |
118
|
|
|
$this->style->getUnselectedUnsetCode() |
119
|
|
|
)); |
120
|
|
|
$this->terminal->moveCursorToTop(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* |
125
|
|
|
* The YesNO dialog box is displayed and the option value is passed to the callback function |
126
|
|
|
* |
127
|
|
|
* @param $callable |
128
|
|
|
*/ |
129
|
|
|
public function display($callable) |
130
|
|
|
{ |
131
|
|
|
$this->assertMenuOpen(); |
132
|
|
|
$this->displayBody(); |
133
|
|
|
$input = $this->terminal->getKeyedInput(); |
134
|
|
|
while ('enter' !== $input) { |
135
|
|
|
if (in_array($input, ['left', 'right'])) { |
136
|
|
|
$this->parentMenu->redraw(); |
137
|
|
|
$this->setOptionValue('left' == $input); |
138
|
|
|
$this->displayBody(); |
139
|
|
|
} |
140
|
|
|
$input = $this->terminal->getKeyedInput(); |
141
|
|
|
} |
142
|
|
|
$this->parentMenu->redraw(); |
143
|
|
|
$callable($this->getOptionValue()); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|